OLDataProvider.m 692 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281
  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. #import "ActiveViewController.h"
  24. #import "RANetwork.h"
  25. @interface OLDataProvider ()
  26. @end
  27. @implementation OLDataProvider
  28. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  29. {
  30. 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];
  31. NSString* ret=@"No Price.";
  32. sqlite3_stmt * statement;
  33. // int count=0;
  34. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  35. {
  36. if (sqlite3_step(statement) == SQLITE_ROW)
  37. {
  38. char *price = (char*)sqlite3_column_text(statement, 0);
  39. if(price==nil)
  40. price="";
  41. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  42. double dp= [nsprice doubleValue];
  43. ret=[NSString stringWithFormat:@"%.2f",dp];
  44. }
  45. sqlite3_finalize(statement);
  46. }
  47. return ret;
  48. // // [iSalesDB close_db:db];
  49. //
  50. // return nil;
  51. //// if(dprice==DBL_MAX)
  52. //// ret= nil;
  53. //// else
  54. //// ret= [NSNumber numberWithDouble:dprice];
  55. //// return ret;
  56. }
  57. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  58. {
  59. NSString* ret= nil;
  60. NSString *sqlQuery = nil;
  61. // 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
  62. 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];
  63. sqlite3_stmt * statement;
  64. // int count=0;
  65. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  66. {
  67. if (sqlite3_step(statement) == SQLITE_ROW)
  68. {
  69. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  70. if(imgurl==nil)
  71. imgurl="";
  72. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  73. ret=nsimgurl;
  74. }
  75. sqlite3_finalize(statement);
  76. }
  77. else
  78. {
  79. ret=@"";
  80. }
  81. // [iSalesDB close_db:db];
  82. // DebugLog(@"data string: %@",ret );
  83. return ret;
  84. }
  85. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  86. {
  87. // assert(params[@"contact_id"]!=nil);
  88. assert(params[@"user"]!=nil);
  89. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  90. //生成portfolio pdf需要的数据
  91. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  92. data[@"npd_url"]=COMPANY_WEB;
  93. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  94. [formatter setDateFormat:@"MMMM yyyy"];
  95. NSString* date = [formatter stringFromDate:[NSDate date]];
  96. data[@"create_month"]=date;
  97. data[@"company_name"]=COMPANY_FULL_NAME;
  98. data[@"catalog_name"]=params[@"catalog_name"];
  99. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  100. data[@"TOTAL_PAGE"]=0;
  101. data[@"CURRENT_PAGE"]=0;
  102. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  103. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  104. bool show_stockout=[params[@"show_stockout"] boolValue];
  105. NSString *sql = nil;
  106. // v1.90 more color
  107. NSString *group_by = @"";
  108. if(values==nil)
  109. {
  110. if (show_group_by) {
  111. group_by = @"group by b.product_group ";
  112. }
  113. 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];
  114. }
  115. else
  116. {
  117. if (show_group_by) {
  118. group_by = @"group by product_group ";
  119. }
  120. 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];
  121. }
  122. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  123. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  124. sqlite3 *db = [iSalesDB get_db];
  125. // NSDictionary *resultDic =
  126. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  127. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  128. int product_id = sqlite3_column_int(stmt, 0);
  129. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  130. NSString *name = [self textAtColumn:1 statement:stmt];
  131. NSString *description = [self textAtColumn:2 statement:stmt];
  132. double price = sqlite3_column_double(stmt, 3);
  133. NSString *s_price = [self textAtColumn:3 statement:stmt];
  134. double discount = sqlite3_column_double(stmt,4);
  135. int qty = sqlite3_column_int(stmt, 5);
  136. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  137. int is_percentage = sqlite3_column_int(stmt, 6);
  138. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  139. int item_id = sqlite3_column_int(stmt, 7);
  140. // int fashion_id = sqlite3_column_int(stmt, 8);
  141. NSString *line_note = [self textAtColumn:9 statement:stmt];
  142. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  143. double percent = sqlite3_column_double(stmt, 10);
  144. // int portfolio_id = sqlite3_column_int(stmt, 11);
  145. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  146. int availability = sqlite3_column_int(stmt, 12);
  147. NSString *color = [self textAtColumn:13 statement:stmt];
  148. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  149. NSString *demension = [self textAtColumn:15 statement:stmt];
  150. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  151. NSString *material = [self textAtColumn:17 statement:stmt];
  152. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  153. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  154. double volume = sqlite3_column_double(stmt, 19);
  155. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  156. double weight = sqlite3_column_double(stmt, 20);
  157. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  158. int model_set = sqlite3_column_int(stmt, 21);
  159. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  160. double load_ability = sqlite3_column_double(stmt, 22);
  161. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  162. NSString *assembling = [self textAtColumn:24 statement:stmt];
  163. NSString *made_in = [self textAtColumn:25 statement:stmt];
  164. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  165. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  166. float bprice=0;
  167. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  168. for(int b=0;b< [bundle[@"count"] intValue];b++)
  169. {
  170. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  171. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  172. }
  173. if(gprice!=nil)
  174. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  175. if(s_percent.length==0||is_percentage==0)
  176. {
  177. percent=100.0;
  178. if([s_qty isEqualToString:@"null"])
  179. qty=availability;
  180. }
  181. else
  182. {
  183. qty=availability;
  184. }
  185. qty=qty*percent/100+0.5;
  186. for(int i=0;i<[values[@"count"] intValue];i++)
  187. {
  188. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  189. if([valueitem[@"product_id"] intValue]==product_id)
  190. {
  191. line_note=valueitem[@"line_note"];
  192. qty=[valueitem[@"available_qty"] intValue];
  193. if([[valueitem allKeys] containsObject:@"available_qty"])
  194. s_qty=[NSString stringWithFormat:@"%d",qty];
  195. else
  196. s_qty=@"null";
  197. if(valueitem[@"regular_price"]!=nil)
  198. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  199. s_price=valueitem[@"special_price"];
  200. price=[valueitem[@"special_price"] floatValue];
  201. discount=0;
  202. // v1.90
  203. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  204. }
  205. }
  206. if(is_percentage==0&&qty==0&&!show_stockout)
  207. {
  208. return;
  209. }
  210. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  211. {
  212. return;
  213. }
  214. NSString* set_price=@"";
  215. if([params[@"entered_price"] boolValue])
  216. {
  217. if (s_price==nil ) {
  218. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  219. }
  220. else
  221. {
  222. if(price*(1-discount/100.0)!=gprice.floatValue)
  223. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  224. }
  225. }
  226. NSString* get_price=@"";
  227. {
  228. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  229. // DebugLog(@"price time interval");
  230. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  231. if(gprice==nil)
  232. get_price=@"Price:No Price.";
  233. else
  234. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  235. }
  236. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  237. // if ([qty_null isEqualToString:@"null"]) {
  238. // qty=availability;
  239. // }
  240. //
  241. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  242. // if ([qty_percent_null isEqualToString:@"null"]) {
  243. // percentage=1;
  244. // }
  245. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  246. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  247. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  248. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  249. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  250. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  251. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  252. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  253. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  254. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  255. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  256. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  257. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  258. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  259. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  260. if([params[@"availability"] boolValue]==false)
  261. str_availability=@"";
  262. else
  263. {
  264. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  265. str_availability=@"<b>Availability:</b> In Production";
  266. }
  267. if([params[@"color"] boolValue]==false || color.length==0 )
  268. str_color=@"";
  269. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  270. str_model_set=@"";
  271. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  272. str_legcolor=@"";
  273. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  274. str_demension=@"";
  275. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  276. str_seat_height=@"";
  277. if([params[@"material"] boolValue]==false || material.length==0 )
  278. str_meterial=@"";
  279. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  280. str_box_dim=@"";
  281. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  282. str_volume=@"";
  283. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  284. str_weight=@"";
  285. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  286. str_load_ability=@"";
  287. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  288. str_fabric_content=@"";
  289. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  290. str_assembling=@"";
  291. if([params[@"made"] boolValue]==false || made_in.length==0 )
  292. str_made_in=@"";
  293. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  294. str_line_note=@"";
  295. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  296. [arr_detail addObject:str_availability];
  297. [arr_detail addObject:str_color];
  298. [arr_detail addObject:str_model_set];
  299. [arr_detail addObject:str_legcolor];
  300. [arr_detail addObject:str_demension];
  301. [arr_detail addObject:str_seat_height];
  302. [arr_detail addObject:str_meterial];
  303. [arr_detail addObject:str_box_dim];
  304. [arr_detail addObject:str_volume];
  305. [arr_detail addObject:str_weight];
  306. [arr_detail addObject:str_load_ability];
  307. [arr_detail addObject:str_fabric_content];
  308. [arr_detail addObject:str_assembling];
  309. [arr_detail addObject:str_made_in];
  310. [arr_detail addObject:str_line_note];
  311. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  312. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  313. //model image;
  314. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  315. // qr image
  316. NSString* qrpath=nil;
  317. if([params[@"show_barcode"] boolValue])
  318. {
  319. NSString* temp = NSTemporaryDirectory();
  320. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  321. qrpath=[temp stringByAppendingPathComponent:filename];
  322. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  323. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  324. }
  325. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  326. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  327. item[@"img"]=img_path;
  328. item[@"img_qr"]=qrpath;
  329. item[@"special_price"]=set_price;
  330. item[@"price"]=get_price;
  331. item[@"name"]=name;
  332. item[@"description"]=description;
  333. item[@"detail"]=detail;
  334. item[@"img"]=img_path;
  335. if (show_group_by && more_color_int != 0) {
  336. item[@"more_color"] = more_color_path;
  337. }
  338. // @{
  339. // //@"linenotes": line_note,
  340. //// @"product_id": product_id_string,
  341. //// @"available_qty": @(qty),
  342. //// @"available_percent" : @(percent),
  343. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  344. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  345. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"": ,
  352. // @"":
  353. // }.mutableCopy;
  354. // if (percentage) {
  355. // [item removeObjectForKey:@"available_qty"];
  356. // } else {
  357. // [item removeObjectForKey:@"available_percent"];
  358. // }
  359. //
  360. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  361. // if ([qty_null isEqualToString:@"null"]) {
  362. // [item removeObjectForKey:@"available_qty"];
  363. // }
  364. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  365. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  366. }];
  367. [iSalesDB close_db:db];
  368. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  369. // [dic setValue:@"" forKey:@"email_content"];
  370. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  371. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  372. //
  373. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  374. // cell[@"count"]=[NSNumber numberWithInt:10];
  375. grid[@"cell0"]=dic;
  376. data[@"grid0"]=grid;
  377. return data;
  378. }
  379. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  380. {
  381. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  382. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  383. NSString *cache_folder=[paths objectAtIndex:0];
  384. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  385. BOOL bdir=NO;
  386. NSFileManager* fileManager = [NSFileManager defaultManager];
  387. if(PDF_DEBUG)
  388. {
  389. NSData *data = [NSData dataWithContentsOfFile:default_path];
  390. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  391. return ret;
  392. }
  393. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  394. {
  395. NSError * error=nil;
  396. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  397. {
  398. return nil;
  399. }
  400. }
  401. NSData *data = [NSData dataWithContentsOfFile:template_path];
  402. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  403. return ret;
  404. }
  405. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  406. {
  407. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  408. 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 ];
  409. DebugLog(@"offline_login sql:%@",sqlQuery);
  410. sqlite3_stmt * statement;
  411. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  412. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  413. {
  414. if (sqlite3_step(statement) == SQLITE_ROW)
  415. {
  416. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  417. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  418. // int can_show_price = sqlite3_column_int(statement, 0);
  419. // int can_see_price = sqlite3_column_int(statement, 1);
  420. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  421. // if(contact_id==nil)
  422. // contact_id="";
  423. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  424. // int user_type = sqlite3_column_int(statement, 3);
  425. // int can_cancel_order = sqlite3_column_int(statement, 4);
  426. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  427. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  428. // int can_delete_order = sqlite3_column_int(statement, 7);
  429. // int can_submit_order = sqlite3_column_int(statement, 8);
  430. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  431. // int can_create_order = sqlite3_column_int(statement, 10);
  432. // char *mode = (char*)sqlite3_column_text(statement, 11);
  433. // if(mode==nil)
  434. // mode="";
  435. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  436. // char *username = (char*)sqlite3_column_text(statement, 12);
  437. // if(username==nil)
  438. // username="";
  439. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  440. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  441. char *first_name = (char*)sqlite3_column_text(statement, 14);
  442. if(first_name==nil)
  443. first_name="";
  444. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  445. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  446. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  447. // [header setValue:nscontact_id forKey:@"contact_id"];
  448. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  449. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  450. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  451. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  452. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  454. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  455. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  456. //
  457. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  458. //
  459. // [header setValue:nsusername forKey:@"username"];
  460. //
  461. // NSError* error=nil;
  462. //
  463. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  464. // [header setValue:statusFilter forKey:@"statusFilter"];
  465. //
  466. //
  467. //
  468. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  469. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  470. //
  471. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  472. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  473. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  474. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  475. //
  476. // [ret setObject:header forKey:@"header"];
  477. [ret setValue:nsfirst_name forKey:@"first_name"];
  478. }
  479. sqlite3_finalize(statement);
  480. }
  481. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  482. return ret;
  483. }
  484. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  485. {
  486. assert(user!=nil);
  487. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  488. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  489. data[@"npd_url"]=COMPANY_WEB;
  490. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  491. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  492. NSString* date = [formatter stringFromDate:[NSDate date]];
  493. data[@"print_date"]=date;
  494. data[@"company_name"]=COMPANY_FULL_NAME;
  495. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  496. data[@"TOTAL_PAGE"]=0;
  497. data[@"CURRENT_PAGE"]=0;
  498. // [formatter setDateFormat:@"MM/dd/yyyy"];
  499. // date = [formatter stringFromDate:[NSDate date]];
  500. // data[@"creat_date"]=date;
  501. NSString* temp = NSTemporaryDirectory();
  502. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  503. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  504. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  505. // bool bwrite=
  506. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  507. data[@"img_barcode"]=qrpath;
  508. data[@"so_id"]=order_code;
  509. data[@"company"]=COMPANY_FULL_NAME;
  510. // 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];
  511. 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];
  512. sqlite3 *db = [iSalesDB get_db];
  513. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  514. sqlite3_stmt * statement;
  515. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  516. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  517. row0[@"count"]=[NSNumber numberWithInt:1];
  518. datasource[@"row0"]=row0;
  519. NSString* print_status=@"";
  520. NSString* order_type=@"Offline Quote";
  521. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  522. {
  523. if (sqlite3_step(statement) == SQLITE_ROW)
  524. {
  525. int erpOrderStatus = sqlite3_column_int(statement, 0);
  526. int status = sqlite3_column_int(statement, 26);
  527. if(status==2)
  528. {
  529. switch (erpOrderStatus) {
  530. case 10:
  531. // order_type=@"Quoted Order";
  532. print_status=@"Quote";
  533. break;
  534. case 11:
  535. print_status=@"Booked";
  536. break;
  537. case 12:
  538. print_status=@"Picked";
  539. break;
  540. case 13:
  541. print_status=@"Shipped";
  542. break;
  543. case 14:
  544. print_status=@"Closed";
  545. break;
  546. case 15:
  547. print_status=@"Canceled";
  548. break;
  549. case 16:
  550. print_status=@"All";
  551. break;
  552. case 18:
  553. print_status=@"Processing";
  554. break;
  555. default:
  556. break;
  557. }
  558. }
  559. else{
  560. switch (status) {
  561. case 0:
  562. {
  563. print_status=@"Quote";
  564. break;
  565. }
  566. case 1:
  567. {
  568. print_status=@"Saved";
  569. break;
  570. }
  571. case 3:
  572. {
  573. //canceled
  574. print_status=@"Canceled";
  575. break;
  576. }
  577. default:
  578. break;
  579. }
  580. }
  581. NSString* sender_name = [self textAtColumn:1 statement:statement];
  582. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  583. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  584. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  585. NSString* sender_email = [self textAtColumn:5 statement:statement];
  586. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  587. NSString* billing_name = [self textAtColumn:7 statement:statement];
  588. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  589. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  590. if(billing_phone.length>0)
  591. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  592. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  593. if(billing_fax.length>0)
  594. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  595. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  596. [billto_arr0 addObject:billing_phone];
  597. [billto_arr0 addObject:billing_fax];
  598. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  599. NSString* billing_email = [self textAtColumn:11 statement:statement];
  600. if(billing_email.length>0)
  601. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  602. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  603. if(billing_contact.length>0)
  604. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  605. NSString* receive_name = [self textAtColumn:12 statement:statement];
  606. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  607. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  608. if(receive_phone.length>0)
  609. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  610. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  611. if(receive_fax.length>0)
  612. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  613. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  614. [receive_arr0 addObject:receive_phone];
  615. [receive_arr0 addObject:receive_fax];
  616. NSString* receive_email = [self textAtColumn:16 statement:statement];
  617. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  618. if(receive_email.length>0)
  619. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  620. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  621. if(receive_contact.length>0)
  622. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  623. NSString* poNumber = [self textAtColumn:17 statement:statement];
  624. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  625. NSString* carrier = [self textAtColumn:19 statement:statement];
  626. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  627. NSString* terms = [self textAtColumn:21 statement:statement];
  628. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  629. NSString* port_destination = [self textAtColumn:23 statement:statement];
  630. NSString* etd = [self textAtColumn:24 statement:statement];
  631. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  632. if(sign_picpath.length==0)
  633. sign_picpath=nil;
  634. NSString* create_date = [self textAtColumn:27 statement:statement];
  635. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  636. NSString* general_notes=[self textAtColumn:33 statement:statement];
  637. NSString *customerID = [self textAtColumn:34 statement:statement];
  638. if (customerID.length == 0) {
  639. customerID = nil;
  640. }
  641. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  642. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  643. if(general_notes.length>0)
  644. {
  645. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  646. row2[@"item_0"]=@{@"g_notes":general_notes };
  647. row2[@"count"]=[NSNumber numberWithInt:1];
  648. datasource[@"row2"]=row2;
  649. }
  650. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  651. [billto_arr addObject:billing_ext];
  652. [billto_arr addObject:billing_pf];
  653. [billto_arr addObject:billing_contact];
  654. [billto_arr addObject:billing_email];
  655. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  656. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  657. [receive_arr addObject:receive_ext];
  658. [receive_arr addObject:receive_pf];
  659. [receive_arr addObject:receive_contact];
  660. [receive_arr addObject:receive_email];
  661. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  662. float shipping = sqlite3_column_double(statement, 28);
  663. float lift_gate_value = sqlite3_column_double(statement, 29);
  664. float handling_fee_value = sqlite3_column_double(statement, 30);
  665. data[@"company"]=sender_name;
  666. data[@"shipfrom_addr"]=sender_ext;
  667. data[@"shipfrom_phone"]=sender_phone;
  668. data[@"shipfrom_fax"]=sender_fax;
  669. data[@"shipfrom_email"]=sender_email;
  670. data[@"warehouse_name"]=warehouse_name;
  671. data[@"bill_to_name"]=billing_name;
  672. data[@"bill_to_ext"]=billto;
  673. data[@"ship_to_name"]=receive_name;
  674. data[@"ship_to_ext"]=receive;
  675. data[@"po_no"]=poNumber;
  676. data[@"rep"]=sales_rep;
  677. data[@"e_schdule"]=etd;
  678. data[@"sales_terms"]=sales_terms;
  679. data[@"port_destination"]=port_destination;
  680. data[@"terms"]=terms;
  681. data[@"carrier"]=carrier;
  682. data[@"vendor_no"]=vendor_no;
  683. data[@"customer_sign"]=sign_picpath;
  684. data[@"print_status"]=print_status;
  685. data[@"create_date"]=create_date;
  686. 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];;
  687. sqlite3_stmt * statement_cart;
  688. // NSDate *date1 = [NSDate date];
  689. int count=0;
  690. // int cart_count=0;
  691. float credits=0;
  692. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  693. if ( dbresult== SQLITE_OK)
  694. {
  695. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  696. int row1count=0;
  697. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  698. {
  699. // NSDate *row_date = [NSDate date];
  700. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  701. // int product_id = sqlite3_column_int(statement_cart, 0);
  702. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  703. int item_id = sqlite3_column_int(statement_cart, 7);
  704. double discount = sqlite3_column_double(statement_cart, 2);
  705. NSString* Price=nil;
  706. if(str_price==nil)
  707. {
  708. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  709. // NSDate *price_date = [NSDate date];
  710. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  711. // DebugLog(@"price time interval");
  712. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  713. if(price==nil)
  714. Price=@"No Price.";
  715. else
  716. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  717. }
  718. else
  719. {
  720. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  721. }
  722. float dunitprice=0;
  723. int item_count = sqlite3_column_int(statement_cart, 3);
  724. NSString* Amount=@"";
  725. if(![Price isEqualToString:@"No Price."])
  726. {
  727. dunitprice=[Price floatValue];
  728. dunitprice=dunitprice* (1-discount/100.0);
  729. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  730. credits+=dunitprice*item_count;
  731. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  732. }
  733. else
  734. {
  735. Price=@"";
  736. }
  737. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  738. // NSString *nsline_note=nil;
  739. // if(line_note!=nil)
  740. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  741. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  742. NSString *nsname=nil;
  743. if(name!=nil)
  744. nsname= [[NSString alloc]initWithUTF8String:name];
  745. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  746. NSString *nsdescription=nil;
  747. if(description!=nil)
  748. nsdescription= [[NSString alloc]initWithUTF8String:description];
  749. // int stockUom = sqlite3_column_int(statement_cart, 8);
  750. // int _id = sqlite3_column_int(statement_cart, 9);
  751. // NSDate *subtotal_date = [NSDate date];
  752. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  753. // DebugLog(@"subtotal_date time interval");
  754. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  755. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  756. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  757. // int carton=[bsubtotaljson[@"carton"] intValue];
  758. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  759. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  760. // NSDate *img_date = [NSDate date];
  761. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  762. // DebugLog(@"img_date time interval");
  763. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  764. itemjson[@"model"]=nsname;
  765. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  766. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  767. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  768. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  769. // itemjson[@"check"]=@"true";
  770. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  771. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  772. itemjson[@"unit_price"]=Price;
  773. itemjson[@"amount"]=Amount;
  774. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  775. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  776. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  777. // itemjson[@"note"]=nsline_note;
  778. // NSDate *date2 = [NSDate date];
  779. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  780. // DebugLog(@"model_bundle time interval");
  781. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  782. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  783. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  784. row1count++;
  785. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  786. for(int c=0;c<[combine[@"count"] intValue];c++)
  787. {
  788. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  789. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  790. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  791. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  792. credits+=combine_amount;
  793. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  794. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  795. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  796. row1count++;
  797. }
  798. count++;
  799. // DebugLog(@"row time interval");
  800. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  801. }
  802. row1[@"count"]=[NSNumber numberWithInt:row1count];
  803. datasource[@"row1"]=row1;
  804. data[@"table_model"]=datasource;
  805. sqlite3_finalize(statement_cart);
  806. }
  807. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  808. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  809. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  810. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  811. // if(credits>0)
  812. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  813. // else
  814. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  815. if(shipping>0)
  816. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  817. else
  818. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  819. if(lift_gate_value>0)
  820. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  821. else
  822. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  823. if(handling_fee_value>0)
  824. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  825. else
  826. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  827. float total = credits+shipping+lift_gate_value+handling_fee_value;
  828. if(total>0)
  829. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  830. else
  831. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  832. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  833. fee[@"row0"]=fee_row0;
  834. data[@"table_fee"]=fee;
  835. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  836. // ret[@"sign_url"] = sign_url;
  837. //
  838. // customer_contact = [self textAtColumn:52 statement:statement];
  839. // customer_email = [self textAtColumn:53 statement:statement];
  840. // customer_phone = [self textAtColumn:54 statement:statement];
  841. // customer_fax = [self textAtColumn:55 statement:statement];
  842. //
  843. //
  844. // int offline_edit=sqlite3_column_int(statement, 56);
  845. //
  846. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  847. //
  848. // char *soid = (char*)sqlite3_column_text(statement, 1);
  849. // if(soid==nil)
  850. // soid= "";
  851. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  852. // // so#
  853. // ret[@"so#"] = nssoid;
  854. //
  855. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  856. // if(poNumber==nil)
  857. // poNumber= "";
  858. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  859. //
  860. //
  861. //
  862. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  863. // if(create_time==nil)
  864. // create_time= "";
  865. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  866. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  867. //
  868. // int status = sqlite3_column_int(statement, 4);
  869. // int erpStatus = sqlite3_column_int(statement, 49);
  870. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  871. //
  872. // // status
  873. // if (status > 1 && status != 3) {
  874. // status = erpStatus;
  875. // } else if (status == 3) {
  876. // status = 15;
  877. // }
  878. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  879. // ret[@"order_status"] = nsstatus;
  880. //
  881. //
  882. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  883. // if(company_name==nil)
  884. // company_name= "";
  885. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  886. // // company name
  887. // ret[@"company_name"] = nscompany_name;
  888. //
  889. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  890. // if(customer_contact==nil)
  891. // customer_contact= "";
  892. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  893. //
  894. //
  895. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  896. // if(addr_1==nil)
  897. // addr_1="";
  898. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  899. //
  900. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  901. // if(addr_2==nil)
  902. // addr_2="";
  903. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  904. //
  905. //
  906. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  907. // if(addr_3==nil)
  908. // addr_3="";
  909. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  910. //
  911. //
  912. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  913. // if(addr_4==nil)
  914. // addr_4="";
  915. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  916. //
  917. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  918. // [arr_addr addObject:nsaddr_1];
  919. // [arr_addr addObject:nsaddr_2];
  920. // [arr_addr addObject:nsaddr_3];
  921. // [arr_addr addObject:nsaddr_4];
  922. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  923. //
  924. //
  925. // char *logist = (char*)sqlite3_column_text(statement, 11);
  926. // if(logist==nil)
  927. // logist= "";
  928. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  929. // if (status == -11 || status == 10 || status == 11) {
  930. // nslogist = [self textAtColumn:59 statement:statement];
  931. // };
  932. //
  933. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  934. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  935. // shipping = @"Shipping To Be Quoted";
  936. // } else {
  937. // shippingFee = sqlite3_column_double(statement, 12);
  938. // }
  939. //
  940. // // Shipping
  941. // ret[@"Shipping"] = shipping;
  942. //
  943. // int have_lift_gate = sqlite3_column_int(statement, 17);
  944. // lift_gate = sqlite3_column_double(statement, 13);
  945. // // Liftgate Fee(No loading dock)
  946. // if (!have_lift_gate) {
  947. // lift_gate = 0;
  948. // }
  949. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  950. // if (sqlite3_column_int(statement, 57)) {
  951. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  952. // }
  953. //
  954. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  955. // if(general_notes==nil)
  956. // general_notes= "";
  957. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  958. //
  959. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  960. // if(internal_notes==nil)
  961. // internal_notes= "";
  962. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  963. //
  964. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  965. // if(payment_type==nil)
  966. // payment_type= "";
  967. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  968. //
  969. //
  970. //
  971. // // order info
  972. // orderinfo = [self textFileName:@"order_info.html"];
  973. //
  974. //
  975. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  978. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  979. //
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  987. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  988. //
  989. // NSString *payment = nil;
  990. // if([nspayment_type isEqualToString:@"Credit Card"])
  991. // {
  992. // payment = [self textFileName:@"creditcardpayment.html"];
  993. //
  994. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  995. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  996. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  997. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  998. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  999. // NSString *card_type = [self textAtColumn:42 statement:statement];
  1000. // if (card_type.length > 0) { // 显示星号
  1001. // card_type = @"****";
  1002. // }
  1003. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1004. // if (card_number.length > 0 && card_number.length > 4) {
  1005. // for (int i = 0; i < card_number.length - 4; i++) {
  1006. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1007. // }
  1008. // } else {
  1009. // card_number = @"";
  1010. // }
  1011. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1012. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1013. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1014. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1015. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1016. // card_expiration = @"****";
  1017. // }
  1018. //
  1019. //
  1020. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1021. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1022. //
  1023. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1024. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1025. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1026. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1027. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1028. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1029. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1030. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1031. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1032. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1033. //
  1034. //
  1035. // }
  1036. // else
  1037. // {
  1038. // payment=[self textFileName:@"normalpayment.html"];
  1039. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1040. // }
  1041. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1042. //
  1043. // ret[@"result"]= [NSNumber numberWithInt:2];
  1044. //
  1045. // // more info
  1046. // moreInfo = [self textFileName:@"more_info.html"];
  1047. //
  1048. //
  1049. // // ShipToCompany_or_&nbsp
  1050. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1051. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1052. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1054. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1056. //
  1057. //
  1058. // // ShipFromCompany_or_&nbsp
  1059. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1060. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1061. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1063. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1065. //
  1066. //
  1067. // // FreightBillToCompany_or_&nbsp
  1068. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1069. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1070. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1071. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1072. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1074. //
  1075. //
  1076. // // MerchandiseBillToCompany_or_&nbsp
  1077. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1078. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1079. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1080. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1081. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1082. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1083. //
  1084. //
  1085. // // ReturnToCompany_or_&nbsp
  1086. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1087. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1088. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1089. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1090. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1091. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1092. // //
  1093. //
  1094. // // DebugLog(@"more info : %@",moreInfo);
  1095. //
  1096. // // handling fee
  1097. // handlingFee = sqlite3_column_double(statement, 33);
  1098. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1099. // if (sqlite3_column_int(statement, 58)) {
  1100. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1101. // }
  1102. // //
  1103. //
  1104. // // customer info
  1105. // customerID = [self textAtColumn:36 statement:statement];
  1106. //
  1107. // // mode
  1108. // ret[@"mode"] = appDelegate.mode;
  1109. //
  1110. // // model_count
  1111. // ret[@"model_count"] = @(0);
  1112. }
  1113. sqlite3_finalize(statement);
  1114. }
  1115. [iSalesDB close_db:db];
  1116. data[@"order_type"]=order_type;
  1117. /*
  1118. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1119. // "customer_email" = "Shui Hu";
  1120. // "customer_fax" = "";
  1121. // "customer_first_name" = F;
  1122. // "customer_last_name" = L;
  1123. // "customer_name" = ",da He Xiang Dong Liu A";
  1124. // "customer_phone" = "Hey Xuan Feng";
  1125. contactInfo[@"customer_phone"] = customer_phone;
  1126. contactInfo[@"customer_fax"] = customer_fax;
  1127. contactInfo[@"customer_email"] = customer_email;
  1128. NSString *first_name = @"";
  1129. NSString *last_name = @"";
  1130. if ([customer_contact isEqualToString:@""]) {
  1131. } else if ([customer_contact containsString:@" "]) {
  1132. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1133. first_name = [customer_contact substringToIndex:first_space_index];
  1134. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1135. }
  1136. contactInfo[@"customer_first_name"] = first_name;
  1137. contactInfo[@"customer_last_name"] = last_name;
  1138. ret[@"customerInfo"] = contactInfo;
  1139. // models
  1140. if (nssoid) {
  1141. __block double TotalCuft = 0;
  1142. __block double TotalWeight = 0;
  1143. __block int TotalCarton = 0;
  1144. __block double allItemPrice = 0;
  1145. 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];
  1146. sqlite3 *db1 = [iSalesDB get_db];
  1147. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1148. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1149. int product_id = sqlite3_column_int(stmt, 0);
  1150. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1151. int item_id = sqlite3_column_int(stmt, 7);
  1152. NSString* Price=nil;
  1153. if(str_price==nil)
  1154. {
  1155. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1156. if(price==nil)
  1157. Price=@"No Price.";
  1158. else
  1159. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1160. }
  1161. else
  1162. {
  1163. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1164. }
  1165. double discount = sqlite3_column_double(stmt, 2);
  1166. int item_count = sqlite3_column_int(stmt, 3);
  1167. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1168. NSString *nsline_note=nil;
  1169. if(line_note!=nil)
  1170. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1171. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1172. // NSString *nsname = nil;
  1173. // if(name!=nil)
  1174. // nsname= [[NSString alloc]initWithUTF8String:name];
  1175. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1176. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1177. // NSString *nsdescription=nil;
  1178. // if(description!=nil)
  1179. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1180. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1181. // int stockUom = sqlite3_column_int(stmt, 8);
  1182. // int _id = sqlite3_column_int(stmt, 9);
  1183. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1184. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1185. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1186. int carton=[bsubtotaljson[@"carton"] intValue];
  1187. TotalCuft += cuft;
  1188. TotalWeight += weight;
  1189. TotalCarton += carton;
  1190. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1191. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1192. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1193. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1194. itemjson[@"note"]=nsline_note;
  1195. itemjson[@"origin_price"] = Price;
  1196. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1197. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1198. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1199. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1200. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1201. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1202. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1203. if(itemjson[@"combine"] != nil)
  1204. {
  1205. // int citem=0;
  1206. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1207. for(int bc=0;bc<bcount;bc++)
  1208. {
  1209. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1210. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1211. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1212. subTotal += uprice * modulus * item_count;
  1213. }
  1214. }
  1215. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1216. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1217. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1218. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1219. allItemPrice += subTotal;
  1220. }];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1223. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1224. // payments/Credits
  1225. // payments_and_credist = sqlite3_column_double(statement, 34);
  1226. payments_and_credist = allItemPrice;
  1227. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1228. // // total
  1229. // totalPrice = sqlite3_column_double(statement, 35);
  1230. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1231. } else {
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1234. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1235. // payments/Credits
  1236. payments_and_credist = 0;
  1237. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1238. }
  1239. // total
  1240. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1241. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1242. ret[@"order_info"]= orderinfo;
  1243. ret[@"more_order_info"] = moreInfo;
  1244. return [RAConvertor dict2data:ret];
  1245. */
  1246. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1247. return data;
  1248. }
  1249. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1250. {
  1251. assert(params[@"user"]!=nil);
  1252. NSString* template_file=nil;
  1253. switch([params[@"thumb"] intValue])
  1254. {
  1255. case 0:
  1256. template_file= @"so.json";
  1257. break;
  1258. case 1:
  1259. template_file= @"so_thumb.json";
  1260. break;
  1261. default:
  1262. template_file= @"so.json";
  1263. }
  1264. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1265. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1266. if(data[@"table_model"][@"row2"]==nil)
  1267. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1268. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1269. DebugLog(@"%@",file);
  1270. // return nil;
  1271. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1272. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1273. if (file) {
  1274. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1275. [dic setObject:file forKey:@"pdf_path"];
  1276. dic[@"isLocalFile"]=@"true";
  1277. return [RAConvertor dict2data:dic];
  1278. }
  1279. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1280. dic[@"isLocalFile"]=@"true";
  1281. return [RAConvertor dict2data:dic];
  1282. }
  1283. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1284. {
  1285. // assert(params[@"contact_id"]!=nil);
  1286. // assert(params[@"user"]!=nil);
  1287. if(params[@"user"]==nil)
  1288. {
  1289. AppDelegate *appDelegate = nil;
  1290. //some UI methods ej
  1291. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1292. if(appDelegate.user!=nil)
  1293. [params setValue:appDelegate.user forKey:@"user"];
  1294. }
  1295. NSString* template_file=nil;
  1296. switch([params[@"pdf_style"] intValue])
  1297. {
  1298. case 0:
  1299. template_file= @"portfolio_2x3.json";
  1300. break;
  1301. case 1:
  1302. template_file= @"portfolio_3x2.json";
  1303. break;
  1304. default:
  1305. template_file= @"portfolio_3x2.json";
  1306. }
  1307. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1308. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1309. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1310. DebugLog(@"%@",file);
  1311. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1312. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1313. if (file) {
  1314. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1315. [dic setObject:file forKey:@"pdf_path"];
  1316. dic[@"isLocalFile"]=@"true";
  1317. return [RAConvertor dict2data:dic];
  1318. }
  1319. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1320. dic[@"isLocalFile"]=@"true";
  1321. return [RAConvertor dict2data:dic];
  1322. }
  1323. +(NSString*) get_offline_soid:(sqlite3*)db
  1324. {
  1325. NSString* soid=nil;
  1326. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1327. [formatter setDateFormat:@"yyMMdd"];
  1328. NSString* date = [formatter stringFromDate:[NSDate date]];
  1329. for(int i=1;i<999;i++)
  1330. {
  1331. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1332. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1333. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1334. if(count==0)
  1335. return soid;
  1336. }
  1337. return nil;
  1338. }
  1339. +(NSArray*) enumOfflineOrder
  1340. {
  1341. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1342. sqlite3 *db = [iSalesDB get_db];
  1343. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1344. sqlite3_stmt * statement;
  1345. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1346. if ( dbresult== SQLITE_OK)
  1347. {
  1348. while (sqlite3_step(statement) == SQLITE_ROW)
  1349. {
  1350. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1351. NSString* nsso_id=nil;
  1352. if(so_id!=nil)
  1353. {
  1354. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1355. }
  1356. [ret addObject:nsso_id];
  1357. }
  1358. sqlite3_finalize(statement);
  1359. }
  1360. [iSalesDB close_db:db];
  1361. return ret;
  1362. }
  1363. //+(void) uploadFile:(NSString*) file
  1364. //{
  1365. // NSData* data = [NSData dataWithContentsOfFile: file];
  1366. // UIApplication * app = [UIApplication sharedApplication];
  1367. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1368. //
  1369. //
  1370. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1371. //
  1372. //
  1373. //
  1374. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1375. //
  1376. // if(appDelegate.user!=nil)
  1377. // [params setValue:appDelegate.user forKey:@"user"];
  1378. // // if(appDelegate.contact_id!=nil)
  1379. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1380. // if(appDelegate.password!=nil)
  1381. // [params setValue:appDelegate.password forKey:@"password"];
  1382. //
  1383. //
  1384. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1385. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1386. // } error:nil];
  1387. //
  1388. //
  1389. //
  1390. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1391. //
  1392. // NSProgress *progress = nil;
  1393. //
  1394. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1395. //
  1396. //
  1397. //
  1398. // if (error) {
  1399. //
  1400. // NSString* err_msg = [error localizedDescription];
  1401. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1402. //
  1403. //
  1404. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1405. //
  1406. // DebugLog(@"data string: %@",str);
  1407. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1408. //
  1409. // } else {
  1410. // DebugLog(@"response ");
  1411. //
  1412. //
  1413. //
  1414. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1415. //
  1416. // // 再将NSData转为字符串
  1417. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1418. //
  1419. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1420. //
  1421. // DebugLog(@"data string: %@",jsonStr);
  1422. //
  1423. // NSDictionary* json = responseObject;
  1424. //
  1425. //
  1426. // if([[json valueForKey:@"result"] intValue]==2)
  1427. // {
  1428. //// NSString* img_url_down = json[@"img_url_aname"];
  1429. //// NSString* img_url_up = json[@"img_url"];
  1430. //
  1431. //
  1432. // }
  1433. // else
  1434. // {
  1435. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1436. // }
  1437. //
  1438. //
  1439. // }
  1440. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1441. // }];
  1442. //
  1443. //
  1444. //
  1445. //
  1446. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1447. // //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. //
  1454. // [uploadTask resume];
  1455. //
  1456. //}
  1457. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1458. //{
  1459. //
  1460. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1461. // NSString *documents = [paths objectAtIndex:0];
  1462. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1463. //
  1464. //
  1465. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1466. //
  1467. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1468. //
  1469. //
  1470. //
  1471. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1472. //
  1473. //
  1474. //
  1475. //
  1476. //
  1477. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1478. //
  1479. //
  1480. //
  1481. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1482. //
  1483. //
  1484. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1485. //
  1486. //
  1487. //
  1488. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1489. //
  1490. //
  1491. //
  1492. //
  1493. // NSMutableDictionary* orderlist = nil;
  1494. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1495. //
  1496. //
  1497. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1498. //
  1499. //
  1500. // orderlist = [NSMutableDictionary new];
  1501. //
  1502. //// [orderlist addObject:appDelegate.order_code];
  1503. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1504. //
  1505. // }
  1506. // else
  1507. // {
  1508. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1509. //
  1510. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1511. //
  1512. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1513. // }
  1514. //
  1515. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1516. // orderitem[@"create_by"] = appDelegate.user;
  1517. // orderitem[@"price"] = upparams[@"total_price"];
  1518. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1519. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1520. // orderitem[@"order_status"] = @"Saved Order";
  1521. // orderitem[@"order_code"] = appDelegate.order_code;
  1522. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1523. //
  1524. //
  1525. // int count = [orderlist[@"count"] intValue];
  1526. //
  1527. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1528. //
  1529. // newlist[@"item_0"]=orderitem;
  1530. // int newcount = 1;
  1531. // for(int i=0;i<count;i++)
  1532. // {
  1533. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1534. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1535. // continue;
  1536. // else
  1537. // {
  1538. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1539. // newcount++;
  1540. // }
  1541. // }
  1542. // newlist[@"count"] = @(newcount);
  1543. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1544. //
  1545. // return;
  1546. //}
  1547. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1548. {
  1549. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1550. // NSString *documents = [paths objectAtIndex:0];
  1551. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1552. NSString *userdir = [OLDataProvider getUserPath];
  1553. NSMutableDictionary* orderlist = nil;
  1554. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1555. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1556. // order list 为空,不用维护
  1557. #ifdef RA_NOTIFICATION
  1558. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1559. #else
  1560. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1561. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1562. #endif
  1563. return;
  1564. // [orderlist addObject:appDelegate.order_code];
  1565. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1566. }
  1567. else
  1568. {
  1569. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1570. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1571. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1572. }
  1573. //==================== order list 删除
  1574. int count = [orderlist[@"count"] intValue];
  1575. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1576. NSMutableDictionary* submititem =nil;
  1577. int newcount = 0;
  1578. for(int i=0;i<count;i++)
  1579. {
  1580. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1581. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1582. {
  1583. submititem = item;
  1584. continue;
  1585. }
  1586. else
  1587. {
  1588. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1589. newcount++;
  1590. }
  1591. }
  1592. newlist[@"count"] = @(newcount);
  1593. [RAUtils dicttofile:orderlistPath dict:newlist];
  1594. //////////////////---=============================== submitlist 添加
  1595. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1596. NSMutableDictionary* submitlist = nil;
  1597. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1598. submitlist = [NSMutableDictionary new];
  1599. }
  1600. else
  1601. {
  1602. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1603. }
  1604. int submitcount =[submitlist[@"count"] intValue];
  1605. submititem[@"order_status"] = soid;//@"Submitted Order";
  1606. submititem[@"customer_name"] = company;//@"Submitted Order";
  1607. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1608. submitlist[@"count"] = @(submitcount+1);
  1609. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1610. #ifdef RA_NOTIFICATION
  1611. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1612. #else
  1613. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1614. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1615. #endif
  1616. }
  1617. +(void) save2submitScanOrder1
  1618. {
  1619. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1620. // NSString *documents = [paths objectAtIndex:0];
  1621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1622. NSString *userdir = [OLDataProvider getUserPath];
  1623. NSMutableDictionary* orderlist = nil;
  1624. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1625. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1626. // order list 为空,不用维护
  1627. #ifdef RA_NOTIFICATION
  1628. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1629. #else
  1630. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1631. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1632. #endif
  1633. return;
  1634. // [orderlist addObject:appDelegate.order_code];
  1635. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1636. }
  1637. else
  1638. {
  1639. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1640. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1641. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1642. }
  1643. int count = [orderlist[@"count"] intValue];
  1644. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1645. int newcount = 0;
  1646. for(int i=0;i<count;i++)
  1647. {
  1648. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1649. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1650. continue;
  1651. else
  1652. {
  1653. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1654. newcount++;
  1655. }
  1656. }
  1657. newlist[@"count"] = @(newcount);
  1658. [RAUtils dicttofile:orderlistPath dict:newlist];
  1659. #ifdef RA_NOTIFICATION
  1660. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1661. #else
  1662. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1663. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1664. #endif
  1665. }
  1666. +(void) createScanTempFolder
  1667. {
  1668. NSString* scanFilePath = [self getScanTempPath];
  1669. BOOL bdir=YES;
  1670. NSFileManager* fileManager = [NSFileManager defaultManager];
  1671. if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1672. {
  1673. NSError *error = nil;
  1674. bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1675. if(!bsuccess)
  1676. DebugLog(@"Create cache folder failed");
  1677. // if(bsuccess)
  1678. // {
  1679. // sqlite3 *db = [self get_db];
  1680. //
  1681. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1682. // [iSalesDB close_db:db];
  1683. // }
  1684. }
  1685. }
  1686. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1687. {
  1688. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1689. NSString *documents = [paths objectAtIndex:0];
  1690. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1691. NSString* servername = addressDic[@"name"];
  1692. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1693. NSString* user = appDelegate.user;
  1694. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1695. orderpath = [orderpath stringByAppendingPathComponent:user];
  1696. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1697. BOOL bdir=YES;
  1698. NSFileManager* fileManager = [NSFileManager defaultManager];
  1699. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1700. {
  1701. NSError *error = nil;
  1702. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1703. if(!bsuccess)
  1704. DebugLog(@"Create cache folder failed");
  1705. // if(bsuccess)
  1706. // {
  1707. // sqlite3 *db = [self get_db];
  1708. //
  1709. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1710. // [iSalesDB close_db:db];
  1711. // }
  1712. }
  1713. return orderpath;
  1714. }
  1715. +(NSString*) getScanOrderPath
  1716. {
  1717. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1718. NSString *documents = [paths objectAtIndex:0];
  1719. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1720. NSString* servername = addressDic[@"name"];
  1721. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1722. NSString* user = appDelegate.user;
  1723. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1724. orderpath = [orderpath stringByAppendingPathComponent:user];
  1725. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1726. BOOL bdir=YES;
  1727. NSFileManager* fileManager = [NSFileManager defaultManager];
  1728. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1729. {
  1730. NSError *error = nil;
  1731. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1732. if(!bsuccess)
  1733. DebugLog(@"Create cache folder failed");
  1734. // if(bsuccess)
  1735. // {
  1736. // sqlite3 *db = [self get_db];
  1737. //
  1738. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1739. // [iSalesDB close_db:db];
  1740. // }
  1741. }
  1742. return orderpath;
  1743. }
  1744. +(NSString*) getScanTempPath
  1745. {
  1746. if(RASingleton.sharedInstance.scan_temp_code == nil)
  1747. RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1748. return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1749. }
  1750. +(void) moveScanTemp2Order
  1751. {
  1752. NSString *orderdir = [OLDataProvider getScanOrderPath];
  1753. NSError *error = nil;
  1754. NSString* tempdir = [OLDataProvider getScanTempPath];
  1755. // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1756. // for(int i=0;i<arr_files.count;i++)
  1757. // {
  1758. // NSString* file=arr_files[i];
  1759. //
  1760. //
  1761. // }
  1762. [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1763. }
  1764. +(NSString*) getUserPath
  1765. {
  1766. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1767. NSString *documents = [paths objectAtIndex:0];
  1768. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1769. NSString* servername = addressDic[@"name"];
  1770. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1771. NSString* user = appDelegate.user;
  1772. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1773. userpath = [userpath stringByAppendingPathComponent:user];
  1774. return userpath;
  1775. }
  1776. +(NSString*) getScanPath
  1777. {
  1778. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1779. if(appDelegate.order_code!=nil)
  1780. {
  1781. return [self getScanOrderPath];
  1782. }
  1783. else
  1784. {
  1785. [self createScanTempFolder];
  1786. return [self getScanTempPath];
  1787. }
  1788. }
  1789. +(NSString*) createScanTempCode
  1790. {
  1791. return [[NSUUID new] UUIDString];
  1792. }
  1793. +(NSString*) scanTemplatePath:(NSString*)file
  1794. {
  1795. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1796. NSString *documents = [paths objectAtIndex:0];
  1797. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1798. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1799. return templatefile;
  1800. }
  1801. +(void) realguestLogin
  1802. {
  1803. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1804. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1805. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1806. }
  1807. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1808. {
  1809. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1810. NSString *documents = [paths objectAtIndex:0];
  1811. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1812. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1813. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1814. return [dict mutableCopy];
  1815. }
  1816. +(void) deleteSubmittedOrder:(NSString*) order_code
  1817. {
  1818. NSString *userdir = [OLDataProvider getUserPath];
  1819. if(order_code.length==0)
  1820. return;
  1821. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1822. // NSString *documents = [paths objectAtIndex:0];
  1823. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1824. if([appDelegate.order_code isEqualToString:order_code])
  1825. {
  1826. //要删除的订单是当前打开的订单;
  1827. appDelegate.order_code = nil;
  1828. appDelegate.customerInfo = nil;
  1829. RASingleton.sharedInstance.scan_cart = nil;
  1830. appDelegate.contact_id = nil;
  1831. // [appDelegate updateScanButton:false];
  1832. [appDelegate update_count_mark];
  1833. }
  1834. //删除订单目录
  1835. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1836. //维护订单列表
  1837. NSMutableDictionary* orderlist = nil;
  1838. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1839. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1840. // order list 为空,不用维护
  1841. #ifdef RA_NOTIFICATION
  1842. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1843. #else
  1844. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1845. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1846. #endif
  1847. return;
  1848. // [orderlist addObject:appDelegate.order_code];
  1849. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1850. }
  1851. else
  1852. {
  1853. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1854. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1855. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1856. }
  1857. int count = [orderlist[@"count"] intValue];
  1858. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1859. int newcount = 0;
  1860. for(int i=0;i<count;i++)
  1861. {
  1862. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1863. if([item[@"order_code"] isEqualToString: order_code ])
  1864. continue;
  1865. else
  1866. {
  1867. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1868. newcount++;
  1869. }
  1870. }
  1871. newlist[@"count"] = @(newcount);
  1872. [RAUtils dicttofile:orderlistPath dict:newlist];
  1873. #ifdef RA_NOTIFICATION
  1874. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1875. #else
  1876. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1877. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1878. #endif
  1879. }
  1880. +(void) updateScanOrder:(NSString*) order_code
  1881. {
  1882. NSString *userdir = [OLDataProvider getUserPath];
  1883. if(order_code.length==0)
  1884. return;
  1885. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1886. // NSString *documents = [paths objectAtIndex:0];
  1887. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1888. NSMutableDictionary* orderlist = nil;
  1889. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1890. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1891. // order list 为空,不用维护
  1892. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1893. return;
  1894. // [orderlist addObject:appDelegate.order_code];
  1895. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1896. }
  1897. else
  1898. {
  1899. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1900. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1901. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1902. }
  1903. int count = [orderlist[@"count"] intValue];
  1904. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1905. float p =0;
  1906. float s =0;
  1907. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1908. {
  1909. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1910. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1911. int mcount = [cartitem[@"count"] intValue];
  1912. double msurcharge =0;
  1913. if(RASingleton.sharedInstance.price_type==1)
  1914. {
  1915. if([cartitem[@"special_price"] boolValue])
  1916. {
  1917. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price2"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1918. }
  1919. else
  1920. {
  1921. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price1"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1922. }
  1923. }
  1924. s+=msurcharge;
  1925. }
  1926. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1927. int order_qty=0;
  1928. for(int c=0;c<[section[@"count"] intValue];c++)
  1929. {
  1930. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1931. order_qty+=[item[@"count"] intValue];
  1932. }
  1933. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1934. // int newcount = 0;
  1935. for(int i=0;i<count;i++)
  1936. {
  1937. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1938. if([item[@"order_code"] isEqualToString: order_code ])
  1939. {
  1940. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1941. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1942. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1943. }
  1944. }
  1945. // newlist[@"count"] = @(newcount);
  1946. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1947. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1948. }
  1949. +(void) deleteScanOrder:(NSString*) order_code
  1950. {
  1951. NSString *userdir = [OLDataProvider getUserPath];
  1952. if(order_code.length==0)
  1953. return;
  1954. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1955. // NSString *documents = [paths objectAtIndex:0];
  1956. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1957. if([appDelegate.order_code isEqualToString:order_code])
  1958. {
  1959. //要删除的订单是当前打开的订单;
  1960. appDelegate.order_code = nil;
  1961. appDelegate.customerInfo = nil;
  1962. RASingleton.sharedInstance.scan_cart = nil;
  1963. appDelegate.contact_id = nil;
  1964. // [appDelegate updateScanButton:false];
  1965. [appDelegate update_count_mark];
  1966. }
  1967. //删除订单目录
  1968. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1969. //维护订单列表
  1970. NSMutableDictionary* orderlist = nil;
  1971. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1972. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1973. // order list 为空,不用维护
  1974. #ifdef RA_NOTIFICATION
  1975. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1976. #else
  1977. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1978. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1979. #endif
  1980. return;
  1981. // [orderlist addObject:appDelegate.order_code];
  1982. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1983. }
  1984. else
  1985. {
  1986. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1987. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1988. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1989. }
  1990. int count = [orderlist[@"count"] intValue];
  1991. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1992. int newcount = 0;
  1993. for(int i=0;i<count;i++)
  1994. {
  1995. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1996. if([item[@"order_code"] isEqualToString: order_code ])
  1997. continue;
  1998. else
  1999. {
  2000. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2001. newcount++;
  2002. }
  2003. }
  2004. newlist[@"count"] = @(newcount);
  2005. [RAUtils dicttofile:orderlistPath dict:newlist];
  2006. #ifdef RA_NOTIFICATION
  2007. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2008. #else
  2009. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2010. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2011. #endif
  2012. }
  2013. +(void) saveScanCart:(NSMutableDictionary*) cart
  2014. {
  2015. if(cart==nil)
  2016. return;
  2017. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2018. // NSString *documents = [paths objectAtIndex:0];
  2019. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2020. if(RASingleton.sharedInstance.price_type==0)
  2021. {
  2022. NSMutableDictionary * section = cart[@"section_0"];
  2023. int count = [section[@"count"] intValue];
  2024. for(int i=0;i<count;i++)
  2025. {
  2026. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2027. [item removeObjectForKey:@"discount"];
  2028. [item removeObjectForKey:@"note"];
  2029. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2030. }
  2031. cart[@"section_0"]=section;
  2032. }
  2033. NSString *orderdir = [self getScanPath];
  2034. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2035. [RAUtils dicttofile:cartpath dict:cart];
  2036. }
  2037. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2038. {
  2039. assert(add_params[@"user"]!=nil);
  2040. // assert(add_params[@"contact_id"]!=nil);
  2041. // assert(add_params[@"password"]!=nil);
  2042. NSString* serial= [[NSUUID UUID] UUIDString];
  2043. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2044. NSString *cachefolder = [paths objectAtIndex:0];
  2045. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2046. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2047. NSFileManager* fileManager = [NSFileManager defaultManager];
  2048. BOOL bdir=YES;
  2049. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2050. {
  2051. NSError *error = nil;
  2052. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2053. if(!bsuccess)
  2054. DebugLog(@"Create UPLOAD folder failed");
  2055. }
  2056. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2057. ret[@"contact"]=[self prepareContact:serial];
  2058. ret[@"wishlist"]=[self prepareWishlist:serial];
  2059. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2060. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2061. ret[@"view_portfolio"] = [self preparePDF:serial];
  2062. NSString* str= [RAConvertor dict2string:ret];
  2063. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2064. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2065. NSError *error=nil;
  2066. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2067. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2068. ZipArchive* zip = [[ZipArchive alloc] init];
  2069. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2070. for(int i=0;i<arr_files.count;i++)
  2071. {
  2072. NSString* file=arr_files[i];
  2073. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2074. }
  2075. if( ![zip CloseZipFile2] )
  2076. {
  2077. zippath = @"";
  2078. }
  2079. ret[@"file"]=zippath;
  2080. return ret;
  2081. }
  2082. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2083. {
  2084. if(filename.length==0)
  2085. return false;
  2086. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2087. bool ret=false;
  2088. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2089. NSString *cachefolder = [paths objectAtIndex:0];
  2090. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2091. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2092. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2093. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2094. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2095. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2096. NSFileManager* fileManager = [NSFileManager defaultManager];
  2097. BOOL bdir=NO;
  2098. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2099. {
  2100. NSError *error = nil;
  2101. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2102. {
  2103. ret=false;
  2104. }
  2105. else
  2106. {
  2107. ret=true;
  2108. }
  2109. }
  2110. return ret;
  2111. }
  2112. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2113. {
  2114. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2115. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2116. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2117. NSString* where=@" 1=1";
  2118. if (ver!=nil) {
  2119. where=@"is_dirty=1";
  2120. }
  2121. sqlite3 *db = [iSalesDB get_db];
  2122. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2123. sqlite3_stmt * statement;
  2124. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2125. int count=0;
  2126. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2127. if ( dbresult== SQLITE_OK)
  2128. {
  2129. while (sqlite3_step(statement) == SQLITE_ROW)
  2130. {
  2131. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2132. int _id = sqlite3_column_int(statement, 0);
  2133. int product_id = sqlite3_column_int(statement, 1);
  2134. int item_id = sqlite3_column_int(statement, 2);
  2135. int qty = sqlite3_column_int(statement, 3);
  2136. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2137. NSString* nscreate_time=nil;
  2138. if(create_time!=nil)
  2139. {
  2140. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2141. }
  2142. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2143. NSString* nsmodify_time=nil;
  2144. if(modify_time!=nil)
  2145. {
  2146. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2147. }
  2148. int is_delete = sqlite3_column_int(statement, 6);
  2149. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2150. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2151. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2152. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2153. itemjson[@"create_time"]=nscreate_time;
  2154. itemjson[@"modify_time"]=nsmodify_time;
  2155. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2156. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2157. count++;
  2158. }
  2159. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2160. sqlite3_finalize(statement);
  2161. }
  2162. ret[@"count"]=[NSNumber numberWithInt:count ];
  2163. [iSalesDB close_db:db];
  2164. return ret;
  2165. }
  2166. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2167. {
  2168. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2169. // UIApplication * app = [UIApplication sharedApplication];
  2170. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2171. assert(add_params[@"user"]!=nil);
  2172. // assert(add_params[@"password"]!=nil);
  2173. 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 ];
  2174. // 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 ];
  2175. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2176. DebugLog(@"offline_login sql:%@",sqlQuery);
  2177. sqlite3_stmt * statement;
  2178. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2179. int count=0;
  2180. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2181. if ( dbresult== SQLITE_OK)
  2182. {
  2183. while (sqlite3_step(statement) == SQLITE_ROW)
  2184. {
  2185. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2186. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2187. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2188. int product_id = sqlite3_column_int(statement, 0);
  2189. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2190. int item_id = sqlite3_column_int(statement, 7);
  2191. NSString* Price=nil;
  2192. if(str_price==nil)
  2193. {
  2194. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2195. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2196. if(price==nil)
  2197. Price=@"No Price.";
  2198. else
  2199. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2200. }
  2201. else
  2202. {
  2203. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2204. }
  2205. double discount = sqlite3_column_double(statement, 2);
  2206. int item_count = sqlite3_column_int(statement, 3);
  2207. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2208. NSString *nsline_note=nil;
  2209. if(line_note!=nil)
  2210. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2211. char *name = (char*)sqlite3_column_text(statement, 5);
  2212. NSString *nsname=nil;
  2213. if(name!=nil)
  2214. nsname= [[NSString alloc]initWithUTF8String:name];
  2215. char *description = (char*)sqlite3_column_text(statement, 6);
  2216. NSString *nsdescription=nil;
  2217. if(description!=nil)
  2218. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2219. // int stockUom = sqlite3_column_int(statement, 8);
  2220. // int _id = sqlite3_column_int(statement, 9);
  2221. //
  2222. //
  2223. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2224. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2225. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2226. // int carton=[bsubtotaljson[@"carton"] intValue];
  2227. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2228. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2229. itemjson[@"model"]=nsname;
  2230. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2231. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2232. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2233. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2234. // itemjson[@"check"]=@"true";
  2235. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2236. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2237. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2238. itemjson[@"unit_price"]=Price;
  2239. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2240. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2241. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2242. itemjson[@"note"]=nsline_note;
  2243. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2244. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2245. count++;
  2246. }
  2247. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2248. sqlite3_finalize(statement);
  2249. }
  2250. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2251. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2252. return ret;
  2253. }
  2254. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2255. {
  2256. assert(add_params[@"user"]!=nil);
  2257. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2258. sqlite3 *db = [iSalesDB get_db];
  2259. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2260. // for(int i=0;i<arr_soid.count;i++)
  2261. sqlite3_stmt * statement;
  2262. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2263. //int count=0;
  2264. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2265. if ( dbresult== SQLITE_OK)
  2266. {
  2267. int count=0;
  2268. while (sqlite3_step(statement) == SQLITE_ROW)
  2269. {
  2270. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2271. int _id = sqlite3_column_int(statement, 0);
  2272. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2273. NSString* nssync_data=nil;
  2274. if(sync_data!=nil)
  2275. {
  2276. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2277. }
  2278. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2279. NSString* nsimg_1=nil;
  2280. if(img_1!=nil)
  2281. {
  2282. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2283. [self copy_upImg:serial file:nsimg_1];
  2284. }
  2285. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2286. NSString* nsso_no=nil;
  2287. if(so_no!=nil)
  2288. {
  2289. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2290. }
  2291. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2292. itemjson[@"order_type"]=@"submit order";
  2293. else
  2294. itemjson[@"order_type"]=@"archive order";
  2295. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2296. itemjson[@"json_data"]= nssync_data;
  2297. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2298. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2299. count++;
  2300. }
  2301. ret[@"count"]=[NSNumber numberWithInt:count ];
  2302. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2303. }
  2304. sqlite3_finalize(statement);
  2305. [iSalesDB close_db:db];
  2306. return ret;
  2307. }
  2308. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2309. {
  2310. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2311. sqlite3 *db = [iSalesDB get_db];
  2312. // UIApplication * app = [UIApplication sharedApplication];
  2313. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2314. 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";
  2315. sqlite3_stmt * statement;
  2316. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2317. int count=0;
  2318. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2319. if ( dbresult== SQLITE_OK)
  2320. {
  2321. while (sqlite3_step(statement) == SQLITE_ROW)
  2322. {
  2323. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2324. int _id = sqlite3_column_int(statement, 0);
  2325. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2326. NSString* nsimg_0=nil;
  2327. if(img_0!=nil)
  2328. {
  2329. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2330. [self copy_upImg:serial file:nsimg_0];
  2331. }
  2332. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2333. NSString* nsimg_1=nil;
  2334. if(img_1!=nil)
  2335. {
  2336. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2337. [self copy_upImg:serial file:nsimg_1];
  2338. }
  2339. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2340. NSString* nsimg_2=nil;
  2341. if(img_2!=nil)
  2342. {
  2343. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2344. [self copy_upImg:serial file:nsimg_2];
  2345. }
  2346. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2347. NSString* nssync_data=nil;
  2348. if(sync_data!=nil)
  2349. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2350. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2351. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2352. itemjson[@"json_data"]= nssync_data;
  2353. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2354. count++;
  2355. }
  2356. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2357. sqlite3_finalize(statement);
  2358. }
  2359. ret[@"count"]=[NSNumber numberWithInt:count ];
  2360. [iSalesDB close_db:db];
  2361. return ret;
  2362. }
  2363. +(bool) check_offlinedata
  2364. {
  2365. // UIApplication * app = [UIApplication sharedApplication];
  2366. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2367. sqlite3 *db = [iSalesDB get_db];
  2368. NSString * where=@"1=1";
  2369. // if(appDelegate.user!=nil)
  2370. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2371. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2372. [iSalesDB close_db:db];
  2373. if(count==0)
  2374. {
  2375. return false;
  2376. }
  2377. return true;
  2378. //
  2379. //[iSalesDB close_db:db];
  2380. }
  2381. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2382. {
  2383. // UIApplication * app = [UIApplication sharedApplication];
  2384. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2385. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2386. sqlite3 *db = [iSalesDB get_db];
  2387. NSString* collectId=params[@"collectId"];
  2388. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2389. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2390. [iSalesDB execSql:sqlQuery db:db];
  2391. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2392. [iSalesDB close_db:db];
  2393. // appDelegate.wish_count =count;
  2394. //
  2395. // [appDelegate update_count_mark];
  2396. ret[@"result"]= [NSNumber numberWithInt:2];
  2397. ret[@"wish_count"]=@(count);
  2398. return ret;
  2399. }
  2400. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2401. //{
  2402. //
  2403. // UIApplication * app = [UIApplication sharedApplication];
  2404. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2405. //
  2406. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2407. // sqlite3 *db = [iSalesDB get_db];
  2408. // NSString* product_id=params[@"product_id"];
  2409. //
  2410. //
  2411. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2412. //
  2413. //
  2414. // for(int i=0;i<arr.count;i++)
  2415. // {
  2416. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2417. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2418. // if(count==0)
  2419. // {
  2420. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2421. // [iSalesDB execSql:sqlQuery db:db];
  2422. // }
  2423. // }
  2424. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2425. // [iSalesDB close_db:db];
  2426. //
  2427. // appDelegate.wish_count =count;
  2428. //
  2429. // [appDelegate update_count_mark];
  2430. // ret[@"result"]= [NSNumber numberWithInt:2];
  2431. // return ret;
  2432. // //
  2433. // //return ret;
  2434. //}
  2435. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2436. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2437. DebugLog(@"time interval: %lf",interval);
  2438. }
  2439. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2440. //{
  2441. // UIApplication * app = [UIApplication sharedApplication];
  2442. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2443. //
  2444. // int sort = [[params objectForKey:@"sort"] intValue];
  2445. // NSString *sort_str = @"";
  2446. // switch (sort) {
  2447. // case 0:{
  2448. // sort_str = @"order by w.modify_time desc";
  2449. // }
  2450. // break;
  2451. // case 1:{
  2452. // sort_str = @"order by w.modify_time asc";
  2453. // }
  2454. // break;
  2455. // case 2:{
  2456. // sort_str = @"order by m.name asc";
  2457. // }
  2458. // break;
  2459. // case 3:{
  2460. // sort_str = @"order by m.name desc";
  2461. // }
  2462. // break;
  2463. // case 4:{
  2464. // sort_str = @"order by m.description asc";
  2465. // }
  2466. // break;
  2467. //
  2468. // default:
  2469. // break;
  2470. //
  2471. // }
  2472. //
  2473. //
  2474. //// NSString* user = appDelegate.user;
  2475. //
  2476. // sqlite3 *db = [iSalesDB get_db];
  2477. //
  2478. // // order by w.create_time
  2479. // 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];
  2480. //
  2481. //// 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];
  2482. //
  2483. //
  2484. // sqlite3_stmt * statement;
  2485. //
  2486. // NSDate *date1 = [NSDate date];
  2487. //// NSDate *date2 = nil;
  2488. //
  2489. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2490. // int count=0;
  2491. //
  2492. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2493. // {
  2494. //
  2495. //
  2496. // while (sqlite3_step(statement) == SQLITE_ROW)
  2497. // {
  2498. //
  2499. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2500. //
  2501. // int product_id = sqlite3_column_double(statement, 0);
  2502. //
  2503. //
  2504. //
  2505. //
  2506. //
  2507. // char *description = (char*)sqlite3_column_text(statement, 1);
  2508. // if(description==nil)
  2509. // description= "";
  2510. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2511. //
  2512. // int item_id = sqlite3_column_double(statement, 2);
  2513. //
  2514. // NSDate *date_image = [NSDate date];
  2515. //
  2516. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2517. //
  2518. // printf("image : ");
  2519. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2520. //
  2521. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2522. // // if(url==nil)
  2523. // // url="";
  2524. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2525. //
  2526. // int qty = sqlite3_column_int(statement, 3);
  2527. //
  2528. //
  2529. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2530. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2531. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2532. // item[@"description"]= nsdescription;
  2533. // item[@"img"]= nsurl;
  2534. //
  2535. //
  2536. //
  2537. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2538. // count++;
  2539. //
  2540. // }
  2541. // printf("total time:");
  2542. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2543. //
  2544. // ret[@"count"]= [NSNumber numberWithInt:count];
  2545. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2546. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2547. // ret[@"result"]= [NSNumber numberWithInt:2];
  2548. //
  2549. //
  2550. // appDelegate.wish_count =count;
  2551. //
  2552. // [appDelegate update_count_mark];
  2553. // sqlite3_finalize(statement);
  2554. //
  2555. //
  2556. //
  2557. //
  2558. // }
  2559. //
  2560. // [iSalesDB close_db:db];
  2561. //
  2562. // return ret;
  2563. //}
  2564. +(NSDictionary*) offline_notimpl
  2565. {
  2566. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2567. ret[@"result"]=@"8";
  2568. ret[@"err_msg"]=@"offline mode does not support this function.";
  2569. return ret;
  2570. }
  2571. +(NSDictionary*) offline_home
  2572. {
  2573. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2574. NSString *cachefolder = [paths objectAtIndex:0];
  2575. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2576. NSData* json =nil;
  2577. json=[NSData dataWithContentsOfFile:img_cache];
  2578. if(json==nil)
  2579. return nil;
  2580. NSError *error=nil;
  2581. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2582. return menu;
  2583. }
  2584. +(NSDictionary*) offline_category_menu
  2585. {
  2586. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2587. NSString *cachefolder = [paths objectAtIndex:0];
  2588. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2589. NSData* json =nil;
  2590. json=[NSData dataWithContentsOfFile:img_cache];
  2591. if(json==nil)
  2592. return nil;
  2593. NSError *error=nil;
  2594. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2595. return menu;
  2596. }
  2597. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2598. {
  2599. NSString* offline_command=params[@"offline_Command"];
  2600. NSDictionary* ret=nil;
  2601. if([offline_command isEqualToString:@"model_NIYMAL"])
  2602. {
  2603. NSString* category = params[@"category"];
  2604. ret = [self refresh_model_NIYMAL:category];
  2605. }
  2606. return ret;
  2607. }
  2608. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2609. {
  2610. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2611. [ret setValue:@"2" forKey:@"result"];
  2612. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2613. sqlite3* db= [iSalesDB get_db];
  2614. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2615. [iSalesDB close_db:db];
  2616. [ret setObject:detail1_section forKey:@"detail_1"];
  2617. return ret;
  2618. }
  2619. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2620. {
  2621. //assert(user!=nil);
  2622. // UIApplication * app = [UIApplication sharedApplication];
  2623. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2624. // NSArray* arr1 = [self get_user_all_price_type];
  2625. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2626. // NSSet *set1 = [NSSet setWithArray:arr1];
  2627. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2628. // if(appDelegate.contact_id==nil)
  2629. // set2=[set1 mutableCopy];
  2630. // else
  2631. // [set2 intersectsSet:set1];
  2632. // NSArray *retarr = [set2 allObjects];
  2633. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2634. // sqlite3 *db = [iSalesDB get_db];
  2635. NSString* sqlQuery = nil;
  2636. if(contact_id==nil)
  2637. {
  2638. if(!blogin)
  2639. return nil;
  2640. 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];
  2641. }
  2642. else
  2643. 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];
  2644. sqlite3_stmt * statement;
  2645. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2646. int count=0;
  2647. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2648. {
  2649. while (sqlite3_step(statement) == SQLITE_ROW)
  2650. {
  2651. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2652. char *name = (char*)sqlite3_column_text(statement, 0);
  2653. if(name==nil)
  2654. name="";
  2655. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2656. // double price = sqlite3_column_double(statement, 1);
  2657. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2658. // if(isnull==nil)
  2659. // item[nsname]= @"No Price";
  2660. // else
  2661. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2662. char *price = (char*)sqlite3_column_text(statement, 1);
  2663. if(price!=nil)
  2664. {
  2665. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2666. nsprice=[AESCrypt fastdecrypt:nsprice];
  2667. if(nsprice.length>0)
  2668. {
  2669. double dp= [nsprice doubleValue];
  2670. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2671. }
  2672. }
  2673. else
  2674. {
  2675. item[nsname]= @"No Price";
  2676. }
  2677. // int type= sqlite3_column_int(statement, 2);
  2678. //item[@"type"]=@"price";
  2679. // item[nsname]= nsprice;
  2680. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2681. count++;
  2682. }
  2683. ret[@"count"]= [NSNumber numberWithInt:count];
  2684. sqlite3_finalize(statement);
  2685. }
  2686. // [iSalesDB close_db:db];
  2687. return ret;
  2688. }
  2689. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2690. //{
  2691. // NSArray* arr1 = [self get_user_all_price_type:db];
  2692. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2693. //
  2694. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2695. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2696. // // [set2 intersectsSet:set1];
  2697. // //
  2698. // //
  2699. // // NSArray *retarr = [set2 allObjects];
  2700. //
  2701. // NSString* whereprice=nil;
  2702. // if(contact_id==nil)
  2703. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2704. // else
  2705. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2706. //
  2707. //
  2708. // // sqlite3 *db = [iSalesDB get_db];
  2709. //
  2710. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2711. // sqlite3_stmt * statement;
  2712. //
  2713. //
  2714. // NSNumber* ret = nil;
  2715. // double dprice=DBL_MAX;
  2716. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2717. // {
  2718. //
  2719. //
  2720. // while (sqlite3_step(statement) == SQLITE_ROW)
  2721. // {
  2722. //
  2723. // // double val = sqlite3_column_double(statement, 0);
  2724. // char *price = (char*)sqlite3_column_text(statement, 0);
  2725. // if(price!=nil)
  2726. // {
  2727. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2728. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2729. // if(nsprice.length>0)
  2730. // {
  2731. // double dp= [nsprice doubleValue];
  2732. // if(dp<dprice)
  2733. // dprice=dp;
  2734. // }
  2735. // }
  2736. // }
  2737. //
  2738. //
  2739. //
  2740. //
  2741. // sqlite3_finalize(statement);
  2742. //
  2743. //
  2744. //
  2745. //
  2746. // }
  2747. //
  2748. // // [iSalesDB close_db:db];
  2749. //
  2750. // if(dprice==DBL_MAX)
  2751. // ret= nil;
  2752. // else
  2753. // ret= [NSNumber numberWithDouble:dprice];
  2754. // return ret;
  2755. //}
  2756. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2757. {
  2758. assert(user!=nil);
  2759. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2760. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2761. NSString* whereprice=nil;
  2762. if(contact_id==nil)
  2763. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2764. else
  2765. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2766. // sqlite3 *db = [iSalesDB get_db];
  2767. NSString *productIdCondition = @"1 = 1";
  2768. if (product_id) {
  2769. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2770. }
  2771. NSString *itemIdCondition = @"";
  2772. if (item_id) {
  2773. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2774. }
  2775. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2776. sqlite3_stmt * statement;
  2777. NSNumber* ret = nil;
  2778. double dprice=DBL_MAX;
  2779. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2780. {
  2781. while (sqlite3_step(statement) == SQLITE_ROW)
  2782. {
  2783. // double val = sqlite3_column_double(statement, 0);
  2784. char *price = (char*)sqlite3_column_text(statement, 0);
  2785. if(price!=nil)
  2786. {
  2787. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2788. nsprice=[AESCrypt fastdecrypt:nsprice];
  2789. if(nsprice.length>0)
  2790. {
  2791. double dp= [nsprice doubleValue];
  2792. if(dp<dprice)
  2793. dprice=dp;
  2794. }
  2795. }
  2796. }
  2797. sqlite3_finalize(statement);
  2798. }
  2799. // [iSalesDB close_db:db];
  2800. if(dprice==DBL_MAX)
  2801. ret= nil;
  2802. else
  2803. ret= [NSNumber numberWithDouble:dprice];
  2804. return ret;
  2805. }
  2806. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2807. {
  2808. assert(user!=nil);
  2809. NSArray* ret=nil;
  2810. // sqlite3 *db = [iSalesDB get_db];
  2811. // no customer assigned , use login user contact_id
  2812. // UIApplication * app = [UIApplication sharedApplication];
  2813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2814. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2815. sqlite3_stmt * statement;
  2816. // int count=0;
  2817. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2818. {
  2819. if (sqlite3_step(statement) == SQLITE_ROW)
  2820. {
  2821. char *val = (char*)sqlite3_column_text(statement, 0);
  2822. if(val==nil)
  2823. val="";
  2824. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2825. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2826. }
  2827. sqlite3_finalize(statement);
  2828. }
  2829. // [iSalesDB close_db:db];
  2830. return ret;
  2831. }
  2832. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2833. {
  2834. // sqlite3 *db = [iSalesDB get_db];
  2835. if(contact_id==nil)
  2836. {
  2837. // no customer assigned , use login user contact_id
  2838. // UIApplication * app = [UIApplication sharedApplication];
  2839. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2840. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2841. sqlite3_stmt * statement;
  2842. // int count=0;
  2843. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2844. {
  2845. if (sqlite3_step(statement) == SQLITE_ROW)
  2846. {
  2847. char *val = (char*)sqlite3_column_text(statement, 0);
  2848. if(val==nil)
  2849. val="";
  2850. contact_id = [[NSString alloc]initWithUTF8String:val];
  2851. }
  2852. sqlite3_finalize(statement);
  2853. }
  2854. if(contact_id.length<=0)
  2855. {
  2856. // [iSalesDB close_db:db];
  2857. return nil;
  2858. }
  2859. }
  2860. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2861. sqlite3_stmt * statement;
  2862. NSArray* ret=nil;
  2863. // int count=0;
  2864. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2865. {
  2866. if (sqlite3_step(statement) == SQLITE_ROW)
  2867. {
  2868. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2869. if(price_type==nil)
  2870. price_type="";
  2871. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2872. if(nsprice_type.length>0)
  2873. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2874. else
  2875. ret=nil;
  2876. }
  2877. sqlite3_finalize(statement);
  2878. }
  2879. // [iSalesDB close_db:db];
  2880. return ret;
  2881. }
  2882. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2883. {
  2884. NSString* ret= nil;
  2885. // sqlite3 *db = [iSalesDB get_db];
  2886. NSString *sqlQuery = nil;
  2887. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2888. // 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;
  2889. // 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
  2890. if(product_id==nil && model_name)
  2891. 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;
  2892. else if (product_id)
  2893. 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
  2894. sqlite3_stmt * statement;
  2895. // int count=0;
  2896. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2897. {
  2898. if (sqlite3_step(statement) == SQLITE_ROW)
  2899. {
  2900. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2901. if(imgurl==nil)
  2902. imgurl="";
  2903. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2904. ret=nsimgurl;
  2905. }
  2906. sqlite3_finalize(statement);
  2907. }
  2908. else
  2909. {
  2910. [ret setValue:@"8" forKey:@"result"];
  2911. }
  2912. // [iSalesDB close_db:db];
  2913. DebugLog(@"data string: %@",ret );
  2914. return ret;
  2915. }
  2916. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2917. {
  2918. NSString* ret= nil;
  2919. sqlite3 *db = [iSalesDB get_db];
  2920. NSString *sqlQuery = nil;
  2921. if(product_id==nil)
  2922. if(upc_code==nil)
  2923. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2924. else
  2925. 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='%@';
  2926. else
  2927. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2928. sqlite3_stmt * statement;
  2929. // int count=0;
  2930. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2931. {
  2932. if (sqlite3_step(statement) == SQLITE_ROW)
  2933. {
  2934. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2935. if(default_category==nil)
  2936. default_category="";
  2937. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2938. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2939. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2940. ret=nsdefault_category;
  2941. }
  2942. sqlite3_finalize(statement);
  2943. }
  2944. else
  2945. {
  2946. [ret setValue:@"8" forKey:@"result"];
  2947. }
  2948. [iSalesDB close_db:db];
  2949. DebugLog(@"data string: %@",ret );
  2950. return ret;
  2951. }
  2952. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2953. NSString *sql = nil;
  2954. if (product_id != nil) {
  2955. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2956. } else {
  2957. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2958. }
  2959. NSString *result_set = [iSalesDB jk_queryText:sql];
  2960. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2961. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2962. return result_set;
  2963. }
  2964. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2965. //{
  2966. //
  2967. //
  2968. // assert(params[@"user"]!=nil);
  2969. //
  2970. //
  2971. //
  2972. // NSString* model_name = [params valueForKey:@"product_name"];
  2973. //
  2974. // NSString* product_id = [params valueForKey:@"product_id"];
  2975. //
  2976. // NSString* category = [params valueForKey:@"category"];
  2977. //
  2978. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2979. // if(category==nil) {
  2980. // category = default_category_id;
  2981. // } else {
  2982. //
  2983. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2984. // // 参数重有多个category id
  2985. // if (arr_0.count > 1) {
  2986. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2987. //
  2988. // // 取交集
  2989. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2990. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2991. // [set_0 intersectSet:set_1];
  2992. //
  2993. // if (set_0.count == 1) {
  2994. // category = (NSString *)[set_0 anyObject];
  2995. // } else {
  2996. // if ([set_0 containsObject:default_category_id]) {
  2997. // category = default_category_id;
  2998. // } else {
  2999. // category = (NSString *)[set_0 anyObject];
  3000. // }
  3001. // }
  3002. // }
  3003. // }
  3004. //
  3005. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3006. //
  3007. //
  3008. //
  3009. //
  3010. //
  3011. //
  3012. // sqlite3 *db = [iSalesDB get_db];
  3013. //
  3014. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3015. //
  3016. //
  3017. // NSString *sqlQuery = nil;
  3018. //
  3019. // if(product_id==nil)
  3020. // 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='%@';
  3021. // else
  3022. //
  3023. // 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=%@;
  3024. //
  3025. //
  3026. // sqlite3_stmt * statement;
  3027. // [ret setValue:@"2" forKey:@"result"];
  3028. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3029. //
  3030. // // int count=0;
  3031. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3032. // {
  3033. //
  3034. //
  3035. // if (sqlite3_step(statement) == SQLITE_ROW)
  3036. // {
  3037. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3038. //
  3039. //
  3040. // char *name = (char*)sqlite3_column_text(statement, 0);
  3041. // if(name==nil)
  3042. // name="";
  3043. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3044. //
  3045. // char *description = (char*)sqlite3_column_text(statement, 1);
  3046. // if(description==nil)
  3047. // description="";
  3048. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3049. //
  3050. //
  3051. // int product_id = sqlite3_column_int(statement, 2);
  3052. //
  3053. //
  3054. // char *color = (char*)sqlite3_column_text(statement, 3);
  3055. // if(color==nil)
  3056. // color="";
  3057. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3058. // //
  3059. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3060. // // if(legcolor==nil)
  3061. // // legcolor="";
  3062. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3063. // //
  3064. // //
  3065. // int availability = sqlite3_column_int(statement, 5);
  3066. // //
  3067. // int incoming_stock = sqlite3_column_int(statement, 6);
  3068. //
  3069. //
  3070. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3071. // if(demension==nil)
  3072. // demension="";
  3073. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3074. //
  3075. //
  3076. //
  3077. // // ,,,,,,,,,
  3078. //
  3079. //
  3080. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3081. // if(seat_height==nil)
  3082. // seat_height="";
  3083. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3084. //
  3085. //
  3086. // char *material = (char*)sqlite3_column_text(statement, 9);
  3087. // if(material==nil)
  3088. // material="";
  3089. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3090. //
  3091. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3092. // if(box_dim==nil)
  3093. // box_dim="";
  3094. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3095. //
  3096. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3097. // if(volume==nil)
  3098. // volume="";
  3099. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3100. //
  3101. // double weight = sqlite3_column_double(statement, 12);
  3102. //
  3103. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3104. // if(model_set==nil)
  3105. // model_set="";
  3106. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3107. //
  3108. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3109. // if(load_ability==nil)
  3110. // load_ability="";
  3111. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3112. //
  3113. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3114. //// if(default_category==nil)
  3115. //// default_category="";
  3116. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3117. //
  3118. //
  3119. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3120. // if(fabric_content==nil)
  3121. // fabric_content="";
  3122. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3123. //
  3124. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3125. // if(assembling==nil)
  3126. // assembling="";
  3127. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3128. //
  3129. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3130. // if(made_in==nil)
  3131. // made_in="";
  3132. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3133. //
  3134. //
  3135. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3136. // if(special_remarks==nil)
  3137. // special_remarks="";
  3138. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3139. //
  3140. //
  3141. // int stockUcom = sqlite3_column_double(statement, 20);
  3142. //
  3143. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3144. // if(product_group==nil)
  3145. // product_group="";
  3146. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3147. //
  3148. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3149. // // if(fashion_selector==nil)
  3150. // // fashion_selector="";
  3151. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3152. //
  3153. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3154. // if(selector_field==nil)
  3155. // selector_field="";
  3156. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3157. //
  3158. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3159. // if(property_field==nil)
  3160. // property_field="";
  3161. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3162. //
  3163. //
  3164. //
  3165. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3166. // if(packaging==nil)
  3167. // packaging="";
  3168. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3169. //
  3170. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3171. //
  3172. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3173. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3174. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3175. //
  3176. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3177. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3178. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3179. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3180. //
  3181. //
  3182. //
  3183. //
  3184. //
  3185. // NSString* Availability=nil;
  3186. // if(availability>0)
  3187. // Availability=[NSString stringWithFormat:@"%d",availability];
  3188. // else
  3189. // Availability = @"Out of Stock";
  3190. //
  3191. // [img_section setValue:Availability forKey:@"Availability"];
  3192. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3193. //
  3194. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3195. //
  3196. //
  3197. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3198. // if(eta==nil)
  3199. // eta="";
  3200. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3201. // if ([nseta isEqualToString:@"null"]) {
  3202. // nseta = @"";
  3203. // }
  3204. // if (availability <= 0) {
  3205. // [img_section setValue:nseta forKey:@"ETA"];
  3206. // }
  3207. //
  3208. //
  3209. // int item_id = sqlite3_column_int(statement, 26);
  3210. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3211. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3212. //
  3213. // NSString* Price=nil;
  3214. // if(appDelegate.bLogin==false)
  3215. // Price=@"Must Sign in.";
  3216. // else
  3217. // {
  3218. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3219. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3220. // if(price==nil)
  3221. // Price=@"No Price.";
  3222. // else
  3223. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3224. // }
  3225. //
  3226. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3227. // [img_section setValue:Price forKey:@"price"];
  3228. // [img_section setValue:nsname forKey:@"model_name"];
  3229. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3230. //
  3231. // if (appDelegate.order_code) { // 离线order code即so#
  3232. //
  3233. // 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];
  3234. // __block int cartQTY = 0;
  3235. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3236. //
  3237. // cartQTY = sqlite3_column_int(stmt, 0);
  3238. //
  3239. // }];
  3240. //
  3241. // if (cartQTY > 0) {
  3242. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3243. // }
  3244. // }
  3245. //
  3246. // [ret setObject:img_section forKey:@"img_section"];
  3247. //
  3248. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3249. // int detail0_item_count=0;
  3250. //
  3251. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3252. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3253. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3254. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3255. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3256. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3257. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3258. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3259. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3260. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3261. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3262. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3263. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3264. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3265. //
  3266. //
  3267. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3268. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3269. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3270. // {
  3271. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3272. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3273. // }
  3274. //
  3275. //
  3276. //
  3277. //
  3278. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3279. // [detail0_section setValue:@"kv" forKey:@"type"];
  3280. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3281. //
  3282. // [ret setObject:detail0_section forKey:@"detail_0"];
  3283. //
  3284. //
  3285. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3286. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3287. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3288. // // [detail1_section setValue:@"content" forKey:@"type"];
  3289. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3290. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3291. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3292. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3293. // [ret setObject:detail1_section forKey:@"detail_1"];
  3294. //
  3295. //
  3296. //
  3297. //
  3298. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3299. // [detail2_section setValue:@"detail" forKey:@"target"];
  3300. // [detail2_section setValue:@"popup" forKey:@"action"];
  3301. // [detail2_section setValue:@"content" forKey:@"type"];
  3302. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3303. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3304. // [detail2_section setValue:@"local" forKey:@"data"];
  3305. // [ret setObject:detail2_section forKey:@"detail_2"];
  3306. // }
  3307. //
  3308. //
  3309. //
  3310. //
  3311. // sqlite3_finalize(statement);
  3312. // }
  3313. // else
  3314. // {
  3315. // [ret setValue:@"8" forKey:@"result"];
  3316. // }
  3317. //// DebugLog(@"count:%d",count);
  3318. //
  3319. //
  3320. // [iSalesDB close_db:db];
  3321. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3322. //
  3323. // return ret;
  3324. //}
  3325. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3326. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3327. NSString* keyword = [params valueForKey:@"keyword"];
  3328. keyword=keyword.lowercaseString;
  3329. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3330. int limit = [[params valueForKey:@"limit"] intValue];
  3331. int offset = [[params valueForKey:@"offset"] intValue];
  3332. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3333. NSString *limit_str = @"";
  3334. if (limited) {
  3335. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3336. }
  3337. sqlite3 *db = [iSalesDB get_db];
  3338. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3339. // UIApplication * app = [UIApplication sharedApplication];
  3340. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3341. NSString *sqlQuery = nil;
  3342. if(exactMatch )
  3343. 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 ;
  3344. else
  3345. 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
  3346. DebugLog(@"offline_search sql:%@",sqlQuery);
  3347. sqlite3_stmt * statement;
  3348. [ret setValue:@"2" forKey:@"result"];
  3349. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3350. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3351. // int count=0;
  3352. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3353. {
  3354. int i=0;
  3355. while (sqlite3_step(statement) == SQLITE_ROW)
  3356. {
  3357. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3358. // char *name = (char*)sqlite3_column_text(statement, 1);
  3359. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3360. char *name = (char*)sqlite3_column_text(statement, 0);
  3361. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3362. char *description = (char*)sqlite3_column_text(statement, 1);
  3363. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3364. int product_id = sqlite3_column_int(statement, 2);
  3365. // char *url = (char*)sqlite3_column_text(statement, 3);
  3366. // if(url==nil)
  3367. // url="";
  3368. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3369. int wid = sqlite3_column_int(statement, 3);
  3370. int closeout = sqlite3_column_int(statement, 4);
  3371. int cid = sqlite3_column_int(statement, 5);
  3372. int wisdelete = sqlite3_column_int(statement, 6);
  3373. int more_color = sqlite3_column_int(statement, 7);
  3374. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3375. if(wid !=0 && wisdelete != 1)
  3376. [item setValue:@"true" forKey:@"wish_exists"];
  3377. else
  3378. [item setValue:@"false" forKey:@"wish_exists"];
  3379. if(closeout==0)
  3380. [item setValue:@"false" forKey:@"is_closeout"];
  3381. else
  3382. [item setValue:@"true" forKey:@"is_closeout"];
  3383. if(cid==0)
  3384. [item setValue:@"false" forKey:@"cart_exists"];
  3385. else
  3386. [item setValue:@"true" forKey:@"cart_exists"];
  3387. if (more_color == 0) {
  3388. [item setObject:@(false) forKey:@"more_color"];
  3389. } else if (more_color == 1) {
  3390. [item setObject:@(true) forKey:@"more_color"];
  3391. }
  3392. [item addEntriesFromDictionary:imgjson];
  3393. // [item setValue:nsurl forKey:@"img"];
  3394. [item setValue:nsname forKey:@"fash_name"];
  3395. [item setValue:nsdescription forKey:@"description"];
  3396. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3397. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3398. i++;
  3399. }
  3400. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3401. [ret setObject:items forKey:@"items"];
  3402. sqlite3_finalize(statement);
  3403. }
  3404. DebugLog(@"count:%d",count);
  3405. [iSalesDB close_db:db];
  3406. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3407. return ret;
  3408. }
  3409. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3410. {
  3411. return [self search:params limited:YES];
  3412. }
  3413. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3414. //{
  3415. // UIApplication * app = [UIApplication sharedApplication];
  3416. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3417. //
  3418. //// [iSalesDB disable_trigger]
  3419. // [iSalesDB disable_trigger];
  3420. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3421. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3422. //
  3423. // [iSalesDB enable_trigger];
  3424. //// [iSalesDB enable_trigger]
  3425. // //
  3426. // // NSString* user = [params valueForKey:@"user"];
  3427. // //
  3428. // // NSString* password = [params valueForKey:@"password"];
  3429. //
  3430. //
  3431. //
  3432. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3433. //
  3434. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3435. //
  3436. //
  3437. //
  3438. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3439. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3440. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3441. //
  3442. // [appDelegate update_count_mark];
  3443. //
  3444. //
  3445. // appDelegate.can_show_price =false;
  3446. // appDelegate.can_see_price =false;
  3447. // appDelegate.can_create_portfolio =false;
  3448. // appDelegate.can_create_order =false;
  3449. //
  3450. //
  3451. // appDelegate.can_cancel_order =false;
  3452. // appDelegate.can_set_cart_price =false;
  3453. // appDelegate.can_delete_order =false;
  3454. // appDelegate.can_submit_order =false;
  3455. // appDelegate.can_set_tearsheet_price =false;
  3456. // appDelegate.can_update_contact_info = false;
  3457. //
  3458. // appDelegate.save_order_logout = false;
  3459. // appDelegate.submit_order_logout = false;
  3460. // appDelegate.alert_sold_in_quantities = false;
  3461. //
  3462. // appDelegate.ipad_perm =nil ;
  3463. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3464. // appDelegate.OrderFilter= nil;
  3465. // [appDelegate SetSo:nil];
  3466. // [appDelegate set_main_button_panel];
  3467. //
  3468. //
  3469. // // sqlite3 *db = [iSalesDB get_db];
  3470. // //
  3471. // //
  3472. // //
  3473. // //
  3474. // //
  3475. // // 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"]];
  3476. // //
  3477. // //
  3478. // //
  3479. // //
  3480. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3481. // // sqlite3_stmt * statement;
  3482. // //
  3483. // //
  3484. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3485. // //
  3486. // //
  3487. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3488. // // {
  3489. // //
  3490. // //
  3491. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3492. // // {
  3493. // //
  3494. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3495. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3496. // //
  3497. // //
  3498. // //
  3499. // // int can_show_price = sqlite3_column_int(statement, 0);
  3500. // // int can_see_price = sqlite3_column_int(statement, 1);
  3501. // //
  3502. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3503. // // if(contact_id==nil)
  3504. // // contact_id="";
  3505. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3506. // //
  3507. // // int user_type = sqlite3_column_int(statement, 3);
  3508. // //
  3509. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3510. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3511. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3512. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3513. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3514. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3515. // // int can_create_order = sqlite3_column_int(statement, 10);
  3516. // //
  3517. // //
  3518. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3519. // // if(mode==nil)
  3520. // // mode="";
  3521. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3522. // //
  3523. // //
  3524. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3525. // // if(username==nil)
  3526. // // username="";
  3527. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3528. // //
  3529. // //
  3530. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3531. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3532. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3533. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3534. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3535. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3536. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3537. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3538. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3539. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3540. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3541. // //
  3542. // // [header setValue:nsusername forKey:@"username"];
  3543. // //
  3544. // //
  3545. // // [ret setObject:header forKey:@"header"];
  3546. // // [ret setValue:nsmode forKey:@"mode"];
  3547. // //
  3548. // //
  3549. // // }
  3550. // //
  3551. // //
  3552. // //
  3553. // // sqlite3_finalize(statement);
  3554. // // }
  3555. // //
  3556. // //
  3557. // //
  3558. // // [iSalesDB close_db:db];
  3559. // //
  3560. // //
  3561. // //
  3562. // //
  3563. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3564. //
  3565. // return ret;
  3566. //}
  3567. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3568. //{
  3569. //
  3570. // UIApplication * app = [UIApplication sharedApplication];
  3571. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3572. //
  3573. // [iSalesDB disable_trigger];
  3574. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3575. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3576. // [iSalesDB enable_trigger];
  3577. //
  3578. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3579. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3580. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3581. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3582. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3583. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3584. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3585. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3586. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3587. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3588. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3589. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3590. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3591. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3592. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3593. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3594. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3595. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3596. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3597. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3598. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3599. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3600. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3601. //
  3602. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3603. //
  3604. // [arr_name addObject:customer_first_name];
  3605. // [arr_name addObject:customer_last_name];
  3606. //
  3607. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3608. //
  3609. // // default ship from
  3610. // 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';";
  3611. //
  3612. // __block NSString *cid = @"";
  3613. // __block NSString *name = @"";
  3614. // __block NSString *ext = @"";
  3615. // __block NSString *contact = @"";
  3616. // __block NSString *email = @"";
  3617. // __block NSString *fax = @"";
  3618. // __block NSString *phone = @"";
  3619. //
  3620. // sqlite3 *db = [iSalesDB get_db];
  3621. //
  3622. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3623. //
  3624. // cid = [self textAtColumn:0 statement:statment];
  3625. // name = [self textAtColumn:1 statement:statment];
  3626. // ext = [self textAtColumn:2 statement:statment];
  3627. // contact = [self textAtColumn:3 statement:statment];
  3628. // email = [self textAtColumn:4 statement:statment];
  3629. // fax = [self textAtColumn:5 statement:statment];
  3630. // phone = [self textAtColumn:6 statement:statment];
  3631. //
  3632. // }];
  3633. //
  3634. // NSString* so_id = [self get_offline_soid:db];
  3635. // if(so_id==nil)
  3636. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3637. //
  3638. // 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];
  3639. //
  3640. //
  3641. //
  3642. // int result =[iSalesDB execSql:sql_neworder db:db];
  3643. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3644. //
  3645. //
  3646. //
  3647. // //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'
  3648. // //soId
  3649. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3650. //
  3651. //
  3652. //
  3653. //
  3654. //
  3655. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3656. // sqlite3_stmt * statement;
  3657. //
  3658. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3659. // {
  3660. // if (sqlite3_step(statement) == SQLITE_ROW)
  3661. // {
  3662. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3663. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3664. //
  3665. // //ret = sqlite3_column_int(statement, 0);
  3666. //
  3667. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3668. // if(soId==nil)
  3669. // soId="";
  3670. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3671. // [ret setValue:nssoId forKey:@"soId"];
  3672. // [ret setValue:nssoId forKey:@"orderCode"];
  3673. //
  3674. // }
  3675. // sqlite3_finalize(statement);
  3676. // }
  3677. //
  3678. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3679. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3680. //
  3681. //
  3682. // [iSalesDB close_db:db];
  3683. //
  3684. // return [RAConvertor dict2data:ret];
  3685. //
  3686. //}
  3687. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3688. {
  3689. //assert(params[@"order_code"]);
  3690. // assert(params[@"order_code"]);
  3691. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3692. NSString* orderCode = [params valueForKey:@"orderCode"];
  3693. NSString* app_order_code= params[@"appDelegate.order_code"];
  3694. // UIApplication * app = [UIApplication sharedApplication];
  3695. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3696. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3697. {
  3698. [iSalesDB disable_trigger];
  3699. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3700. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3701. [iSalesDB enable_trigger];
  3702. }
  3703. sqlite3 *db = [iSalesDB get_db];
  3704. int cart_count=[self query_ordercartcount:orderCode db:db];
  3705. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3706. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3707. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3708. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3709. [iSalesDB close_db:db];
  3710. return [RAConvertor dict2data:ret];
  3711. }
  3712. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3713. {
  3714. assert(params[@"can_create_backorder"]!=nil);
  3715. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3716. sqlite3 *db = [iSalesDB get_db];
  3717. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3718. int count =0;
  3719. if(params[@"count"]!=nil)
  3720. {
  3721. count = [params[@"count"] intValue];
  3722. }
  3723. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3724. NSString* product_id=params[@"product_id"];
  3725. NSString* orderCode=params[@"orderCode"];
  3726. NSString *qty = params[@"qty"];
  3727. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3728. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3729. __block int number_of_outOfStock = 0;
  3730. for(int i=0;i<arr_id.count;i++)
  3731. {
  3732. NSInteger item_qty= count;
  3733. if (qty) {
  3734. item_qty = [qty_arr[i] integerValue];
  3735. }
  3736. if(item_qty==0)
  3737. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3738. // 检查新加Model数量是否大于库存
  3739. if (![params[@"can_create_backorder"] boolValue]) {
  3740. __block BOOL needContinue = NO;
  3741. [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) {
  3742. int availability = sqlite3_column_int(stmt, 0);
  3743. // 库存小于购买量为缺货
  3744. if (availability < item_qty || availability <= 0) {
  3745. number_of_outOfStock++;
  3746. needContinue = YES;
  3747. }
  3748. }];
  3749. if (needContinue) {
  3750. continue;
  3751. }
  3752. }
  3753. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3754. NSString* sql=nil;
  3755. sqlite3_stmt *stmt = nil;
  3756. BOOL shouldStep = NO;
  3757. if(_id<0)
  3758. {
  3759. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3760. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3761. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3762. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3763. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3764. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3765. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3766. shouldStep = YES;
  3767. }
  3768. else
  3769. {
  3770. if (qty) { // wish list move to cart
  3771. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3772. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3773. sqlite3_bind_int(stmt, 1, _id);
  3774. shouldStep = YES;
  3775. } else {
  3776. 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];
  3777. __block BOOL update = YES;
  3778. if (![params[@"can_create_backorder"] boolValue]) {
  3779. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3780. int newQTY = sqlite3_column_int(stmt, 0);
  3781. int availability = sqlite3_column_int(stmt, 1);
  3782. if (newQTY > availability) { // 库存不够
  3783. update = NO;
  3784. number_of_outOfStock++;
  3785. }
  3786. }];
  3787. }
  3788. if (update) {
  3789. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3790. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3791. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3792. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3793. shouldStep = YES;
  3794. }
  3795. }
  3796. }
  3797. if (shouldStep) {
  3798. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3799. [iSalesDB execSql:@"ROLLBACK" db:db];
  3800. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3801. [iSalesDB close_db:db];
  3802. DebugLog(@"add to cart error");
  3803. return [RAConvertor dict2data:ret];
  3804. }
  3805. }
  3806. }
  3807. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3808. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3809. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3810. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3811. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3812. if (![params[@"can_create_backorder"] boolValue]) {
  3813. if (number_of_outOfStock > 0) {
  3814. ret[@"result"]=[NSNumber numberWithInt:8];
  3815. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3816. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3817. }
  3818. }
  3819. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3820. [iSalesDB close_db:db];
  3821. return [RAConvertor dict2data:ret];
  3822. }
  3823. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3824. {
  3825. // UIApplication * app = [UIApplication sharedApplication];
  3826. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3827. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3828. 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];
  3829. sqlite3_stmt * statement;
  3830. int count=0;
  3831. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3832. {
  3833. while (sqlite3_step(statement) == SQLITE_ROW)
  3834. {
  3835. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3836. int bitem_id = sqlite3_column_int(statement, 0);
  3837. int bitem_qty = sqlite3_column_int(statement, 1);
  3838. char *name = (char*)sqlite3_column_text(statement, 2);
  3839. if(name==nil)
  3840. name="";
  3841. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3842. char *description = (char*)sqlite3_column_text(statement, 3);
  3843. if(description==nil)
  3844. description="";
  3845. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3846. double unit_price = sqlite3_column_double(statement, 4);
  3847. int use_unitprice = sqlite3_column_int(statement, 5);
  3848. if(use_unitprice!=1)
  3849. {
  3850. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3851. }
  3852. itemjson[@"model"]=nsname;
  3853. itemjson[@"description"]=nsdescription;
  3854. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3855. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3856. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3857. if(compute)
  3858. {
  3859. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3860. }
  3861. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3862. count++;
  3863. }
  3864. sqlite3_finalize(statement);
  3865. }
  3866. ret[@"count"]=@(count);
  3867. if(count==0)
  3868. return nil;
  3869. else
  3870. return ret;
  3871. }
  3872. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3873. {
  3874. //compute: add part to subtotal;
  3875. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3876. dict_item[@(item_id)]=@"1";
  3877. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3878. double cuft=0;
  3879. double weight=0;
  3880. int carton=0;
  3881. 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];
  3882. sqlite3_stmt * statement;
  3883. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3884. {
  3885. if (sqlite3_step(statement) == SQLITE_ROW)
  3886. {
  3887. double ulength = sqlite3_column_double(statement, 0);
  3888. double uwidth = sqlite3_column_double(statement, 1);
  3889. double uheight = sqlite3_column_double(statement, 2);
  3890. double uweight = sqlite3_column_double(statement, 3);
  3891. double mlength = sqlite3_column_double(statement, 4);
  3892. double mwidth = sqlite3_column_double(statement, 5);
  3893. double mheight = sqlite3_column_double(statement, 6);
  3894. double mweight = sqlite3_column_double(statement, 7);
  3895. double ilength = sqlite3_column_double(statement, 8);
  3896. double iwidth = sqlite3_column_double(statement, 9);
  3897. double iheight = sqlite3_column_double(statement, 10);
  3898. double iweight = sqlite3_column_double(statement, 11);
  3899. // int pcs = sqlite3_column_int(statement,12);
  3900. int mpack = sqlite3_column_int(statement, 13);
  3901. int ipack = sqlite3_column_int(statement, 14);
  3902. double ucbf = sqlite3_column_double(statement, 15);
  3903. // double icbf = sqlite3_column_double(statement, 16);
  3904. // double mcbf = sqlite3_column_double(statement, 17);
  3905. if(ipack==0)
  3906. {
  3907. carton= count/mpack ;
  3908. weight = mweight*carton;
  3909. cuft= carton*(mlength*mwidth*mheight);
  3910. int remain=count%mpack;
  3911. if(remain==0)
  3912. {
  3913. //do nothing;
  3914. }
  3915. else
  3916. {
  3917. carton++;
  3918. weight += uweight*remain;
  3919. cuft += (ulength*uwidth*uheight)*remain;
  3920. }
  3921. }
  3922. else
  3923. {
  3924. carton = count/(mpack*ipack);
  3925. weight = mweight*carton;
  3926. cuft= carton*(mlength*mwidth*mheight);
  3927. int remain=count%(mpack*ipack);
  3928. if(remain==0)
  3929. {
  3930. // do nothing;
  3931. }
  3932. else
  3933. {
  3934. carton++;
  3935. int icarton =remain/ipack;
  3936. int iremain=remain%ipack;
  3937. weight += iweight*icarton;
  3938. cuft += (ilength*iwidth*iheight)*icarton;
  3939. if(iremain==0)
  3940. {
  3941. //do nothing;
  3942. }
  3943. else
  3944. {
  3945. weight += uweight*iremain;
  3946. cuft += (ulength*uwidth*uheight)*iremain;
  3947. }
  3948. }
  3949. }
  3950. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3951. cuft=ucbf*count;
  3952. weight= uweight*count;
  3953. #endif
  3954. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3955. cuft=ucbf*count;
  3956. weight= uweight*count;
  3957. #endif
  3958. }
  3959. sqlite3_finalize(statement);
  3960. }
  3961. if(compute)
  3962. {
  3963. NSArray * arr_count=nil;
  3964. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3965. for(int i=0;i<arr_bundle.count;i++)
  3966. {
  3967. dict_item[arr_bundle[i]]=@"1";
  3968. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3969. cuft+=[bundlejson[@"cuft"] doubleValue];
  3970. weight+=[bundlejson[@"weight"] doubleValue];
  3971. carton+=[bundlejson[@"carton"] intValue];
  3972. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3973. }
  3974. }
  3975. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3976. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3977. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3978. ret[@"items"]=dict_item;
  3979. return ret;
  3980. }
  3981. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3982. {
  3983. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3984. // 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 ];
  3985. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3986. DebugLog(@"offline_login sql:%@",sqlQuery);
  3987. sqlite3_stmt * statement;
  3988. int cart_count=0;
  3989. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3990. if ( dbresult== SQLITE_OK)
  3991. {
  3992. while (sqlite3_step(statement) == SQLITE_ROW)
  3993. {
  3994. int item_id = sqlite3_column_int(statement, 0);
  3995. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3996. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3997. }
  3998. sqlite3_finalize(statement);
  3999. }
  4000. return cart_count;
  4001. }
  4002. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4003. //{
  4004. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4005. // sqlite3 *db = [iSalesDB get_db];
  4006. // UIApplication * app = [UIApplication sharedApplication];
  4007. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4008. //
  4009. // NSString* orderCode=params[@"orderCode"];
  4010. //
  4011. // int sort = [[params objectForKey:@"sort"] intValue];
  4012. // NSString *sort_str = @"";
  4013. // switch (sort) {
  4014. // case 0:{
  4015. // sort_str = @"order by c.modify_time desc";
  4016. // }
  4017. // break;
  4018. // case 1:{
  4019. // sort_str = @"order by c.modify_time asc";
  4020. // }
  4021. // break;
  4022. // case 2:{
  4023. // sort_str = @"order by m.name asc";
  4024. // }
  4025. // break;
  4026. // case 3:{
  4027. // sort_str = @"order by m.name desc";
  4028. // }
  4029. // break;
  4030. // case 4:{
  4031. // sort_str = @"order by m.description asc";
  4032. // }
  4033. // break;
  4034. //
  4035. // default:
  4036. // break;
  4037. //
  4038. // }
  4039. //
  4040. //
  4041. //
  4042. // 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 ];
  4043. //
  4044. //
  4045. //// 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 ];
  4046. //
  4047. //
  4048. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4049. //
  4050. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4051. // sqlite3_stmt * statement;
  4052. //
  4053. //
  4054. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4055. //
  4056. // NSDate *date1 = [NSDate date];
  4057. //
  4058. // int count=0;
  4059. // int cart_count=0;
  4060. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4061. // if ( dbresult== SQLITE_OK)
  4062. // {
  4063. //
  4064. //
  4065. // while (sqlite3_step(statement) == SQLITE_ROW)
  4066. // {
  4067. //// NSDate *row_date = [NSDate date];
  4068. //
  4069. //
  4070. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4071. //
  4072. //
  4073. //
  4074. //
  4075. // int product_id = sqlite3_column_int(statement, 0);
  4076. //
  4077. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4078. //
  4079. // int item_id = sqlite3_column_int(statement, 7);
  4080. //
  4081. // NSString* Price=nil;
  4082. // if(str_price==nil)
  4083. // {
  4084. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4085. //// NSDate *price_date = [NSDate date];
  4086. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4087. //// DebugLog(@"price time interval");
  4088. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4089. //
  4090. // if(price==nil)
  4091. // Price=@"No Price.";
  4092. // else
  4093. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4094. // }
  4095. // else
  4096. // {
  4097. //
  4098. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4099. // }
  4100. //
  4101. //
  4102. // double discount = sqlite3_column_double(statement, 2);
  4103. // int item_count = sqlite3_column_int(statement, 3);
  4104. //
  4105. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4106. // NSString *nsline_note=nil;
  4107. // if(line_note!=nil)
  4108. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4109. //
  4110. //
  4111. // char *name = (char*)sqlite3_column_text(statement, 5);
  4112. // NSString *nsname=nil;
  4113. // if(name!=nil)
  4114. // nsname= [[NSString alloc]initWithUTF8String:name];
  4115. //
  4116. // char *description = (char*)sqlite3_column_text(statement, 6);
  4117. // NSString *nsdescription=nil;
  4118. // if(description!=nil)
  4119. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4120. //
  4121. //
  4122. //
  4123. // int stockUom = sqlite3_column_int(statement, 8);
  4124. // int _id = sqlite3_column_int(statement, 9);
  4125. // int availability = sqlite3_column_int(statement, 10);
  4126. //
  4127. //// NSDate *subtotal_date = [NSDate date];
  4128. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4129. //// DebugLog(@"subtotal_date time interval");
  4130. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4131. //
  4132. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4133. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4134. // int carton=[bsubtotaljson[@"carton"] intValue];
  4135. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4136. //
  4137. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4138. //// NSDate *img_date = [NSDate date];
  4139. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4140. //// DebugLog(@"img_date time interval");
  4141. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4142. //
  4143. // itemjson[@"model"]=nsname;
  4144. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4145. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4146. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4147. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4148. // itemjson[@"check"]=@"true";
  4149. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4150. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4151. // itemjson[@"unit_price"]=Price;
  4152. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4153. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4154. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4155. // itemjson[@"note"]=nsline_note;
  4156. // if (!appDelegate.can_create_backorder) {
  4157. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4158. // }
  4159. //// NSDate *date2 = [NSDate date];
  4160. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4161. //// DebugLog(@"model_bundle time interval");
  4162. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4163. //
  4164. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4165. // count++;
  4166. //
  4167. //// DebugLog(@"row time interval");
  4168. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4169. // }
  4170. //
  4171. //
  4172. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4173. // sqlite3_finalize(statement);
  4174. // }
  4175. //
  4176. //
  4177. // DebugLog(@"request cart total time interval");
  4178. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4179. //
  4180. //
  4181. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4182. //
  4183. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4184. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4185. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4186. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4187. //
  4188. // ret[@"mode"]=@"Regular Mode";
  4189. //
  4190. // [iSalesDB close_db:db];
  4191. //
  4192. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4193. //
  4194. // DebugLog(@"general notes :%@",general_note);
  4195. //
  4196. // ret[@"general_note"]= general_note;
  4197. //
  4198. // return [RAConvertor dict2data:ret];
  4199. //}
  4200. +(NSData*) offline_login :(NSMutableDictionary *) params
  4201. {
  4202. NSString* user = [params valueForKey:@"user"];
  4203. NSString* password = [params valueForKey:@"password"];
  4204. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4205. sqlite3 *db = [iSalesDB get_db];
  4206. 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"]];
  4207. DebugLog(@"offline_login sql:%@",sqlQuery);
  4208. sqlite3_stmt * statement;
  4209. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4210. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4211. {
  4212. if (sqlite3_step(statement) == SQLITE_ROW)
  4213. {
  4214. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4215. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4216. int can_show_price = sqlite3_column_int(statement, 0);
  4217. int can_see_price = sqlite3_column_int(statement, 1);
  4218. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4219. if(contact_id==nil)
  4220. contact_id="";
  4221. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4222. int user_type = sqlite3_column_int(statement, 3);
  4223. int can_cancel_order = sqlite3_column_int(statement, 4);
  4224. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4225. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4226. int can_delete_order = sqlite3_column_int(statement, 7);
  4227. int can_submit_order = sqlite3_column_int(statement, 8);
  4228. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4229. int can_create_order = sqlite3_column_int(statement, 10);
  4230. char *mode = (char*)sqlite3_column_text(statement, 11);
  4231. if(mode==nil)
  4232. mode="";
  4233. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4234. char *username = (char*)sqlite3_column_text(statement, 12);
  4235. if(username==nil)
  4236. username="";
  4237. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4238. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4239. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4240. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4241. [header setValue:nscontact_id forKey:@"contact_id"];
  4242. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4243. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4244. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4245. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4246. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4247. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4248. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4249. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4250. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4251. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4252. [header setValue:nsusername forKey:@"username"];
  4253. NSError* error=nil;
  4254. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4255. [header setValue:statusFilter forKey:@"statusFilter"];
  4256. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4257. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4258. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4259. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4260. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4261. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4262. [ret setObject:header forKey:@"header"];
  4263. [ret setValue:nsmode forKey:@"mode"];
  4264. }
  4265. sqlite3_finalize(statement);
  4266. }
  4267. [iSalesDB close_db:db];
  4268. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4269. return [RAConvertor dict2data:ret];
  4270. }
  4271. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4272. {
  4273. assert(params[@"mode"]!=nil);
  4274. NSString* contactId = [params valueForKey:@"contactId"];
  4275. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4276. sqlite3 *db = [iSalesDB get_db];
  4277. NSString *sqlQuery = nil;
  4278. {
  4279. 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];
  4280. }
  4281. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4282. sqlite3_stmt * statement;
  4283. [ret setValue:@"2" forKey:@"result"];
  4284. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4285. {
  4286. //int i = 0;
  4287. if (sqlite3_step(statement) == SQLITE_ROW)
  4288. {
  4289. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4290. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4291. // int editable = sqlite3_column_int(statement, 0);
  4292. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4293. NSString *nscompany_name =nil;
  4294. if(company_name==nil)
  4295. nscompany_name=@"";
  4296. else
  4297. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4298. char *country = (char*)sqlite3_column_text(statement, 2);
  4299. if(country==nil)
  4300. country="";
  4301. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4302. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4303. // if(addr==nil)
  4304. // addr="";
  4305. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4306. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4307. if(zipcode==nil)
  4308. zipcode="";
  4309. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4310. char *state = (char*)sqlite3_column_text(statement, 5);
  4311. if(state==nil)
  4312. state="";
  4313. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4314. char *city = (char*)sqlite3_column_text(statement, 6);
  4315. if(city==nil)
  4316. city="";
  4317. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4318. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4319. // NSString *nscontact_name = nil;
  4320. // if(contact_name==nil)
  4321. // nscontact_name=@"";
  4322. // else
  4323. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4324. char *phone = (char*)sqlite3_column_text(statement, 8);
  4325. NSString *nsphone = nil;
  4326. if(phone==nil)
  4327. nsphone=@"";
  4328. else
  4329. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4330. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4331. // if(contact_id==nil)
  4332. // contact_id="";
  4333. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4334. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4335. if(addr_1==nil)
  4336. addr_1="";
  4337. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4338. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4339. if(addr_2==nil)
  4340. addr_2="";
  4341. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4342. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4343. if(addr_3==nil)
  4344. addr_3="";
  4345. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4346. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4347. if(addr_4==nil)
  4348. addr_4="";
  4349. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4350. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4351. if(first_name==nil)
  4352. first_name="";
  4353. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4354. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4355. if(last_name==nil)
  4356. last_name="";
  4357. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4358. char *fax = (char*)sqlite3_column_text(statement, 16);
  4359. NSString *nsfax = nil;
  4360. if(fax==nil)
  4361. nsfax=@"";
  4362. else
  4363. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4364. char *email = (char*)sqlite3_column_text(statement, 17);
  4365. NSString *nsemail = nil;
  4366. if(email==nil)
  4367. nsemail=@"";
  4368. else
  4369. nsemail= [[NSString alloc]initWithUTF8String:email];
  4370. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4371. NSString *nsimg_0 = nil;
  4372. if(img_0==nil)
  4373. nsimg_0=@"";
  4374. else
  4375. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4376. [self copy_bcardImg:nsimg_0];
  4377. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4378. NSString *nsimg_1 = nil;
  4379. if(img_1==nil)
  4380. nsimg_1=@"";
  4381. else
  4382. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4383. [self copy_bcardImg:nsimg_1];
  4384. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4385. NSString *nsimg_2 = nil;
  4386. if(img_2==nil)
  4387. nsimg_2=@"";
  4388. else
  4389. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4390. [self copy_bcardImg:nsimg_2];
  4391. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4392. NSString *nsprice_type = nil;
  4393. if(price_type==nil)
  4394. nsprice_type=@"";
  4395. else
  4396. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4397. char *notes = (char*)sqlite3_column_text(statement, 22);
  4398. NSString *nsnotes = nil;
  4399. if(notes==nil)
  4400. nsnotes=@"";
  4401. else
  4402. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4403. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4404. NSString *nssalesrep = nil;
  4405. if(salesrep==nil)
  4406. nssalesrep=@"";
  4407. else
  4408. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4409. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4410. {
  4411. // decrypt
  4412. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4413. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4414. nsphone=[AESCrypt fastdecrypt:nsphone];
  4415. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4416. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4417. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4418. }
  4419. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4420. [arr_name addObject:nsfirst_name];
  4421. [arr_name addObject:nslast_name];
  4422. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4423. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4424. [arr_ext addObject:nsaddr_1];
  4425. [arr_ext addObject:nsaddr_2];
  4426. [arr_ext addObject:nsaddr_3];
  4427. [arr_ext addObject:nsaddr_4];
  4428. [arr_ext addObject:@"\r\n"];
  4429. [arr_ext addObject:nscity];
  4430. [arr_ext addObject:nsstate];
  4431. [arr_ext addObject:nszipcode];
  4432. [arr_ext addObject:nscountry];
  4433. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4434. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4435. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4436. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4437. [item setValue:nscountry forKey:@"customer_country"];
  4438. [item setValue:nsphone forKey:@"customer_phone"];
  4439. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4440. [item setValue:nscompany_name forKey:@"customer_name"];
  4441. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4442. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4443. [item setValue:nsext forKey:@"customer_contact_ext"];
  4444. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4445. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4446. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4447. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4448. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4449. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4450. [item setValue:nslast_name forKey:@"customer_last_name"];
  4451. [item setValue:nscity forKey:@"customer_city"];
  4452. [item setValue:nsstate forKey:@"customer_state"];
  4453. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4454. [item setValue:contactId forKey:@"customer_cid"];
  4455. [item setValue:nscontact_name forKey:@"customer_contact"];
  4456. [item setValue:nsfax forKey:@"customer_fax"];
  4457. [item setValue:nsemail forKey:@"customer_email"];
  4458. [item setValue:contact_type forKey:@"customer_contact_type"];
  4459. [ret setObject:item forKey:@"customerInfo"];
  4460. // i++;
  4461. }
  4462. // UIApplication * app = [UIApplication sharedApplication];
  4463. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4464. [ret setValue:params[@"mode"] forKey:@"mode"];
  4465. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4466. sqlite3_finalize(statement);
  4467. }
  4468. [iSalesDB close_db:db];
  4469. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4470. return ret;
  4471. }
  4472. + (bool) copy_bcardImg:(NSString*) filename
  4473. {
  4474. if(filename.length==0)
  4475. return false;
  4476. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4477. bool ret=false;
  4478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4479. NSString *cachefolder = [paths objectAtIndex:0];
  4480. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4481. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4482. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4483. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4484. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4485. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4486. //
  4487. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4488. NSFileManager* fileManager = [NSFileManager defaultManager];
  4489. BOOL bdir=NO;
  4490. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4491. {
  4492. NSError *error = nil;
  4493. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4494. {
  4495. ret=false;
  4496. }
  4497. else
  4498. {
  4499. ret=true;
  4500. }
  4501. // NSError *error = nil;
  4502. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4503. //
  4504. // if(!bsuccess)
  4505. // {
  4506. // DebugLog(@"Create offline_createimg folder failed");
  4507. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4508. // return [RAConvertor dict2data:ret];
  4509. // }
  4510. // if(bsuccess)
  4511. // {
  4512. // sqlite3 *db = [self get_db];
  4513. //
  4514. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4515. // [iSalesDB close_db:db];
  4516. // }
  4517. }
  4518. return ret;
  4519. //
  4520. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4521. // if(bsuccess)
  4522. // {
  4523. // NSError *error = nil;
  4524. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4525. // {
  4526. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4527. // }
  4528. // else
  4529. // {
  4530. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4531. //
  4532. // ret[@"img_url_aname"]=filename;
  4533. // ret[@"img_url"]=savedImagePath;
  4534. // }
  4535. // }
  4536. }
  4537. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4538. {
  4539. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4540. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4541. NSString *cachefolder = [paths objectAtIndex:0];
  4542. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4543. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4544. NSFileManager* fileManager = [NSFileManager defaultManager];
  4545. BOOL bdir=YES;
  4546. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4547. {
  4548. NSError *error = nil;
  4549. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4550. if(!bsuccess)
  4551. {
  4552. DebugLog(@"Create offline_createimg folder failed");
  4553. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4554. return [RAConvertor dict2data:ret];
  4555. }
  4556. // if(bsuccess)
  4557. // {
  4558. // sqlite3 *db = [self get_db];
  4559. //
  4560. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4561. // [iSalesDB close_db:db];
  4562. // }
  4563. }
  4564. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4565. {
  4566. NSError *error = nil;
  4567. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4568. if(!bsuccess)
  4569. {
  4570. DebugLog(@"Create img_cache folder failed");
  4571. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4572. return [RAConvertor dict2data:ret];
  4573. }
  4574. // if(bsuccess)
  4575. // {
  4576. // sqlite3 *db = [self get_db];
  4577. //
  4578. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4579. // [iSalesDB close_db:db];
  4580. // }
  4581. }
  4582. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4583. //JEPG格式
  4584. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4585. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4586. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4587. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4588. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4589. if(bsuccess)
  4590. {
  4591. NSError *error = nil;
  4592. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4593. {
  4594. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4595. }
  4596. else
  4597. {
  4598. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4599. ret[@"img_url_aname"]=filename;
  4600. ret[@"img_url"]=filename;
  4601. }
  4602. }
  4603. else
  4604. {
  4605. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4606. }
  4607. return [RAConvertor dict2data:ret];
  4608. }
  4609. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4610. {
  4611. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4612. category = [category substringToIndex:3];
  4613. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4614. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4615. params[@"category"]= category;
  4616. ret[@"params"]= params;
  4617. [ret setValue:@"detail" forKey:@"target"];
  4618. [ret setValue:@"popup" forKey:@"action"];
  4619. [ret setValue:@"content" forKey:@"type"];
  4620. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4621. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4622. [ret setValue:@"true" forKey:@"single_row"];
  4623. [ret setValue:@"true" forKey:@"partial_refresh"];
  4624. // sqlite3 *db = [iSalesDB get_db];
  4625. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4626. NSString *sqlQuery =nil;
  4627. #ifdef BUILD_NPD
  4628. 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 ;
  4629. #else
  4630. 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];
  4631. #endif
  4632. sqlite3_stmt * statement;
  4633. int count = 0;
  4634. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4635. // int count=0;
  4636. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4637. {
  4638. int i=0;
  4639. while (sqlite3_step(statement) == SQLITE_ROW)
  4640. {
  4641. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4642. // char *name = (char*)sqlite3_column_text(statement, 1);
  4643. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4644. char *name = (char*)sqlite3_column_text(statement, 0);
  4645. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4646. int product_id = sqlite3_column_int(statement, 1);
  4647. char *url = (char*)sqlite3_column_text(statement, 2);
  4648. if(url==nil)
  4649. url="";
  4650. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4651. [item setValue:nsurl forKey:@"picture_path"];
  4652. [item setValue:nsname forKey:@"fash_name"];
  4653. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4654. [item setValue:category forKey:@"category"];
  4655. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4656. i++;
  4657. }
  4658. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4659. sqlite3_finalize(statement);
  4660. }
  4661. DebugLog(@"count:%d",count);
  4662. // [iSalesDB close_db:db];
  4663. return ret;
  4664. }
  4665. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4666. {
  4667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4668. [ret setValue:key forKey:@"key"];
  4669. [ret setValue:value forKey:@"val"];
  4670. [ret setValue:@"price" forKey:@"type"];
  4671. return ret;
  4672. }
  4673. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4674. {
  4675. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4676. [ret setValue:key forKey:@"key"];
  4677. [ret setValue:value forKey:@"val"];
  4678. return ret;
  4679. }
  4680. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4681. {
  4682. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4683. [ret setValue:@"0" forKey:@"img_count"];
  4684. // sqlite3 *db = [iSalesDB get_db];
  4685. 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 ;
  4686. sqlite3_stmt * statement;
  4687. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4688. {
  4689. int i=0;
  4690. if (sqlite3_step(statement) == SQLITE_ROW)
  4691. {
  4692. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4693. // char *name = (char*)sqlite3_column_text(statement, 1);
  4694. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4695. char *value = (char*)sqlite3_column_text(statement, 0);
  4696. if(value==nil)
  4697. value="";
  4698. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4699. char *key = (char*)sqlite3_column_text(statement, 1);
  4700. if(key==nil)
  4701. key="";
  4702. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4703. [item setValue:nsvalue forKey:@"val"];
  4704. [item setValue:nskey forKey:@"key"];
  4705. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4706. [ret setValue:@"1" forKey:@"count"];
  4707. i++;
  4708. }
  4709. sqlite3_finalize(statement);
  4710. }
  4711. // [iSalesDB close_db:db];
  4712. return ret;
  4713. }
  4714. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4715. {
  4716. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4717. [ret setValue:@"0" forKey:@"count"];
  4718. // sqlite3 *db = [iSalesDB get_db];
  4719. 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;
  4720. sqlite3_stmt * statement;
  4721. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4722. {
  4723. int i=0;
  4724. while (sqlite3_step(statement) == SQLITE_ROW)
  4725. {
  4726. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4727. // char *name = (char*)sqlite3_column_text(statement, 1);
  4728. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4729. char *value = (char*)sqlite3_column_text(statement, 0);
  4730. if(value==nil)
  4731. value="";
  4732. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4733. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4734. if(selector_display==nil)
  4735. selector_display="";
  4736. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4737. int product_id = sqlite3_column_int(statement, 2);
  4738. char *category = (char*)sqlite3_column_text(statement, 3);
  4739. if(category==nil)
  4740. category="";
  4741. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4742. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4743. [item setValue:nsvalue forKey:@"title"];
  4744. [item setValue:url forKey:@"pic_url"];
  4745. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4746. [params setValue:@"2" forKey:@"count"];
  4747. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4748. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4749. [param0 setValue:@"product_id" forKey:@"name"];
  4750. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4751. [param1 setValue:nscategory forKey:@"val"];
  4752. [param1 setValue:@"category" forKey:@"name"];
  4753. [params setObject:param0 forKey:@"param_0"];
  4754. [params setObject:param1 forKey:@"param_1"];
  4755. [item setObject:params forKey:@"params"];
  4756. [ret setValue:nsselector_display forKey:@"name"];
  4757. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4758. i++;
  4759. }
  4760. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4761. [ret setValue:@"switch" forKey:@"action"];
  4762. sqlite3_finalize(statement);
  4763. }
  4764. // [iSalesDB close_db:db];
  4765. return ret;
  4766. }
  4767. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4768. {
  4769. // model 在 category search 显示的图片。
  4770. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4771. // sqlite3 *db = [iSalesDB get_db];
  4772. 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];
  4773. sqlite3_stmt * statement;
  4774. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4775. {
  4776. while (sqlite3_step(statement) == SQLITE_ROW)
  4777. {
  4778. char *url = (char*)sqlite3_column_text(statement, 0);
  4779. if(url==nil)
  4780. url="";
  4781. int type = sqlite3_column_int(statement, 1);
  4782. if(type==0)
  4783. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4784. else
  4785. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4786. }
  4787. sqlite3_finalize(statement);
  4788. }
  4789. // [iSalesDB close_db:db];
  4790. return ret;
  4791. }
  4792. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4793. {
  4794. int item_id=-1;
  4795. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4796. sqlite3_stmt * statement;
  4797. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4798. {
  4799. if (sqlite3_step(statement) == SQLITE_ROW)
  4800. {
  4801. item_id = sqlite3_column_int(statement, 0);
  4802. }
  4803. sqlite3_finalize(statement);
  4804. }
  4805. return item_id;
  4806. }
  4807. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4808. {
  4809. // NSString* ret = @"";
  4810. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4811. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4812. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4813. sqlite3_stmt * statement;
  4814. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4815. {
  4816. while (sqlite3_step(statement) == SQLITE_ROW)
  4817. {
  4818. int bitem_id = sqlite3_column_int(statement, 0);
  4819. int bitem_qty = sqlite3_column_int(statement, 1);
  4820. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4821. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4822. }
  4823. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4824. sqlite3_finalize(statement);
  4825. }
  4826. // if(ret==nil)
  4827. // ret=@"";
  4828. *count=arr_count;
  4829. return arr_bundle;
  4830. }
  4831. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4832. {
  4833. // get default sold qty, return -1 if model not found;
  4834. int ret = -1;
  4835. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4836. sqlite3_stmt * statement;
  4837. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4838. {
  4839. if (sqlite3_step(statement) == SQLITE_ROW)
  4840. {
  4841. ret = sqlite3_column_int(statement, 0);
  4842. }
  4843. sqlite3_finalize(statement);
  4844. }
  4845. return ret;
  4846. }
  4847. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4848. {
  4849. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4850. [ret setValue:@"0" forKey:@"img_count"];
  4851. // sqlite3 *db = [iSalesDB get_db];
  4852. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4853. sqlite3_stmt * statement;
  4854. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4855. {
  4856. int i=0;
  4857. while (sqlite3_step(statement) == SQLITE_ROW)
  4858. {
  4859. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4860. // char *name = (char*)sqlite3_column_text(statement, 1);
  4861. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4862. char *url = (char*)sqlite3_column_text(statement, 0);
  4863. if(url==nil)
  4864. url="";
  4865. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4866. [item setValue:nsurl forKey:@"s"];
  4867. [item setValue:nsurl forKey:@"l"];
  4868. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4869. i++;
  4870. }
  4871. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4872. sqlite3_finalize(statement);
  4873. }
  4874. // [iSalesDB close_db:db];
  4875. return ret;
  4876. }
  4877. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4878. // UIApplication * app = [UIApplication sharedApplication];
  4879. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4880. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4881. sqlite3 *db = [iSalesDB get_db];
  4882. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4883. NSString* product_id=params[@"product_id"];
  4884. NSString *item_count_str = params[@"item_count"];
  4885. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4886. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4887. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4888. // NSString *sql = @"";
  4889. for(int i=0;i<arr.count;i++)
  4890. {
  4891. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4892. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4893. __block int cart_count = 0;
  4894. if (!item_count_str) {
  4895. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4896. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4897. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4898. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4899. cart_count = [[model_set_components lastObject] intValue];
  4900. }];
  4901. }
  4902. if(count==0)
  4903. {
  4904. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4905. sqlite3_stmt *stmt;
  4906. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4907. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4908. if (item_count_arr) {
  4909. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4910. } else {
  4911. sqlite3_bind_int(stmt,2,cart_count);
  4912. }
  4913. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4914. [iSalesDB execSql:@"ROLLBACK" db:db];
  4915. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4916. [iSalesDB close_db:db];
  4917. DebugLog(@"add to wishlist error");
  4918. return ret;
  4919. }
  4920. } else {
  4921. int qty = 0;
  4922. if (item_count_arr) {
  4923. qty = [item_count_arr[i] intValue];
  4924. } else {
  4925. qty = cart_count;
  4926. }
  4927. 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]];
  4928. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4929. [iSalesDB execSql:@"ROLLBACK" db:db];
  4930. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4931. [iSalesDB close_db:db];
  4932. DebugLog(@"add to wishlist error");
  4933. return ret;
  4934. }
  4935. }
  4936. }
  4937. // [iSalesDB execSql:sql db:db];
  4938. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4939. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4940. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4941. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4942. [iSalesDB close_db:db];
  4943. ret[@"wish_count"]=@(count);
  4944. ret[@"result"]= [NSNumber numberWithInt:2];
  4945. return ret;
  4946. }
  4947. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4948. // 0 category
  4949. // 1 search
  4950. // 2 itemsearch
  4951. NSData *ret = nil;
  4952. NSDictionary *items = nil;
  4953. switch (from) {
  4954. case 0:{
  4955. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4956. }
  4957. break;
  4958. case 1:{
  4959. items = [[self search:params limited:NO] objectForKey:@"items"];
  4960. }
  4961. break;
  4962. case 2:{
  4963. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4964. }
  4965. break;
  4966. default:
  4967. break;
  4968. }
  4969. if (!items) {
  4970. return ret;
  4971. }
  4972. int count = [[items objectForKey:@"count"] intValue];
  4973. NSMutableString *product_id_str = [@"" mutableCopy];
  4974. for (int i = 0; i < count; i++) {
  4975. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4976. NSDictionary *item = [items objectForKey:key];
  4977. NSString *product_id = [item objectForKey:@"product_id"];
  4978. if (i == 0) {
  4979. [product_id_str appendString:product_id];
  4980. } else {
  4981. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4982. }
  4983. }
  4984. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4985. if ([add_to isEqualToString:@"cart"]) {
  4986. NSString *order_code = [params objectForKey:@"orderCode"];
  4987. if (order_code.length) {
  4988. NSDictionary *newParams = @{
  4989. @"product_id" : product_id_str,
  4990. @"orderCode" : order_code,
  4991. @"can_create_backorder":params[@"can_create_backorder"]
  4992. };
  4993. ret = [self offline_add2cart:newParams.mutableCopy];
  4994. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4995. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4996. ret = [RAConvertor dict2data:retDic];
  4997. }
  4998. } else if([add_to isEqualToString:@"wishlist"]) {
  4999. NSDictionary *newParams = @{
  5000. @"product_id" : product_id_str
  5001. };
  5002. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5003. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5004. ret = [RAConvertor dict2data:retDic];
  5005. } else if([add_to isEqualToString:@"portfolio"]) {
  5006. NSDictionary *newParams = @{
  5007. @"product_id" : product_id_str
  5008. };
  5009. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5010. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5011. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5012. ret = [RAConvertor dict2data:retDic];
  5013. }
  5014. return ret;
  5015. }
  5016. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5017. {
  5018. return [self addAll:params from:0];
  5019. }
  5020. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5021. {
  5022. return [self addAll:params from:1];
  5023. }
  5024. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5025. {
  5026. return [self addAll:params from:2];
  5027. }
  5028. #pragma mark - Jack
  5029. #warning 做SQL操作时转义!!
  5030. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5031. // "val_227" : {
  5032. // "check" : 1,
  5033. // "value" : "US United States",
  5034. // "value_id" : "228"
  5035. // },
  5036. if (!countryCode) {
  5037. countryCode = @"US";
  5038. }
  5039. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5040. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5041. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5042. int code_id = sqlite3_column_int(stmt, 3); // id
  5043. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5044. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5045. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5046. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5047. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5048. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5049. }
  5050. long n = *count;
  5051. *count = n + 1;
  5052. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5053. [container setValue:countryDic forKey:key];
  5054. }] mutableCopy];
  5055. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5056. return ret;
  5057. }
  5058. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5059. countryCode = [self translateSingleQuote:countryCode];
  5060. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5061. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5062. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5063. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5064. if (name == NULL) {
  5065. name = "";
  5066. }
  5067. if (code == NULL) {
  5068. code = "";
  5069. }
  5070. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5071. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5072. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5073. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5074. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5075. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5076. }
  5077. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5078. [container setValue:stateDic forKey:key];
  5079. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5080. DebugLog(@"query all state error: %@",err_msg);
  5081. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5082. // [stateDic setValue:@"Other" forKey:@"value"];
  5083. // [stateDic setValue:@"" forKey:@"value_id"];
  5084. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5085. //
  5086. // if (state_code && [@"" isEqualToString:state_code]) {
  5087. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5088. // }
  5089. //
  5090. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5091. // [container setValue:stateDic forKey:key];
  5092. }] mutableCopy];
  5093. [ret removeObjectForKey:@"result"];
  5094. // failure 可以不用了,一样的
  5095. if (ret.allKeys.count == 0) {
  5096. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5097. [stateDic setValue:@"Other" forKey:@"value"];
  5098. [stateDic setValue:@"" forKey:@"value_id"];
  5099. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5100. if (state_code && [@"" isEqualToString:state_code]) {
  5101. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5102. }
  5103. NSString *key = [NSString stringWithFormat:@"val_0"];
  5104. [ret setValue:stateDic forKey:key];
  5105. }
  5106. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5107. return ret;
  5108. }
  5109. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5110. codeId = [self translateSingleQuote:codeId];
  5111. 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];
  5112. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5113. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5114. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5115. if (name == NULL) {
  5116. name = "";
  5117. }
  5118. if (code == NULL) {
  5119. code = "";
  5120. }
  5121. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5122. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5123. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5124. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5125. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5126. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5127. }
  5128. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5129. [container setValue:stateDic forKey:key];
  5130. }] mutableCopy];
  5131. [ret removeObjectForKey:@"result"];
  5132. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5133. return ret;
  5134. }
  5135. + (NSDictionary *)offline_getPrice {
  5136. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5137. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5138. char *name = (char *) sqlite3_column_text(stmt, 1);
  5139. int type = sqlite3_column_int(stmt, 2);
  5140. int orderBy = sqlite3_column_int(stmt, 3);
  5141. if (name == NULL) {
  5142. name = "";
  5143. }
  5144. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5145. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5146. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5147. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5148. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5149. if (orderBy == 0) {
  5150. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5151. }
  5152. [container setValue:priceDic forKey:key];
  5153. }] mutableCopy];
  5154. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5155. return ret;
  5156. }
  5157. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5158. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5159. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5160. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5161. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5162. // int _id = sqlite3_column_int(stmt, 0);
  5163. NSString *name = [self textAtColumn:1 statement:stmt];
  5164. NSDictionary *typeDic = @{
  5165. @"value_id" : name,
  5166. @"value" : name,
  5167. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5168. };
  5169. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5170. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5171. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5172. }];
  5173. return ret;
  5174. }
  5175. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5176. // 首先从offline_login表中取出sales_code
  5177. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5178. // NSString *user = app.user;
  5179. user = [self translateSingleQuote:user];
  5180. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5181. __block NSString *user_code = @"";
  5182. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5183. char *code = (char *)sqlite3_column_text(stmt, 0);
  5184. if (code == NULL) {
  5185. code = "";
  5186. }
  5187. user_code = [NSString stringWithUTF8String:code];
  5188. }];
  5189. // 再取所有salesRep
  5190. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5191. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5192. // 1 name 2 code 3 salesrep_id
  5193. char *name = (char *)sqlite3_column_text(stmt, 1);
  5194. char *code = (char *)sqlite3_column_text(stmt, 2);
  5195. int salesrep_id = sqlite3_column_int(stmt, 3);
  5196. if (name == NULL) {
  5197. name = "";
  5198. }
  5199. if (code == NULL) {
  5200. code = "";
  5201. }
  5202. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5203. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5204. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5205. // 比较code 相等则check
  5206. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5207. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5208. }
  5209. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5210. }] mutableCopy];
  5211. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5212. return ret;
  5213. }
  5214. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5215. zipcode = [self translateSingleQuote:zipcode];
  5216. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5217. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5218. char *country = (char *)sqlite3_column_text(stmt, 0);
  5219. char *state = (char *)sqlite3_column_text(stmt, 1);
  5220. char *city = (char *)sqlite3_column_text(stmt, 2);
  5221. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5222. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5223. if (country == NULL) {
  5224. country = "";
  5225. }
  5226. if (state == NULL) {
  5227. state = "";
  5228. }
  5229. if (city == NULL) {
  5230. city = "";
  5231. }
  5232. if (country_code == NULL) {
  5233. country_code = "";
  5234. }
  5235. if (state_code == NULL) {
  5236. state_code = "";
  5237. }
  5238. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5239. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5240. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5241. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5242. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5243. }] mutableCopy];
  5244. return ret;
  5245. }
  5246. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5247. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5248. [item setValue:value forKey:valueKey];
  5249. [dic setValue:item forKey:itemKey];
  5250. }
  5251. + (NSString *)countryCodeByid:(NSString *)code_id {
  5252. NSString *ret = nil;
  5253. code_id = [self translateSingleQuote:code_id];
  5254. sqlite3 *db = [iSalesDB get_db];
  5255. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5256. sqlite3_stmt * statement;
  5257. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5258. while (sqlite3_step(statement) == SQLITE_ROW) {
  5259. char *code = (char *)sqlite3_column_text(statement, 0);
  5260. if (code == NULL) {
  5261. code = "";
  5262. }
  5263. ret = [NSString stringWithUTF8String:code];
  5264. }
  5265. sqlite3_finalize(statement);
  5266. }
  5267. [iSalesDB close_db:db];
  5268. return ret;
  5269. }
  5270. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5271. NSString *ret = nil;
  5272. code = [self translateSingleQuote:code];
  5273. sqlite3 *db = [iSalesDB get_db];
  5274. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5275. sqlite3_stmt * statement;
  5276. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5277. while (sqlite3_step(statement) == SQLITE_ROW) {
  5278. char *_id = (char *)sqlite3_column_text(statement, 0);
  5279. if (_id == NULL) {
  5280. _id = "";
  5281. }
  5282. ret = [NSString stringWithFormat:@"%s",_id];
  5283. }
  5284. sqlite3_finalize(statement);
  5285. }
  5286. [iSalesDB close_db:db];
  5287. return ret;
  5288. }
  5289. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5290. NSString *name = nil;
  5291. codeId = [self translateSingleQuote:codeId];
  5292. sqlite3 *db = [iSalesDB get_db];
  5293. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5294. sqlite3_stmt * statement;
  5295. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5296. while (sqlite3_step(statement) == SQLITE_ROW) {
  5297. char *value = (char *)sqlite3_column_text(statement, 0);
  5298. if (value == NULL) {
  5299. value = "";
  5300. }
  5301. name = [NSString stringWithUTF8String:value];
  5302. }
  5303. sqlite3_finalize(statement);
  5304. }
  5305. [iSalesDB close_db:db];
  5306. return name;
  5307. }
  5308. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5309. NSString *ret = nil;
  5310. sqlite3 *db = [iSalesDB get_db];
  5311. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5312. sqlite3_stmt * statement;
  5313. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5314. while (sqlite3_step(statement) == SQLITE_ROW) {
  5315. char *name = (char *)sqlite3_column_text(statement, 0);
  5316. if (name == NULL) {
  5317. name = "";
  5318. }
  5319. ret = [NSString stringWithUTF8String:name];
  5320. }
  5321. sqlite3_finalize(statement);
  5322. }
  5323. [iSalesDB close_db:db];
  5324. return ret;
  5325. }
  5326. + (NSString *)salesRepCodeById:(NSString *)_id {
  5327. NSString *ret = nil;
  5328. _id = [self translateSingleQuote:_id];
  5329. sqlite3 *db = [iSalesDB get_db];
  5330. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5331. sqlite3_stmt * statement;
  5332. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5333. while (sqlite3_step(statement) == SQLITE_ROW) {
  5334. char *rep = (char *)sqlite3_column_text(statement, 0);
  5335. if (rep == NULL) {
  5336. rep = "";
  5337. }
  5338. ret = [NSString stringWithUTF8String:rep];
  5339. }
  5340. sqlite3_finalize(statement);
  5341. }
  5342. [iSalesDB close_db:db];
  5343. return ret;
  5344. }
  5345. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5346. char *tx = (char *)sqlite3_column_text(stmt, col);
  5347. if (tx == NULL) {
  5348. tx = "";
  5349. }
  5350. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5351. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5352. if (!text) {
  5353. text = @"";
  5354. }
  5355. // 将字符全部为' '的字符串干掉
  5356. int spaceCount = 0;
  5357. for (int i = 0; i < text.length; i++) {
  5358. if ([text characterAtIndex:i] == ' ') {
  5359. spaceCount++;
  5360. }
  5361. }
  5362. if (spaceCount == text.length) {
  5363. text = @"";
  5364. }
  5365. return text;
  5366. }
  5367. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5368. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5369. NSData *data = [NSData dataWithContentsOfFile:path];
  5370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5371. return ret;
  5372. }
  5373. + (NSString *)textFileName:(NSString *)name {
  5374. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5375. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5376. if (!text) {
  5377. text = @"";
  5378. }
  5379. return text;
  5380. }
  5381. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5382. return [[dic objectForKey:key] mutableCopy];
  5383. }
  5384. + (id)translateSingleQuote:(NSString *)string {
  5385. if ([string isKindOfClass:[NSString class]])
  5386. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5387. return string;
  5388. }
  5389. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5390. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5391. }
  5392. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5393. NSString* ret= nil;
  5394. NSString *sqlQuery = nil;
  5395. // 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
  5396. 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];
  5397. sqlite3_stmt * statement;
  5398. // int count=0;
  5399. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5400. {
  5401. if (sqlite3_step(statement) == SQLITE_ROW)
  5402. {
  5403. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5404. if(imgurl==nil)
  5405. imgurl="";
  5406. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5407. ret=nsimgurl;
  5408. }
  5409. sqlite3_finalize(statement);
  5410. }
  5411. else
  5412. {
  5413. [ret setValue:@"8" forKey:@"result"];
  5414. }
  5415. // [iSalesDB close_db:db];
  5416. // DebugLog(@"data string: %@",ret );
  5417. return ret;
  5418. }
  5419. #pragma mark contact Advanced search
  5420. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5421. {
  5422. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5423. return [RAConvertor dict2data:contactAdvanceDic];
  5424. }
  5425. #pragma mark create new contact
  5426. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5427. assert(params[@"user"]!=nil);
  5428. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5429. NSData *data = [NSData dataWithContentsOfFile:path];
  5430. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5431. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5432. NSString *countryCode = nil;
  5433. NSString *countryCode_id = nil;
  5434. NSString *stateCode = nil;
  5435. NSString *city = nil;
  5436. NSString *zipCode = nil;
  5437. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5438. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5439. NSString *code_id = params[@"country"];
  5440. countryCode_id = code_id;
  5441. countryCode = [self countryCodeByid:code_id];
  5442. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5443. NSString *zip_code = params[@"zipcode"];
  5444. // 剔除全部为空格
  5445. int spaceCount = 0;
  5446. for (int i = 0; i < zip_code.length; i++) {
  5447. if ([zip_code characterAtIndex:i] == ' ') {
  5448. spaceCount++;
  5449. }
  5450. }
  5451. if (spaceCount == zip_code.length) {
  5452. zip_code = @"";
  5453. }
  5454. zipCode = zip_code;
  5455. if (zipCode.length > 0) {
  5456. countryCode_id = params[@"country"];
  5457. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5458. countryCode = [dic valueForKey:@"country_code"];
  5459. if (!countryCode) {
  5460. // countryCode = @"US";
  5461. NSString *code_id = params[@"country"];
  5462. countryCode = [self countryCodeByid:code_id];
  5463. }
  5464. stateCode = [dic valueForKey:@"state_code"];
  5465. if (!stateCode.length) {
  5466. stateCode = params[@"state"];
  5467. }
  5468. city = [dic valueForKey:@"city"];
  5469. if (!city.length) {
  5470. city = params[@"city"];
  5471. }
  5472. // zip code
  5473. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5474. [zipDic setValue:zipCode forKey:@"value"];
  5475. [section_0 setValue:zipDic forKey:@"item_11"];
  5476. } else {
  5477. NSString *code_id = params[@"country"];
  5478. countryCode = [self countryCodeByid:code_id];
  5479. stateCode = params[@"state"];
  5480. city = params[@"city"];
  5481. }
  5482. }
  5483. } else {
  5484. // default: US United States
  5485. countryCode = @"US";
  5486. countryCode_id = @"228";
  5487. }
  5488. // country
  5489. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5490. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5491. // state
  5492. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5493. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5494. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5495. // city
  5496. if (city) {
  5497. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5498. [cityDic setValue:city forKey:@"value"];
  5499. [section_0 setValue:cityDic forKey:@"item_13"];
  5500. }
  5501. // price type
  5502. NSDictionary *priceDic = [self offline_getPrice];
  5503. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5504. // contact type
  5505. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5506. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5507. // Sales Rep
  5508. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5509. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5510. [ret setValue:section_0 forKey:@"section_0"];
  5511. return [RAConvertor dict2data:ret];
  5512. }
  5513. #pragma mark save
  5514. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5515. NSString *addr = nil;
  5516. NSString *contact_name = nil;
  5517. NSString *companyName = [params objectForKey:@"company"];
  5518. if (companyName) {
  5519. companyName = [AESCrypt fastencrypt:companyName];
  5520. } else {
  5521. companyName = @"";
  5522. }
  5523. DebugLog(@"company");
  5524. companyName = [self translateSingleQuote:companyName];
  5525. NSString *addr1 = [params objectForKey:@"address"];
  5526. NSString *addr2 = [params objectForKey:@"address2"];
  5527. NSString *addr3 = [params objectForKey:@"address_3"];
  5528. NSString *addr4 = [params objectForKey:@"address_4"];
  5529. if (!addr2) {
  5530. addr2 = @"";
  5531. }
  5532. if (!addr3) {
  5533. addr3 = @"";
  5534. }
  5535. if (!addr4) {
  5536. addr4 = @"";
  5537. }
  5538. if (!addr1) {
  5539. addr1 = @"";
  5540. }
  5541. DebugLog(@"addr");
  5542. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5543. addr = [AESCrypt fastencrypt:addr];
  5544. addr = [self translateSingleQuote:addr];
  5545. if (addr1 && ![addr1 isEqualToString:@""]) {
  5546. addr1 = [AESCrypt fastencrypt:addr1];
  5547. }
  5548. addr1 = [self translateSingleQuote:addr1];
  5549. addr2 = [self translateSingleQuote:addr2];
  5550. addr3 = [self translateSingleQuote:addr3];
  5551. addr4 = [self translateSingleQuote:addr4];
  5552. NSString *country = [params objectForKey:@"country"];
  5553. if (country) {
  5554. country = [self countryNameByCountryCodeId:country];
  5555. } else {
  5556. country = @"";
  5557. }
  5558. DebugLog(@"country");
  5559. country = [self translateSingleQuote:country];
  5560. NSString *state = [params objectForKey:@"state"];
  5561. if (!state) {
  5562. state = @"";
  5563. }
  5564. DebugLog(@"state");
  5565. state = [self translateSingleQuote:state];
  5566. NSString *city = [params objectForKey:@"city"];
  5567. if (!city) {
  5568. city = @"";
  5569. }
  5570. city = [self translateSingleQuote:city];
  5571. NSString *zipcode = [params objectForKey:@"zipcode"];
  5572. if (!zipcode) {
  5573. zipcode = @"";
  5574. }
  5575. DebugLog(@"zip");
  5576. zipcode = [self translateSingleQuote:zipcode];
  5577. NSString *fistName = [params objectForKey:@"firstname"];
  5578. if (!fistName) {
  5579. fistName = @"";
  5580. }
  5581. NSString *lastName = [params objectForKey:@"lastname"];
  5582. if (!lastName) {
  5583. lastName = @"";
  5584. }
  5585. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5586. DebugLog(@"contact_name");
  5587. contact_name = [self translateSingleQuote:contact_name];
  5588. fistName = [self translateSingleQuote:fistName];
  5589. lastName = [self translateSingleQuote:lastName];
  5590. NSString *phone = [params objectForKey:@"phone"];
  5591. if (phone) {
  5592. phone = [AESCrypt fastencrypt:phone];
  5593. } else {
  5594. phone = @"";
  5595. }
  5596. DebugLog(@"PHONE");
  5597. phone = [self translateSingleQuote:phone];
  5598. NSString *fax = [params objectForKey:@"fax"];
  5599. if (!fax) {
  5600. fax = @"";
  5601. }
  5602. DebugLog(@"FAX");
  5603. fax = [self translateSingleQuote:fax];
  5604. NSString *email = [params objectForKey:@"email"];
  5605. if (!email) {
  5606. email = @"";
  5607. }
  5608. DebugLog(@"EMAIL:%@",email);
  5609. email = [self translateSingleQuote:email];
  5610. NSString *notes = [params objectForKey:@"contact_notes"];
  5611. if (!notes) {
  5612. notes = @"";
  5613. }
  5614. DebugLog(@"NOTE:%@",notes);
  5615. notes = [self translateSingleQuote:notes];
  5616. NSString *price = [params objectForKey:@"price_name"];
  5617. if (price) {
  5618. price = [self priceNameByPriceId:price];
  5619. } else {
  5620. price = @"";
  5621. }
  5622. DebugLog(@"PRICE");
  5623. price = [self translateSingleQuote:price];
  5624. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5625. if (salesRep) {
  5626. salesRep = [self salesRepCodeById:salesRep];
  5627. } else {
  5628. salesRep = @"";
  5629. }
  5630. salesRep = [self translateSingleQuote:salesRep];
  5631. NSString *img = [params objectForKey:@"business_card"];
  5632. NSArray *array = [img componentsSeparatedByString:@","];
  5633. NSString *img_0 = array[0];
  5634. if (!img_0) {
  5635. img_0 = @"";
  5636. }
  5637. img_0 = [self translateSingleQuote:img_0];
  5638. NSString *img_1 = array[1];
  5639. if (!img_1) {
  5640. img_1 = @"";
  5641. }
  5642. img_1 = [self translateSingleQuote:img_1];
  5643. NSString *img_2 = array[2];
  5644. if (!img_2) {
  5645. img_2 = @"";
  5646. }
  5647. img_2 = [self translateSingleQuote:img_2];
  5648. NSString *contact_id = [NSUUID UUID].UUIDString;
  5649. NSString *contact_type = [params objectForKey:@"type_name"];
  5650. if (!contact_type) {
  5651. contact_type = @"";
  5652. }
  5653. contact_type = [self translateSingleQuote:contact_type];
  5654. // 判断更新时是否为customer
  5655. if (update) {
  5656. contact_id = [params objectForKey:@"contact_id"];
  5657. if (!contact_id) {
  5658. contact_id = @"";
  5659. }
  5660. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5661. __block int customer = 0;
  5662. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5663. customer = sqlite3_column_int(stmt, 0);
  5664. }];
  5665. isCustomer = customer ? YES : NO;
  5666. }
  5667. NSMutableDictionary *sync_dic = [params mutableCopy];
  5668. if (isCustomer) {
  5669. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5670. } else {
  5671. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5672. }
  5673. NSString *sync_data = nil;
  5674. NSString *sql = nil;
  5675. if (update){
  5676. contact_id = [params objectForKey:@"contact_id"];
  5677. if (!contact_id) {
  5678. contact_id = @"";
  5679. }
  5680. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5681. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5682. sync_data = [RAConvertor dict2string:sync_dic];
  5683. sync_data = [self translateSingleQuote:sync_data];
  5684. 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];
  5685. } else {
  5686. contact_id = [self translateSingleQuote:contact_id];
  5687. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5688. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5689. sync_data = [RAConvertor dict2string:sync_dic];
  5690. sync_data = [self translateSingleQuote:sync_data];
  5691. 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];
  5692. }
  5693. int result = [iSalesDB execSql:sql];
  5694. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5695. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5696. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5697. }
  5698. #pragma mark save new contact
  5699. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5700. {
  5701. return [self offline_saveContact:params update:NO isCustomer:YES];
  5702. }
  5703. #pragma mark edit contact
  5704. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5705. {
  5706. assert(params[@"user"]!=nil);
  5707. // {
  5708. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5709. // password = 123456;
  5710. // user = EvanK;
  5711. // }
  5712. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5713. NSData *data = [NSData dataWithContentsOfFile:path];
  5714. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5715. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5716. NSString *countryCode = nil;
  5717. NSString *countryCode_id = nil;
  5718. NSString *stateCode = nil;
  5719. /*------contact infor------*/
  5720. __block NSString *country = nil;
  5721. __block NSString *company_name = nil;
  5722. __block NSString *contact_id = params[@"contact_id"];
  5723. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5724. __block NSString *zipcode = nil;
  5725. __block NSString *state = nil; // state_code
  5726. __block NSString *city = nil; //
  5727. __block NSString *firt_name,*last_name;
  5728. __block NSString *phone,*fax,*email;
  5729. __block NSString *notes,*price_type,*sales_rep;
  5730. __block NSString *img_0,*img_1,*img_2;
  5731. __block NSString *contact_type;
  5732. contact_id = [self translateSingleQuote:contact_id];
  5733. 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];
  5734. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5735. country = [self textAtColumn:0 statement:stmt]; // country name
  5736. company_name = [self textAtColumn:1 statement:stmt];
  5737. addr_1 = [self textAtColumn:2 statement:stmt];
  5738. addr_2 = [self textAtColumn:3 statement:stmt];
  5739. addr_3 = [self textAtColumn:4 statement:stmt];
  5740. addr_4 = [self textAtColumn:5 statement:stmt];
  5741. zipcode = [self textAtColumn:6 statement:stmt];
  5742. state = [self textAtColumn:7 statement:stmt]; // state code
  5743. city = [self textAtColumn:8 statement:stmt];
  5744. firt_name = [self textAtColumn:9 statement:stmt];
  5745. last_name = [self textAtColumn:10 statement:stmt];
  5746. phone = [self textAtColumn:11 statement:stmt];
  5747. fax = [self textAtColumn:12 statement:stmt];
  5748. email = [self textAtColumn:13 statement:stmt];
  5749. notes = [self textAtColumn:14 statement:stmt];
  5750. price_type = [self textAtColumn:15 statement:stmt]; // name
  5751. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5752. img_0 = [self textAtColumn:17 statement:stmt];
  5753. img_1 = [self textAtColumn:18 statement:stmt];
  5754. img_2 = [self textAtColumn:19 statement:stmt];
  5755. contact_type = [self textAtColumn:20 statement:stmt];
  5756. }];
  5757. // decrypt
  5758. if (company_name) {
  5759. company_name = [AESCrypt fastdecrypt:company_name];
  5760. }
  5761. if (addr_1) {
  5762. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5763. }
  5764. if (phone) {
  5765. phone = [AESCrypt fastdecrypt:phone];
  5766. }
  5767. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5768. countryCode = [iSalesDB jk_queryText:countrySql];
  5769. stateCode = state;
  5770. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5771. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5772. NSString *code_id = params[@"country"];
  5773. countryCode_id = code_id;
  5774. countryCode = [self countryCodeByid:code_id];
  5775. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5776. NSString *zip_code = params[@"zipcode"];
  5777. // 剔除全部为空格
  5778. int spaceCount = 0;
  5779. for (int i = 0; i < zip_code.length; i++) {
  5780. if ([zip_code characterAtIndex:i] == ' ') {
  5781. spaceCount++;
  5782. }
  5783. }
  5784. if (spaceCount == zip_code.length) {
  5785. zip_code = @"";
  5786. }
  5787. if (zipcode.length > 0) {
  5788. zipcode = zip_code;
  5789. countryCode_id = params[@"country"];
  5790. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5791. countryCode = [dic valueForKey:@"country_code"];
  5792. if (!countryCode) {
  5793. NSString *code_id = params[@"country"];
  5794. countryCode = [self countryCodeByid:code_id];
  5795. }
  5796. stateCode = [dic valueForKey:@"state_code"];
  5797. if (!stateCode.length) {
  5798. stateCode = params[@"state"];
  5799. }
  5800. city = [dic valueForKey:@"city"];
  5801. if (!city.length) {
  5802. city = params[@"city"];
  5803. }
  5804. // zip code
  5805. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5806. [zipDic setValue:zipcode forKey:@"value"];
  5807. [section_0 setValue:zipDic forKey:@"item_8"];
  5808. } else {
  5809. NSString *code_id = params[@"country"];
  5810. countryCode = [self countryCodeByid:code_id];
  5811. stateCode = params[@"state"];
  5812. city = params[@"city"];
  5813. }
  5814. }
  5815. }
  5816. // 0 Country
  5817. // 1 Company Name
  5818. // 2 Contact ID
  5819. // 3 Picture
  5820. // 4 Address 1
  5821. // 5 Address 2
  5822. // 6 Address 3
  5823. // 7 Address 4
  5824. // 8 Zip Code
  5825. // 9 State/Province
  5826. // 10 City
  5827. // 11 Contact First Name
  5828. // 12 Contact Last Name
  5829. // 13 Phone
  5830. // 14 Fax
  5831. // 15 Email
  5832. // 16 Contact Notes
  5833. // 17 Price Type
  5834. // 18 Contact Type
  5835. // 19 Sales Rep
  5836. // country
  5837. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5838. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5839. // company
  5840. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5841. // contact_id
  5842. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5843. // picture
  5844. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5845. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5846. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5847. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5848. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5849. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5850. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5851. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5852. // addr 1 2 3 4
  5853. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5854. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5855. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5856. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5857. // zip code
  5858. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5859. // state
  5860. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5861. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5862. // city
  5863. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5864. // first last
  5865. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5866. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5867. // phone fax email
  5868. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5869. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5870. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5871. // notes
  5872. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5873. // price
  5874. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5875. for (NSString *key in priceDic.allKeys) {
  5876. if ([key containsString:@"val_"]) {
  5877. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5878. if ([dic[@"value"] isEqualToString:price_type]) {
  5879. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5880. [priceDic setValue:dic forKey:key];
  5881. }
  5882. }
  5883. }
  5884. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5885. // Contact Rep
  5886. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5887. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5888. // Sales Rep
  5889. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5890. for (NSString *key in repDic.allKeys) {
  5891. if ([key containsString:@"val_"]) {
  5892. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5893. NSString *value = dic[@"value"];
  5894. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5895. if (code && [code isEqualToString:sales_rep]) {
  5896. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5897. [repDic setValue:dic forKey:key];
  5898. }
  5899. }
  5900. }
  5901. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5902. [ret setValue:section_0 forKey:@"section_0"];
  5903. return [RAConvertor dict2data:ret];
  5904. }
  5905. #pragma mark save contact
  5906. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5907. {
  5908. return [self offline_saveContact:params update:YES isCustomer:YES];
  5909. }
  5910. #pragma mark category
  5911. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5912. if (ck) {
  5913. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5914. for (NSString *key in res.allKeys) {
  5915. if (![key isEqualToString:@"count"]) {
  5916. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5917. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5918. if ([val[@"value"] isEqualToString:ck]) {
  5919. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5920. }
  5921. [res setValue:val forKey:key];
  5922. }
  5923. }
  5924. [dic setValue:res forKey:valueKey];
  5925. }
  5926. }
  5927. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5928. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5929. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5930. NSString* category = [params valueForKey:@"category"];
  5931. if (!category || [category isEqualToString:@""]) {
  5932. category = @"%";
  5933. }
  5934. category = [self translateSingleQuote:category];
  5935. int limit = [[params valueForKey:@"limit"] intValue];
  5936. int offset = [[params valueForKey:@"offset"] intValue];
  5937. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5938. NSString *limit_str = @"";
  5939. if (limited) {
  5940. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5941. }
  5942. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5943. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5944. sqlite3 *db = [iSalesDB get_db];
  5945. // [iSalesDB AddExFunction:db];
  5946. int count;
  5947. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5948. 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];
  5949. double price_min = 0;
  5950. double price_max = 0;
  5951. if ([params.allKeys containsObject:@"alert"]) {
  5952. // alert
  5953. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5954. NSString *alert = params[@"alert"];
  5955. if ([alert isEqualToString:@"Display All"]) {
  5956. alert = [NSString stringWithFormat:@""];
  5957. } else {
  5958. alert = [self translateSingleQuote:alert];
  5959. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5960. }
  5961. // available
  5962. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5963. NSString *available = params[@"available"];
  5964. NSString *available_condition;
  5965. if ([available isEqualToString:@"Display All"]) {
  5966. available_condition = @"";
  5967. } else if ([available isEqualToString:@"Available Now"]) {
  5968. available_condition = @"and availability > 0";
  5969. } else {
  5970. available_condition = @"and availability == 0";
  5971. }
  5972. // best seller
  5973. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5974. NSString *best_seller = @"";
  5975. NSString *order_best_seller = @"m.name asc";
  5976. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5977. best_seller = @"and best_seller > 0";
  5978. order_best_seller = @"m.best_seller desc,m.name asc";
  5979. }
  5980. // price
  5981. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5982. NSString *price = params[@"price"];
  5983. price_min = 0;
  5984. price_max = MAXFLOAT;
  5985. if (params[@"user"] && price != nil) {
  5986. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5987. NSMutableString *priceName = [NSMutableString string];
  5988. for (int i = 0; i < priceTypeArray.count; i++) {
  5989. NSString *pricetype = priceTypeArray[i];
  5990. pricetype = [self translateSingleQuote:pricetype];
  5991. if (i == 0) {
  5992. [priceName appendFormat:@"'%@'",pricetype];
  5993. } else {
  5994. [priceName appendFormat:@",'%@'",pricetype];
  5995. }
  5996. }
  5997. if ([price isEqualToString:@"Display All"]) {
  5998. price = [NSString stringWithFormat:@""];
  5999. } else if([price containsString:@"+"]){
  6000. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6001. price_min = [price doubleValue];
  6002. 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];
  6003. } else {
  6004. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6005. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6006. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6007. 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];
  6008. }
  6009. } else {
  6010. price = @"";
  6011. }
  6012. // sold_by_qty : Sold in quantities of %@
  6013. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6014. NSString *qty = params[@"sold_by_qty"];
  6015. if ([qty isEqualToString:@"Display All"]) {
  6016. qty = @"";
  6017. } else {
  6018. qty = [self translateSingleQuote:qty];
  6019. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6020. }
  6021. // model name;
  6022. NSString* modelname =params[@"modelName"];
  6023. if(modelname.length==0)
  6024. {
  6025. modelname=@"";
  6026. }
  6027. else
  6028. {
  6029. modelname = modelname.lowercaseString;
  6030. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6031. }
  6032. //modelDescription
  6033. NSString* modelDescription =params[@"modelDescription"];
  6034. if(modelDescription.length==0)
  6035. {
  6036. modelDescription=@"";
  6037. }
  6038. else
  6039. {
  6040. modelDescription = modelDescription.lowercaseString;
  6041. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6042. }
  6043. // cate
  6044. // category = [self translateSingleQuote:category];
  6045. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6046. // cate mutiple selection
  6047. NSString *category_id = params[@"category"];
  6048. NSMutableArray *cate_id_array = nil;
  6049. NSMutableString *cateWhere = [NSMutableString string];
  6050. if ([category_id isEqualToString:@""] || !category_id) {
  6051. [cateWhere appendString:@"1 = 1"];
  6052. } else {
  6053. if ([category_id containsString:@","]) {
  6054. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6055. } else {
  6056. cate_id_array = [@[category_id] mutableCopy];
  6057. }
  6058. [cateWhere appendString:@"("];
  6059. for (int i = 0; i < cate_id_array.count; i++) {
  6060. if (i == 0) {
  6061. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6062. } else {
  6063. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6064. }
  6065. }
  6066. [cateWhere appendString:@")"];
  6067. }
  6068. // where bestseller > 0 order by bestseller desc
  6069. // sql query: alert availability(int) best_seller(int) price qty
  6070. 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];
  6071. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6072. }
  6073. DebugLog(@"offline category where: %@",where);
  6074. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6075. if (!params[@"user"]) {
  6076. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6077. }
  6078. [ret setValue:filter forKey:@"filter"];
  6079. DebugLog(@"offline_category sql:%@",sqlQuery);
  6080. sqlite3_stmt * statement;
  6081. [ret setValue:@"2" forKey:@"result"];
  6082. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6083. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6084. // int count=0;
  6085. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6086. {
  6087. int i=0;
  6088. while (sqlite3_step(statement) == SQLITE_ROW)
  6089. {
  6090. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6091. char *name = (char*)sqlite3_column_text(statement, 0);
  6092. if(name==nil)
  6093. name="";
  6094. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6095. char *description = (char*)sqlite3_column_text(statement, 1);
  6096. if(description==nil)
  6097. description="";
  6098. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6099. int product_id = sqlite3_column_int(statement, 2);
  6100. int wid = sqlite3_column_int(statement, 3);
  6101. int closeout = sqlite3_column_int(statement, 4);
  6102. int cid = sqlite3_column_int(statement, 5);
  6103. int wisdelete = sqlite3_column_int(statement, 6);
  6104. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6105. int more_color = sqlite3_column_int(statement, 8);
  6106. // Defaul Category ID
  6107. __block NSString *categoryID = nil;
  6108. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6109. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6110. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6111. if(default_category==nil)
  6112. default_category="";
  6113. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6114. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6115. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6116. categoryID = nsdefault_category;
  6117. }];
  6118. if (!categoryID.length) {
  6119. NSString *cateIDs = params[@"category"];
  6120. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6121. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6122. for (NSString *cateID in requestCategoryArr) {
  6123. BOOL needBreak = NO;
  6124. for (NSString *itemCateIDBox in itemCategoryArr) {
  6125. if (itemCateIDBox.length > 4) {
  6126. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6127. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6128. if ([itemCategoryID isEqualToString:cateID]) {
  6129. needBreak = YES;
  6130. categoryID = itemCategoryID;
  6131. break;
  6132. }
  6133. }
  6134. }
  6135. if (needBreak) {
  6136. break;
  6137. }
  6138. }
  6139. }
  6140. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6141. if(wid !=0 && wisdelete != 1)
  6142. [item setValue:@"true" forKey:@"wish_exists"];
  6143. else
  6144. [item setValue:@"false" forKey:@"wish_exists"];
  6145. if(closeout==0)
  6146. [item setValue:@"false" forKey:@"is_closeout"];
  6147. else
  6148. [item setValue:@"true" forKey:@"is_closeout"];
  6149. if(cid==0)
  6150. [item setValue:@"false" forKey:@"cart_exists"];
  6151. else
  6152. [item setValue:@"true" forKey:@"cart_exists"];
  6153. if (more_color == 0) {
  6154. [item setObject:@(false) forKey:@"more_color"];
  6155. } else if (more_color == 1) {
  6156. [item setObject:@(true) forKey:@"more_color"];
  6157. }
  6158. [item addEntriesFromDictionary:imgjson];
  6159. // [item setValue:nsurl forKey:@"img"];
  6160. [item setValue:nsname forKey:@"name"];
  6161. [item setValue:nsdescription forKey:@"description"];
  6162. if (categoryID) {
  6163. [item setValue:categoryID forKey:@"item_category_id"];
  6164. }
  6165. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6166. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6167. i++;
  6168. }
  6169. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6170. [ret setObject:items forKey:@"items"];
  6171. sqlite3_finalize(statement);
  6172. } else {
  6173. DebugLog(@"nothing...");
  6174. }
  6175. DebugLog(@"count:%d",count);
  6176. [iSalesDB close_db:db];
  6177. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6178. return ret;
  6179. }
  6180. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6181. {
  6182. return [self categoryList:params limited:YES];
  6183. }
  6184. # pragma mark item search
  6185. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6186. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6187. // assert(params[@"order_code"]);
  6188. // params[@"user"]
  6189. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6190. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6191. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6192. // category
  6193. NSDictionary *category_menu = [self offline_category_menu];
  6194. [filter setValue:category_menu forKey:@"category"];
  6195. NSString* where= nil;
  6196. NSString* orderby= @"m.name";
  6197. if (!filterSearch) {
  6198. int covertype = [[params valueForKey:@"covertype"] intValue];
  6199. switch (covertype) {
  6200. case 0:
  6201. {
  6202. where=@"m.category like'%%#005#%%'";
  6203. break;
  6204. }
  6205. case 1:
  6206. {
  6207. where=@"m.alert like '%QS%'";
  6208. break;
  6209. }
  6210. case 2:
  6211. {
  6212. where=@"m.availability>0";
  6213. break;
  6214. }
  6215. case 3:
  6216. {
  6217. where=@"m.best_seller>0";
  6218. orderby=@"m.best_seller desc,m.name asc";
  6219. break;
  6220. }
  6221. default:
  6222. where=@"1=1";
  6223. break;
  6224. }
  6225. }
  6226. int limit = [[params valueForKey:@"limit"] intValue];
  6227. int offset = [[params valueForKey:@"offset"] intValue];
  6228. NSString *limit_str = @"";
  6229. if (limited) {
  6230. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6231. }
  6232. sqlite3 *db = [iSalesDB get_db];
  6233. // [iSalesDB AddExFunction:db];
  6234. int count;
  6235. NSString *sqlQuery = nil;
  6236. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6237. 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];
  6238. double price_min = 0;
  6239. double price_max = 0;
  6240. if (filterSearch) {
  6241. // alert
  6242. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6243. NSString *alert = params[@"alert"];
  6244. if ([alert isEqualToString:@"Display All"]) {
  6245. alert = [NSString stringWithFormat:@""];
  6246. } else {
  6247. alert = [self translateSingleQuote:alert];
  6248. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6249. }
  6250. // available
  6251. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6252. NSString *available = params[@"available"];
  6253. NSString *available_condition;
  6254. if ([available isEqualToString:@"Display All"]) {
  6255. available_condition = @"";
  6256. } else if ([available isEqualToString:@"Available Now"]) {
  6257. available_condition = @"and availability > 0";
  6258. } else {
  6259. available_condition = @"and availability == 0";
  6260. }
  6261. // best seller
  6262. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6263. NSString *best_seller = @"";
  6264. NSString *order_best_seller = @"m.name asc";
  6265. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6266. best_seller = @"and best_seller > 0";
  6267. order_best_seller = @"m.best_seller desc,m.name asc";
  6268. }
  6269. // price
  6270. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6271. NSString *price = params[@"price"];
  6272. price_min = 0;
  6273. price_max = MAXFLOAT;
  6274. if (params[@"user"]) {
  6275. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6276. NSMutableString *priceName = [NSMutableString string];
  6277. for (int i = 0; i < priceTypeArray.count; i++) {
  6278. NSString *pricetype = priceTypeArray[i];
  6279. pricetype = [self translateSingleQuote:pricetype];
  6280. if (i == 0) {
  6281. [priceName appendFormat:@"'%@'",pricetype];
  6282. } else {
  6283. [priceName appendFormat:@",'%@'",pricetype];
  6284. }
  6285. }
  6286. if ([price isEqualToString:@"Display All"]) {
  6287. price = [NSString stringWithFormat:@""];
  6288. } else if([price containsString:@"+"]){
  6289. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6290. price_min = [price doubleValue];
  6291. 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];
  6292. } else {
  6293. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6294. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6295. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6296. 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];
  6297. }
  6298. } else {
  6299. price = @"";
  6300. }
  6301. // sold_by_qty : Sold in quantities of %@
  6302. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6303. NSString *qty = params[@"sold_by_qty"];
  6304. if ([qty isEqualToString:@"Display All"]) {
  6305. qty = @"";
  6306. } else {
  6307. qty = [self translateSingleQuote:qty];
  6308. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6309. }
  6310. // model name;
  6311. NSString* modelname =params[@"modelName"];
  6312. if(modelname.length==0)
  6313. {
  6314. modelname=@"";
  6315. }
  6316. else
  6317. {
  6318. modelname = modelname.lowercaseString;
  6319. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6320. }
  6321. //modelDescription
  6322. NSString* modelDescription =params[@"modelDescription"];
  6323. if(modelDescription.length==0)
  6324. {
  6325. modelDescription=@"";
  6326. }
  6327. else
  6328. {
  6329. modelDescription = modelDescription.lowercaseString;
  6330. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6331. }
  6332. //lower(description) like'%%%@%%'
  6333. // category
  6334. NSString *category_id = params[@"ctgId"];
  6335. NSMutableArray *cate_id_array = nil;
  6336. NSMutableString *cateWhere = [NSMutableString string];
  6337. if ([category_id isEqualToString:@""] || !category_id) {
  6338. [cateWhere appendString:@"1 = 1"];
  6339. } else {
  6340. if ([category_id containsString:@","]) {
  6341. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6342. } else {
  6343. cate_id_array = [@[category_id] mutableCopy];
  6344. }
  6345. /*-----------*/
  6346. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6347. [cateWhere appendString:@"("];
  6348. for (int i = 0; i < cate_id_array.count; i++) {
  6349. for (NSString *key0 in cateDic.allKeys) {
  6350. if ([key0 containsString:@"category_"]) {
  6351. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6352. for (NSString *key1 in category0.allKeys) {
  6353. if ([key1 containsString:@"category_"]) {
  6354. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6355. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6356. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6357. cate_id_array[i] = [category1 objectForKey:@"id"];
  6358. if (i == 0) {
  6359. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6360. } else {
  6361. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6362. }
  6363. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6364. [category0 setValue:category1 forKey:key1];
  6365. [cateDic setValue:category0 forKey:key0];
  6366. }
  6367. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6368. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6369. cate_id_array[i] = [category0 objectForKey:@"id"];
  6370. if (i == 0) {
  6371. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6372. } else {
  6373. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6374. }
  6375. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6376. [cateDic setValue:category0 forKey:key0];
  6377. }
  6378. }
  6379. }
  6380. }
  6381. }
  6382. }
  6383. [cateWhere appendString:@")"];
  6384. [filter setValue:cateDic forKey:@"category"];
  6385. }
  6386. // where bestseller > 0 order by bestseller desc
  6387. // sql query: alert availability(int) best_seller(int) price qty
  6388. 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];
  6389. // count
  6390. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6391. }
  6392. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6393. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6394. if (!params[@"user"]) {
  6395. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6396. }
  6397. [ret setValue:filter forKey:@"filter"];
  6398. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6399. sqlite3_stmt * statement;
  6400. [ret setValue:@"2" forKey:@"result"];
  6401. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6402. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6403. // int count=0;
  6404. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6405. {
  6406. int i=0;
  6407. while (sqlite3_step(statement) == SQLITE_ROW)
  6408. {
  6409. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6410. char *name = (char*)sqlite3_column_text(statement, 0);
  6411. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6412. char *description = (char*)sqlite3_column_text(statement, 1);
  6413. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6414. int product_id = sqlite3_column_int(statement, 2);
  6415. int wid = sqlite3_column_int(statement, 3);
  6416. int closeout = sqlite3_column_int(statement, 4);
  6417. int cid = sqlite3_column_int(statement, 5);
  6418. int wisdelete = sqlite3_column_int(statement, 6);
  6419. int more_color = sqlite3_column_int(statement, 7);
  6420. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6421. if(wid !=0 && wisdelete != 1)
  6422. [item setValue:@"true" forKey:@"wish_exists"];
  6423. else
  6424. [item setValue:@"false" forKey:@"wish_exists"];
  6425. if(closeout==0)
  6426. [item setValue:@"false" forKey:@"is_closeout"];
  6427. else
  6428. [item setValue:@"true" forKey:@"is_closeout"];
  6429. if(cid==0)
  6430. [item setValue:@"false" forKey:@"cart_exists"];
  6431. else
  6432. [item setValue:@"true" forKey:@"cart_exists"];
  6433. if (more_color == 0) {
  6434. [item setObject:@(false) forKey:@"more_color"];
  6435. } else if (more_color == 1) {
  6436. [item setObject:@(true) forKey:@"more_color"];
  6437. }
  6438. [item addEntriesFromDictionary:imgjson];
  6439. // [item setValue:nsurl forKey:@"img"];
  6440. [item setValue:nsname forKey:@"fash_name"];
  6441. [item setValue:nsdescription forKey:@"description"];
  6442. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6443. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6444. i++;
  6445. }
  6446. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6447. [ret setObject:items forKey:@"items"];
  6448. sqlite3_finalize(statement);
  6449. }
  6450. [iSalesDB close_db:db];
  6451. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6452. return ret;
  6453. }
  6454. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6455. {
  6456. return [self itemsearch:params limited:YES];
  6457. }
  6458. #pragma mark order detail
  6459. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6460. if (str1.length == 0) {
  6461. str1 = @"&nbsp";
  6462. }
  6463. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6464. return str;
  6465. }
  6466. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6467. {
  6468. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6469. [fromformatter setDateFormat:from];
  6470. NSDate *date = [fromformatter dateFromString:datetime];
  6471. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6472. [toformatter setDateFormat:to];
  6473. NSString * ret = [toformatter stringFromDate:date];
  6474. return ret;
  6475. }
  6476. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6477. // 把毫秒去掉
  6478. if ([dateTime containsString:@"."]) {
  6479. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6480. }
  6481. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6482. formatter.dateFormat = formate;
  6483. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6484. NSDate *date = [formatter dateFromString:dateTime];
  6485. formatter.dateFormat = newFormate;
  6486. NSString *result = [formatter stringFromDate:date];
  6487. return result ? result : @"";
  6488. }
  6489. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6490. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6491. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6492. }
  6493. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6494. {
  6495. assert(params[@"mode"]!=nil);
  6496. assert(params[@"user"]!=nil);
  6497. DebugLog(@"offline oderdetail params: %@",params);
  6498. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6499. int orderId = [params[@"orderId"] intValue];
  6500. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6501. // decrypt card number and card security code
  6502. // 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 ];
  6503. 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];
  6504. sqlite3 *db = [iSalesDB get_db];
  6505. sqlite3_stmt * statement;
  6506. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6507. NSString *nssoid = @"";
  6508. NSString* orderinfo = @"";
  6509. NSString *moreInfo = @"";
  6510. double handlingFee = 0;
  6511. double payments_and_credist = 0;
  6512. double totalPrice = 0;
  6513. double shippingFee = 0;
  6514. double lift_gate = 0;
  6515. NSString *customer_contact = @"";
  6516. NSString *customer_email = @"";
  6517. NSString *customer_fax = @"";
  6518. NSString *customer_phone = @"";
  6519. NSString *customer_city = @"";
  6520. NSString *customer_state = @"";
  6521. NSString *customer_zipcode = @"";
  6522. NSString *customer_country = @"";
  6523. BOOL must_call = NO;
  6524. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6525. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6526. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6527. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6528. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6529. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6530. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6531. {
  6532. if (sqlite3_step(statement) == SQLITE_ROW)
  6533. {
  6534. // int order_id = sqlite3_column_int(statement, 0);
  6535. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6536. // ret[@"sign_url"] = sign_url;
  6537. section_1 = @{
  6538. @"data":sign_url,
  6539. @"title":@"Signature",
  6540. @"type":@"sign_url"
  6541. }.mutableCopy;
  6542. [ret setObject:section_1 forKey:@"section_1"];
  6543. customer_contact = [self textAtColumn:52 statement:statement];
  6544. customer_email = [self textAtColumn:53 statement:statement];
  6545. customer_phone = [self textAtColumn:54 statement:statement];
  6546. customer_fax = [self textAtColumn:55 statement:statement];
  6547. customer_city = [self textAtColumn:60 statement:statement];
  6548. customer_state = [self textAtColumn:61 statement:statement];
  6549. customer_zipcode = [self textAtColumn:62 statement:statement];
  6550. customer_country = [self textAtColumn:63 statement:statement];
  6551. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6552. int offline_edit=sqlite3_column_int(statement, 56);
  6553. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6554. char *soid = (char*)sqlite3_column_text(statement, 1);
  6555. if(soid==nil)
  6556. soid= "";
  6557. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6558. // so#
  6559. ret[@"so#"] = nssoid;
  6560. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6561. if(poNumber==nil)
  6562. poNumber= "";
  6563. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6564. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6565. if(create_time==nil)
  6566. create_time= "";
  6567. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6568. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6569. int status = sqlite3_column_int(statement, 4);
  6570. int erpStatus = sqlite3_column_int(statement, 49);
  6571. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6572. // status
  6573. if (status > 1 && status != 3) {
  6574. status = erpStatus;
  6575. } else if (status == 3) {
  6576. status = 15;
  6577. }
  6578. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6579. ret[@"order_status"] = nsstatus;
  6580. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6581. if(company_name==nil)
  6582. company_name= "";
  6583. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6584. // company name
  6585. ret[@"company_name"] = nscompany_name;
  6586. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6587. if(customer_contact==nil)
  6588. customer_contact= "";
  6589. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6590. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6591. if(addr_1==nil)
  6592. addr_1="";
  6593. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6594. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6595. if(addr_2==nil)
  6596. addr_2="";
  6597. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6598. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6599. if(addr_3==nil)
  6600. addr_3="";
  6601. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6602. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6603. if(addr_4==nil)
  6604. addr_4="";
  6605. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6606. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6607. [arr_addr addObject:nsaddr_1];
  6608. [arr_addr addObject:nsaddr_2];
  6609. [arr_addr addObject:nsaddr_3];
  6610. [arr_addr addObject:nsaddr_4];
  6611. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6612. [arr_addr addObject:customer_state];
  6613. [arr_addr addObject:customer_zipcode];
  6614. [arr_addr addObject:customer_country];
  6615. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6616. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6617. char *logist = (char*)sqlite3_column_text(statement, 11);
  6618. if(logist==nil)
  6619. logist= "";
  6620. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6621. if (/*status == -11 || */status == 10 || status == 11) {
  6622. nslogist = [self textAtColumn:59 statement:statement];
  6623. };
  6624. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6625. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6626. shipping = @"Shipping To Be Quoted";
  6627. } else {
  6628. shippingFee = sqlite3_column_double(statement, 12);
  6629. }
  6630. // Shipping
  6631. // ret[@"Shipping"] = shipping;
  6632. NSDictionary *shipping_item = @{
  6633. @"title":@"Shipping",
  6634. @"value":shipping
  6635. };
  6636. [price_data setObject:shipping_item forKey:@"item_1"];
  6637. int have_lift_gate = sqlite3_column_int(statement, 17);
  6638. lift_gate = sqlite3_column_double(statement, 13);
  6639. // Liftgate Fee(No loading dock)
  6640. if (!have_lift_gate) {
  6641. lift_gate = 0;
  6642. }
  6643. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6644. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6645. if (sqlite3_column_int(statement, 57)) {
  6646. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6647. liftgate_value = @"Shipping To Be Quoted";
  6648. }
  6649. NSDictionary *liftgate_item = @{
  6650. @"title":@"Liftgate Fee(No loading dock)",
  6651. @"value":liftgate_value
  6652. };
  6653. [price_data setObject:liftgate_item forKey:@"item_2"];
  6654. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6655. // [ret removeObjectForKey:@"Shipping"];
  6656. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6657. // }
  6658. //
  6659. // if (have_lift_gate) {
  6660. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6661. // }
  6662. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6663. if(general_notes==nil)
  6664. general_notes= "";
  6665. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6666. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6667. if(internal_notes==nil)
  6668. internal_notes= "";
  6669. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6670. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6671. if(payment_type==nil)
  6672. payment_type= "";
  6673. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6674. // order info
  6675. orderinfo = [self textFileName:@"order_info.html"];
  6676. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6677. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6678. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6679. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6680. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6681. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6682. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6683. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6684. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6685. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6686. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6687. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6688. NSString *payment = nil;
  6689. // id -> show
  6690. __block NSString *show_pay_type = nspayment_type;
  6691. [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) {
  6692. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6693. if (show_typ_ch != NULL) {
  6694. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6695. }
  6696. }];
  6697. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6698. {
  6699. payment = [self textFileName:@"creditcardpayment.html"];
  6700. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6701. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6702. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6703. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6704. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6705. NSString *card_type = [self textAtColumn:42 statement:statement];
  6706. if (card_type.length > 0) { // 显示星号
  6707. card_type = @"****";
  6708. }
  6709. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6710. if (card_number.length > 0 && card_number.length > 4) {
  6711. for (int i = 0; i < card_number.length - 4; i++) {
  6712. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6713. }
  6714. } else {
  6715. card_number = @"";
  6716. }
  6717. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6718. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6719. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6720. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6721. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6722. card_expiration = @"****";
  6723. }
  6724. NSString *card_city = [self textAtColumn:46 statement:statement];
  6725. NSString *card_state = [self textAtColumn:47 statement:statement];
  6726. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6727. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6728. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6729. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6730. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6731. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6732. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6733. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6734. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6735. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6736. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6737. }
  6738. else
  6739. {
  6740. payment=[self textFileName:@"normalpayment.html"];
  6741. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6742. }
  6743. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6744. ret[@"result"]= [NSNumber numberWithInt:2];
  6745. // more info
  6746. moreInfo = [self textFileName:@"more_info.html"];
  6747. /*****ship to******/
  6748. // ShipToCompany_or_&nbsp
  6749. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6750. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6751. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6752. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6753. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6754. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6755. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6756. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6757. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6758. /*****ship from******/
  6759. // ShipFromCompany_or_&nbsp
  6760. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6761. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6762. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6763. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6764. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6765. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6766. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6767. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6768. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6769. /*****freight to******/
  6770. // FreightBillToCompany_or_&nbsp
  6771. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6772. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6773. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6774. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6775. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6776. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6777. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6778. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6779. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6780. /*****merchandise to******/
  6781. // MerchandiseBillToCompany_or_&nbsp
  6782. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6783. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6784. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6785. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6786. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6787. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6788. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6789. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6790. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6791. /*****return to******/
  6792. // ReturnToCompany_or_&nbsp
  6793. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6794. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6795. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6796. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6797. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6798. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6799. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6800. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6801. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6802. //
  6803. // DebugLog(@"more info : %@",moreInfo);
  6804. // handling fee
  6805. handlingFee = sqlite3_column_double(statement, 33);
  6806. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6807. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6808. if (sqlite3_column_int(statement, 58)) {
  6809. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6810. handling_fee_value = @"Shipping To Be Quoted";
  6811. }
  6812. NSDictionary *handling_fee_item = @{
  6813. @"title":@"Handling Fee",
  6814. @"value":handling_fee_value
  6815. };
  6816. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6817. //
  6818. // customer info
  6819. customerID = [self textAtColumn:36 statement:statement];
  6820. // mode
  6821. ret[@"mode"] = params[@"mode"];
  6822. // model_count
  6823. ret[@"model_count"] = @(0);
  6824. }
  6825. sqlite3_finalize(statement);
  6826. }
  6827. [iSalesDB close_db:db];
  6828. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6829. // "customer_email" = "Shui Hu";
  6830. // "customer_fax" = "";
  6831. // "customer_first_name" = F;
  6832. // "customer_last_name" = L;
  6833. // "customer_name" = ",da He Xiang Dong Liu A";
  6834. // "customer_phone" = "Hey Xuan Feng";
  6835. contactInfo[@"customer_phone"] = customer_phone;
  6836. contactInfo[@"customer_fax"] = customer_fax;
  6837. contactInfo[@"customer_email"] = customer_email;
  6838. NSString *first_name = @"";
  6839. NSString *last_name = @"";
  6840. if ([customer_contact isEqualToString:@""]) {
  6841. } else if ([customer_contact containsString:@" "]) {
  6842. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6843. first_name = [customer_contact substringToIndex:first_space_index];
  6844. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6845. }
  6846. contactInfo[@"customer_first_name"] = first_name;
  6847. contactInfo[@"customer_last_name"] = last_name;
  6848. ret[@"customerInfo"] = contactInfo;
  6849. // models
  6850. if (nssoid) {
  6851. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6852. __block double TotalCuft = 0;
  6853. __block double TotalWeight = 0;
  6854. __block int TotalCarton = 0;
  6855. __block double allItemPrice = 0;
  6856. 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];
  6857. sqlite3 *db1 = [iSalesDB get_db];
  6858. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6859. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6860. int product_id = sqlite3_column_int(stmt, 0);
  6861. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6862. int item_id = sqlite3_column_int(stmt, 7);
  6863. NSString* Price=nil;
  6864. if(str_price==nil)
  6865. {
  6866. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6867. if(price==nil)
  6868. Price=@"No Price.";
  6869. else
  6870. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6871. }
  6872. else
  6873. {
  6874. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6875. }
  6876. double discount = sqlite3_column_double(stmt, 2);
  6877. int item_count = sqlite3_column_int(stmt, 3);
  6878. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6879. NSString *nsline_note=nil;
  6880. if(line_note!=nil)
  6881. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6882. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6883. // NSString *nsname = nil;
  6884. // if(name!=nil)
  6885. // nsname= [[NSString alloc]initWithUTF8String:name];
  6886. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6887. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6888. // NSString *nsdescription=nil;
  6889. // if(description!=nil)
  6890. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6891. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6892. // int stockUom = sqlite3_column_int(stmt, 8);
  6893. // int _id = sqlite3_column_int(stmt, 9);
  6894. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6895. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6896. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6897. int carton=[bsubtotaljson[@"carton"] intValue];
  6898. TotalCuft += cuft;
  6899. TotalWeight += weight;
  6900. TotalCarton += carton;
  6901. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6902. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6903. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6904. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6905. itemjson[@"note"]=nsline_note;
  6906. itemjson[@"origin_price"] = Price;
  6907. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6908. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6909. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6910. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6911. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6912. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6913. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6914. if(itemjson[@"combine"] != nil)
  6915. {
  6916. // int citem=0;
  6917. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6918. for(int bc=0;bc<bcount;bc++)
  6919. {
  6920. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6921. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6922. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6923. subTotal += uprice * modulus * item_count;
  6924. }
  6925. }
  6926. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6927. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6928. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6929. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6930. itemjson[@"type"] = @"order_item";
  6931. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6932. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6933. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6934. allItemPrice += subTotal;
  6935. }];
  6936. section_3[@"data"] = model_data;
  6937. section_3[@"type"] = @"sub_order";
  6938. section_3[@"title"] = @"Models";
  6939. section_3[@"switch"] = @(false);
  6940. ret[@"section_3"] = section_3;
  6941. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6942. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6943. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6944. // payments/Credits
  6945. // payments_and_credist = sqlite3_column_double(statement, 34);
  6946. payments_and_credist = allItemPrice;
  6947. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6948. NSDictionary *sub_total_item = @{
  6949. @"title":@"Sub-Total",
  6950. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6951. };
  6952. [price_data setObject:sub_total_item forKey:@"item_0"];
  6953. // // total
  6954. // totalPrice = sqlite3_column_double(statement, 35);
  6955. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6956. } else {
  6957. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6958. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6959. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6960. // payments/Credits
  6961. payments_and_credist = 0;
  6962. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6963. NSDictionary *sub_total_item = @{
  6964. @"title":@"Sub-Total",
  6965. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6966. };
  6967. [price_data setObject:sub_total_item forKey:@"item_0"];
  6968. }
  6969. // total
  6970. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6971. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6972. NSDictionary *total_item = @{
  6973. @"title":@"Total",
  6974. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6975. };
  6976. [price_data setObject:total_item forKey:@"item_4"];
  6977. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6978. section_4 = @{
  6979. @"data":price_data,
  6980. @"title":@"Price Info",
  6981. @"type":@"price_info"
  6982. }.mutableCopy;
  6983. ret[@"section_4"] = section_4;
  6984. // ret[@"order_info"]= orderinfo;
  6985. section_0 = @{
  6986. @"data":orderinfo,
  6987. @"title":@"Order Info",
  6988. @"type":@"order_info"
  6989. }.mutableCopy;
  6990. ret[@"section_0"] = section_0;
  6991. // ret[@"more_order_info"] = moreInfo;
  6992. section_2 = @{
  6993. @"data":moreInfo,
  6994. @"title":@"More Info",
  6995. @"type":@"more_order_info"
  6996. }.mutableCopy;
  6997. ret[@"section_2"] = section_2;
  6998. ret[@"count"] = @(5);
  6999. return [RAConvertor dict2data:ret];
  7000. }
  7001. #pragma mark order list
  7002. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7003. NSString *nsstatus = @"";
  7004. switch (status) {
  7005. case 0:
  7006. {
  7007. nsstatus=@"Temp Order";
  7008. break;
  7009. }
  7010. case 1:
  7011. {
  7012. nsstatus=@"Saved Order";
  7013. break;
  7014. }
  7015. case 2: {
  7016. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7017. break;
  7018. }
  7019. case 3:
  7020. case 15:
  7021. {
  7022. nsstatus=@"Cancelled";
  7023. break;
  7024. }
  7025. case 10:
  7026. {
  7027. nsstatus=@"Quote Submitted";
  7028. break;
  7029. }
  7030. case 11:
  7031. {
  7032. nsstatus=@"Sales Order Submitted";
  7033. break;
  7034. }
  7035. case 12:
  7036. {
  7037. nsstatus=@"Processing";
  7038. break;
  7039. }
  7040. case 13:
  7041. {
  7042. nsstatus=@"Shipped";
  7043. break;
  7044. }
  7045. case 14:
  7046. {
  7047. nsstatus=@"Closed";
  7048. break;
  7049. }
  7050. case -11:
  7051. {
  7052. nsstatus = @"Ready For Submit";
  7053. break;
  7054. }
  7055. default:
  7056. break;
  7057. }
  7058. return nsstatus;
  7059. }
  7060. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7061. {
  7062. double total = 0;
  7063. __block double payments_and_credist = 0;
  7064. __block double allItemPrice = 0;
  7065. // sqlite3 *db1 = [iSalesDB get_db];
  7066. if (so_id) {
  7067. // 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];
  7068. 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];
  7069. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7070. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7071. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7072. int product_id = sqlite3_column_int(stmt, 0);
  7073. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7074. int discount = sqlite3_column_int(stmt, 2);
  7075. int item_count = sqlite3_column_int(stmt, 3);
  7076. // int item_id = sqlite3_column_int(stmt, 7);
  7077. int item_id = sqlite3_column_int(stmt, 4);
  7078. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7079. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7080. NSString* Price=nil;
  7081. if(str_price==nil)
  7082. {
  7083. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7084. if(price==nil)
  7085. Price=@"No Price.";
  7086. else
  7087. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7088. }
  7089. else
  7090. {
  7091. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7092. }
  7093. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7094. itemjson[@"The unit price"]=Price;
  7095. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7096. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7097. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7098. if(itemjson[@"combine"] != nil)
  7099. {
  7100. // int citem=0;
  7101. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7102. for(int bc=0;bc<bcount;bc++)
  7103. {
  7104. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7105. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7106. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7107. subTotal += uprice * modulus * item_count;
  7108. }
  7109. }
  7110. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7111. allItemPrice += subTotal;
  7112. }];
  7113. payments_and_credist = allItemPrice;
  7114. } else {
  7115. // payments/Credits
  7116. payments_and_credist = 0;
  7117. }
  7118. // lift_gate handlingFee shippingFee
  7119. __block double lift_gate = 0;
  7120. __block double handlingFee = 0;
  7121. __block double shippingFee = 0;
  7122. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7123. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7124. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7125. if (have_lift_gate) {
  7126. lift_gate = sqlite3_column_double(stmt, 1);
  7127. }
  7128. handlingFee = sqlite3_column_double(stmt, 2);
  7129. shippingFee = sqlite3_column_double(stmt, 3);
  7130. }];
  7131. // total
  7132. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7133. if (close) {
  7134. [iSalesDB close_db:db1];
  7135. }
  7136. return total;
  7137. }
  7138. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7139. {
  7140. assert(params[@"user"]!=nil);
  7141. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7142. int limit = [[params valueForKey:@"limit"] intValue];
  7143. int offset = [[params valueForKey:@"offset"] intValue];
  7144. NSString* keyword = [params valueForKey:@"keyWord"];
  7145. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7146. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7147. NSString* where=@"1 = 1";
  7148. if(keyword.length>0)
  7149. 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]];
  7150. if (orderStatus.length > 0) {
  7151. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7152. if (order_status_array.count == 1) {
  7153. int status_value = [[order_status_array firstObject] intValue];
  7154. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7155. if (status_value == 15 || status_value == 3) {
  7156. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7157. } else {
  7158. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7159. }
  7160. } else {
  7161. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7162. }
  7163. } else if (order_status_array.count > 1) {
  7164. for (int i = 0; i < order_status_array.count;i++) {
  7165. NSString *status = order_status_array[i];
  7166. NSString *condition = @" or";
  7167. if (i == 0) {
  7168. condition = @" and (";
  7169. }
  7170. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7171. int status_value = [status intValue];
  7172. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7173. if (status_value == 15 || status_value == 3) {
  7174. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7175. } else {
  7176. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7177. }
  7178. } else {
  7179. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7180. }
  7181. }
  7182. where = [where stringByAppendingString:@" )"];
  7183. }
  7184. }
  7185. 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];
  7186. // DebugLog(@"order list sql: %@",sqlQuery);
  7187. sqlite3 *db = [iSalesDB get_db];
  7188. sqlite3_stmt * statement;
  7189. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7190. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7191. {
  7192. int count=0;
  7193. while (sqlite3_step(statement) == SQLITE_ROW)
  7194. {
  7195. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7196. int order_id = sqlite3_column_double(statement, 0);
  7197. char *soid = (char*)sqlite3_column_text(statement, 1);
  7198. if(soid==nil)
  7199. soid= "";
  7200. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7201. int status = sqlite3_column_double(statement, 2);
  7202. int erpStatus = sqlite3_column_double(statement, 9);
  7203. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7204. if(sales_rep==nil)
  7205. sales_rep= "";
  7206. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7207. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7208. if(create_by==nil)
  7209. create_by= "";
  7210. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7211. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7212. if(company_name==nil)
  7213. company_name= "";
  7214. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7215. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7216. if(create_time==nil)
  7217. create_time= "";
  7218. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7219. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7220. // double total_price = sqlite3_column_double(statement, 7);
  7221. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7222. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7223. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7224. int offline_edit = sqlite3_column_int(statement, 10);
  7225. // ": "JH",
  7226. // "": "$8307.00",
  7227. // "": "MOB1608050001",
  7228. // "": "ArpithaT",
  7229. // "": "1st Stage Property Transformations",
  7230. // "": "JANICE SUTTON",
  7231. // "": 2255,
  7232. // "": "08/02/2016 09:49:18",
  7233. // "": 1,
  7234. // "": "Saved Order"
  7235. // "": "1470384050483",
  7236. // "model_count": "6 / 28"
  7237. item[@"sales_rep"]= nssales_rep;
  7238. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7239. item[@"so#"]= nssoid;
  7240. item[@"create_by"]= nscreate_by;
  7241. item[@"customer_name"]= nscompany_name;
  7242. item[@"customer_contact"] = customer_contact;
  7243. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7244. item[@"purchase_time"]= nscreate_time;
  7245. int statusCode = status;
  7246. if (statusCode == 2) {
  7247. statusCode = erpStatus;
  7248. } else if (statusCode == 3) {
  7249. statusCode = 15;
  7250. }
  7251. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7252. item[@"order_status"]= nsstatus;
  7253. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7254. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7255. // item[@"model_count"]
  7256. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7257. count++;
  7258. }
  7259. ret[@"count"]= [NSNumber numberWithInt:count];
  7260. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7261. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7262. ret[@"result"]= [NSNumber numberWithInt:2];
  7263. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7264. ret[@"time_zone"] = @"PST";
  7265. sqlite3_finalize(statement);
  7266. }
  7267. 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];
  7268. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7269. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7270. [iSalesDB close_db:db];
  7271. return [RAConvertor dict2data:ret];
  7272. }
  7273. #pragma mark update gnotes
  7274. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7275. {
  7276. DebugLog(@"params: %@",params);
  7277. // comments = Meyoyoyoyoyoyoy;
  7278. // orderCode = MOB1608110001;
  7279. // password = 123456;
  7280. // user = EvanK;
  7281. 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]];
  7282. int ret = [iSalesDB execSql:sql];
  7283. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7284. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7285. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7286. // [dic setValue:@"160409" forKey:@"min_ver"];
  7287. return [RAConvertor dict2data:dic];
  7288. }
  7289. #pragma mark move to wishlist
  7290. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7291. {
  7292. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7293. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7294. _id = [NSString stringWithFormat:@"(%@)",_id];
  7295. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7296. sqlite3 *db = [iSalesDB get_db];
  7297. __block NSString *product_id = @"";
  7298. __block NSString *item_count_str = @"";
  7299. // __block NSString *item_id = nil;
  7300. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7301. // product_id = [self textAtColumn:0 statement:stmt];
  7302. int item_count = sqlite3_column_int(stmt, 1);
  7303. // item_id = [self textAtColumn:2 statement:stmt];
  7304. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7305. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7306. if (p_id.length) {
  7307. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7308. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7309. }
  7310. }];
  7311. [iSalesDB close_db:db];
  7312. // 去除第一个,
  7313. if (product_id.length > 1) {
  7314. product_id = [product_id substringFromIndex:1];
  7315. }
  7316. if (item_count_str.length > 1) {
  7317. item_count_str = [item_count_str substringFromIndex:1];
  7318. }
  7319. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7320. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7321. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7322. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7323. sqlite3 *db1 = [iSalesDB get_db];
  7324. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7325. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7326. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7327. // 删除
  7328. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7329. int ret = [iSalesDB execSql:deleteSql db:db1];
  7330. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7331. [iSalesDB close_db:db1];
  7332. return [RAConvertor dict2data:dic];
  7333. }
  7334. #pragma mark cart delete
  7335. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7336. {
  7337. // cartItemId = 548;
  7338. // orderCode = MOB1608110001;
  7339. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7340. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7341. _id = [NSString stringWithFormat:@"(%@)",_id];
  7342. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7343. int ret = [iSalesDB execSql:sql];
  7344. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7345. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7346. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7347. // [dic setValue:@"160409" forKey:@"min_ver"];
  7348. return [RAConvertor dict2data:dic];
  7349. }
  7350. #pragma mark set price
  7351. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7352. {
  7353. DebugLog(@"cart set price params: %@",params);
  7354. // "cartitem_id" = 1;
  7355. // discount = "0.000000";
  7356. // "item_note" = "";
  7357. // price = "269.000000";
  7358. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7359. NSString *notes = [self valueInParams:params key:@"item_note"];
  7360. NSString *discount = [self valueInParams:params key:@"discount"];
  7361. NSString *price = [self valueInParams:params key:@"price"];
  7362. // bool badd_price_changed=false;
  7363. // sqlite3* db=[iSalesDB get_db];
  7364. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7365. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7366. // NSRange range;
  7367. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7368. //
  7369. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7370. // badd_price_changed=true;
  7371. // [iSalesDB close_db:db];
  7372. //
  7373. // if(badd_price_changed)
  7374. // {
  7375. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7376. // }
  7377. //
  7378. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7379. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7380. int ret = [iSalesDB execSql:sql];
  7381. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7382. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7383. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7384. // [dic setValue:@"160409" forKey:@"min_ver"];
  7385. return [RAConvertor dict2data:dic];
  7386. }
  7387. #pragma mark set line notes
  7388. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7389. {
  7390. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7391. NSString *notes = [self valueInParams:params key:@"notes"];
  7392. notes = [self translateSingleQuote:notes];
  7393. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7394. int ret = [iSalesDB execSql:sql];
  7395. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7396. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7397. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7398. // [dic setValue:@"160409" forKey:@"min_ver"];
  7399. return [RAConvertor dict2data:dic];
  7400. }
  7401. #pragma mark set qty
  7402. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7403. {
  7404. assert(params[@"can_create_backorder"]!=nil);
  7405. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7406. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7407. int item_count = [params[@"inputInt"] intValue];
  7408. // 购买检查数量大于库存
  7409. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7410. if (!params[@"can_create_backorder"]) {
  7411. 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];
  7412. __block BOOL out_of_stock = NO;
  7413. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7414. int availability = sqlite3_column_int(stmt, 0);
  7415. if (availability < item_count) {
  7416. out_of_stock = YES;
  7417. }
  7418. }];
  7419. if (out_of_stock) { // 缺货
  7420. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7421. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7422. return [RAConvertor dict2data:dic];
  7423. }
  7424. }
  7425. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7426. int ret = [iSalesDB execSql:sql];
  7427. __block int item_id = 0;
  7428. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7429. item_id = sqlite3_column_int(stmt, 0);
  7430. }];
  7431. sqlite3 *db = [iSalesDB get_db];
  7432. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7433. [iSalesDB close_db:db];
  7434. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7435. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7436. // [dic setValue:@"160409" forKey:@"min_ver"];
  7437. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7438. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7439. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7440. return [RAConvertor dict2data:dic];
  7441. }
  7442. #pragma mark place order
  7443. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7444. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7445. if (aname) {
  7446. [dic setValue:aname forKey:@"aname"];
  7447. }
  7448. if (control) {
  7449. [dic setValue:control forKey:@"control"];
  7450. }
  7451. if (keyboard) {
  7452. [dic setValue:keyboard forKey:@"keyboard"];
  7453. }
  7454. if (name) {
  7455. [dic setValue:name forKey:@"name"];
  7456. }
  7457. if (value) {
  7458. [dic setValue:value forKey:@"value"];
  7459. }
  7460. return dic;
  7461. }
  7462. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7463. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7464. orderCode = [self translateSingleQuote:orderCode];
  7465. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7466. 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];
  7467. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7468. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7469. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7470. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7471. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7472. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7473. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7474. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7475. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7476. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7477. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7478. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7479. NSString *email = [self textAtColumn:11 statement:stmt];
  7480. NSString *phone = [self textAtColumn:12 statement:stmt];
  7481. NSString *fax = [self textAtColumn:13 statement:stmt];
  7482. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7483. NSString *city = [self textAtColumn:15 statement:stmt];
  7484. NSString *state = [self textAtColumn:16 statement:stmt];
  7485. NSString *country = [self textAtColumn:17 statement:stmt];
  7486. NSString *name = [self textAtColumn:18 statement:stmt];
  7487. // contact id
  7488. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7489. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7490. [contact_id_dic setValue:@"text" forKey:@"control"];
  7491. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7492. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7493. [contact_id_dic setValue:@"true" forKey:@"required"];
  7494. [contact_id_dic setValue:contact_id forKey:@"value"];
  7495. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7496. // business card
  7497. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7498. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7499. [business_card_dic setValue:@"img" forKey:@"control"];
  7500. [business_card_dic setValue:@"1" forKey:@"disable"];
  7501. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7502. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7503. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7504. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7505. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7506. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7507. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7508. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7509. // fax
  7510. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7511. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7512. // zipcode
  7513. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7514. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7515. // city
  7516. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7517. [customer_dic setValue:city_dic forKey:@"item_12"];
  7518. // state
  7519. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7520. [customer_dic setValue:state_dic forKey:@"item_13"];
  7521. // country
  7522. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7523. [customer_dic setValue:country_dic forKey:@"item_14"];
  7524. // company name
  7525. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7526. [customer_dic setValue:company_dic forKey:@"item_2"];
  7527. // addr_1
  7528. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7529. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7530. // addr_2
  7531. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7532. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7533. // addr_3
  7534. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7535. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7536. // addr_4
  7537. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7538. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7539. // Contact
  7540. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7541. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7542. // email
  7543. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7544. [customer_dic setValue:email_dic forKey:@"item_8"];
  7545. // phone
  7546. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7547. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7548. // title
  7549. [customer_dic setValue:@"Customer" forKey:@"title"];
  7550. // count
  7551. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7552. }];
  7553. // setting
  7554. NSDictionary *setting = params[@"setting"];
  7555. NSNumber *hide = setting[@"CustomerHide"];
  7556. [customer_dic setValue:hide forKey:@"hide"];
  7557. return customer_dic;
  7558. }
  7559. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7560. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7561. // setting
  7562. NSDictionary *setting = params[@"setting"];
  7563. NSNumber *hide = setting[@"ShipToHide"];
  7564. [dic setValue:hide forKey:@"hide"];
  7565. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7566. orderCode = [self translateSingleQuote:orderCode];
  7567. 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];
  7568. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7569. NSString *cid = [self textAtColumn:0 statement:stmt];
  7570. NSString *name = [self textAtColumn:1 statement:stmt];
  7571. NSString *ext = [self textAtColumn:2 statement:stmt];
  7572. NSString *contact = [self textAtColumn:3 statement:stmt];
  7573. NSString *email = [self textAtColumn:4 statement:stmt];
  7574. NSString *fax = [self textAtColumn:5 statement:stmt];
  7575. NSString *phone = [self textAtColumn:6 statement:stmt];
  7576. // count
  7577. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7578. // title
  7579. [dic setValue:@"Ship To" forKey:@"title"];
  7580. // choose
  7581. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7582. [choose_dic setValue:@"choose" forKey:@"aname"];
  7583. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7584. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7585. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7586. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7587. @"receive_contact" : @"customer_contact",
  7588. @"receive_email" : @"customer_email",
  7589. @"receive_ext" : @"customer_contact_ext",
  7590. @"receive_fax" : @"customer_fax",
  7591. @"receive_name" : @"customer_name",
  7592. @"receive_phone" : @"customer_phone"},
  7593. @"refresh" : [NSNumber numberWithInteger:1],
  7594. @"type" : @"pull"};
  7595. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7596. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7597. @"name" : @"Add new address",
  7598. @"refresh" : [NSNumber numberWithInteger:1],
  7599. @"value" : @"new_addr"
  7600. };
  7601. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7602. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7603. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7604. @"receive_contact" : @"customer_contact",
  7605. @"receive_email" : @"customer_email",
  7606. @"receive_ext" : @"customer_contact_ext",
  7607. @"receive_fax" : @"customer_fax",
  7608. @"receive_name" : @"customer_name",
  7609. @"receive_phone" : @"customer_phone"},
  7610. @"name" : @"select_ship_to",
  7611. @"refresh" : [NSNumber numberWithInteger:1],
  7612. @"value" : @"Sales_Order_Ship_To"};
  7613. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7614. [dic setValue:choose_dic forKey:@"item_0"];
  7615. // contact id
  7616. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7617. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7618. [contact_id_dic setValue:@"true" forKey:@"required"];
  7619. [dic setValue:contact_id_dic forKey:@"item_1"];
  7620. // company name
  7621. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7622. [dic setValue:company_name_dic forKey:@"item_2"];
  7623. // address
  7624. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7625. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7626. if ([required integerValue]) {
  7627. [ext_dic setValue:@"true" forKey:@"required"];
  7628. }
  7629. [dic setValue:ext_dic forKey:@"item_3"];
  7630. // contact
  7631. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7632. [dic setValue:contact_dic forKey:@"item_4"];
  7633. // phone
  7634. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7635. [dic setValue:phone_dic forKey:@"item_5"];
  7636. // fax
  7637. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7638. [dic setValue:fax_dic forKey:@"item_6"];
  7639. // email
  7640. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7641. [dic setValue:email_dic forKey:@"item_7"];
  7642. }];
  7643. return dic;
  7644. }
  7645. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7646. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7647. // setting
  7648. NSDictionary *setting = params[@"setting"];
  7649. NSNumber *hide = setting[@"ShipFromHide"];
  7650. [dic setValue:hide forKey:@"hide"];
  7651. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7652. orderCode = [self translateSingleQuote:orderCode];
  7653. 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];
  7654. 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';";
  7655. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7656. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7657. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7658. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7659. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7660. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7661. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7662. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7663. if (!cid.length) {
  7664. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7665. cid = [self textAtColumn:0 statement:statment];
  7666. name = [self textAtColumn:1 statement:statment];
  7667. ext = [self textAtColumn:2 statement:statment];
  7668. contact = [self textAtColumn:3 statement:statment];
  7669. email = [self textAtColumn:4 statement:statment];
  7670. fax = [self textAtColumn:5 statement:statment];
  7671. phone = [self textAtColumn:6 statement:statment];
  7672. }];
  7673. }
  7674. // count
  7675. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7676. // title
  7677. [dic setValue:@"Ship From" forKey:@"title"];
  7678. // hide
  7679. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7680. // choose
  7681. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7682. [choose_dic setValue:@"choose" forKey:@"aname"];
  7683. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7684. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7685. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7686. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7687. @"sender_contact" : @"customer_contact",
  7688. @"sender_email" : @"customer_email",
  7689. @"sender_ext" : @"customer_contact_ext",
  7690. @"sender_fax" : @"customer_fax",
  7691. @"sender_name" : @"customer_name",
  7692. @"sender_phone" : @"customer_phone"},
  7693. @"name" : @"select_cid",
  7694. @"refresh" : [NSNumber numberWithInteger:0],
  7695. @"value" : @"Sales_Order_Ship_From"};
  7696. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7697. [dic setValue:choose_dic forKey:@"item_0"];
  7698. // contact id
  7699. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7700. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7701. [contact_id_dic setValue:@"true" forKey:@"required"];
  7702. [dic setValue:contact_id_dic forKey:@"item_1"];
  7703. // company name
  7704. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7705. [dic setValue:company_name_dic forKey:@"item_2"];
  7706. // address
  7707. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7708. [dic setValue:ext_dic forKey:@"item_3"];
  7709. // contact
  7710. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7711. [dic setValue:contact_dic forKey:@"item_4"];
  7712. // phone
  7713. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7714. [dic setValue:phone_dic forKey:@"item_5"];
  7715. // fax
  7716. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7717. [dic setValue:fax_dic forKey:@"item_6"];
  7718. // email
  7719. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7720. [dic setValue:email_dic forKey:@"item_7"];
  7721. }];
  7722. return dic;
  7723. }
  7724. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7725. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7726. // setting
  7727. NSDictionary *setting = params[@"setting"];
  7728. NSNumber *hide = setting[@"FreightBillToHide"];
  7729. [dic setValue:hide forKey:@"hide"];
  7730. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7731. orderCode = [self translateSingleQuote:orderCode];
  7732. 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];
  7733. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7734. NSString *cid = [self textAtColumn:0 statement:stmt];
  7735. NSString *name = [self textAtColumn:1 statement:stmt];
  7736. NSString *ext = [self textAtColumn:2 statement:stmt];
  7737. NSString *contact = [self textAtColumn:3 statement:stmt];
  7738. NSString *email = [self textAtColumn:4 statement:stmt];
  7739. NSString *fax = [self textAtColumn:5 statement:stmt];
  7740. NSString *phone = [self textAtColumn:6 statement:stmt];
  7741. // count
  7742. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7743. // title
  7744. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7745. // hide
  7746. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7747. // choose
  7748. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7749. [choose_dic setValue:@"choose" forKey:@"aname"];
  7750. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7751. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7752. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7753. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7754. @"shipping_billto_contact" : @"receive_contact",
  7755. @"shipping_billto_email" : @"receive_email",
  7756. @"shipping_billto_ext" : @"receive_ext",
  7757. @"shipping_billto_fax" : @"receive_fax",
  7758. @"shipping_billto_name" : @"receive_name",
  7759. @"shipping_billto_phone" : @"receive_phone"},
  7760. @"type" : @"pull"};
  7761. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7762. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7763. @"type" : @"pull",
  7764. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7765. @"shipping_billto_contact" : @"customer_contact",
  7766. @"shipping_billto_email" : @"customer_email",
  7767. @"shipping_billto_ext" : @"customer_contact_ext",
  7768. @"shipping_billto_fax" : @"customer_fax",
  7769. @"shipping_billto_name" : @"customer_name",
  7770. @"shipping_billto_phone" : @"customer_phone"}
  7771. };
  7772. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7773. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7774. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7775. @"shipping_billto_contact" : @"sender_contact",
  7776. @"shipping_billto_email" : @"sender_email",
  7777. @"shipping_billto_ext" : @"sender_ext",
  7778. @"shipping_billto_fax" : @"sender_fax",
  7779. @"shipping_billto_name" : @"sender_name",
  7780. @"shipping_billto_phone" : @"sender_phone"},
  7781. @"type" : @"pull"
  7782. };
  7783. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7784. NSDictionary *select_freight_bill_to_dic = @{
  7785. @"aname" : @"Select freight bill to",
  7786. @"name" : @"select_cid",
  7787. @"refresh" : [NSNumber numberWithInteger:0],
  7788. @"value" : @"Sales_Order_Freight_Bill_To",
  7789. @"key_map" : @{
  7790. @"shipping_billto_cid" : @"customer_cid",
  7791. @"shipping_billto_contact" : @"customer_contact",
  7792. @"shipping_billto_email" : @"customer_email",
  7793. @"shipping_billto_ext" : @"customer_contact_ext",
  7794. @"shipping_billto_fax" : @"customer_fax",
  7795. @"shipping_billto_name" : @"customer_name",
  7796. @"shipping_billto_phone" : @"customer_phone"
  7797. }
  7798. };
  7799. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7800. [dic setValue:choose_dic forKey:@"item_0"];
  7801. // contact id
  7802. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7803. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7804. [contact_id_dic setValue:@"true" forKey:@"required"];
  7805. [dic setValue:contact_id_dic forKey:@"item_1"];
  7806. // company name
  7807. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7808. [dic setValue:company_name_dic forKey:@"item_2"];
  7809. // address
  7810. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7811. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7812. if ([ext_required integerValue]) {
  7813. [ext_dic setValue:@"true" forKey:@"required"];
  7814. }
  7815. [dic setValue:ext_dic forKey:@"item_3"];
  7816. // contact
  7817. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7818. [dic setValue:contact_dic forKey:@"item_4"];
  7819. // phone
  7820. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7821. [dic setValue:phone_dic forKey:@"item_5"];
  7822. // fax
  7823. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7824. [dic setValue:fax_dic forKey:@"item_6"];
  7825. // email
  7826. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7827. [dic setValue:email_dic forKey:@"item_7"];
  7828. }];
  7829. return dic;
  7830. }
  7831. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7832. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7833. // setting
  7834. NSDictionary *setting = params[@"setting"];
  7835. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7836. [dic setValue:hide forKey:@"hide"];
  7837. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7838. orderCode = [self translateSingleQuote:orderCode];
  7839. 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];
  7840. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7841. NSString *cid = [self textAtColumn:0 statement:stmt];
  7842. NSString *name = [self textAtColumn:1 statement:stmt];
  7843. NSString *ext = [self textAtColumn:2 statement:stmt];
  7844. NSString *contact = [self textAtColumn:3 statement:stmt];
  7845. NSString *email = [self textAtColumn:4 statement:stmt];
  7846. NSString *fax = [self textAtColumn:5 statement:stmt];
  7847. NSString *phone = [self textAtColumn:6 statement:stmt];
  7848. // count
  7849. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7850. // title
  7851. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7852. // hide
  7853. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7854. // choose
  7855. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7856. [choose_dic setValue:@"choose" forKey:@"aname"];
  7857. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7858. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7859. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7860. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7861. @"billing_contact" : @"receive_contact",
  7862. @"billing_email" : @"receive_email",
  7863. @"billing_ext" : @"receive_ext",
  7864. @"billing_fax" : @"receive_fax",
  7865. @"billing_name" : @"receive_name",
  7866. @"billing_phone" : @"receive_phone"},
  7867. @"type" : @"pull"};
  7868. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7869. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7870. @"type" : @"pull",
  7871. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7872. @"billing_contact" : @"customer_contact",
  7873. @"billing_email" : @"customer_email",
  7874. @"billing_ext" : @"customer_contact_ext",
  7875. @"billing_fax" : @"customer_fax",
  7876. @"billing_name" : @"customer_name",
  7877. @"billing_phone" : @"customer_phone"}
  7878. };
  7879. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7880. NSDictionary *select_bill_to_dic = @{
  7881. @"aname" : @"Select bill to",
  7882. @"name" : @"select_cid",
  7883. @"refresh" : [NSNumber numberWithInteger:0],
  7884. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7885. @"key_map" : @{
  7886. @"billing_cid" : @"customer_cid",
  7887. @"billing_contact" : @"customer_contact",
  7888. @"billing_email" : @"customer_email",
  7889. @"billing_ext" : @"customer_contact_ext",
  7890. @"billing_fax" : @"customer_fax",
  7891. @"billing_name" : @"customer_name",
  7892. @"billing_phone" : @"customer_phone"
  7893. }
  7894. };
  7895. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7896. [dic setValue:choose_dic forKey:@"item_0"];
  7897. // contact id
  7898. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7899. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7900. [contact_id_dic setValue:@"true" forKey:@"required"];
  7901. [dic setValue:contact_id_dic forKey:@"item_1"];
  7902. // company name
  7903. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7904. [dic setValue:company_name_dic forKey:@"item_2"];
  7905. // address
  7906. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7907. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7908. if ([ext_required integerValue]) {
  7909. [ext_dic setValue:@"true" forKey:@"required"];
  7910. }
  7911. [dic setValue:ext_dic forKey:@"item_3"];
  7912. // contact
  7913. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7914. [dic setValue:contact_dic forKey:@"item_4"];
  7915. // phone
  7916. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7917. [dic setValue:phone_dic forKey:@"item_5"];
  7918. // fax
  7919. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7920. [dic setValue:fax_dic forKey:@"item_6"];
  7921. // email
  7922. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7923. [dic setValue:email_dic forKey:@"item_7"];
  7924. }];
  7925. return dic;
  7926. }
  7927. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7928. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7929. // setting
  7930. NSDictionary *setting = params[@"setting"];
  7931. NSNumber *hide = setting[@"ReturnToHide"];
  7932. [dic setValue:hide forKey:@"hide"];
  7933. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7934. orderCode = [self translateSingleQuote:orderCode];
  7935. 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];
  7936. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7937. NSString *cid = [self textAtColumn:0 statement:stmt];
  7938. NSString *name = [self textAtColumn:1 statement:stmt];
  7939. NSString *ext = [self textAtColumn:2 statement:stmt];
  7940. NSString *contact = [self textAtColumn:3 statement:stmt];
  7941. NSString *email = [self textAtColumn:4 statement:stmt];
  7942. NSString *fax = [self textAtColumn:5 statement:stmt];
  7943. NSString *phone = [self textAtColumn:6 statement:stmt];
  7944. // count
  7945. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7946. // title
  7947. [dic setValue:@"Return To" forKey:@"title"];
  7948. // hide
  7949. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7950. // choose
  7951. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7952. [choose_dic setValue:@"choose" forKey:@"aname"];
  7953. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7954. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7955. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7956. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7957. @"returnto_contact" : @"sender_contact",
  7958. @"returnto_email" : @"sender_email",
  7959. @"returnto_ext" : @"sender_ext",
  7960. @"returnto_fax" : @"sender_fax",
  7961. @"returnto_name" : @"sender_name",
  7962. @"returnto_phone" : @"sender_phone"},
  7963. @"type" : @"pull"};
  7964. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7965. NSDictionary *select_return_to_dic = @{
  7966. @"aname" : @"Select return to",
  7967. @"name" : @"select_cid",
  7968. @"refresh" : [NSNumber numberWithInteger:0],
  7969. @"value" : @"Contact_Return_To",
  7970. @"key_map" : @{
  7971. @"returnto_cid" : @"customer_cid",
  7972. @"returnto_contact" : @"customer_contact",
  7973. @"returnto_email" : @"customer_email",
  7974. @"returnto_ext" : @"customer_contact_ext",
  7975. @"returnto_fax" : @"customer_fax",
  7976. @"returnto_name" : @"customer_name",
  7977. @"returnto_phone" : @"customer_phone"
  7978. }
  7979. };
  7980. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7981. [dic setValue:choose_dic forKey:@"item_0"];
  7982. // contact id
  7983. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7984. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7985. [contact_id_dic setValue:@"true" forKey:@"required"];
  7986. [dic setValue:contact_id_dic forKey:@"item_1"];
  7987. // company name
  7988. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7989. [dic setValue:company_name_dic forKey:@"item_2"];
  7990. // address
  7991. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7992. [dic setValue:ext_dic forKey:@"item_3"];
  7993. // contact
  7994. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7995. [dic setValue:contact_dic forKey:@"item_4"];
  7996. // phone
  7997. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7998. [dic setValue:phone_dic forKey:@"item_5"];
  7999. // fax
  8000. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8001. [dic setValue:fax_dic forKey:@"item_6"];
  8002. // email
  8003. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8004. [dic setValue:email_dic forKey:@"item_7"];
  8005. }];
  8006. return dic;
  8007. }
  8008. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8009. {
  8010. assert(params[@"user"]!=nil);
  8011. assert(params[@"contact_id"]!=nil);
  8012. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8013. orderCode = [self translateSingleQuote:orderCode];
  8014. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8015. __block double TotalCuft = 0;
  8016. __block double TotalWeight = 0;
  8017. __block int TotalCarton = 0;
  8018. __block double payments = 0;
  8019. // setting
  8020. NSDictionary *setting = params[@"setting"];
  8021. NSNumber *hide = setting[@"ModelInformationHide"];
  8022. [dic setValue:hide forKey:@"hide"];
  8023. 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];
  8024. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8025. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8026. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8027. // item id
  8028. int item_id = sqlite3_column_int(stmt, 0);
  8029. // count
  8030. int item_count = sqlite3_column_int(stmt, 1);
  8031. // stockUom
  8032. int stockUom = sqlite3_column_int(stmt, 2);
  8033. // unit price
  8034. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8035. NSString* Price=nil;
  8036. if([str_price isEqualToString:@""])
  8037. {
  8038. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8039. if(price==nil)
  8040. Price=@"No Price.";
  8041. else
  8042. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8043. }
  8044. else
  8045. {
  8046. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8047. }
  8048. // discount
  8049. double discount = sqlite3_column_double(stmt, 4);
  8050. // name
  8051. NSString *name = [self textAtColumn:5 statement:stmt];
  8052. // description
  8053. NSString *description = [self textAtColumn:6 statement:stmt];
  8054. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8055. // line note
  8056. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8057. int avaulability = sqlite3_column_int(stmt, 8);
  8058. // img
  8059. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8060. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8061. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8062. if(combine != nil)
  8063. {
  8064. // int citem=0;
  8065. int bcount=[[combine valueForKey:@"count"] intValue];
  8066. for(int bc=0;bc<bcount;bc++)
  8067. {
  8068. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8069. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8070. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8071. subTotal += uprice * modulus * item_count;
  8072. }
  8073. }
  8074. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8075. [model_dic setValue:@"model" forKey:@"control"];
  8076. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8077. [model_dic setValue:description forKey:@"description"];
  8078. [model_dic setValue:line_note forKey:@"note"];
  8079. [model_dic setValue:img forKey:@"img_url"];
  8080. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8081. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8082. [model_dic setValue:Price forKey:@"unit_price"];
  8083. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8084. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8085. if (combine) {
  8086. [model_dic setValue:combine forKey:@"combine"];
  8087. }
  8088. // well,what under the row is the info for total
  8089. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8090. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8091. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8092. int carton=[bsubtotaljson[@"carton"] intValue];
  8093. TotalCuft += cuft;
  8094. TotalWeight += weight;
  8095. TotalCarton += carton;
  8096. payments += subTotal;
  8097. //---------------------------
  8098. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8099. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8100. }];
  8101. [dic setValue:@"Model Information" forKey:@"title"];
  8102. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8103. return dic;
  8104. }
  8105. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8106. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8107. [dic setValue:@"Remarks Content" forKey:@"title"];
  8108. // setting
  8109. NSDictionary *setting = params[@"setting"];
  8110. NSNumber *hide = setting[@"RemarksContentHide"];
  8111. [dic setValue:hide forKey:@"hide"];
  8112. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8113. orderCode = [self translateSingleQuote:orderCode];
  8114. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  8115. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8116. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8117. int mustCall = sqlite3_column_int(stmt, 1);
  8118. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8119. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8120. NSDictionary *po_dic = @{
  8121. @"aname" : @"PO#",
  8122. @"control" : @"edit",
  8123. @"keyboard" : @"text",
  8124. @"name" : @"poNumber",
  8125. @"value" : poNumber
  8126. };
  8127. NSDictionary *must_call_dic = @{
  8128. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8129. @"control" : @"switch",
  8130. @"name" : @"must_call",
  8131. @"value" : mustCall ? @"true" : @"false"
  8132. };
  8133. NSDictionary *general_notes_dic = @{
  8134. @"aname" : @"General notes",
  8135. @"control" : @"text_view",
  8136. @"keyboard" : @"text",
  8137. @"name" : @"comments",
  8138. @"value" : generalNotes
  8139. };
  8140. // NSDictionary *internal_notes_dic = @{
  8141. // @"aname" : @"Internal notes",
  8142. // @"control" : @"text_view",
  8143. // @"keyboard" : @"text",
  8144. // @"name" : @"internal_notes",
  8145. // @"value" : internalNotes
  8146. // };
  8147. [dic setValue:po_dic forKey:@"item_0"];
  8148. [dic setValue:must_call_dic forKey:@"item_1"];
  8149. [dic setValue:general_notes_dic forKey:@"item_2"];
  8150. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8151. }];
  8152. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8153. return dic;
  8154. }
  8155. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8156. // params
  8157. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8158. orderCode = [self translateSingleQuote:orderCode];
  8159. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8160. // setting
  8161. NSDictionary *setting = params[@"setting"];
  8162. NSNumber *hide = setting[@"OrderTotalHide"];
  8163. [dic setValue:hide forKey:@"hide"];
  8164. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8165. __block double lift_gate_value = 0;
  8166. __block double handling_fee = 0;
  8167. __block double shipping = 0;
  8168. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8169. int lift_gate = sqlite3_column_int(stmt, 0);
  8170. lift_gate_value = sqlite3_column_double(stmt, 1);
  8171. shipping = sqlite3_column_double(stmt, 2);
  8172. handling_fee = sqlite3_column_double(stmt, 3);
  8173. if (!lift_gate) {
  8174. lift_gate_value = 0;
  8175. }
  8176. }];
  8177. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8178. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8179. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8180. double payments = [[total valueForKey:@"payments"] doubleValue];
  8181. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8182. double totalPrice = payments;
  8183. [dic setValue:@"Order Total" forKey:@"title"];
  8184. NSDictionary *payments_dic = @{
  8185. @"align" : @"right",
  8186. @"aname" : @"Payments/Credits",
  8187. @"control" : @"text",
  8188. @"name" : @"paymentsAndCredits",
  8189. @"type" : @"price",
  8190. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8191. };
  8192. [dic setValue:payments_dic forKey:@"item_0"];
  8193. // version 1.71 remove
  8194. // NSDictionary *handling_fee_dic = @{
  8195. // @"align" : @"right",
  8196. // @"aname" : @"Handling Fee",
  8197. // @"control" : @"text",
  8198. // @"name" : @"handling_fee_value",
  8199. // @"required" : @"true",
  8200. // @"type" : @"price",
  8201. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8202. // };
  8203. NSDictionary *shipping_dic = @{
  8204. @"align" : @"right",
  8205. @"aname" : @"Shipping*",
  8206. @"control" : @"text",
  8207. @"name" : @"shipping",
  8208. @"required" : @"true",
  8209. @"type" : @"price",
  8210. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8211. };
  8212. NSDictionary *lift_gate_dic = @{
  8213. @"align" : @"right",
  8214. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8215. @"control" : @"text",
  8216. @"name" : @"lift_gate_value",
  8217. @"required" : @"true",
  8218. @"type" : @"price",
  8219. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8220. };
  8221. int item_count = 1;
  8222. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8223. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8224. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8225. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8226. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8227. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8228. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8229. } else {
  8230. }
  8231. }
  8232. // version 1.71 remove
  8233. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8234. NSDictionary *total_price_dic = @{
  8235. @"align" : @"right",
  8236. @"aname" : @"Total",
  8237. @"control" : @"text",
  8238. @"name" : @"totalPrice",
  8239. @"type" : @"price",
  8240. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8241. };
  8242. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8243. NSDictionary *total_cuft_dic = @{
  8244. @"align" : @"right",
  8245. @"aname" : @"Total Cuft",
  8246. @"control" : @"text",
  8247. @"name" : @"",
  8248. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8249. };
  8250. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8251. NSDictionary *total_weight_dic = @{
  8252. @"align" : @"right",
  8253. @"aname" : @"Total Weight",
  8254. @"control" : @"text",
  8255. @"name" : @"",
  8256. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8257. };
  8258. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8259. NSDictionary *total_carton_dic = @{
  8260. @"align" : @"right",
  8261. @"aname" : @"Total Carton",
  8262. @"control" : @"text",
  8263. @"name" : @"",
  8264. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8265. };
  8266. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8267. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8268. return dic;
  8269. }
  8270. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8271. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8272. orderCode = [self translateSingleQuote:orderCode];
  8273. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8274. // setting
  8275. NSDictionary *setting = params[@"setting"];
  8276. NSNumber *hide = setting[@"SignatureHide"];
  8277. [dic setValue:hide forKey:@"hide"];
  8278. [dic setValue:@"Signature" forKey:@"title"];
  8279. __block NSString *pic_path = @"";
  8280. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8281. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8282. pic_path = [self textAtColumn:0 statement:stmt];
  8283. }];
  8284. NSDictionary *pic_dic = @{
  8285. @"aname" : @"Signature",
  8286. @"avalue" :pic_path,
  8287. @"control" : @"signature",
  8288. @"name" : @"sign_picpath",
  8289. @"value" : pic_path
  8290. };
  8291. [dic setValue:pic_dic forKey:@"item_0"];
  8292. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8293. return dic;
  8294. }
  8295. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8296. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8297. // setting
  8298. NSDictionary *setting = params[@"setting"];
  8299. NSNumber *hide = setting[@"ShippingMethodHide"];
  8300. [dic setValue:hide forKey:@"hide"];
  8301. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8302. orderCode = [self translateSingleQuote:orderCode];
  8303. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8304. __block NSString *logist = @"";
  8305. __block NSString *lift_gate = @"";
  8306. __block int lift_gate_integer = 0;
  8307. __block NSString *logistic_note = @"";
  8308. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8309. logist = [self textAtColumn:0 statement:stmt];
  8310. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8311. logistic_note = [self textAtColumn:2 statement:stmt];
  8312. }];
  8313. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8314. [dic setValue:@"Shipping Method" forKey:@"title"];
  8315. // val_0
  8316. int PERSONAL_PICK_UP_check = 0;
  8317. int USE_MY_CARRIER_check = 0;
  8318. NSString *logistic_note_text = @"";
  8319. int will_call_check = 0;
  8320. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8321. will_call_check = 1;
  8322. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8323. PERSONAL_PICK_UP_check = 1;
  8324. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8325. USE_MY_CARRIER_check = 1;
  8326. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8327. logistic_note = [logistic_note_array firstObject];
  8328. if (logistic_note_array.count > 1) {
  8329. logistic_note_text = [logistic_note_array lastObject];
  8330. }
  8331. }
  8332. }
  8333. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8334. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8335. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8336. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8337. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8338. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8339. NSDictionary *val0_subItem_item0 = @{
  8340. @"aname" : @"Option",
  8341. @"cadedate" : @{
  8342. @"count" : [NSNumber numberWithInteger:2],
  8343. @"val_0" : @{
  8344. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8345. @"refresh" : [NSNumber numberWithInteger:0],
  8346. @"value" : @"PERSONAL PICK UP",
  8347. @"value_id" : @"PERSONAL PICK UP"
  8348. },
  8349. @"val_1" : @{
  8350. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8351. @"refresh" : [NSNumber numberWithInteger:0],
  8352. @"sub_item" : @{
  8353. @"count" : [NSNumber numberWithInteger:1],
  8354. @"item_0" : @{
  8355. @"aname" : @"BOL",
  8356. @"control" : @"edit",
  8357. @"keyboard" : @"text",
  8358. @"name" : @"logist_note_text",
  8359. @"refresh" : [NSNumber numberWithInteger:0],
  8360. @"required" : @"false",
  8361. @"value" : logistic_note_text
  8362. }
  8363. },
  8364. @"value" : @"USE MY CARRIER",
  8365. @"value_id" : @"USE MY CARRIER"
  8366. }
  8367. },
  8368. @"control" : @"enum",
  8369. @"name" : @"logistic_note",
  8370. @"required" : @"true",
  8371. @"single_select" : @"true"
  8372. };
  8373. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8374. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8375. // val_1
  8376. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8377. if([lift_gate isEqualToString:@""]) {
  8378. if (lift_gate_integer == 1) {
  8379. lift_gate = @"true";
  8380. } else {
  8381. lift_gate = @"false";
  8382. }
  8383. }
  8384. int common_carrier_check = 0;
  8385. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8386. common_carrier_check = 1;
  8387. [params setValue:lift_gate forKey:@"lift_gate"];
  8388. }
  8389. NSDictionary *val_1 = @{
  8390. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8391. @"sub_item" : @{
  8392. @"count" : [NSNumber numberWithInteger:1],
  8393. @"item_0" : @{
  8394. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8395. @"control" : @"switch",
  8396. @"name" : @"lift_gate",
  8397. @"refresh" : [NSNumber numberWithInteger:1],
  8398. @"required" : @"true",
  8399. @"value" : lift_gate
  8400. }
  8401. },
  8402. @"value" : @"COMMON CARRIER",
  8403. @"value_id" : @"COMMON CARRIER"
  8404. };
  8405. // cadedate
  8406. NSDictionary *cadedate = @{
  8407. @"count" : [NSNumber numberWithInteger:2],
  8408. @"val_0" : val_0,
  8409. @"val_1" : val_1
  8410. };
  8411. // item_0
  8412. NSMutableDictionary *item_0 = @{
  8413. @"aname" : @"Shipping",
  8414. @"cadedate" : cadedate,
  8415. @"control" : @"enum",
  8416. @"name" : @"logist",
  8417. @"refresh" : [NSNumber numberWithInteger:1],
  8418. @"single_select" : @"true",
  8419. }.mutableCopy;
  8420. NSNumber *required = setting[@"ShippingMethodRequire"];
  8421. if ([required integerValue]) {
  8422. [item_0 setValue:@"true" forKey:@"required"];
  8423. }
  8424. [dic setValue:item_0 forKey:@"item_0"];
  8425. if ([logist isEqualToString:@"WILL CALL"]) {
  8426. [dic removeObjectForKey:@"lift_gate"];
  8427. }
  8428. return dic;
  8429. }
  8430. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8431. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8432. orderCode = [self translateSingleQuote:orderCode];
  8433. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8434. __block int submit_as_integer = 0;
  8435. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8436. submit_as_integer = sqlite3_column_int(stmt, 0);
  8437. }];
  8438. int check11 = 0;
  8439. int check10 = 0;
  8440. if (submit_as_integer == 11) {
  8441. check11 = 1;
  8442. }
  8443. if (submit_as_integer == 10) {
  8444. check10 = 1;
  8445. }
  8446. // section_0
  8447. NSMutableDictionary *dic = @{
  8448. @"count" : @(1),
  8449. @"item_0" : @{
  8450. @"aname" : @"Submit Order As",
  8451. @"cadedate" : @{
  8452. @"count" : @(2),
  8453. @"val_0" : @{
  8454. @"check" : [NSNumber numberWithInteger:check11],
  8455. @"value" : @"Sales Order",
  8456. @"value_id" : @(11)
  8457. },
  8458. @"val_1" : @{
  8459. @"check" : [NSNumber numberWithInteger:check10],
  8460. @"value" : @"Quote",
  8461. @"value_id" : @(10)
  8462. }
  8463. },
  8464. @"control" : @"enum",
  8465. @"name" : @"erpOrderStatus",
  8466. @"required" : @"true",
  8467. @"single_select" : @"true"
  8468. },
  8469. @"title" : @"Order Type"
  8470. }.mutableCopy;
  8471. // setting
  8472. NSDictionary *setting = params[@"setting"];
  8473. NSNumber *hide = setting[@"OrderTypeHide"];
  8474. [dic setValue:hide forKey:@"hide"];
  8475. return dic;
  8476. }
  8477. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8478. countryCode = [self translateSingleQuote:countryCode];
  8479. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8480. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8481. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8482. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8483. if (name == NULL) {
  8484. name = "";
  8485. }
  8486. if (code == NULL) {
  8487. code = "";
  8488. }
  8489. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8490. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8491. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8492. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8493. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8494. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8495. }
  8496. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8497. [container setValue:stateDic forKey:key];
  8498. }] mutableCopy];
  8499. [ret removeObjectForKey:@"result"];
  8500. // failure 可以不用了,一样的
  8501. if (ret.allKeys.count == 0) {
  8502. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8503. [stateDic setValue:@"Other" forKey:@"value"];
  8504. [stateDic setValue:@"" forKey:@"value_id"];
  8505. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8506. if (state_code && [@"" isEqualToString:state_code]) {
  8507. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8508. }
  8509. NSString *key = [NSString stringWithFormat:@"val_0"];
  8510. [ret setValue:stateDic forKey:key];
  8511. }
  8512. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8513. return ret;
  8514. }
  8515. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8516. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8517. orderCode = [self translateSingleQuote:orderCode];
  8518. 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];
  8519. __block NSString *payType = @"";
  8520. __block NSString *firstName = @"";
  8521. __block NSString *lastName = @"";
  8522. __block NSString *addr1 = @"";
  8523. __block NSString *addr2 = @"";
  8524. __block NSString *zipcode = @"";
  8525. __block NSString *cardType = @"";
  8526. __block NSString *cardNumber = @"";
  8527. __block NSString *securityCode = @"";
  8528. __block NSString *month = @"";
  8529. __block NSString *year = @"";
  8530. __block NSString *city = @"";
  8531. __block NSString *state = @"";
  8532. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8533. payType = [self textAtColumn:0 statement:stmt];
  8534. firstName = [self textAtColumn:1 statement:stmt];
  8535. lastName = [self textAtColumn:2 statement:stmt];
  8536. addr1 = [self textAtColumn:3 statement:stmt];
  8537. addr2 = [self textAtColumn:4 statement:stmt];
  8538. zipcode = [self textAtColumn:5 statement:stmt];
  8539. cardType = [self textAtColumn:6 statement:stmt];
  8540. cardNumber = [self textAtColumn:7 statement:stmt];
  8541. securityCode = [self textAtColumn:8 statement:stmt];
  8542. month = [self textAtColumn:9 statement:stmt];
  8543. year = [self textAtColumn:10 statement:stmt];
  8544. city = [self textAtColumn:11 statement:stmt];
  8545. state = [self textAtColumn:12 statement:stmt];
  8546. }];
  8547. NSString *required = @"true";
  8548. // setting
  8549. NSDictionary *setting = params[@"setting"];
  8550. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8551. if ([requiredNumber integerValue]) {
  8552. required = @"true";
  8553. } else {
  8554. required = @"false";
  8555. }
  8556. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8557. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8558. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8559. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8560. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8561. NSString *type = [self textAtColumn:1 statement:stmt];
  8562. NSMutableDictionary *val = @{
  8563. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8564. @"value" : type,
  8565. @"value_id" : type_id
  8566. }.mutableCopy;
  8567. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  8568. NSDictionary *sub_item = @{
  8569. @"count" : @(3),
  8570. @"item_0" : @{
  8571. @"aname" : @"choose",
  8572. @"control" : @"multi_action",
  8573. @"count" : @(1),
  8574. @"item_0" : @{
  8575. @"aname" : @"Same as customer",
  8576. @"key_map" : @{
  8577. @"credit_card_address1" : @"customer_address1",
  8578. @"credit_card_address2" : @"customer_address2",
  8579. @"credit_card_city" : @"customer_city",
  8580. @"credit_card_first_name" : @"customer_first_name",
  8581. @"credit_card_last_name" : @"customer_last_name",
  8582. @"credit_card_state" : @"customer_state",
  8583. @"credit_card_zipcode" : @"customer_zipcode"
  8584. },
  8585. @"type" : @"pull"
  8586. }
  8587. },
  8588. @"item_1" : @{
  8589. @"aname" : @"",
  8590. @"color" : @"red",
  8591. @"control" : @"text",
  8592. @"name" : @"",
  8593. @"value" : @"USA Credit cards only"
  8594. },
  8595. @"item_2" : @{
  8596. @"aname" : @"Fill",
  8597. @"cadedate" : @{
  8598. @"count" : @(2),
  8599. @"val_0" : @{
  8600. @"check" : @(1),
  8601. @"sub_item" : @{
  8602. @"count" : @(11),
  8603. @"item_0" : @{
  8604. @"aname" : @"Type",
  8605. @"cadedate" : @{
  8606. @"count" : @(2),
  8607. @"val_0" : @{
  8608. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8609. @"value" : @"VISA",
  8610. @"value_id" : @"VISA"/*@(0)*/
  8611. },
  8612. @"val_1" : @{
  8613. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8614. @"value" : @"MASTER CARD",
  8615. @"value_id" : @"MASTER CARD"/*@(1)*/
  8616. }
  8617. },
  8618. @"control" : @"enum",
  8619. @"name" : @"credit_card_type",
  8620. @"required" : @"true",
  8621. @"single_select" : @"true"
  8622. },
  8623. @"item_1" : @{
  8624. @"aname" : @"Number",
  8625. @"control" : @"edit",
  8626. @"keyboard" : @"int",
  8627. @"length" : @"16",
  8628. @"name" : @"credit_card_number",
  8629. @"required" : @"true",
  8630. @"value" : cardNumber
  8631. },
  8632. @"item_10" : @{
  8633. @"aname" : @"State",
  8634. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8635. @"control" : @"enum",
  8636. @"enum" : @"true",
  8637. @"name" : @"credit_card_state",
  8638. @"required" : @"true",
  8639. @"single_select" : @"true"
  8640. },
  8641. @"item_2" : @{
  8642. @"aname" : @"Expiration Date",
  8643. @"control" : @"monthpicker",
  8644. @"name" : @"credit_card_expiration",
  8645. @"required" : @"true",
  8646. @"type" : @"date",
  8647. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8648. },
  8649. @"item_3" : @{
  8650. @"aname" : @"Security Code",
  8651. @"control" : @"edit",
  8652. @"keyboard" : @"int",
  8653. @"length" : @"3",
  8654. @"name" : @"credit_card_security_code",
  8655. @"required" : @"true",
  8656. @"value" : securityCode
  8657. },
  8658. @"item_4" : @{
  8659. @"aname" : @"First Name",
  8660. @"control" : @"edit",
  8661. @"keyboard" : @"text",
  8662. @"name" : @"credit_card_first_name",
  8663. @"required" : @"true",
  8664. @"value" : firstName
  8665. },
  8666. @"item_5" : @{
  8667. @"aname" : @"Last Name",
  8668. @"control" : @"edit",
  8669. @"keyboard" : @"text",
  8670. @"name" : @"credit_card_last_name",
  8671. @"required" : @"true",
  8672. @"value" : lastName
  8673. },
  8674. @"item_6" : @{
  8675. @"aname" : @"Address 1",
  8676. @"control" : @"edit",
  8677. @"keyboard" : @"text",
  8678. @"name" : @"credit_card_address1",
  8679. @"required" : @"true",
  8680. @"value" : addr1
  8681. },
  8682. @"item_7" : @{
  8683. @"aname" : @"Address 2",
  8684. @"control" : @"edit",
  8685. @"keyboard" : @"text",
  8686. @"name" : @"credit_card_address2",
  8687. @"value" : addr2
  8688. },
  8689. @"item_8" : @{
  8690. @"aname" : @"zip code",
  8691. @"control" : @"edit",
  8692. @"keyboard" : @"text",
  8693. @"name" : @"credit_card_zipcode",
  8694. @"required" : @"true",
  8695. @"value" : zipcode
  8696. },
  8697. @"item_9" : @{
  8698. @"aname" : @"City",
  8699. @"control" : @"edit",
  8700. @"keyboard" : @"text",
  8701. @"name" : @"credit_card_city",
  8702. @"required" : @"true",
  8703. @"value" : city
  8704. }
  8705. },
  8706. @"value" : @"Fill Now",
  8707. @"value_id" : @""
  8708. },
  8709. @"val_1" : @{
  8710. @"check" : @(0),
  8711. @"value" : @"Fill Later",
  8712. @"value_id" : @""
  8713. }
  8714. },
  8715. @"control" : @"enum",
  8716. @"name" : @"",
  8717. @"single_select" : @"true"
  8718. }
  8719. };
  8720. [val setObject:sub_item forKey:@"sub_item"];
  8721. }
  8722. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8723. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8724. }];
  8725. // "section_2"
  8726. NSMutableDictionary *dic = @{
  8727. @"count" : @(1),
  8728. @"item_0" : @{
  8729. @"aname" : @"Payment",
  8730. @"required" : required,
  8731. @"cadedate" : cadedate,
  8732. // @{
  8733. // @"count" : @(6),
  8734. // @"val_3" : @{
  8735. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8736. // @"value" : @"Check",
  8737. // @"value_id" : @"Check"
  8738. // },
  8739. // @"val_2" : @{
  8740. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8741. // @"value" : @"Cash",
  8742. // @"value_id" : @"Cash"
  8743. // },
  8744. // @"val_1" : @{
  8745. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8746. // @"value" : @"NET 60",
  8747. // @"value_id" : @"NET 30"
  8748. // },
  8749. // @"val_4" : @{
  8750. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8751. // @"value" : @"Wire Transfer",
  8752. // @"value_id" : @"Wire Transfer"
  8753. // },
  8754. // @"val_0" : @{
  8755. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8756. // @"sub_item" : @{
  8757. // @"count" : @(3),
  8758. // @"item_0" : @{
  8759. // @"aname" : @"choose",
  8760. // @"control" : @"multi_action",
  8761. // @"count" : @(1),
  8762. // @"item_0" : @{
  8763. // @"aname" : @"Same as customer",
  8764. // @"key_map" : @{
  8765. // @"credit_card_address1" : @"customer_address1",
  8766. // @"credit_card_address2" : @"customer_address2",
  8767. // @"credit_card_city" : @"customer_city",
  8768. // @"credit_card_first_name" : @"customer_first_name",
  8769. // @"credit_card_last_name" : @"customer_last_name",
  8770. // @"credit_card_state" : @"customer_state",
  8771. // @"credit_card_zipcode" : @"customer_zipcode"
  8772. // },
  8773. // @"type" : @"pull"
  8774. // }
  8775. // },
  8776. // @"item_1" : @{
  8777. // @"aname" : @"",
  8778. // @"color" : @"red",
  8779. // @"control" : @"text",
  8780. // @"name" : @"",
  8781. // @"value" : @"USA Credit cards only"
  8782. // },
  8783. // @"item_2" : @{
  8784. // @"aname" : @"Fill",
  8785. // @"cadedate" : @{
  8786. // @"count" : @(2),
  8787. // @"val_0" : @{
  8788. // @"check" : @(1),
  8789. // @"sub_item" : @{
  8790. // @"count" : @(11),
  8791. // @"item_0" : @{
  8792. // @"aname" : @"Type",
  8793. // @"cadedate" : @{
  8794. // @"count" : @(2),
  8795. // @"val_0" : @{
  8796. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8797. // @"value" : @"VISA",
  8798. // @"value_id" : @(0)
  8799. // },
  8800. // @"val_1" : @{
  8801. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8802. // @"value" : @"MASTER CARD",
  8803. // @"value_id" : @(1)
  8804. // }
  8805. // },
  8806. // @"control" : @"enum",
  8807. // @"name" : @"credit_card_type",
  8808. // @"required" : @"true",
  8809. // @"single_select" : @"true"
  8810. // },
  8811. // @"item_1" : @{
  8812. // @"aname" : @"Number",
  8813. // @"control" : @"edit",
  8814. // @"keyboard" : @"int",
  8815. // @"length" : @"16",
  8816. // @"name" : @"credit_card_number",
  8817. // @"required" : @"true",
  8818. // @"value" : cardNumber
  8819. // },
  8820. // @"item_10" : @{
  8821. // @"aname" : @"State",
  8822. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8823. // @"control" : @"enum",
  8824. // @"enum" : @"true",
  8825. // @"name" : @"credit_card_state",
  8826. // @"required" : @"true",
  8827. // @"single_select" : @"true"
  8828. // },
  8829. // @"item_2" : @{
  8830. // @"aname" : @"Expiration Date",
  8831. // @"control" : @"monthpicker",
  8832. // @"name" : @"credit_card_expiration",
  8833. // @"required" : @"true",
  8834. // @"type" : @"date",
  8835. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8836. // },
  8837. // @"item_3" : @{
  8838. // @"aname" : @"Security Code",
  8839. // @"control" : @"edit",
  8840. // @"keyboard" : @"int",
  8841. // @"length" : @"3",
  8842. // @"name" : @"credit_card_security_code",
  8843. // @"required" : @"true",
  8844. // @"value" : securityCode
  8845. // },
  8846. // @"item_4" : @{
  8847. // @"aname" : @"First Name",
  8848. // @"control" : @"edit",
  8849. // @"keyboard" : @"text",
  8850. // @"name" : @"credit_card_first_name",
  8851. // @"required" : @"true",
  8852. // @"value" : firstName
  8853. // },
  8854. // @"item_5" : @{
  8855. // @"aname" : @"Last Name",
  8856. // @"control" : @"edit",
  8857. // @"keyboard" : @"text",
  8858. // @"name" : @"credit_card_last_name",
  8859. // @"required" : @"true",
  8860. // @"value" : lastName
  8861. // },
  8862. // @"item_6" : @{
  8863. // @"aname" : @"Address 1",
  8864. // @"control" : @"edit",
  8865. // @"keyboard" : @"text",
  8866. // @"name" : @"credit_card_address1",
  8867. // @"required" : @"true",
  8868. // @"value" : addr1
  8869. // },
  8870. // @"item_7" : @{
  8871. // @"aname" : @"Address 2",
  8872. // @"control" : @"edit",
  8873. // @"keyboard" : @"text",
  8874. // @"name" : @"credit_card_address2",
  8875. // @"value" : addr2
  8876. // },
  8877. // @"item_8" : @{
  8878. // @"aname" : @"zip code",
  8879. // @"control" : @"edit",
  8880. // @"keyboard" : @"text",
  8881. // @"name" : @"credit_card_zipcode",
  8882. // @"required" : @"true",
  8883. // @"value" : zipcode
  8884. // },
  8885. // @"item_9" : @{
  8886. // @"aname" : @"City",
  8887. // @"control" : @"edit",
  8888. // @"keyboard" : @"text",
  8889. // @"name" : @"credit_card_city",
  8890. // @"required" : @"true",
  8891. // @"value" : city
  8892. // }
  8893. // },
  8894. // @"value" : @"Fill Now",
  8895. // @"value_id" : @""
  8896. // },
  8897. // @"val_1" : @{
  8898. // @"check" : @(0),
  8899. // @"value" : @"Fill Later",
  8900. // @"value_id" : @""
  8901. // }
  8902. // },
  8903. // @"control" : @"enum",
  8904. // @"name" : @"",
  8905. // @"single_select" : @"true"
  8906. // }
  8907. // },
  8908. // @"value" : @"Visa/Master",
  8909. // @"value_id" : @"Credit Card"
  8910. // },
  8911. // @"val_5" : @{
  8912. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8913. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8914. // @"value_id" : @"FOLLOW EXISTING"
  8915. // }
  8916. // },
  8917. @"control" : @"enum",
  8918. @"name" : @"paymentType",
  8919. @"single_select" : @"true"
  8920. },
  8921. @"title" : @"Payment Information"
  8922. }.mutableCopy;
  8923. NSNumber *hide = setting[@"PaymentInformationHide"];
  8924. [dic setValue:hide forKey:@"hide"];
  8925. return dic;
  8926. }
  8927. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8928. // params
  8929. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8930. orderCode = [self translateSingleQuote:orderCode];
  8931. // 缺货检查
  8932. 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];
  8933. __block BOOL outOfStock = NO;
  8934. sqlite3 *database = db;
  8935. if (!db) {
  8936. database = [iSalesDB get_db];
  8937. }
  8938. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8939. int availability = sqlite3_column_int(stmt, 0);
  8940. int item_qty = sqlite3_column_int(stmt, 1);
  8941. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8942. }];
  8943. if (!db) {
  8944. [iSalesDB close_db:database];
  8945. }
  8946. return outOfStock;
  8947. }
  8948. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8949. {
  8950. assert(params[@"user"]!=nil);
  8951. assert(params[@"contact_id"]!=nil);
  8952. assert(params[@"can_create_backorder"]!=nil);
  8953. sqlite3 *db = [iSalesDB get_db];
  8954. // params
  8955. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8956. orderCode = [self translateSingleQuote:orderCode];
  8957. // 缺货检查
  8958. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8959. if (![params[@"can_create_backorder"] boolValue]) {
  8960. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8961. if (out_of_stock) {
  8962. [iSalesDB close_db:db];
  8963. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8964. [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"];
  8965. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8966. return [RAConvertor dict2data:resultDic];
  8967. }
  8968. }
  8969. // UISetting
  8970. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8971. NSString *cachefolder = [paths objectAtIndex:0];
  8972. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8973. NSData* json =nil;
  8974. json=[NSData dataWithContentsOfFile:img_cache];
  8975. NSError *error=nil;
  8976. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8977. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8978. [params setObject:setting forKey:@"setting"];
  8979. int section_count = 0;
  8980. // 0 Order Type 1 Shipping Method 2 Payment Information
  8981. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8982. // 0 Order Type
  8983. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8984. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8985. [ret setValue:order_type_dic forKey:key0];
  8986. // 1 Shipping Method
  8987. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8988. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8989. [ret setValue:shipping_method_dic forKey:key1];
  8990. // 2 Payment Information
  8991. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8992. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8993. [ret setValue:payment_info_dic forKey:key2];
  8994. // 3 Customer
  8995. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8996. NSDictionary *customer_dic = [self customerDic:params db:db];
  8997. [ret setValue:customer_dic forKey:key3];
  8998. // 4 Ship To
  8999. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9000. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9001. [ret setValue:ship_to_dic forKey:key4];
  9002. // 5 Ship From
  9003. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9004. if (![shipFromDisable integerValue]) {
  9005. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9006. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9007. [ret setValue:ship_from_dic forKey:key5];
  9008. }
  9009. // 6 Freight Bill To
  9010. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9011. if (![freightBillToDisable integerValue]) {
  9012. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9013. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9014. [ret setValue:freight_bill_to forKey:key6];
  9015. }
  9016. // 7 Merchandise Bill To
  9017. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9018. if (![merchandiseBillToDisable integerValue]) {
  9019. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9020. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9021. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9022. }
  9023. // 8 Return To
  9024. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9025. if (![returnToDisable integerValue]) {
  9026. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9027. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9028. [ret setValue:return_to_dic forKey:key8];
  9029. }
  9030. // 9 Model Information
  9031. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9032. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9033. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9034. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9035. [ret setValue:model_info_dic forKey:key9];
  9036. // 10 Remarks Content
  9037. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9038. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9039. [ret setValue:remarks_content_dic forKey:key10];
  9040. // 11 Order Total
  9041. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9042. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9043. [ret setValue:order_total_dic forKey:key11];
  9044. // 12 Signature
  9045. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9046. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9047. [ret setValue:sign_dic forKey:key12];
  9048. [ret setValue:@(section_count) forKey:@"section_count"];
  9049. [iSalesDB close_db:db];
  9050. return [RAConvertor dict2data:ret];
  9051. // return nil;
  9052. }
  9053. #pragma mark addr editor
  9054. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9055. NSMutableDictionary *new_item = [item mutableCopy];
  9056. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9057. return new_item;
  9058. }
  9059. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9060. {
  9061. // "is_subaction" = true;
  9062. // orderCode = MOB1608240002;
  9063. // password = 123456;
  9064. // "subaction_tag" = 1;
  9065. // user = EvanK;
  9066. // {
  9067. // "is_subaction" = true;
  9068. // orderCode = MOB1608240002;
  9069. // password = 123456;
  9070. // "refresh_trigger" = zipcode;
  9071. // "subaction_tag" = 1;
  9072. // user = EvanK;
  9073. // }
  9074. NSString *country_code = nil;
  9075. NSString *zipCode = nil;
  9076. NSString *stateCode = nil;
  9077. NSString *city = nil;
  9078. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9079. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9080. NSString *code_id = params[@"country"];
  9081. country_code = [self countryCodeByid:code_id];
  9082. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9083. NSString *zip_code = params[@"zipcode"];
  9084. // 剔除全部为空格
  9085. int spaceCount = 0;
  9086. for (int i = 0; i < zip_code.length; i++) {
  9087. if ([zip_code characterAtIndex:i] == ' ') {
  9088. spaceCount++;
  9089. }
  9090. }
  9091. if (spaceCount == zip_code.length) {
  9092. zip_code = @"";
  9093. }
  9094. zipCode = zip_code;
  9095. if (zipCode.length > 0) {
  9096. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9097. country_code = [dic valueForKey:@"country_code"];
  9098. if (!country_code) {
  9099. // country_code = @"US";
  9100. NSString *code_id = params[@"country"];
  9101. country_code = [self countryCodeByid:code_id];
  9102. }
  9103. stateCode = [dic valueForKey:@"state_code"];
  9104. if(!stateCode.length) {
  9105. stateCode = params[@"state"];
  9106. }
  9107. city = [dic valueForKey:@"city"];
  9108. if (!city.length) {
  9109. city = params[@"city"];
  9110. }
  9111. // zip code
  9112. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9113. // [zipDic setValue:zipCode forKey:@"value"];
  9114. // [section_0 setValue:zipDic forKey:@"item_11"];
  9115. } else {
  9116. NSString *code_id = params[@"country"];
  9117. country_code = [self countryCodeByid:code_id];
  9118. stateCode = params[@"state"];
  9119. city = params[@"city"];
  9120. }
  9121. }
  9122. }
  9123. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9124. // [ret removeObjectForKey:@"up_params"];
  9125. [ret setObject:@"New Address" forKey:@"title"];
  9126. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9127. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9128. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9129. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9130. // [country_dic removeObjectForKey:@"refresh"];
  9131. // [country_dic removeObjectForKey:@"restore"];
  9132. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9133. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9134. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9135. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9136. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9137. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9138. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9139. // [zip_code_dic removeObjectForKey:@"refresh"];
  9140. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9141. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9142. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9143. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9144. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9145. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9146. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9147. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9148. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9149. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9150. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9151. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9152. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9153. // country
  9154. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9155. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9156. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9157. // state
  9158. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9159. // NSDictionary *tmpDic = @{
  9160. // @"value" : @"Other",
  9161. // @"value_id" : @"",
  9162. // @"check" : [NSNumber numberWithInteger:0]
  9163. // };
  9164. //
  9165. // for (int i = 0; i < allState.allKeys.count; i++) {
  9166. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9167. //
  9168. // NSDictionary *tmp = allState[key];
  9169. // [allState setValue:tmpDic forKey:key];
  9170. // tmpDic = tmp;
  9171. // }
  9172. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9173. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9174. [ret setValue:new_section_0 forKey:@"section_0"];
  9175. return [RAConvertor dict2data:ret];
  9176. }
  9177. #pragma mark save addr
  9178. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9179. {
  9180. // NSString *companyName = [self valueInParams:params key:@"company"];
  9181. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9182. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9183. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9184. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9185. // NSString *countryId = [self valueInParams:params key:@"country"];
  9186. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9187. // NSString *city = [self valueInParams:params key:@"city"];
  9188. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9189. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9190. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9191. // NSString *phone = [self valueInParams:params key:@"phone"];
  9192. // NSString *fax = [self valueInParams:params key:@"fax"];
  9193. // NSString *email = [self valueInParams:params key:@"email"];
  9194. return [self offline_saveContact:params update:NO isCustomer:NO];
  9195. }
  9196. #pragma mark cancel order
  9197. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9198. {
  9199. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9200. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9201. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9202. if (order_id.length) {
  9203. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9204. }
  9205. int ret = [iSalesDB execSql:sql];
  9206. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9207. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9208. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9209. // [dic setValue:@"160409" forKey:@"min_ver"];
  9210. return [RAConvertor dict2data:dic];
  9211. }
  9212. #pragma mark sign order
  9213. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9214. {
  9215. //参考 offline_saveBusinesscard
  9216. DebugLog(@"sign order params: %@",params);
  9217. // orderCode = MOB1608240002;
  9218. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9219. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9220. orderCode = [self translateSingleQuote:orderCode];
  9221. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9222. picPath = [self translateSingleQuote:picPath];
  9223. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9224. int ret = [iSalesDB execSql:sql];
  9225. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9226. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9227. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9228. // [dic setValue:@"160409" forKey:@"min_ver"];
  9229. return [RAConvertor dict2data:dic];
  9230. }
  9231. #pragma mark save order
  9232. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9233. NSString *ret = [self valueInParams:params key:key];
  9234. if (translate) {
  9235. ret = [self translateSingleQuote:ret];
  9236. }
  9237. return ret;
  9238. }
  9239. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9240. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9241. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9242. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9243. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9244. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9245. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9246. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9247. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9248. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9249. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9250. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9251. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9252. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9253. if (![total_price isEqualToString:@""]) {
  9254. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9255. } else {
  9256. total_price = @"";
  9257. }
  9258. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9259. if ([paymentsAndCredits isEqualToString:@""]) {
  9260. paymentsAndCredits = @"";
  9261. } else {
  9262. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9263. }
  9264. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9265. if ([handling_fee_value isEqualToString:@""]) {
  9266. handling_fee_value = @"";
  9267. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9268. handling_fee_value = @"";
  9269. } else {
  9270. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9271. }
  9272. NSString *handling_fee_placeholder = handling_fee_value;
  9273. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9274. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9275. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9276. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9277. NSString *lift_gate_placeholder = @"";
  9278. if ([lift_gate_value isEqualToString:@""]) {
  9279. lift_gate_placeholder = @"";
  9280. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9281. lift_gate_placeholder = @"";
  9282. } else {
  9283. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9284. }
  9285. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9286. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9287. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9288. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9289. 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];
  9290. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9291. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9292. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9293. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9294. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9295. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9296. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9297. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9298. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9299. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9300. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9301. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9302. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9303. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9304. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9305. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9306. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9307. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9308. if (!number_nil) {
  9309. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9310. }
  9311. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9312. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9313. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9314. if (!security_code_nil) {
  9315. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9316. }
  9317. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9318. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9319. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9320. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9321. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9322. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9323. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9324. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9325. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9326. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9327. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9328. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9329. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9330. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9331. NSString *contact_id = customer_cid;
  9332. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9333. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9334. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9335. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9336. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9337. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9338. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9339. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9340. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9341. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9342. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9343. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9344. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9345. //
  9346. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9347. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9348. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9349. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9350. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9351. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9352. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9353. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9354. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9355. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9356. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9357. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9358. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9359. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9360. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9361. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9362. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9363. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9364. 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];
  9365. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9366. if (receive_cid.length) {
  9367. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9368. }
  9369. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9370. if (receive_name.length) {
  9371. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9372. }
  9373. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9374. if (receive_ext.length) {
  9375. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9376. }
  9377. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9378. if (receive_contact.length) {
  9379. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9380. }
  9381. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9382. if (receive_phone.length) {
  9383. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9384. }
  9385. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9386. if (receive_email.length) {
  9387. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9388. }
  9389. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9390. if (receive_fax.length) {
  9391. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9392. }
  9393. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9394. if (sender_cid.length) {
  9395. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9396. }
  9397. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9398. if (sender_name.length) {
  9399. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9400. }
  9401. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9402. if (sender_ext.length) {
  9403. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9404. }
  9405. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9406. if(sender_contact.length) {
  9407. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9408. }
  9409. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9410. if (sender_phone.length) {
  9411. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9412. }
  9413. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9414. if (sender_fax.length) {
  9415. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9416. }
  9417. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9418. if (sender_email.length) {
  9419. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9420. }
  9421. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9422. if (shipping_billto_cid.length) {
  9423. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9424. }
  9425. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9426. if (shipping_billto_name.length) {
  9427. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9428. }
  9429. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9430. if (shipping_billto_ext.length) {
  9431. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9432. }
  9433. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9434. if (shipping_billto_contact.length) {
  9435. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9436. }
  9437. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9438. if (shipping_billto_phone.length) {
  9439. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9440. }
  9441. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9442. if (shipping_billto_fax.length) {
  9443. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9444. }
  9445. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9446. if (shipping_billto_email.length) {
  9447. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9448. }
  9449. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9450. if (billing_cid.length) {
  9451. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9452. }
  9453. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9454. if (billing_name.length) {
  9455. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9456. }
  9457. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9458. if (billing_ext.length) {
  9459. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9460. }
  9461. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9462. if (billing_contact.length) {
  9463. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9464. }
  9465. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9466. if (billing_phone.length) {
  9467. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9468. }
  9469. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9470. if (billing_fax.length) {
  9471. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9472. }
  9473. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9474. if (billing_email.length) {
  9475. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9476. }
  9477. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9478. if (returnto_cid.length) {
  9479. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9480. }
  9481. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9482. if (returnto_name.length) {
  9483. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9484. }
  9485. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9486. if (returnto_ext.length) {
  9487. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9488. }
  9489. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9490. if (returnto_contact.length) {
  9491. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9492. }
  9493. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9494. if (returnto_phone.length) {
  9495. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9496. }
  9497. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9498. if (returnto_fax.length) {
  9499. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9500. }
  9501. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9502. if (returnto_email.length) {
  9503. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9504. }
  9505. NSString *order_status = @"status = 1,";
  9506. if (submit) {
  9507. order_status = @"status = -11,";
  9508. }
  9509. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9510. NSString *sync_sql = @"";
  9511. if (submit) {
  9512. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9513. [param setValue:sales_rep forKey:@"sales_rep"];
  9514. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9515. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9516. }
  9517. 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];
  9518. DebugLog(@"save order contactSql: %@",contactSql);
  9519. DebugLog(@"save order orderSql: %@",orderSql);
  9520. // int contact_ret = [iSalesDB execSql:contactSql];
  9521. int order_ret = [iSalesDB execSql:orderSql];
  9522. int ret = order_ret;
  9523. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9524. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9525. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9526. // [dic setValue:@"160409" forKey:@"min_ver"];
  9527. [dic setObject:so_id forKey:@"so#"];
  9528. return [RAConvertor dict2data:dic];
  9529. }
  9530. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9531. {
  9532. // id foo = nil;
  9533. // NSMutableArray *a = @[].mutableCopy;
  9534. // [a addObject:foo];
  9535. return [self saveorder:param submit:NO];
  9536. }
  9537. #pragma mark add to cart by name
  9538. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  9539. {
  9540. NSString *orderCode = [params objectForKey:@"orderCode"];
  9541. NSDictionary *newParams = @{
  9542. @"product_id" : params[@"product_id_string"],
  9543. @"orderCode" : orderCode,
  9544. @"can_create_backorder":params[@"can_create_backorder"]
  9545. };
  9546. return [self offline_add2cart:[newParams mutableCopy]];
  9547. }
  9548. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  9549. {
  9550. NSString *orderCode = [params objectForKey:@"orderCode"];
  9551. NSString *upccode = [params objectForKey:@"upc_code"];
  9552. // product_name = [self translateSingleQuote:product_name];
  9553. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9554. // bool search_upc = [params[@"search_upc"] boolValue];
  9555. sqlite3 *db = [iSalesDB get_db];
  9556. __block NSMutableString *product_id_string = [NSMutableString string];
  9557. // NSString *name = [product_name_array objectAtIndex:i];
  9558. NSString *sql =nil;
  9559. 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];
  9560. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9561. int product_id = sqlite3_column_int(stmt, 0);
  9562. [product_id_string appendFormat:@"%d",product_id];
  9563. }];
  9564. if (!orderCode) {
  9565. orderCode = @"";
  9566. }
  9567. NSDictionary *newParams = @{
  9568. @"product_id" : product_id_string,
  9569. @"orderCode" : orderCode,
  9570. @"can_create_backorder":params[@"can_create_backorder"]
  9571. };
  9572. [iSalesDB close_db:db];
  9573. return [self offline_add2cart:[newParams mutableCopy]];
  9574. }
  9575. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9576. {
  9577. NSString *orderCode = [params objectForKey:@"orderCode"];
  9578. NSString *product_name = [params objectForKey:@"product_name"];
  9579. product_name = [self translateSingleQuote:product_name];
  9580. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9581. bool search_upc = [params[@"search_upc"] boolValue];
  9582. sqlite3 *db = [iSalesDB get_db];
  9583. __block NSMutableString *product_id_string = [NSMutableString string];
  9584. for (int i = 0; i < product_name_array.count; i++) {
  9585. NSString *name = [product_name_array objectAtIndex:i];
  9586. NSString *sql =nil;
  9587. if(search_upc)
  9588. 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];
  9589. else
  9590. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9591. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9592. int product_id = sqlite3_column_int(stmt, 0);
  9593. if (i == product_name_array.count - 1) {
  9594. [product_id_string appendFormat:@"%d",product_id];
  9595. } else {
  9596. [product_id_string appendFormat:@"%d,",product_id];
  9597. }
  9598. }];
  9599. }
  9600. if (!orderCode) {
  9601. orderCode = @"";
  9602. }
  9603. NSDictionary *newParams = @{
  9604. @"product_id" : product_id_string,
  9605. @"orderCode" : orderCode,
  9606. @"can_create_backorder":params[@"can_create_backorder"]
  9607. };
  9608. [iSalesDB close_db:db];
  9609. return [self offline_add2cart:[newParams mutableCopy]];
  9610. }
  9611. #pragma mark reset order
  9612. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9613. {
  9614. // UIApplication * app = [UIApplication sharedApplication];
  9615. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9616. [iSalesDB disable_trigger];
  9617. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9618. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9619. [iSalesDB enable_trigger];
  9620. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9621. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9622. return [RAConvertor dict2data:dic];
  9623. }
  9624. #pragma mark submit order
  9625. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9626. {
  9627. return [self saveorder:params submit:YES];
  9628. }
  9629. #pragma mark copy order
  9630. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9631. {
  9632. NSMutableDictionary *ret = @{}.mutableCopy;
  9633. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9634. sqlite3 *db = [iSalesDB get_db];
  9635. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9636. // 首先查看联系人是否active
  9637. 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];
  9638. __block int customer_is_active = 1;
  9639. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9640. customer_is_active = sqlite3_column_int(stmt, 0);
  9641. }];
  9642. if (!customer_is_active) {
  9643. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9644. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9645. [iSalesDB close_db:db];
  9646. return [RAConvertor dict2data:ret];
  9647. }
  9648. // new order
  9649. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9650. NSString *new_order_code = [self get_offline_soid:db];
  9651. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9652. user = [self translateSingleQuote:user];
  9653. 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
  9654. __block int result = 1;
  9655. result = [iSalesDB execSql:insert_order_sql db:db];
  9656. if (!result) {
  9657. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9658. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9659. [iSalesDB close_db:db];
  9660. return [RAConvertor dict2data:ret];
  9661. }
  9662. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9663. // __block NSString *product_id = @"";
  9664. __weak typeof(self) weakSelf = self;
  9665. 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];
  9666. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9667. int is_active = sqlite3_column_int(stmt, 1);
  9668. if (is_active) {
  9669. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9670. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9671. 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];
  9672. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9673. }
  9674. }];
  9675. // product_id = [product_id substringFromIndex:1];
  9676. //
  9677. // [self offline_add2cart:@{}.mutableCopy];
  9678. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9679. [iSalesDB close_db:db];
  9680. if (result) {
  9681. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9682. } else {
  9683. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9684. }
  9685. [ret setObject:new_order_code forKey:@"so_id"];
  9686. return [RAConvertor dict2data:ret];
  9687. }
  9688. #pragma mark move wish list to cart
  9689. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9690. assert(params[@"can_create_backorder"]!=nil);
  9691. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9692. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9693. NSString *collectId = params[@"collectId"];
  9694. 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];
  9695. __block NSString *product_id = @"";
  9696. __block NSString *qty = @"";
  9697. __block int number_of_outOfStock = 0;
  9698. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9699. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9700. int productId = sqlite3_column_int(stmt, 0);
  9701. int item_qty = sqlite3_column_int(stmt, 1);
  9702. int availability = sqlite3_column_int(stmt, 2);
  9703. int _id = sqlite3_column_int(stmt, 3);
  9704. if (![params[@"can_create_backorder"] boolValue]) {
  9705. // 库存小于购买量为缺货
  9706. if (availability >= item_qty) {
  9707. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9708. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9709. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9710. } else {
  9711. number_of_outOfStock++;
  9712. }
  9713. } else {
  9714. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9715. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9716. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9717. }
  9718. }];
  9719. NSMutableDictionary *retDic = nil;
  9720. if (![params[@"can_create_backorder"] boolValue]) {
  9721. if (delete_collectId.count == 0) {
  9722. retDic = [NSMutableDictionary dictionary];
  9723. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9724. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9725. return [RAConvertor dict2data:retDic];
  9726. }
  9727. }
  9728. if (product_id.length > 1) {
  9729. product_id = [product_id substringFromIndex:1];
  9730. }
  9731. if (qty.length > 1) {
  9732. qty = [qty substringFromIndex:1];
  9733. }
  9734. NSString *orderCode = params[@"orderCode"];
  9735. if (!orderCode) {
  9736. orderCode = @"";
  9737. }
  9738. NSDictionary *newParams = @{
  9739. @"product_id" : product_id,
  9740. @"orderCode" : orderCode,
  9741. @"qty" : qty,
  9742. @"can_create_backorder":params[@"can_create_backorder"]
  9743. };
  9744. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9745. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9746. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9747. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9748. NSInteger ret = [wish_return[@"result"] intValue];
  9749. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9750. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9751. }
  9752. if (![params[@"can_create_backorder"] boolValue]) {
  9753. if (number_of_outOfStock > 0) {
  9754. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9755. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9756. }
  9757. }
  9758. return [RAConvertor dict2data:retDic];
  9759. }
  9760. #pragma mark - portfolio
  9761. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9762. // assert(params[@"contact_id"]!=nil);
  9763. assert(params[@"user"]!=nil);
  9764. assert(params[@"can_see_price"]!=nil);
  9765. int sort = [[params valueForKey:@"sort"] intValue];
  9766. int offset = [[params valueForKey:@"offset"] intValue];
  9767. int limit = [[params valueForKey:@"limit"] intValue];
  9768. NSString *orderBy = @"";
  9769. switch (sort) {
  9770. case 0:{
  9771. orderBy = @"p.modify_time desc";
  9772. }
  9773. break;
  9774. case 1:{
  9775. orderBy = @"modify_time asc";
  9776. }
  9777. break;
  9778. case 2:{
  9779. orderBy = @"p.name asc";
  9780. }
  9781. break;
  9782. case 3:{
  9783. orderBy = @"p.name desc";
  9784. }
  9785. break;
  9786. case 4:{
  9787. orderBy = @"p.description asc";
  9788. }
  9789. break;
  9790. case 5: {
  9791. orderBy = @"m.default_category asc";
  9792. }
  9793. default:
  9794. break;
  9795. }
  9796. // 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];
  9797. 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];
  9798. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9799. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9800. sqlite3 *db = [iSalesDB get_db];
  9801. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9802. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9803. int product_id = sqlite3_column_int(stmt, 0);
  9804. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9805. NSString *name = [self textAtColumn:1 statement:stmt];
  9806. NSString *description = [self textAtColumn:2 statement:stmt];
  9807. double price = sqlite3_column_double(stmt, 3);
  9808. double discount = sqlite3_column_double(stmt,4);
  9809. int qty = sqlite3_column_int(stmt, 5);
  9810. int percentage = sqlite3_column_int(stmt, 6);
  9811. int item_id = sqlite3_column_int(stmt, 7);
  9812. // int fashion_id = sqlite3_column_int(stmt, 8);
  9813. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9814. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9815. double percent = sqlite3_column_double(stmt, 11);
  9816. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9817. if ([price_null isEqualToString:@"null"]) {
  9818. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9819. }
  9820. NSMutableDictionary *item = @{
  9821. @"linenotes": line_note,
  9822. @"check": @(1),
  9823. @"product_id": product_id_string,
  9824. @"available_qty": @(qty),
  9825. @"available_percent" : @(percent),
  9826. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9827. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9828. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9829. @"img": img_path,
  9830. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9831. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9832. }.mutableCopy;
  9833. if (percentage) {
  9834. [item removeObjectForKey:@"available_qty"];
  9835. } else {
  9836. [item removeObjectForKey:@"available_percent"];
  9837. }
  9838. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9839. if ([qty_null isEqualToString:@"null"]) {
  9840. [item removeObjectForKey:@"available_qty"];
  9841. }
  9842. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9843. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9844. }];
  9845. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9846. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9847. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9848. }
  9849. [iSalesDB close_db:db];
  9850. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9851. [dic setValue:@"" forKey:@"email_content"];
  9852. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9853. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9854. return [RAConvertor dict2data:dic];
  9855. }
  9856. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9857. __block int qty = 0;
  9858. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9859. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9860. qty = sqlite3_column_int(stmt, 0);
  9861. }];
  9862. return qty;
  9863. }
  9864. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9865. {
  9866. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9867. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9868. // NSMutableDictionary * values = params[@"replaceValue"];
  9869. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9870. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9871. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9872. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9873. NSString *pdf_path = @"";
  9874. if (direct) {
  9875. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9876. } else {
  9877. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9878. }
  9879. NSString *create_user = [self valueInParams:params key:@"user"];
  9880. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9881. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9882. // model info
  9883. // 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];
  9884. // V1.90 more color
  9885. 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];
  9886. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9887. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9888. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9889. sqlite3 *db = [iSalesDB get_db];
  9890. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9891. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9892. int product_id = sqlite3_column_int(stmt, 0);
  9893. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9894. double price = sqlite3_column_double(stmt, 1);
  9895. double discount = sqlite3_column_double(stmt,2);
  9896. int qty = sqlite3_column_int(stmt, 3);
  9897. int percentage = sqlite3_column_int(stmt, 4);
  9898. int item_id = sqlite3_column_int(stmt, 5);
  9899. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9900. double percent = sqlite3_column_double(stmt, 7);
  9901. int more_color = sqlite3_column_int(stmt, 8);
  9902. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9903. /* if ([price_null isEqualToString:@"null"]) {
  9904. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9905. }
  9906. */
  9907. [product_ids_string appendFormat:@"%@,",product_id_string];
  9908. // Regular Price
  9909. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9910. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9911. // QTY
  9912. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9913. if ([qty_null isEqualToString:@"null"]) {
  9914. // 查available
  9915. qty = [self model_QTY:product_id_string db:db];
  9916. }
  9917. if (percentage) {
  9918. qty = qty * percent / 100;
  9919. }
  9920. // Special Price
  9921. if ([price_null isEqualToString:@"null"]) {
  9922. // price = regular price
  9923. price = [regular_price_str doubleValue];
  9924. }
  9925. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9926. if (![discount_null isEqualToString:@"null"]) {
  9927. price = price * (1 - discount / 100.0);
  9928. }
  9929. NSMutableDictionary *item = @{
  9930. @"line_note": line_note,
  9931. @"product_id": product_id_string,
  9932. @"available_qty": @(qty),
  9933. @"more_color":@(more_color),
  9934. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9935. @"regular_price" : regular_price_str,
  9936. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9937. }.mutableCopy;
  9938. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9939. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9940. }];
  9941. [iSalesDB close_db:db];
  9942. if (product_ids_string.length > 0) {
  9943. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9944. }
  9945. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9946. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9947. return [RAConvertor dict2data:resultDictionary];
  9948. }
  9949. NSString *model_info = [RAConvertor dict2string:dic];
  9950. // 创建PDF
  9951. // 在preview情况下保存,则不需要新建了
  9952. if (direct) {
  9953. NSMutableDictionary *tear_sheet_params = params;
  9954. // if (tear_sheet_id) {
  9955. // tear_sheet_params = values;
  9956. // }
  9957. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9958. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9959. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9960. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9961. resultDictionary = pdfInfo.mutableCopy;
  9962. } else { // 创建PDF失败
  9963. return pdfData;
  9964. }
  9965. } else {
  9966. // pdf_path 就是本地路径
  9967. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9968. }
  9969. // 将文件移动到PDF Cache文件夹
  9970. NSString *newPath = [pdf_path lastPathComponent];
  9971. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9972. NSString *cachefolder = [paths objectAtIndex:0];
  9973. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9974. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9975. NSFileManager *fileManager = [NSFileManager defaultManager];
  9976. NSError *error = nil;
  9977. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9978. if (error) { // 移动文件失败
  9979. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9980. return [RAConvertor dict2data:resultDictionary];
  9981. }
  9982. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9983. pdf_path = [newPath lastPathComponent];
  9984. // 保存信息
  9985. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9986. NSString *off_params = [RAConvertor dict2string:params];
  9987. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9988. pdf_path = [self translateSingleQuote:pdf_path];
  9989. create_user = [self translateSingleQuote:create_user];
  9990. tear_note = [self translateSingleQuote:tear_note];
  9991. tear_name = [self translateSingleQuote:tear_name];
  9992. model_info = [self translateSingleQuote:model_info];
  9993. configureParams = [self translateSingleQuote:configureParams];
  9994. off_params = [self translateSingleQuote:off_params];
  9995. 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];
  9996. if (tear_sheet_id) {
  9997. int _id = [tear_sheet_id intValue];
  9998. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9999. }
  10000. int result = [iSalesDB execSql:save_pdf_sql];
  10001. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10002. //
  10003. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10004. if (remove_Item) {
  10005. // portfolioId
  10006. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10007. }
  10008. return [RAConvertor dict2data:resultDictionary];
  10009. }
  10010. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10011. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10012. }
  10013. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10014. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10015. }
  10016. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10017. int offset = [[params valueForKey:@"offset"] intValue];
  10018. int limit = [[params valueForKey:@"limit"] intValue];
  10019. NSString *keyword = [params valueForKey:@"keyWord"];
  10020. NSString *where = @"where is_delete is null or is_delete = 0";
  10021. if (keyword.length) {
  10022. keyword = [self translateSingleQuote:keyword];
  10023. 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];
  10024. }
  10025. 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
  10026. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10027. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10028. NSString *cachefolder = [paths objectAtIndex:0];
  10029. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10030. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10031. NSString *name = [self textAtColumn:0 statement:stmt];
  10032. NSString *note = [self textAtColumn:1 statement:stmt];
  10033. NSString *time = [self textAtColumn:2 statement:stmt];
  10034. NSString *user = [self textAtColumn:3 statement:stmt];
  10035. NSString *path = [self textAtColumn:4 statement:stmt];
  10036. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10037. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10038. path = [pdfFolder stringByAppendingPathComponent:path];
  10039. BOOL bdir=NO;
  10040. NSFileManager* fileManager = [NSFileManager defaultManager];
  10041. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10042. {
  10043. //pdf文件不存在
  10044. path=nil;
  10045. }
  10046. time = [self changeDateTimeFormate:time];
  10047. time = [time stringByAppendingString:@" PST"];
  10048. int sheet_id = sqlite3_column_int(stmt, 5);
  10049. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10050. item[@"tearsheetsId"]=@(sheet_id);
  10051. item[@"pdf_path"]=path;
  10052. item[@"create_time"]=time;
  10053. item[@"create_user"]=user;
  10054. item[@"tear_note"]=note;
  10055. item[@"tear_name"]=name;
  10056. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10057. item[@"model_info"]=model_info;
  10058. item[@"off_params"]=off_params;
  10059. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10060. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10061. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10062. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10063. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10064. }];
  10065. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10066. return [RAConvertor dict2data:dic];
  10067. }
  10068. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10069. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10070. // NSString *user = [params objectForKey:@"user"];
  10071. // 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];
  10072. 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];
  10073. int result = [iSalesDB execSql:sql];
  10074. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10075. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10076. if (result == RESULT_TRUE) {
  10077. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10078. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10079. sqlite3 *db = [iSalesDB get_db];
  10080. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10081. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10082. [iSalesDB close_db:db];
  10083. }
  10084. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10085. return [RAConvertor dict2data:dic];
  10086. }
  10087. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10088. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10089. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10090. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10091. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10092. NSDictionary *company_item = @{
  10093. @"control": @"text",
  10094. @"keyboard": @"text",
  10095. @"name": @"company_name",
  10096. @"value": COMPANY_FULL_NAME,
  10097. @"aname": @"Company Name"
  10098. };
  10099. [section_0 setObject:company_item forKey:@"item_0"];
  10100. [dic setObject:section_0 forKey:@"section_0"];
  10101. // Regurlar Price
  10102. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10103. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10104. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10105. __block long val_count = 0;
  10106. NSString *sql = @"select name,type,order_by from price order by order_by";
  10107. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10108. NSString *name = [self textAtColumn:0 statement:stmt];
  10109. int type = sqlite3_column_int(stmt, 1);
  10110. int order_by = sqlite3_column_int(stmt, 2);
  10111. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10112. NSDictionary *price_dic = @{
  10113. @"value" : name,
  10114. @"value_id" : [NSNumber numberWithInteger:type],
  10115. @"check" : order_by == 0 ? @(1) : @(0)
  10116. };
  10117. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10118. val_count = ++(*count);
  10119. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10120. val_count = 0;
  10121. }];
  10122. [cadedate setObject:@{@"value" : @"None",
  10123. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10124. val_count++;
  10125. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10126. [price setObject:cadedate forKey:@"cadedate"];
  10127. [section1 setObject:price forKey:@"item_2"];
  10128. [dic setObject:section1 forKey:@"section_1"];
  10129. return [RAConvertor dict2data:dic];
  10130. }
  10131. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10132. NSString *product_id = [params objectForKey:@"fashionId"];
  10133. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10134. __block int result = RESULT_TRUE;
  10135. __block int qty = 0;
  10136. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10137. qty = sqlite3_column_int(stmt, 0);
  10138. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10139. result = RESULT_FALSE;
  10140. }];
  10141. NSMutableDictionary *dic = @{
  10142. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10143. @"mode" : @"Regular Mode",
  10144. @"quantity_available" : @(qty),
  10145. @"result" : @(result),
  10146. }.mutableCopy;
  10147. return [RAConvertor dict2data:dic];
  10148. }
  10149. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10150. NSString *item_ids = [params objectForKey:@"item_id"];
  10151. NSString *line_notes = [params objectForKey:@"notes"];
  10152. NSString *price_str = [params objectForKey:@"price"];
  10153. NSString *discount_str = [params objectForKey:@"discount"];
  10154. NSString *percent = [params objectForKey:@"available_percent"];
  10155. NSString *qty = [params objectForKey:@"available_qty"];
  10156. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10157. int dot = 0;
  10158. if (line_notes) {
  10159. line_notes = [self translateSingleQuote:line_notes];
  10160. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10161. sql = [sql stringByAppendingString:line_notes];
  10162. dot = 1;
  10163. } else {
  10164. line_notes = @"";
  10165. }
  10166. if (price_str) {
  10167. if (dot) {
  10168. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10169. } else {
  10170. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10171. dot = 1;
  10172. }
  10173. sql = [sql stringByAppendingString:price_str];
  10174. } else {
  10175. price_str = @"";
  10176. }
  10177. if (discount_str) {
  10178. if (dot) {
  10179. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10180. } else {
  10181. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10182. dot = 1;
  10183. }
  10184. sql = [sql stringByAppendingString:discount_str];
  10185. } else {
  10186. discount_str = @"";
  10187. }
  10188. if (percent) {
  10189. if (dot) {
  10190. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10191. } else {
  10192. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10193. dot = 1;
  10194. }
  10195. sql = [sql stringByAppendingString:percent];
  10196. } else {
  10197. percent = @"";
  10198. }
  10199. if (qty) {
  10200. if (dot) {
  10201. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10202. } else {
  10203. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10204. dot = 1;
  10205. }
  10206. sql = [sql stringByAppendingString:qty];
  10207. } else {
  10208. qty = @"";
  10209. }
  10210. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10211. sql = [sql stringByAppendingString:where];
  10212. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10213. int result = [iSalesDB execSql:sql];
  10214. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10215. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10216. return [RAConvertor dict2data:dic];
  10217. }
  10218. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10219. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10220. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10221. sqlite3 *db = [iSalesDB get_db];
  10222. 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];
  10223. int result = [iSalesDB execSql:sql db:db];
  10224. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10225. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10226. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10227. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10228. [iSalesDB close_db:db];
  10229. return [RAConvertor dict2data:dic];
  10230. }
  10231. + (void)offline_removePDFWithName:(NSString *)name {
  10232. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10233. NSString *cachefolder = [paths objectAtIndex:0];
  10234. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10235. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10236. NSFileManager *fileManager = [NSFileManager defaultManager];
  10237. [fileManager removeItemAtPath:path error:nil];
  10238. }
  10239. + (void)offline_clear_PDFCache {
  10240. NSFileManager *fileManager = [NSFileManager defaultManager];
  10241. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10242. NSString *cachefolder = [paths objectAtIndex:0];
  10243. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10244. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10245. for (NSString *path in pdf_files) {
  10246. [self offline_removePDFWithName:[path lastPathComponent]];
  10247. }
  10248. }
  10249. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10250. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10251. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10252. NSString *user = [params objectForKey:@"user"];
  10253. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10254. if (![create_user isEqualToString:user]) {
  10255. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10256. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10257. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10258. return [RAConvertor dict2data:dic];
  10259. }
  10260. 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]];
  10261. __block int is_local = 0;
  10262. __block NSString *path = @"";
  10263. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10264. is_local = sqlite3_column_int(stmt, 0);
  10265. path = [self textAtColumn:1 statement:stmt];
  10266. }];
  10267. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10268. if (is_local == 1) {
  10269. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10270. // 删除文件
  10271. [self offline_removePDFWithName:path];
  10272. }
  10273. int result = [iSalesDB execSql:sql];
  10274. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10275. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10276. return [RAConvertor dict2data:dic];
  10277. }
  10278. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10279. {
  10280. // assert(add_params[@"contact_id"]!=nil);
  10281. assert(add_params[@"user"]!=nil);
  10282. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10283. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10284. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10285. NSString* where=@"1=1";
  10286. if (ver!=nil) {
  10287. where=@"is_dirty=1";
  10288. }
  10289. 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];
  10290. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10291. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10292. sqlite3 *db = [iSalesDB get_db];
  10293. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10294. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10295. NSInteger _id = sqlite3_column_int(stmt, 0);
  10296. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10297. NSString *name = [self textAtColumn:2 statement:stmt];
  10298. NSString *desc = [self textAtColumn:3 statement:stmt];
  10299. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10300. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10301. NSInteger qty = sqlite3_column_int(stmt, 6);
  10302. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10303. double percent = sqlite3_column_double(stmt, 8);
  10304. double price = sqlite3_column_double(stmt, 9);
  10305. double discount = sqlite3_column_double(stmt, 10);
  10306. NSString *img = [self textAtColumn:11 statement:stmt];
  10307. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10308. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10309. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10310. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10311. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10312. if ([price_null isEqualToString:@"null"]) {
  10313. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10314. }
  10315. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10316. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10317. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10318. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10319. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10320. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10321. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10322. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10323. if ([qty_null isEqualToString:@"null"]) {
  10324. // [item setValue:@"null" forKey:@"available_qty"];
  10325. } else {
  10326. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10327. }
  10328. if ([is_percent_null isEqualToString:@"null"]) {
  10329. // [item setValue:@"null" forKey:@"percentage"];
  10330. } else {
  10331. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10332. }
  10333. if ([percent_null isEqualToString:@"null"]) {
  10334. // [item setValue:@"null" forKey:@"percent"];
  10335. } else {
  10336. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10337. }
  10338. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10339. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10340. [item setValue:name forKey:@"name"];
  10341. [item setValue:desc forKey:@"description"];
  10342. [item setValue:img forKey:@"img"];
  10343. [item setValue:line_note forKey:@"line_note"];
  10344. [item setValue:create_time forKey:@"createtime"];
  10345. [item setValue:modify_time forKey:@"modifytime"];
  10346. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10347. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10348. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10349. }];
  10350. [iSalesDB close_db:db];
  10351. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10352. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10353. }
  10354. return ret;
  10355. }
  10356. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10357. {
  10358. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10359. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10360. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10361. NSString* where=@"1=1";
  10362. if (ver!=nil) {
  10363. where=@"is_dirty=1";
  10364. }
  10365. 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];
  10366. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10367. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10368. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10369. NSInteger _id = sqlite3_column_int(stmt, 0);
  10370. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10371. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10372. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10373. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10374. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10375. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10376. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10377. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10378. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10379. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10380. NSString *uuid = [NSUUID UUID].UUIDString;
  10381. int is_delete = sqlite3_column_int(stmt, 11);
  10382. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10383. int is_local = sqlite3_column_int(stmt, 12);
  10384. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10385. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10386. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10387. }
  10388. [item setObject:pdf_path forKey:@"pdf_path"];
  10389. [item setObject:create_user forKey:@"create_user"];
  10390. [item setObject:tear_note forKey:@"tear_note"];
  10391. [item setObject:tear_name forKey:@"tear_name"];
  10392. [item setObject:model_info forKey:@"model_info"];
  10393. [item setObject:createtime forKey:@"createtime"];
  10394. [item setObject:modifytime forKey:@"modifytime"];
  10395. [item setObject:urlParams forKey:@"urlParams"];
  10396. [item setObject:off_params forKey:@"off_params"];
  10397. [item setObject:uuid forKey:@"pdf_token"];
  10398. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10399. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10400. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10401. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10402. } else {
  10403. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10404. }
  10405. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10406. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10407. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10408. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10409. }];
  10410. return ret;
  10411. }
  10412. #pragma mark 2020
  10413. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10414. {
  10415. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10416. sqlite3 *db = [iSalesDB get_db];
  10417. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10418. if (contactType) {
  10419. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10420. } else {
  10421. contactType = @"1 = 1";
  10422. }
  10423. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10424. DebugLog(@"offline contact list keyword: %@",keyword);
  10425. // advanced search
  10426. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10427. if (contact_name) {
  10428. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10429. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10430. } else {
  10431. contact_name = @"";
  10432. }
  10433. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10434. if (customer_phone) {
  10435. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10436. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10437. } else {
  10438. customer_phone = @"";
  10439. }
  10440. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10441. if (customer_fax) {
  10442. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10443. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10444. } else {
  10445. customer_fax = @"";
  10446. }
  10447. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10448. if (customer_zipcode) {
  10449. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10450. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10451. } else {
  10452. customer_zipcode = @"";
  10453. }
  10454. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10455. if (customer_sales_rep) {
  10456. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10457. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10458. } else {
  10459. customer_sales_rep = @"";
  10460. }
  10461. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10462. if (customer_state) {
  10463. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10464. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10465. } else {
  10466. customer_state = @"";
  10467. }
  10468. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10469. if (customer_name) {
  10470. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10471. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10472. } else {
  10473. customer_name = @"";
  10474. }
  10475. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10476. if (customer_country) {
  10477. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10478. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10479. } else {
  10480. customer_country = @"";
  10481. }
  10482. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10483. if (customer_cid) {
  10484. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10485. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10486. } else {
  10487. customer_cid = @"";
  10488. }
  10489. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10490. if (customer_city) {
  10491. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10492. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  10493. } else {
  10494. customer_city = @"";
  10495. }
  10496. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  10497. if (customer_address) {
  10498. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10499. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  10500. } else {
  10501. customer_address = @"";
  10502. }
  10503. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  10504. if (customer_email) {
  10505. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10506. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  10507. } else {
  10508. customer_email = @"";
  10509. }
  10510. NSString *price_name = [params valueForKey:@"price_name"];
  10511. if (price_name) {
  10512. if ([price_name containsString:@","]) {
  10513. // 首先从 price表中查处name
  10514. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  10515. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  10516. for (int i = 1;i < pArray.count;i++) {
  10517. NSString *p = pArray[i];
  10518. [mutablePStr appendFormat:@" or type = %@ ",p];
  10519. }
  10520. [mutablePStr appendString:@";"];
  10521. __block NSMutableArray *price_name_array = [NSMutableArray array];
  10522. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10523. char *name = (char *)sqlite3_column_text(stmt, 0);
  10524. if (!name)
  10525. name = "";
  10526. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  10527. }];
  10528. // 再根据name 拼sql
  10529. NSMutableString *mutable_price_name = [NSMutableString string];
  10530. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  10531. for (int i = 1; i < price_name_array.count; i++) {
  10532. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  10533. }
  10534. [mutable_price_name appendString:@")"];
  10535. price_name = mutable_price_name;
  10536. } else {
  10537. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  10538. if ([price_name isEqualToString:@""]) {
  10539. price_name = @"";
  10540. } else {
  10541. __block NSString *price;
  10542. price_name = [self translateSingleQuote:price_name];
  10543. [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) {
  10544. char *p = (char *)sqlite3_column_text(stmt, 0);
  10545. if (p == NULL) {
  10546. p = "";
  10547. }
  10548. price = [NSString stringWithUTF8String:p];
  10549. }];
  10550. if ([price isEqualToString:@""]) {
  10551. price_name = @"";
  10552. } else {
  10553. price = [self translateSingleQuote:price];
  10554. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  10555. }
  10556. }
  10557. }
  10558. } else {
  10559. price_name = @"";
  10560. }
  10561. int limit = [[params valueForKey:@"limit"] intValue];
  10562. int offset = [[params valueForKey:@"offset"] intValue];
  10563. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10564. 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];
  10565. 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];
  10566. // int result= [iSalesDB AddExFunction:db];
  10567. int count =0;
  10568. NSString *sqlQuery = nil;
  10569. if(keyword.length==0)
  10570. {
  10571. // 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];
  10572. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  10573. sqlQuery = sql;
  10574. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  10575. }
  10576. else
  10577. {
  10578. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  10579. keyword = keyword.lowercaseString;
  10580. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10581. 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];
  10582. 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]];
  10583. }
  10584. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10585. sqlite3_stmt * statement;
  10586. [ret setValue:@"2" forKey:@"result"];
  10587. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  10588. // 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";
  10589. int i = 0;
  10590. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10591. {
  10592. while (sqlite3_step(statement) == SQLITE_ROW)
  10593. {
  10594. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10595. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10596. int editable = sqlite3_column_int(statement, 0);
  10597. char *company_name = (char*)sqlite3_column_text(statement, 1);
  10598. NSString *nscompany_name =nil;
  10599. if(company_name==nil)
  10600. nscompany_name=@"";
  10601. else
  10602. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  10603. char *country = (char*)sqlite3_column_text(statement, 2);
  10604. if(country==nil)
  10605. country="";
  10606. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10607. // char *addr = (char*)sqlite3_column_text(statement, 3);
  10608. // if(addr==nil)
  10609. // addr="";
  10610. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10611. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10612. if(zipcode==nil)
  10613. zipcode="";
  10614. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10615. char *state = (char*)sqlite3_column_text(statement, 5);
  10616. if(state==nil)
  10617. state="";
  10618. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10619. char *city = (char*)sqlite3_column_text(statement, 6);
  10620. if(city==nil)
  10621. city="";
  10622. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10623. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10624. // NSString *nscontact_name = nil;
  10625. // if(contact_name==nil)
  10626. // nscontact_name=@"";
  10627. // else
  10628. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10629. char *phone = (char*)sqlite3_column_text(statement, 8);
  10630. NSString *nsphone = nil;
  10631. if(phone==nil)
  10632. nsphone=@"";
  10633. else
  10634. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10635. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10636. if(contact_id==nil)
  10637. contact_id="";
  10638. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10639. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10640. if(addr_1==nil)
  10641. addr_1="";
  10642. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10643. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10644. if(addr_2==nil)
  10645. addr_2="";
  10646. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10647. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10648. if(addr_3==nil)
  10649. addr_3="";
  10650. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10651. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10652. if(addr_4==nil)
  10653. addr_4="";
  10654. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10655. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10656. if(first_name==nil)
  10657. first_name="";
  10658. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10659. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10660. if(last_name==nil)
  10661. last_name="";
  10662. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10663. char *fax = (char*)sqlite3_column_text(statement, 16);
  10664. NSString *nsfax = nil;
  10665. if(fax==nil)
  10666. nsfax=@"";
  10667. else
  10668. {
  10669. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10670. if(nsfax.length>0)
  10671. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10672. }
  10673. char *email = (char*)sqlite3_column_text(statement, 17);
  10674. NSString *nsemail = nil;
  10675. if(email==nil)
  10676. nsemail=@"";
  10677. else
  10678. {
  10679. nsemail= [[NSString alloc]initWithUTF8String:email];
  10680. if(nsemail.length>0)
  10681. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10682. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10683. }
  10684. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10685. [arr_name addObject:nsfirst_name];
  10686. [arr_name addObject:nslast_name];
  10687. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10688. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10689. {
  10690. // decrypt
  10691. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10692. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10693. nsphone=[AESCrypt fastdecrypt:nsphone];
  10694. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10695. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10696. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10697. }
  10698. [arr_addr addObject:nscompany_name];
  10699. [arr_addr addObject:nscontact_name];
  10700. [arr_addr addObject:@"<br>"];
  10701. [arr_addr addObject:nsaddr_1];
  10702. [arr_addr addObject:nsaddr_2];
  10703. [arr_addr addObject:nsaddr_3];
  10704. [arr_addr addObject:nsaddr_4];
  10705. //[arr_addr addObject:nsaddr];
  10706. [arr_addr addObject:nszipcode];
  10707. [arr_addr addObject:nscity];
  10708. [arr_addr addObject:nsstate];
  10709. [arr_addr addObject:nscountry];
  10710. [arr_addr addObject:@"<br>"];
  10711. [arr_addr addObject:nsphone];
  10712. [arr_addr addObject:nsfax];
  10713. [arr_addr addObject:nsemail];
  10714. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10715. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10716. [item setValue:name forKey:@"name"];
  10717. [item setValue:nscontact_id forKey:@"contact_id"];
  10718. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10719. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10720. i++;
  10721. }
  10722. sqlite3_finalize(statement);
  10723. }
  10724. [iSalesDB close_db:db];
  10725. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10726. dispatch_async(dispatch_get_main_queue(), ^{
  10727. UIApplication * app = [UIApplication sharedApplication];
  10728. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10729. [ret setValue:appDelegate.mode forKey:@"mode"];
  10730. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10731. result(ret);
  10732. });
  10733. return ;
  10734. });
  10735. }
  10736. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10737. //{
  10738. //
  10739. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10740. //
  10741. // NSString* contactId = [params valueForKey:@"contactId"];
  10742. //
  10743. //
  10744. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10745. //
  10746. //
  10747. //
  10748. //
  10749. //
  10750. //
  10751. // sqlite3 *db = [iSalesDB get_db];
  10752. //
  10753. //
  10754. //
  10755. //
  10756. //
  10757. // NSString *sqlQuery = nil;
  10758. //
  10759. //
  10760. // {
  10761. // 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];
  10762. //
  10763. // }
  10764. //
  10765. //
  10766. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10767. // sqlite3_stmt * statement;
  10768. //
  10769. //
  10770. // [ret setValue:@"2" forKey:@"result"];
  10771. //
  10772. //
  10773. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10774. // {
  10775. //
  10776. // //int i = 0;
  10777. // if (sqlite3_step(statement) == SQLITE_ROW)
  10778. // {
  10779. //
  10780. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10781. //
  10782. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10783. //
  10784. //
  10785. // // int editable = sqlite3_column_int(statement, 0);
  10786. //
  10787. //
  10788. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10789. // NSString *nscompany_name =nil;
  10790. // if(company_name==nil)
  10791. // nscompany_name=@"";
  10792. // else
  10793. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10794. //
  10795. //
  10796. // char *country = (char*)sqlite3_column_text(statement, 2);
  10797. // if(country==nil)
  10798. // country="";
  10799. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10800. //
  10801. //
  10802. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10803. // // if(addr==nil)
  10804. // // addr="";
  10805. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10806. //
  10807. //
  10808. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10809. // if(zipcode==nil)
  10810. // zipcode="";
  10811. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10812. //
  10813. //
  10814. // char *state = (char*)sqlite3_column_text(statement, 5);
  10815. // if(state==nil)
  10816. // state="";
  10817. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10818. //
  10819. // char *city = (char*)sqlite3_column_text(statement, 6);
  10820. // if(city==nil)
  10821. // city="";
  10822. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10823. //
  10824. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10825. // // NSString *nscontact_name = nil;
  10826. // // if(contact_name==nil)
  10827. // // nscontact_name=@"";
  10828. // // else
  10829. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10830. //
  10831. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10832. // NSString *nsphone = nil;
  10833. // if(phone==nil)
  10834. // nsphone=@"";
  10835. // else
  10836. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10837. //
  10838. //
  10839. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10840. // // if(contact_id==nil)
  10841. // // contact_id="";
  10842. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10843. //
  10844. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10845. // if(addr_1==nil)
  10846. // addr_1="";
  10847. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10848. //
  10849. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10850. // if(addr_2==nil)
  10851. // addr_2="";
  10852. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10853. //
  10854. //
  10855. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10856. // if(addr_3==nil)
  10857. // addr_3="";
  10858. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10859. //
  10860. //
  10861. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10862. // if(addr_4==nil)
  10863. // addr_4="";
  10864. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10865. //
  10866. //
  10867. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10868. // if(first_name==nil)
  10869. // first_name="";
  10870. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10871. //
  10872. //
  10873. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10874. // if(last_name==nil)
  10875. // last_name="";
  10876. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10877. //
  10878. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10879. // NSString *nsfax = nil;
  10880. // if(fax==nil)
  10881. // nsfax=@"";
  10882. // else
  10883. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10884. //
  10885. // char *email = (char*)sqlite3_column_text(statement, 17);
  10886. // NSString *nsemail = nil;
  10887. // if(email==nil)
  10888. // nsemail=@"";
  10889. // else
  10890. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10891. //
  10892. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10893. // NSString *nsimg_0 = nil;
  10894. // if(img_0==nil)
  10895. // nsimg_0=@"";
  10896. // else
  10897. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10898. //
  10899. // [self copy_bcardImg:nsimg_0];
  10900. //
  10901. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10902. // NSString *nsimg_1 = nil;
  10903. // if(img_1==nil)
  10904. // nsimg_1=@"";
  10905. // else
  10906. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10907. // [self copy_bcardImg:nsimg_1];
  10908. //
  10909. //
  10910. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10911. // NSString *nsimg_2 = nil;
  10912. // if(img_2==nil)
  10913. // nsimg_2=@"";
  10914. // else
  10915. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10916. // [self copy_bcardImg:nsimg_2];
  10917. //
  10918. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10919. // NSString *nsprice_type = nil;
  10920. // if(price_type==nil)
  10921. // nsprice_type=@"";
  10922. // else
  10923. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10924. //
  10925. //
  10926. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10927. // NSString *nsnotes = nil;
  10928. // if(notes==nil)
  10929. // nsnotes=@"";
  10930. // else
  10931. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10932. //
  10933. //
  10934. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10935. // NSString *nssalesrep = nil;
  10936. // if(salesrep==nil)
  10937. // nssalesrep=@"";
  10938. // else
  10939. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10940. //
  10941. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10942. //
  10943. //
  10944. // {
  10945. // // decrypt
  10946. //
  10947. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10948. //
  10949. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10950. //
  10951. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10952. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10953. //
  10954. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10955. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10956. //
  10957. // }
  10958. //
  10959. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10960. //
  10961. // [arr_name addObject:nsfirst_name];
  10962. // [arr_name addObject:nslast_name];
  10963. //
  10964. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10965. //
  10966. //
  10967. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10968. //
  10969. // [arr_ext addObject:nsaddr_1];
  10970. // [arr_ext addObject:nsaddr_2];
  10971. // [arr_ext addObject:nsaddr_3];
  10972. // [arr_ext addObject:nsaddr_4];
  10973. // [arr_ext addObject:@"\r\n"];
  10974. //
  10975. // [arr_ext addObject:nscity];
  10976. // [arr_ext addObject:nsstate];
  10977. // [arr_ext addObject:nszipcode];
  10978. // [arr_ext addObject:nscountry];
  10979. //
  10980. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10981. //
  10982. //
  10983. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10984. //
  10985. //
  10986. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10987. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10988. // [item setValue:nscountry forKey:@"customer_country"];
  10989. // [item setValue:nsphone forKey:@"customer_phone"];
  10990. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10991. // [item setValue:nscompany_name forKey:@"customer_name"];
  10992. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10993. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10994. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10995. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10996. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10997. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10998. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10999. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11000. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11001. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11002. // [item setValue:nscity forKey:@"customer_city"];
  11003. // [item setValue:nsstate forKey:@"customer_state"];
  11004. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11005. // [item setValue:contactId forKey:@"customer_cid"];
  11006. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11007. // [item setValue:nsfax forKey:@"customer_fax"];
  11008. // [item setValue:nsemail forKey:@"customer_email"];
  11009. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11010. //
  11011. //
  11012. // [ret setObject:item forKey:@"customerInfo"];
  11013. // // i++;
  11014. //
  11015. //
  11016. //
  11017. // }
  11018. //
  11019. //
  11020. //
  11021. //
  11022. // sqlite3_finalize(statement);
  11023. // }
  11024. //
  11025. //
  11026. //
  11027. // [iSalesDB close_db:db];
  11028. //
  11029. //
  11030. //
  11031. //
  11032. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11033. // dispatch_async(dispatch_get_main_queue(), ^{
  11034. // UIApplication * app = [UIApplication sharedApplication];
  11035. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11036. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11037. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11038. // result(ret);
  11039. // });
  11040. //
  11041. // return ;
  11042. // });
  11043. //}
  11044. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11045. {
  11046. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11047. NSString* order_code= appDelegate.order_code;
  11048. NSString* user = appDelegate.user;
  11049. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11050. NSString* category = [params valueForKey:@"category"];
  11051. if (!category || [category isEqualToString:@""]) {
  11052. category = @"%";
  11053. }
  11054. category = [self translateSingleQuote:category];
  11055. int limit = [[params valueForKey:@"limit"] intValue];
  11056. int offset = [[params valueForKey:@"offset"] intValue];
  11057. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11058. NSString *limit_str = @"";
  11059. if (limited) {
  11060. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11061. }
  11062. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11063. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11064. sqlite3 *db = [iSalesDB get_db];
  11065. // [iSalesDB AddExFunction:db];
  11066. int count;
  11067. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11068. 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];
  11069. double price_min = 0;
  11070. double price_max = 0;
  11071. if ([params.allKeys containsObject:@"alert"]) {
  11072. // alert
  11073. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11074. NSString *alert = params[@"alert"];
  11075. if ([alert isEqualToString:@"Display All"]) {
  11076. alert = [NSString stringWithFormat:@""];
  11077. } else {
  11078. alert = [self translateSingleQuote:alert];
  11079. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11080. }
  11081. // available
  11082. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11083. NSString *available = params[@"available"];
  11084. NSString *available_condition;
  11085. if ([available isEqualToString:@"Display All"]) {
  11086. available_condition = @"";
  11087. } else if ([available isEqualToString:@"Available Now"]) {
  11088. available_condition = @"and availability > 0";
  11089. } else {
  11090. available_condition = @"and availability == 0";
  11091. }
  11092. // best seller
  11093. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11094. NSString *best_seller = @"";
  11095. NSString *order_best_seller = @"m.name asc";
  11096. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11097. best_seller = @"and best_seller > 0";
  11098. order_best_seller = @"m.best_seller desc,m.name asc";
  11099. }
  11100. // price
  11101. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11102. NSString *price = params[@"price"];
  11103. price_min = 0;
  11104. price_max = MAXFLOAT;
  11105. if (user && price != nil) {
  11106. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11107. NSMutableString *priceName = [NSMutableString string];
  11108. for (int i = 0; i < priceTypeArray.count; i++) {
  11109. NSString *pricetype = priceTypeArray[i];
  11110. pricetype = [self translateSingleQuote:pricetype];
  11111. if (i == 0) {
  11112. [priceName appendFormat:@"'%@'",pricetype];
  11113. } else {
  11114. [priceName appendFormat:@",'%@'",pricetype];
  11115. }
  11116. }
  11117. if ([price isEqualToString:@"Display All"]) {
  11118. price = [NSString stringWithFormat:@""];
  11119. } else if([price containsString:@"+"]){
  11120. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11121. price_min = [price doubleValue];
  11122. 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];
  11123. } else {
  11124. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11125. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11126. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11127. 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];
  11128. }
  11129. } else {
  11130. price = @"";
  11131. }
  11132. // sold_by_qty : Sold in quantities of %@
  11133. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11134. NSString *qty = params[@"sold_by_qty"];
  11135. if ([qty isEqualToString:@"Display All"]) {
  11136. qty = @"";
  11137. } else {
  11138. qty = [self translateSingleQuote:qty];
  11139. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11140. }
  11141. // cate
  11142. // category = [self translateSingleQuote:category];
  11143. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11144. // cate mutiple selection
  11145. NSString *category_id = params[@"category"];
  11146. NSMutableArray *cate_id_array = nil;
  11147. NSMutableString *cateWhere = [NSMutableString string];
  11148. if ([category_id isEqualToString:@""] || !category_id) {
  11149. [cateWhere appendString:@"1 = 1"];
  11150. } else {
  11151. if ([category_id containsString:@","]) {
  11152. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11153. } else {
  11154. cate_id_array = [@[category_id] mutableCopy];
  11155. }
  11156. [cateWhere appendString:@"("];
  11157. for (int i = 0; i < cate_id_array.count; i++) {
  11158. if (i == 0) {
  11159. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11160. } else {
  11161. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11162. }
  11163. }
  11164. [cateWhere appendString:@")"];
  11165. }
  11166. // where bestseller > 0 order by bestseller desc
  11167. // sql query: alert availability(int) best_seller(int) price qty
  11168. 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];
  11169. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11170. }
  11171. DebugLog(@"offline category where: %@",where);
  11172. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11173. if (!user) {
  11174. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11175. }
  11176. [ret setValue:filter forKey:@"filter"];
  11177. DebugLog(@"offline_category sql:%@",sqlQuery);
  11178. sqlite3_stmt * statement;
  11179. [ret setValue:@"2" forKey:@"result"];
  11180. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11181. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11182. // int count=0;
  11183. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11184. {
  11185. int i=0;
  11186. while (sqlite3_step(statement) == SQLITE_ROW)
  11187. {
  11188. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11189. char *name = (char*)sqlite3_column_text(statement, 0);
  11190. if(name==nil)
  11191. name="";
  11192. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11193. char *description = (char*)sqlite3_column_text(statement, 1);
  11194. if(description==nil)
  11195. description="";
  11196. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11197. int product_id = sqlite3_column_int(statement, 2);
  11198. int wid = sqlite3_column_int(statement, 3);
  11199. int closeout = sqlite3_column_int(statement, 4);
  11200. int cid = sqlite3_column_int(statement, 5);
  11201. int wisdelete = sqlite3_column_int(statement, 6);
  11202. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11203. int more_color = sqlite3_column_int(statement, 8);
  11204. // Defaul Category ID
  11205. __block NSString *categoryID = nil;
  11206. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11207. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11208. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11209. if(default_category==nil)
  11210. default_category="";
  11211. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11212. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11213. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11214. categoryID = nsdefault_category;
  11215. }];
  11216. if (!categoryID.length) {
  11217. NSString *cateIDs = params[@"category"];
  11218. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11219. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11220. for (NSString *cateID in requestCategoryArr) {
  11221. BOOL needBreak = NO;
  11222. for (NSString *itemCateIDBox in itemCategoryArr) {
  11223. if (itemCateIDBox.length > 4) {
  11224. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11225. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11226. if ([itemCategoryID isEqualToString:cateID]) {
  11227. needBreak = YES;
  11228. categoryID = itemCategoryID;
  11229. break;
  11230. }
  11231. }
  11232. }
  11233. if (needBreak) {
  11234. break;
  11235. }
  11236. }
  11237. }
  11238. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11239. if(wid !=0 && wisdelete != 1)
  11240. [item setValue:@"true" forKey:@"wish_exists"];
  11241. else
  11242. [item setValue:@"false" forKey:@"wish_exists"];
  11243. if(closeout==0)
  11244. [item setValue:@"false" forKey:@"is_closeout"];
  11245. else
  11246. [item setValue:@"true" forKey:@"is_closeout"];
  11247. if(cid==0)
  11248. [item setValue:@"false" forKey:@"cart_exists"];
  11249. else
  11250. [item setValue:@"true" forKey:@"cart_exists"];
  11251. if (more_color == 0) {
  11252. [item setObject:@(false) forKey:@"more_color"];
  11253. } else if (more_color == 1) {
  11254. [item setObject:@(true) forKey:@"more_color"];
  11255. }
  11256. [item addEntriesFromDictionary:imgjson];
  11257. // [item setValue:nsurl forKey:@"img"];
  11258. [item setValue:nsname forKey:@"name"];
  11259. [item setValue:nsdescription forKey:@"description"];
  11260. if (categoryID) {
  11261. [item setValue:categoryID forKey:@"item_category_id"];
  11262. }
  11263. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11264. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11265. i++;
  11266. }
  11267. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11268. [ret setObject:items forKey:@"items"];
  11269. sqlite3_finalize(statement);
  11270. } else {
  11271. DebugLog(@"nothing...");
  11272. }
  11273. DebugLog(@"count:%d",count);
  11274. [iSalesDB close_db:db];
  11275. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11276. dispatch_async(dispatch_get_main_queue(), ^{
  11277. // UIApplication * app = [UIApplication sharedApplication];
  11278. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11279. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11280. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11281. result(ret);
  11282. });
  11283. });
  11284. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11285. }
  11286. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11287. {
  11288. [self categoryList:params limited:YES completionHandler:result];
  11289. }
  11290. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11291. {
  11292. UIApplication * app = [UIApplication sharedApplication];
  11293. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11294. NSString* order_code = appDelegate.order_code;
  11295. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11296. NSString* keyword = [params valueForKey:@"keyword"];
  11297. keyword=keyword.lowercaseString;
  11298. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11299. int limit = [[params valueForKey:@"limit"] intValue];
  11300. int offset = [[params valueForKey:@"offset"] intValue];
  11301. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11302. NSString *limit_str = @"";
  11303. if (limited) {
  11304. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11305. }
  11306. sqlite3 *db = [iSalesDB get_db];
  11307. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11308. NSString *sqlQuery = nil;
  11309. if(exactMatch )
  11310. 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 ;
  11311. else
  11312. 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
  11313. DebugLog(@"offline_search sql:%@",sqlQuery);
  11314. sqlite3_stmt * statement;
  11315. [ret setValue:@"2" forKey:@"result"];
  11316. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11317. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11318. // int count=0;
  11319. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11320. {
  11321. int i=0;
  11322. while (sqlite3_step(statement) == SQLITE_ROW)
  11323. {
  11324. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11325. // char *name = (char*)sqlite3_column_text(statement, 1);
  11326. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11327. char *name = (char*)sqlite3_column_text(statement, 0);
  11328. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11329. char *description = (char*)sqlite3_column_text(statement, 1);
  11330. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11331. int product_id = sqlite3_column_int(statement, 2);
  11332. // char *url = (char*)sqlite3_column_text(statement, 3);
  11333. // if(url==nil)
  11334. // url="";
  11335. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11336. int wid = sqlite3_column_int(statement, 3);
  11337. int closeout = sqlite3_column_int(statement, 4);
  11338. int cid = sqlite3_column_int(statement, 5);
  11339. int wisdelete = sqlite3_column_int(statement, 6);
  11340. int more_color = sqlite3_column_int(statement, 7);
  11341. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11342. if(wid !=0 && wisdelete != 1)
  11343. [item setValue:@"true" forKey:@"wish_exists"];
  11344. else
  11345. [item setValue:@"false" forKey:@"wish_exists"];
  11346. if(closeout==0)
  11347. [item setValue:@"false" forKey:@"is_closeout"];
  11348. else
  11349. [item setValue:@"true" forKey:@"is_closeout"];
  11350. if(cid==0)
  11351. [item setValue:@"false" forKey:@"cart_exists"];
  11352. else
  11353. [item setValue:@"true" forKey:@"cart_exists"];
  11354. if (more_color == 0) {
  11355. [item setObject:@(false) forKey:@"more_color"];
  11356. } else if (more_color == 1) {
  11357. [item setObject:@(true) forKey:@"more_color"];
  11358. }
  11359. [item addEntriesFromDictionary:imgjson];
  11360. // [item setValue:nsurl forKey:@"img"];
  11361. [item setValue:nsname forKey:@"fash_name"];
  11362. [item setValue:nsdescription forKey:@"description"];
  11363. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11364. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11365. i++;
  11366. }
  11367. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11368. [ret setObject:items forKey:@"items"];
  11369. sqlite3_finalize(statement);
  11370. }
  11371. DebugLog(@"count:%d",count);
  11372. [iSalesDB close_db:db];
  11373. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11374. dispatch_async(dispatch_get_main_queue(), ^{
  11375. result(ret);
  11376. });
  11377. });
  11378. }
  11379. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11380. {
  11381. [self search:params limited:YES completionHandler:result];
  11382. }
  11383. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11384. //{
  11385. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11386. // NSString* order_code = appDelegate.order_code;
  11387. // NSString* user = appDelegate.user;
  11388. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11389. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11390. //
  11391. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11392. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11393. //
  11394. // // category
  11395. // NSDictionary *category_menu = [self offline_category_menu];
  11396. // [filter setValue:category_menu forKey:@"category"];
  11397. //
  11398. // NSString* where= nil;
  11399. // NSString* orderby= @"m.name";
  11400. // if (!filterSearch) {
  11401. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11402. //
  11403. // switch (covertype) {
  11404. // case 0:
  11405. // {
  11406. // where=@"m.category like'%%#005#%%'";
  11407. // break;
  11408. // }
  11409. // case 1:
  11410. // {
  11411. // where=@"m.alert like '%QS%'";
  11412. // break;
  11413. // }
  11414. // case 2:
  11415. // {
  11416. // where=@"m.availability>0";
  11417. // break;
  11418. // }
  11419. // case 3:
  11420. // {
  11421. // where=@"m.best_seller>0";
  11422. // orderby=@"m.best_seller desc,m.name asc";
  11423. // break;
  11424. // }
  11425. // default:
  11426. // where=@"1=1";
  11427. // break;
  11428. // }
  11429. //
  11430. // }
  11431. //
  11432. //
  11433. // int limit = [[params valueForKey:@"limit"] intValue];
  11434. // int offset = [[params valueForKey:@"offset"] intValue];
  11435. //
  11436. // NSString *limit_str = @"";
  11437. // if (limited) {
  11438. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11439. // }
  11440. //
  11441. //
  11442. // sqlite3 *db = [iSalesDB get_db];
  11443. // // [iSalesDB AddExFunction:db];
  11444. //
  11445. // int count;
  11446. //
  11447. // NSString *sqlQuery = nil;
  11448. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11449. // 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];
  11450. //
  11451. //
  11452. // double price_min = 0;
  11453. // double price_max = 0;
  11454. // if (filterSearch) {
  11455. // // alert
  11456. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11457. // NSString *alert = params[@"alert"];
  11458. // if ([alert isEqualToString:@"Display All"]) {
  11459. // alert = [NSString stringWithFormat:@""];
  11460. // } else {
  11461. // alert = [self translateSingleQuote:alert];
  11462. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11463. // }
  11464. //
  11465. // // available
  11466. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11467. // NSString *available = params[@"available"];
  11468. // NSString *available_condition;
  11469. // if ([available isEqualToString:@"Display All"]) {
  11470. // available_condition = @"";
  11471. // } else if ([available isEqualToString:@"Available Now"]) {
  11472. // available_condition = @"and availability > 0";
  11473. // } else {
  11474. // available_condition = @"and availability == 0";
  11475. // }
  11476. //
  11477. // // best seller
  11478. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11479. // NSString *best_seller = @"";
  11480. // NSString *order_best_seller = @"m.name asc";
  11481. //
  11482. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11483. // best_seller = @"and best_seller > 0";
  11484. // order_best_seller = @"m.best_seller desc,m.name asc";
  11485. // }
  11486. //
  11487. // // price
  11488. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11489. // NSString *price = params[@"price"];
  11490. // price_min = 0;
  11491. // price_max = MAXFLOAT;
  11492. // if (user) {
  11493. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11494. // NSMutableString *priceName = [NSMutableString string];
  11495. // for (int i = 0; i < priceTypeArray.count; i++) {
  11496. // NSString *pricetype = priceTypeArray[i];
  11497. // pricetype = [self translateSingleQuote:pricetype];
  11498. // if (i == 0) {
  11499. // [priceName appendFormat:@"'%@'",pricetype];
  11500. // } else {
  11501. // [priceName appendFormat:@",'%@'",pricetype];
  11502. // }
  11503. // }
  11504. //
  11505. // if ([price isEqualToString:@"Display All"]) {
  11506. // price = [NSString stringWithFormat:@""];
  11507. // } else if([price containsString:@"+"]){
  11508. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11509. // price_min = [price doubleValue];
  11510. // 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];
  11511. // } else {
  11512. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11513. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  11514. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  11515. // 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];
  11516. // }
  11517. // } else {
  11518. // price = @"";
  11519. // }
  11520. //
  11521. // // sold_by_qty : Sold in quantities of %@
  11522. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11523. // NSString *qty = params[@"sold_by_qty"];
  11524. // if ([qty isEqualToString:@"Display All"]) {
  11525. // qty = @"";
  11526. // } else {
  11527. // qty = [self translateSingleQuote:qty];
  11528. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11529. // }
  11530. //
  11531. // // category
  11532. // NSString *category_id = params[@"ctgId"];
  11533. // NSMutableArray *cate_id_array = nil;
  11534. // NSMutableString *cateWhere = [NSMutableString string];
  11535. //
  11536. // if ([category_id isEqualToString:@""] || !category_id) {
  11537. // [cateWhere appendString:@"1 = 1"];
  11538. // } else {
  11539. //
  11540. // if ([category_id containsString:@","]) {
  11541. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11542. // } else {
  11543. // cate_id_array = [@[category_id] mutableCopy];
  11544. // }
  11545. // /*-----------*/
  11546. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  11547. //
  11548. // [cateWhere appendString:@"("];
  11549. // for (int i = 0; i < cate_id_array.count; i++) {
  11550. //
  11551. // for (NSString *key0 in cateDic.allKeys) {
  11552. //
  11553. // if ([key0 containsString:@"category_"]) {
  11554. //
  11555. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  11556. //
  11557. // for (NSString *key1 in category0.allKeys) {
  11558. //
  11559. // if ([key1 containsString:@"category_"]) {
  11560. //
  11561. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  11562. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  11563. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11564. //
  11565. // cate_id_array[i] = [category1 objectForKey:@"id"];
  11566. // if (i == 0) {
  11567. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11568. // } else {
  11569. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11570. // }
  11571. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11572. // [category0 setValue:category1 forKey:key1];
  11573. // [cateDic setValue:category0 forKey:key0];
  11574. //
  11575. // }
  11576. //
  11577. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  11578. //
  11579. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11580. //
  11581. // cate_id_array[i] = [category0 objectForKey:@"id"];
  11582. // if (i == 0) {
  11583. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11584. // } else {
  11585. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11586. // }
  11587. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11588. // [cateDic setValue:category0 forKey:key0];
  11589. //
  11590. // }
  11591. //
  11592. // }
  11593. //
  11594. // }
  11595. //
  11596. // }
  11597. //
  11598. // }
  11599. //
  11600. // }
  11601. // [cateWhere appendString:@")"];
  11602. //
  11603. // [filter setValue:cateDic forKey:@"category"];
  11604. // }
  11605. //
  11606. // // where bestseller > 0 order by bestseller desc
  11607. // // sql query: alert availability(int) best_seller(int) price qty
  11608. //
  11609. // 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];
  11610. //
  11611. //
  11612. // // count
  11613. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11614. //
  11615. // }
  11616. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11617. //
  11618. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11619. //
  11620. //
  11621. //
  11622. //
  11623. // if (!user) {
  11624. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11625. // }
  11626. //
  11627. // [ret setValue:filter forKey:@"filter"];
  11628. //
  11629. //
  11630. //
  11631. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11632. //
  11633. // sqlite3_stmt * statement;
  11634. // [ret setValue:@"2" forKey:@"result"];
  11635. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11636. //
  11637. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11638. // // int count=0;
  11639. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11640. // {
  11641. //
  11642. // int i=0;
  11643. // while (sqlite3_step(statement) == SQLITE_ROW)
  11644. // {
  11645. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11646. //
  11647. //
  11648. // char *name = (char*)sqlite3_column_text(statement, 0);
  11649. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11650. //
  11651. // char *description = (char*)sqlite3_column_text(statement, 1);
  11652. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11653. //
  11654. //
  11655. //
  11656. // int product_id = sqlite3_column_int(statement, 2);
  11657. //
  11658. //
  11659. // int wid = sqlite3_column_int(statement, 3);
  11660. // int closeout = sqlite3_column_int(statement, 4);
  11661. // int cid = sqlite3_column_int(statement, 5);
  11662. // int wisdelete = sqlite3_column_int(statement, 6);
  11663. // int more_color = sqlite3_column_int(statement, 7);
  11664. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11665. //
  11666. // if(wid !=0 && wisdelete != 1)
  11667. // [item setValue:@"true" forKey:@"wish_exists"];
  11668. // else
  11669. // [item setValue:@"false" forKey:@"wish_exists"];
  11670. //
  11671. // if(closeout==0)
  11672. // [item setValue:@"false" forKey:@"is_closeout"];
  11673. // else
  11674. // [item setValue:@"true" forKey:@"is_closeout"];
  11675. //
  11676. // if(cid==0)
  11677. // [item setValue:@"false" forKey:@"cart_exists"];
  11678. // else
  11679. // [item setValue:@"true" forKey:@"cart_exists"];
  11680. //
  11681. // if (more_color == 0) {
  11682. // [item setObject:@(false) forKey:@"more_color"];
  11683. // } else if (more_color == 1) {
  11684. // [item setObject:@(true) forKey:@"more_color"];
  11685. // }
  11686. //
  11687. // [item addEntriesFromDictionary:imgjson];
  11688. //
  11689. //
  11690. // // [item setValue:nsurl forKey:@"img"];
  11691. // [item setValue:nsname forKey:@"fash_name"];
  11692. // [item setValue:nsdescription forKey:@"description"];
  11693. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11694. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11695. // i++;
  11696. // }
  11697. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11698. // [ret setObject:items forKey:@"items"];
  11699. // sqlite3_finalize(statement);
  11700. // }
  11701. //
  11702. // [iSalesDB close_db:db];
  11703. //
  11704. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11705. //
  11706. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11707. // dispatch_async(dispatch_get_main_queue(), ^{
  11708. //
  11709. // result(ret);
  11710. // });
  11711. //
  11712. // });
  11713. //
  11714. //
  11715. //
  11716. //}
  11717. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11718. //{
  11719. // [self itemsearch:params limited:YES completionHandler:result];
  11720. //}
  11721. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11722. {
  11723. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11724. NSString* offline_command=params[@"offline_Command"];
  11725. NSMutableDictionary* ret=nil;
  11726. if([offline_command isEqualToString:@"model_NIYMAL"])
  11727. {
  11728. NSString* category = params[@"category"];
  11729. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11730. }
  11731. dispatch_async(dispatch_get_main_queue(), ^{
  11732. result(ret);
  11733. });
  11734. });
  11735. }
  11736. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11737. {
  11738. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11739. int sort = [[params objectForKey:@"sort"] intValue];
  11740. NSString *sort_str = @"";
  11741. switch (sort) {
  11742. case 0:{
  11743. sort_str = @"order by w.modify_time desc";
  11744. }
  11745. break;
  11746. case 1:{
  11747. sort_str = @"order by w.modify_time asc";
  11748. }
  11749. break;
  11750. case 2:{
  11751. sort_str = @"order by m.name asc";
  11752. }
  11753. break;
  11754. case 3:{
  11755. sort_str = @"order by m.name desc";
  11756. }
  11757. break;
  11758. case 4:{
  11759. sort_str = @"order by m.description asc";
  11760. }
  11761. break;
  11762. default:
  11763. break;
  11764. }
  11765. // NSString* user = appDelegate.user;
  11766. sqlite3 *db = [iSalesDB get_db];
  11767. // order by w.create_time
  11768. 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];
  11769. // 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];
  11770. sqlite3_stmt * statement;
  11771. NSDate *date1 = [NSDate date];
  11772. // NSDate *date2 = nil;
  11773. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11774. int count=0;
  11775. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11776. {
  11777. while (sqlite3_step(statement) == SQLITE_ROW)
  11778. {
  11779. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11780. int product_id = sqlite3_column_double(statement, 0);
  11781. char *description = (char*)sqlite3_column_text(statement, 1);
  11782. if(description==nil)
  11783. description= "";
  11784. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11785. int item_id = sqlite3_column_double(statement, 2);
  11786. NSDate *date_image = [NSDate date];
  11787. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11788. printf("image : ");
  11789. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11790. // char *url = (char*)sqlite3_column_text(statement, 3);
  11791. // if(url==nil)
  11792. // url="";
  11793. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11794. int qty = sqlite3_column_int(statement, 3);
  11795. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11796. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11797. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11798. item[@"description"]= nsdescription;
  11799. item[@"img"]= nsurl;
  11800. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11801. count++;
  11802. }
  11803. printf("total time:");
  11804. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11805. ret[@"count"]= [NSNumber numberWithInt:count];
  11806. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11807. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11808. ret[@"result"]= [NSNumber numberWithInt:2];
  11809. sqlite3_finalize(statement);
  11810. }
  11811. [iSalesDB close_db:db];
  11812. dispatch_async(dispatch_get_main_queue(), ^{
  11813. UIApplication * app = [UIApplication sharedApplication];
  11814. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11815. appDelegate.wish_count =count;
  11816. [appDelegate update_count_mark];
  11817. result(ret);
  11818. });
  11819. });
  11820. }
  11821. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11822. {
  11823. // UIApplication * app = [UIApplication sharedApplication];
  11824. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11825. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11826. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11827. sqlite3 *db = [iSalesDB get_db];
  11828. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11829. NSString* product_id=params[@"product_id"];
  11830. NSString *item_count_str = params[@"item_count"];
  11831. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11832. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11833. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11834. // NSString *sql = @"";
  11835. for(int i=0;i<arr.count;i++)
  11836. {
  11837. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11838. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11839. __block int cart_count = 0;
  11840. if (!item_count_str) {
  11841. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11842. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11843. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11844. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11845. cart_count = [[model_set_components lastObject] intValue];
  11846. }];
  11847. }
  11848. if(count==0)
  11849. {
  11850. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11851. sqlite3_stmt *stmt;
  11852. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11853. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11854. if (item_count_arr) {
  11855. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11856. } else {
  11857. sqlite3_bind_int(stmt,2,cart_count);
  11858. }
  11859. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11860. [iSalesDB execSql:@"ROLLBACK" db:db];
  11861. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11862. [iSalesDB close_db:db];
  11863. DebugLog(@"add to wishlist error");
  11864. dispatch_async(dispatch_get_main_queue(), ^{
  11865. result(ret);
  11866. });
  11867. }
  11868. } else {
  11869. int qty = 0;
  11870. if (item_count_arr) {
  11871. qty = [item_count_arr[i] intValue];
  11872. } else {
  11873. qty = cart_count;
  11874. }
  11875. 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]];
  11876. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11877. [iSalesDB execSql:@"ROLLBACK" db:db];
  11878. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11879. [iSalesDB close_db:db];
  11880. DebugLog(@"add to wishlist error");
  11881. dispatch_async(dispatch_get_main_queue(), ^{
  11882. result(ret);
  11883. });
  11884. }
  11885. }
  11886. }
  11887. // [iSalesDB execSql:sql db:db];
  11888. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11889. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11890. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11891. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11892. [iSalesDB close_db:db];
  11893. ret[@"result"]= [NSNumber numberWithInt:2];
  11894. dispatch_async(dispatch_get_main_queue(), ^{
  11895. UIApplication * app = [UIApplication sharedApplication];
  11896. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11897. appDelegate.wish_count =count;
  11898. [appDelegate update_count_mark];
  11899. result(ret);
  11900. });
  11901. });
  11902. }
  11903. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11904. {
  11905. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11906. bool bLogin = appDelegate.bLogin;
  11907. __block NSString* contact_id = appDelegate.contact_id;
  11908. __block NSString* user = appDelegate.user;
  11909. __block NSString* order_code = appDelegate.order_code;
  11910. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11911. NSString* model_name = [params valueForKey:@"product_name"];
  11912. NSString* product_id = [params valueForKey:@"product_id"];
  11913. NSString* upc_code = [params valueForKey:@"upc_code"];
  11914. NSString* category = [params valueForKey:@"category"];
  11915. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11916. if(category==nil) {
  11917. category = default_category_id;
  11918. } else {
  11919. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11920. // 参数重有多个category id
  11921. if (arr_0.count > 1) {
  11922. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11923. // 取交集
  11924. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11925. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11926. [set_0 intersectSet:set_1];
  11927. if (set_0.count == 1) {
  11928. category = (NSString *)[set_0 anyObject];
  11929. } else {
  11930. if ([set_0 containsObject:default_category_id]) {
  11931. category = default_category_id;
  11932. } else {
  11933. category = (NSString *)[set_0 anyObject];
  11934. }
  11935. }
  11936. }
  11937. }
  11938. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11939. sqlite3 *db = [iSalesDB get_db];
  11940. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11941. NSString *sqlQuery = nil;
  11942. if(product_id==nil)
  11943. if(model_name==nil)
  11944. {
  11945. 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='%@';
  11946. }
  11947. else
  11948. {
  11949. 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='%@';
  11950. }
  11951. else
  11952. 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=%@;
  11953. sqlite3_stmt * statement;
  11954. [ret setValue:@"2" forKey:@"result"];
  11955. [ret setValue:@"3" forKey:@"detail_section_count"];
  11956. // int count=0;
  11957. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11958. {
  11959. if (sqlite3_step(statement) == SQLITE_ROW)
  11960. {
  11961. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11962. char *name = (char*)sqlite3_column_text(statement, 0);
  11963. if(name==nil)
  11964. name="";
  11965. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11966. char *description = (char*)sqlite3_column_text(statement, 1);
  11967. if(description==nil)
  11968. description="";
  11969. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11970. int product_id = sqlite3_column_int(statement, 2);
  11971. char *color = (char*)sqlite3_column_text(statement, 3);
  11972. if(color==nil)
  11973. color="";
  11974. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11975. //
  11976. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11977. // if(legcolor==nil)
  11978. // legcolor="";
  11979. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11980. //
  11981. //
  11982. int availability = sqlite3_column_int(statement, 5);
  11983. //
  11984. int incoming_stock = sqlite3_column_int(statement, 6);
  11985. char *demension = (char*)sqlite3_column_text(statement, 7);
  11986. if(demension==nil)
  11987. demension="";
  11988. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11989. // ,,,,,,,,,
  11990. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11991. if(seat_height==nil)
  11992. seat_height="";
  11993. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11994. char *material = (char*)sqlite3_column_text(statement, 9);
  11995. if(material==nil)
  11996. material="";
  11997. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11998. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11999. if(box_dim==nil)
  12000. box_dim="";
  12001. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12002. char *volume = (char*)sqlite3_column_text(statement, 11);
  12003. if(volume==nil)
  12004. volume="";
  12005. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12006. double weight = sqlite3_column_double(statement, 12);
  12007. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12008. if(model_set==nil)
  12009. model_set="";
  12010. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12011. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12012. if(load_ability==nil)
  12013. load_ability="";
  12014. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12015. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12016. // if(default_category==nil)
  12017. // default_category="";
  12018. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12019. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12020. if(fabric_content==nil)
  12021. fabric_content="";
  12022. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12023. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12024. if(assembling==nil)
  12025. assembling="";
  12026. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12027. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12028. if(made_in==nil)
  12029. made_in="";
  12030. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12031. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12032. if(special_remarks==nil)
  12033. special_remarks="";
  12034. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12035. int stockUcom = sqlite3_column_double(statement, 20);
  12036. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12037. if(product_group==nil)
  12038. product_group="";
  12039. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12040. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12041. // if(fashion_selector==nil)
  12042. // fashion_selector="";
  12043. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12044. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12045. if(selector_field==nil)
  12046. selector_field="";
  12047. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12048. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12049. if(property_field==nil)
  12050. property_field="";
  12051. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12052. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12053. if(packaging==nil)
  12054. packaging="";
  12055. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12056. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12057. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12058. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12059. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12060. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12061. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12062. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12063. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12064. NSString* Availability=nil;
  12065. if(availability>0)
  12066. Availability=[NSString stringWithFormat:@"%d",availability];
  12067. else
  12068. Availability = @"Out of Stock";
  12069. [img_section setValue:Availability forKey:@"Availability"];
  12070. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12071. char *eta = (char*)sqlite3_column_text(statement, 25);
  12072. if(eta==nil)
  12073. eta="";
  12074. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12075. if ([nseta isEqualToString:@"null"]) {
  12076. nseta = @"";
  12077. }
  12078. if (availability <= 0) {
  12079. [img_section setValue:nseta forKey:@"ETA"];
  12080. }
  12081. int item_id = sqlite3_column_int(statement, 26);
  12082. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12083. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12084. NSString* Price=nil;
  12085. if(bLogin==false)
  12086. Price=@"Must Sign in.";
  12087. else
  12088. {
  12089. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12090. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12091. if(price==nil)
  12092. Price=@"No Price.";
  12093. else
  12094. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12095. }
  12096. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12097. [img_section setValue:Price forKey:@"price"];
  12098. [img_section setValue:nsname forKey:@"model_name"];
  12099. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12100. if (order_code) { // 离线order code即so#
  12101. 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];
  12102. __block int cartQTY = 0;
  12103. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12104. cartQTY = sqlite3_column_int(stmt, 0);
  12105. }];
  12106. if (cartQTY > 0) {
  12107. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12108. }
  12109. }
  12110. [ret setObject:img_section forKey:@"img_section"];
  12111. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12112. int detail0_item_count=0;
  12113. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12114. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12115. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12116. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12117. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12118. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12119. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12120. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12121. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12122. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12123. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12124. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12125. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12126. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12127. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12128. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12129. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12130. {
  12131. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12132. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12133. }
  12134. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12135. [detail0_section setValue:@"kv" forKey:@"type"];
  12136. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12137. [ret setObject:detail0_section forKey:@"detail_0"];
  12138. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12139. // [detail1_section setValue:@"detail" forKey:@"target"];
  12140. // [detail1_section setValue:@"popup" forKey:@"action"];
  12141. // [detail1_section setValue:@"content" forKey:@"type"];
  12142. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12143. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12144. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12145. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12146. [ret setObject:detail1_section forKey:@"detail_1"];
  12147. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12148. [detail2_section setValue:@"detail" forKey:@"target"];
  12149. [detail2_section setValue:@"popup" forKey:@"action"];
  12150. [detail2_section setValue:@"content" forKey:@"type"];
  12151. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12152. [detail2_section setValue:@"true" forKey:@"single_row"];
  12153. [detail2_section setValue:@"local" forKey:@"data"];
  12154. [ret setObject:detail2_section forKey:@"detail_2"];
  12155. }
  12156. sqlite3_finalize(statement);
  12157. }
  12158. else
  12159. {
  12160. [ret setValue:@"8" forKey:@"result"];
  12161. }
  12162. // DebugLog(@"count:%d",count);
  12163. [iSalesDB close_db:db];
  12164. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12165. dispatch_async(dispatch_get_main_queue(), ^{
  12166. result(ret);
  12167. });
  12168. });
  12169. }
  12170. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12171. //{
  12172. //
  12173. // UIApplication * app = [UIApplication sharedApplication];
  12174. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12175. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12176. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12177. // sqlite3 *db = [iSalesDB get_db];
  12178. // NSString* collectId=params[@"collectId"];
  12179. //
  12180. //
  12181. //
  12182. //
  12183. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12184. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12185. // [iSalesDB execSql:sqlQuery db:db];
  12186. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12187. // [iSalesDB close_db:db];
  12188. //
  12189. //
  12190. // ret[@"result"]= [NSNumber numberWithInt:2];
  12191. // dispatch_async(dispatch_get_main_queue(), ^{
  12192. //
  12193. // appDelegate.wish_count =count;
  12194. //
  12195. // [appDelegate update_count_mark];
  12196. // result(ret);
  12197. // });
  12198. //
  12199. // });
  12200. //}
  12201. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12202. {
  12203. UIApplication * app = [UIApplication sharedApplication];
  12204. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12205. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12206. [iSalesDB disable_trigger];
  12207. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12208. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12209. [iSalesDB enable_trigger];
  12210. dispatch_async(dispatch_get_main_queue(), ^{
  12211. //
  12212. // NSString* user = [params valueForKey:@"user"];
  12213. //
  12214. // NSString* password = [params valueForKey:@"password"];
  12215. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12216. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12217. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12218. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12219. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12220. [appDelegate update_count_mark];
  12221. appDelegate.can_show_price =false;
  12222. appDelegate.can_see_price =false;
  12223. appDelegate.can_create_portfolio =false;
  12224. appDelegate.can_create_order =false;
  12225. appDelegate.can_cancel_order =false;
  12226. appDelegate.can_set_cart_price =false;
  12227. appDelegate.can_delete_order =false;
  12228. appDelegate.can_submit_order =false;
  12229. appDelegate.can_set_tearsheet_price =false;
  12230. appDelegate.can_update_contact_info = false;
  12231. appDelegate.save_order_logout = false;
  12232. appDelegate.submit_order_logout = false;
  12233. appDelegate.alert_sold_in_quantities = false;
  12234. appDelegate.ipad_perm =nil ;
  12235. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12236. appDelegate.OrderFilter= nil;
  12237. [appDelegate SetSo:nil];
  12238. [appDelegate set_main_button_panel];
  12239. result(ret);
  12240. });
  12241. });
  12242. }
  12243. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12244. {
  12245. // iSalesDB.
  12246. // UIApplication * app = [UIApplication sharedApplication];
  12247. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12248. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12249. [iSalesDB disable_trigger];
  12250. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12251. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12252. [iSalesDB enable_trigger];
  12253. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12254. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12255. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12256. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12257. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12258. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12259. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12260. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12261. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12262. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12263. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12264. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12265. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12266. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12267. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12268. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12269. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12270. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12271. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12272. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12273. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12274. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12275. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12276. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12277. [arr_name addObject:customer_first_name];
  12278. [arr_name addObject:customer_last_name];
  12279. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12280. // default ship from
  12281. 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';";
  12282. __block NSString *cid = @"";
  12283. __block NSString *name = @"";
  12284. __block NSString *ext = @"";
  12285. __block NSString *contact = @"";
  12286. __block NSString *email = @"";
  12287. __block NSString *fax = @"";
  12288. __block NSString *phone = @"";
  12289. sqlite3 *db = [iSalesDB get_db];
  12290. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12291. cid = [self textAtColumn:0 statement:statment];
  12292. name = [self textAtColumn:1 statement:statment];
  12293. ext = [self textAtColumn:2 statement:statment];
  12294. contact = [self textAtColumn:3 statement:statment];
  12295. email = [self textAtColumn:4 statement:statment];
  12296. fax = [self textAtColumn:5 statement:statment];
  12297. phone = [self textAtColumn:6 statement:statment];
  12298. }];
  12299. NSString* so_id = [self get_offline_soid:db];
  12300. if(so_id==nil)
  12301. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12302. 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];
  12303. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12304. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12305. //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'
  12306. //soId
  12307. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12308. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12309. sqlite3_stmt * statement;
  12310. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12311. {
  12312. if (sqlite3_step(statement) == SQLITE_ROW)
  12313. {
  12314. // char *name = (char*)sqlite3_column_text(statement, 1);
  12315. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12316. //ret = sqlite3_column_int(statement, 0);
  12317. char *soId = (char*)sqlite3_column_text(statement, 0);
  12318. if(soId==nil)
  12319. soId="";
  12320. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12321. [ret setValue:nssoId forKey:@"soId"];
  12322. [ret setValue:nssoId forKey:@"orderCode"];
  12323. }
  12324. sqlite3_finalize(statement);
  12325. }
  12326. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12327. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12328. [iSalesDB close_db:db];
  12329. dispatch_async(dispatch_get_main_queue(), ^{
  12330. result(ret);
  12331. });
  12332. });
  12333. }
  12334. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12335. {
  12336. UIApplication * app = [UIApplication sharedApplication];
  12337. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12338. bool can_create_backorder= appDelegate.can_create_backorder;
  12339. NSString* user = appDelegate.user;
  12340. NSString* contact_id = appDelegate.contact_id;
  12341. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12342. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12343. sqlite3 *db = [iSalesDB get_db];
  12344. NSString* orderCode=params[@"orderCode"];
  12345. int sort = [[params objectForKey:@"sort"] intValue];
  12346. NSString *sort_str = @"";
  12347. switch (sort) {
  12348. case 0:{
  12349. sort_str = @"order by c.modify_time desc";
  12350. }
  12351. break;
  12352. case 1:{
  12353. sort_str = @"order by c.modify_time asc";
  12354. }
  12355. break;
  12356. case 2:{
  12357. sort_str = @"order by m.name asc";
  12358. }
  12359. break;
  12360. case 3:{
  12361. sort_str = @"order by m.name desc";
  12362. }
  12363. break;
  12364. case 4:{
  12365. sort_str = @"order by m.description asc";
  12366. }
  12367. break;
  12368. default:
  12369. break;
  12370. }
  12371. 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 ];
  12372. // 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 ];
  12373. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12374. DebugLog(@"offline_login sql:%@",sqlQuery);
  12375. sqlite3_stmt * statement;
  12376. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12377. NSDate *date1 = [NSDate date];
  12378. int count=0;
  12379. int cart_count=0;
  12380. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12381. if ( dbresult== SQLITE_OK)
  12382. {
  12383. while (sqlite3_step(statement) == SQLITE_ROW)
  12384. {
  12385. // NSDate *row_date = [NSDate date];
  12386. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12387. int product_id = sqlite3_column_int(statement, 0);
  12388. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12389. int item_id = sqlite3_column_int(statement, 7);
  12390. NSString* Price=nil;
  12391. if(str_price==nil)
  12392. {
  12393. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12394. // NSDate *price_date = [NSDate date];
  12395. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12396. // DebugLog(@"price time interval");
  12397. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12398. if(price==nil)
  12399. Price=@"No Price.";
  12400. else
  12401. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12402. }
  12403. else
  12404. {
  12405. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12406. }
  12407. double discount = sqlite3_column_double(statement, 2);
  12408. int item_count = sqlite3_column_int(statement, 3);
  12409. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12410. NSString *nsline_note=nil;
  12411. if(line_note!=nil)
  12412. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12413. char *name = (char*)sqlite3_column_text(statement, 5);
  12414. NSString *nsname=nil;
  12415. if(name!=nil)
  12416. nsname= [[NSString alloc]initWithUTF8String:name];
  12417. char *description = (char*)sqlite3_column_text(statement, 6);
  12418. NSString *nsdescription=nil;
  12419. if(description!=nil)
  12420. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12421. int stockUom = sqlite3_column_int(statement, 8);
  12422. int _id = sqlite3_column_int(statement, 9);
  12423. int availability = sqlite3_column_int(statement, 10);
  12424. // NSDate *subtotal_date = [NSDate date];
  12425. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12426. // DebugLog(@"subtotal_date time interval");
  12427. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12428. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12429. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12430. int carton=[bsubtotaljson[@"carton"] intValue];
  12431. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12432. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12433. // NSDate *img_date = [NSDate date];
  12434. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12435. // DebugLog(@"img_date time interval");
  12436. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12437. itemjson[@"model"]=nsname;
  12438. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12439. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12440. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12441. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12442. itemjson[@"check"]=@"true";
  12443. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12444. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12445. itemjson[@"unit_price"]=Price;
  12446. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12447. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12448. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12449. itemjson[@"note"]=nsline_note;
  12450. if (!can_create_backorder) {
  12451. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12452. }
  12453. // NSDate *date2 = [NSDate date];
  12454. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12455. // DebugLog(@"model_bundle time interval");
  12456. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12457. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12458. count++;
  12459. // DebugLog(@"row time interval");
  12460. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12461. }
  12462. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12463. sqlite3_finalize(statement);
  12464. }
  12465. DebugLog(@"request cart total time interval");
  12466. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12467. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12468. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12469. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12470. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12471. ret[@"count"]=[NSNumber numberWithInt:count ];
  12472. ret[@"mode"]=@"Regular Mode";
  12473. [iSalesDB close_db:db];
  12474. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12475. DebugLog(@"general notes :%@",general_note);
  12476. ret[@"general_note"]= general_note;
  12477. dispatch_async(dispatch_get_main_queue(), ^{
  12478. result(ret);
  12479. });
  12480. });
  12481. }
  12482. @end