OLDataProvider.m 690 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236
  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*) getScanOrderPath
  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:appDelegate.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*) getScanTempPath
  1716. {
  1717. if(RASingleton.sharedInstance.scan_temp_code == nil)
  1718. RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1719. return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1720. }
  1721. +(void) moveScanTemp2Order
  1722. {
  1723. NSString *orderdir = [OLDataProvider getScanOrderPath];
  1724. NSError *error = nil;
  1725. NSString* tempdir = [OLDataProvider getScanTempPath];
  1726. // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1727. // for(int i=0;i<arr_files.count;i++)
  1728. // {
  1729. // NSString* file=arr_files[i];
  1730. //
  1731. //
  1732. // }
  1733. [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1734. }
  1735. +(NSString*) getUserPath
  1736. {
  1737. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1738. NSString *documents = [paths objectAtIndex:0];
  1739. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1740. NSString* servername = addressDic[@"name"];
  1741. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1742. NSString* user = appDelegate.user;
  1743. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1744. userpath = [userpath stringByAppendingPathComponent:user];
  1745. return userpath;
  1746. }
  1747. +(NSString*) getScanPath
  1748. {
  1749. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1750. if(appDelegate.order_code!=nil)
  1751. {
  1752. return [self getScanOrderPath];
  1753. }
  1754. else
  1755. {
  1756. [self createScanTempFolder];
  1757. return [self getScanTempPath];
  1758. }
  1759. }
  1760. +(NSString*) createScanTempCode
  1761. {
  1762. return [[NSUUID new] UUIDString];
  1763. }
  1764. +(NSString*) scanTemplatePath:(NSString*)file
  1765. {
  1766. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1767. NSString *documents = [paths objectAtIndex:0];
  1768. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1769. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1770. return templatefile;
  1771. }
  1772. +(void) guestLogin
  1773. {
  1774. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1775. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1776. }
  1777. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1778. {
  1779. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1780. NSString *documents = [paths objectAtIndex:0];
  1781. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1782. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1783. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1784. return [dict mutableCopy];
  1785. }
  1786. +(void) deleteSubmittedOrder:(NSString*) order_code
  1787. {
  1788. NSString *userdir = [OLDataProvider getUserPath];
  1789. if(order_code.length==0)
  1790. return;
  1791. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1792. // NSString *documents = [paths objectAtIndex:0];
  1793. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1794. if([appDelegate.order_code isEqualToString:order_code])
  1795. {
  1796. //要删除的订单是当前打开的订单;
  1797. appDelegate.order_code = nil;
  1798. appDelegate.customerInfo = nil;
  1799. RASingleton.sharedInstance.scan_cart = nil;
  1800. appDelegate.contact_id = nil;
  1801. // [appDelegate updateScanButton:false];
  1802. [appDelegate update_count_mark];
  1803. }
  1804. //删除订单目录
  1805. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1806. //维护订单列表
  1807. NSMutableDictionary* orderlist = nil;
  1808. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1809. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1810. // order list 为空,不用维护
  1811. #ifdef RA_NOTIFICATION
  1812. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1813. #else
  1814. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1815. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1816. #endif
  1817. return;
  1818. // [orderlist addObject:appDelegate.order_code];
  1819. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1820. }
  1821. else
  1822. {
  1823. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1824. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1825. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1826. }
  1827. int count = [orderlist[@"count"] intValue];
  1828. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1829. int newcount = 0;
  1830. for(int i=0;i<count;i++)
  1831. {
  1832. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1833. if([item[@"order_code"] isEqualToString: order_code ])
  1834. continue;
  1835. else
  1836. {
  1837. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1838. newcount++;
  1839. }
  1840. }
  1841. newlist[@"count"] = @(newcount);
  1842. [RAUtils dicttofile:orderlistPath dict:newlist];
  1843. #ifdef RA_NOTIFICATION
  1844. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1845. #else
  1846. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1847. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1848. #endif
  1849. }
  1850. +(void) updateScanOrder:(NSString*) order_code
  1851. {
  1852. NSString *userdir = [OLDataProvider getUserPath];
  1853. if(order_code.length==0)
  1854. return;
  1855. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1856. // NSString *documents = [paths objectAtIndex:0];
  1857. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1858. NSMutableDictionary* orderlist = nil;
  1859. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1860. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1861. // order list 为空,不用维护
  1862. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1863. return;
  1864. // [orderlist addObject:appDelegate.order_code];
  1865. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1866. }
  1867. else
  1868. {
  1869. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1870. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1871. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1872. }
  1873. int count = [orderlist[@"count"] intValue];
  1874. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1875. float p =0;
  1876. float s =0;
  1877. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1878. {
  1879. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1880. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1881. int mcount = [cartitem[@"count"] intValue];
  1882. double msurcharge =0;
  1883. if(RASingleton.sharedInstance.price_type==1)
  1884. {
  1885. if([cartitem[@"special_price"] boolValue])
  1886. {
  1887. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price2"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1888. }
  1889. else
  1890. {
  1891. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price1"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1892. }
  1893. }
  1894. s+=msurcharge;
  1895. }
  1896. // int newcount = 0;
  1897. for(int i=0;i<count;i++)
  1898. {
  1899. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1900. if([item[@"order_code"] isEqualToString: order_code ])
  1901. {
  1902. item[@"price"] = [NSString stringWithFormat:@"%.2f",p+s];
  1903. item[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1904. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1905. }
  1906. }
  1907. // newlist[@"count"] = @(newcount);
  1908. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1909. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1910. }
  1911. +(void) deleteScanOrder:(NSString*) order_code
  1912. {
  1913. NSString *userdir = [OLDataProvider getUserPath];
  1914. if(order_code.length==0)
  1915. return;
  1916. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1917. // NSString *documents = [paths objectAtIndex:0];
  1918. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1919. if([appDelegate.order_code isEqualToString:order_code])
  1920. {
  1921. //要删除的订单是当前打开的订单;
  1922. appDelegate.order_code = nil;
  1923. appDelegate.customerInfo = nil;
  1924. RASingleton.sharedInstance.scan_cart = nil;
  1925. appDelegate.contact_id = nil;
  1926. // [appDelegate updateScanButton:false];
  1927. [appDelegate update_count_mark];
  1928. }
  1929. //删除订单目录
  1930. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1931. //维护订单列表
  1932. NSMutableDictionary* orderlist = nil;
  1933. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1934. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1935. // order list 为空,不用维护
  1936. #ifdef RA_NOTIFICATION
  1937. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1938. #else
  1939. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1940. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1941. #endif
  1942. return;
  1943. // [orderlist addObject:appDelegate.order_code];
  1944. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1945. }
  1946. else
  1947. {
  1948. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1949. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1950. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1951. }
  1952. int count = [orderlist[@"count"] intValue];
  1953. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1954. int newcount = 0;
  1955. for(int i=0;i<count;i++)
  1956. {
  1957. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1958. if([item[@"order_code"] isEqualToString: order_code ])
  1959. continue;
  1960. else
  1961. {
  1962. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1963. newcount++;
  1964. }
  1965. }
  1966. newlist[@"count"] = @(newcount);
  1967. [RAUtils dicttofile:orderlistPath dict:newlist];
  1968. #ifdef RA_NOTIFICATION
  1969. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1970. #else
  1971. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1972. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1973. #endif
  1974. }
  1975. +(void) saveScanCart:(NSMutableDictionary*) cart
  1976. {
  1977. if(cart==nil)
  1978. return;
  1979. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1980. // NSString *documents = [paths objectAtIndex:0];
  1981. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1982. if(RASingleton.sharedInstance.price_type==0)
  1983. {
  1984. NSMutableDictionary * section = cart[@"section_0"];
  1985. int count = [section[@"count"] intValue];
  1986. for(int i=0;i<count;i++)
  1987. {
  1988. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1989. [item removeObjectForKey:@"discount"];
  1990. [item removeObjectForKey:@"note"];
  1991. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  1992. }
  1993. cart[@"section_0"]=section;
  1994. }
  1995. NSString *orderdir = [self getScanPath];
  1996. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1997. [RAUtils dicttofile:cartpath dict:cart];
  1998. }
  1999. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2000. {
  2001. assert(add_params[@"user"]!=nil);
  2002. // assert(add_params[@"contact_id"]!=nil);
  2003. // assert(add_params[@"password"]!=nil);
  2004. NSString* serial= [[NSUUID UUID] UUIDString];
  2005. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2006. NSString *cachefolder = [paths objectAtIndex:0];
  2007. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2008. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2009. NSFileManager* fileManager = [NSFileManager defaultManager];
  2010. BOOL bdir=YES;
  2011. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2012. {
  2013. NSError *error = nil;
  2014. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2015. if(!bsuccess)
  2016. DebugLog(@"Create UPLOAD folder failed");
  2017. }
  2018. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2019. ret[@"contact"]=[self prepareContact:serial];
  2020. ret[@"wishlist"]=[self prepareWishlist:serial];
  2021. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2022. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2023. ret[@"view_portfolio"] = [self preparePDF:serial];
  2024. NSString* str= [RAConvertor dict2string:ret];
  2025. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2026. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2027. NSError *error=nil;
  2028. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2029. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2030. ZipArchive* zip = [[ZipArchive alloc] init];
  2031. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2032. for(int i=0;i<arr_files.count;i++)
  2033. {
  2034. NSString* file=arr_files[i];
  2035. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2036. }
  2037. if( ![zip CloseZipFile2] )
  2038. {
  2039. zippath = @"";
  2040. }
  2041. ret[@"file"]=zippath;
  2042. return ret;
  2043. }
  2044. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2045. {
  2046. if(filename.length==0)
  2047. return false;
  2048. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2049. bool ret=false;
  2050. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2051. NSString *cachefolder = [paths objectAtIndex:0];
  2052. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2053. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2054. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2055. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2056. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2057. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2058. NSFileManager* fileManager = [NSFileManager defaultManager];
  2059. BOOL bdir=NO;
  2060. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2061. {
  2062. NSError *error = nil;
  2063. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2064. {
  2065. ret=false;
  2066. }
  2067. else
  2068. {
  2069. ret=true;
  2070. }
  2071. }
  2072. return ret;
  2073. }
  2074. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2075. {
  2076. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2077. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2078. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2079. NSString* where=@" 1=1";
  2080. if (ver!=nil) {
  2081. where=@"is_dirty=1";
  2082. }
  2083. sqlite3 *db = [iSalesDB get_db];
  2084. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2085. sqlite3_stmt * statement;
  2086. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2087. int count=0;
  2088. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2089. if ( dbresult== SQLITE_OK)
  2090. {
  2091. while (sqlite3_step(statement) == SQLITE_ROW)
  2092. {
  2093. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2094. int _id = sqlite3_column_int(statement, 0);
  2095. int product_id = sqlite3_column_int(statement, 1);
  2096. int item_id = sqlite3_column_int(statement, 2);
  2097. int qty = sqlite3_column_int(statement, 3);
  2098. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2099. NSString* nscreate_time=nil;
  2100. if(create_time!=nil)
  2101. {
  2102. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2103. }
  2104. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2105. NSString* nsmodify_time=nil;
  2106. if(modify_time!=nil)
  2107. {
  2108. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2109. }
  2110. int is_delete = sqlite3_column_int(statement, 6);
  2111. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2112. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2113. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2114. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2115. itemjson[@"create_time"]=nscreate_time;
  2116. itemjson[@"modify_time"]=nsmodify_time;
  2117. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2118. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2119. count++;
  2120. }
  2121. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2122. sqlite3_finalize(statement);
  2123. }
  2124. ret[@"count"]=[NSNumber numberWithInt:count ];
  2125. [iSalesDB close_db:db];
  2126. return ret;
  2127. }
  2128. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2129. {
  2130. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2131. // UIApplication * app = [UIApplication sharedApplication];
  2132. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2133. assert(add_params[@"user"]!=nil);
  2134. // assert(add_params[@"password"]!=nil);
  2135. 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 ];
  2136. // 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 ];
  2137. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2138. DebugLog(@"offline_login sql:%@",sqlQuery);
  2139. sqlite3_stmt * statement;
  2140. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2141. int count=0;
  2142. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2143. if ( dbresult== SQLITE_OK)
  2144. {
  2145. while (sqlite3_step(statement) == SQLITE_ROW)
  2146. {
  2147. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2148. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2149. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2150. int product_id = sqlite3_column_int(statement, 0);
  2151. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2152. int item_id = sqlite3_column_int(statement, 7);
  2153. NSString* Price=nil;
  2154. if(str_price==nil)
  2155. {
  2156. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2157. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2158. if(price==nil)
  2159. Price=@"No Price.";
  2160. else
  2161. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2162. }
  2163. else
  2164. {
  2165. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2166. }
  2167. double discount = sqlite3_column_double(statement, 2);
  2168. int item_count = sqlite3_column_int(statement, 3);
  2169. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2170. NSString *nsline_note=nil;
  2171. if(line_note!=nil)
  2172. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2173. char *name = (char*)sqlite3_column_text(statement, 5);
  2174. NSString *nsname=nil;
  2175. if(name!=nil)
  2176. nsname= [[NSString alloc]initWithUTF8String:name];
  2177. char *description = (char*)sqlite3_column_text(statement, 6);
  2178. NSString *nsdescription=nil;
  2179. if(description!=nil)
  2180. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2181. // int stockUom = sqlite3_column_int(statement, 8);
  2182. // int _id = sqlite3_column_int(statement, 9);
  2183. //
  2184. //
  2185. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2186. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2187. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2188. // int carton=[bsubtotaljson[@"carton"] intValue];
  2189. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2190. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2191. itemjson[@"model"]=nsname;
  2192. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2193. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2194. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2195. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2196. // itemjson[@"check"]=@"true";
  2197. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2198. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2199. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2200. itemjson[@"unit_price"]=Price;
  2201. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2202. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2203. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2204. itemjson[@"note"]=nsline_note;
  2205. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2206. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2207. count++;
  2208. }
  2209. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2210. sqlite3_finalize(statement);
  2211. }
  2212. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2213. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2214. return ret;
  2215. }
  2216. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2217. {
  2218. assert(add_params[@"user"]!=nil);
  2219. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2220. sqlite3 *db = [iSalesDB get_db];
  2221. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2222. // for(int i=0;i<arr_soid.count;i++)
  2223. sqlite3_stmt * statement;
  2224. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2225. //int count=0;
  2226. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2227. if ( dbresult== SQLITE_OK)
  2228. {
  2229. int count=0;
  2230. while (sqlite3_step(statement) == SQLITE_ROW)
  2231. {
  2232. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2233. int _id = sqlite3_column_int(statement, 0);
  2234. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2235. NSString* nssync_data=nil;
  2236. if(sync_data!=nil)
  2237. {
  2238. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2239. }
  2240. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2241. NSString* nsimg_1=nil;
  2242. if(img_1!=nil)
  2243. {
  2244. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2245. [self copy_upImg:serial file:nsimg_1];
  2246. }
  2247. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2248. NSString* nsso_no=nil;
  2249. if(so_no!=nil)
  2250. {
  2251. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2252. }
  2253. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2254. itemjson[@"order_type"]=@"submit order";
  2255. else
  2256. itemjson[@"order_type"]=@"archive order";
  2257. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2258. itemjson[@"json_data"]= nssync_data;
  2259. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2260. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2261. count++;
  2262. }
  2263. ret[@"count"]=[NSNumber numberWithInt:count ];
  2264. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2265. }
  2266. sqlite3_finalize(statement);
  2267. [iSalesDB close_db:db];
  2268. return ret;
  2269. }
  2270. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2271. {
  2272. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2273. sqlite3 *db = [iSalesDB get_db];
  2274. // UIApplication * app = [UIApplication sharedApplication];
  2275. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2276. 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";
  2277. sqlite3_stmt * statement;
  2278. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2279. int count=0;
  2280. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2281. if ( dbresult== SQLITE_OK)
  2282. {
  2283. while (sqlite3_step(statement) == SQLITE_ROW)
  2284. {
  2285. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2286. int _id = sqlite3_column_int(statement, 0);
  2287. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2288. NSString* nsimg_0=nil;
  2289. if(img_0!=nil)
  2290. {
  2291. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2292. [self copy_upImg:serial file:nsimg_0];
  2293. }
  2294. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2295. NSString* nsimg_1=nil;
  2296. if(img_1!=nil)
  2297. {
  2298. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2299. [self copy_upImg:serial file:nsimg_1];
  2300. }
  2301. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2302. NSString* nsimg_2=nil;
  2303. if(img_2!=nil)
  2304. {
  2305. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2306. [self copy_upImg:serial file:nsimg_2];
  2307. }
  2308. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2309. NSString* nssync_data=nil;
  2310. if(sync_data!=nil)
  2311. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2312. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2313. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2314. itemjson[@"json_data"]= nssync_data;
  2315. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2316. count++;
  2317. }
  2318. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2319. sqlite3_finalize(statement);
  2320. }
  2321. ret[@"count"]=[NSNumber numberWithInt:count ];
  2322. [iSalesDB close_db:db];
  2323. return ret;
  2324. }
  2325. +(bool) check_offlinedata
  2326. {
  2327. // UIApplication * app = [UIApplication sharedApplication];
  2328. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2329. sqlite3 *db = [iSalesDB get_db];
  2330. NSString * where=@"1=1";
  2331. // if(appDelegate.user!=nil)
  2332. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2333. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2334. [iSalesDB close_db:db];
  2335. if(count==0)
  2336. {
  2337. return false;
  2338. }
  2339. return true;
  2340. //
  2341. //[iSalesDB close_db:db];
  2342. }
  2343. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2344. {
  2345. // UIApplication * app = [UIApplication sharedApplication];
  2346. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2347. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2348. sqlite3 *db = [iSalesDB get_db];
  2349. NSString* collectId=params[@"collectId"];
  2350. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2351. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2352. [iSalesDB execSql:sqlQuery db:db];
  2353. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2354. [iSalesDB close_db:db];
  2355. // appDelegate.wish_count =count;
  2356. //
  2357. // [appDelegate update_count_mark];
  2358. ret[@"result"]= [NSNumber numberWithInt:2];
  2359. ret[@"wish_count"]=@(count);
  2360. return ret;
  2361. }
  2362. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2363. //{
  2364. //
  2365. // UIApplication * app = [UIApplication sharedApplication];
  2366. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2367. //
  2368. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2369. // sqlite3 *db = [iSalesDB get_db];
  2370. // NSString* product_id=params[@"product_id"];
  2371. //
  2372. //
  2373. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2374. //
  2375. //
  2376. // for(int i=0;i<arr.count;i++)
  2377. // {
  2378. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2379. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2380. // if(count==0)
  2381. // {
  2382. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2383. // [iSalesDB execSql:sqlQuery db:db];
  2384. // }
  2385. // }
  2386. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2387. // [iSalesDB close_db:db];
  2388. //
  2389. // appDelegate.wish_count =count;
  2390. //
  2391. // [appDelegate update_count_mark];
  2392. // ret[@"result"]= [NSNumber numberWithInt:2];
  2393. // return ret;
  2394. // //
  2395. // //return ret;
  2396. //}
  2397. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2398. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2399. DebugLog(@"time interval: %lf",interval);
  2400. }
  2401. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2402. //{
  2403. // UIApplication * app = [UIApplication sharedApplication];
  2404. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2405. //
  2406. // int sort = [[params objectForKey:@"sort"] intValue];
  2407. // NSString *sort_str = @"";
  2408. // switch (sort) {
  2409. // case 0:{
  2410. // sort_str = @"order by w.modify_time desc";
  2411. // }
  2412. // break;
  2413. // case 1:{
  2414. // sort_str = @"order by w.modify_time asc";
  2415. // }
  2416. // break;
  2417. // case 2:{
  2418. // sort_str = @"order by m.name asc";
  2419. // }
  2420. // break;
  2421. // case 3:{
  2422. // sort_str = @"order by m.name desc";
  2423. // }
  2424. // break;
  2425. // case 4:{
  2426. // sort_str = @"order by m.description asc";
  2427. // }
  2428. // break;
  2429. //
  2430. // default:
  2431. // break;
  2432. //
  2433. // }
  2434. //
  2435. //
  2436. //// NSString* user = appDelegate.user;
  2437. //
  2438. // sqlite3 *db = [iSalesDB get_db];
  2439. //
  2440. // // order by w.create_time
  2441. // 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];
  2442. //
  2443. //// 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];
  2444. //
  2445. //
  2446. // sqlite3_stmt * statement;
  2447. //
  2448. // NSDate *date1 = [NSDate date];
  2449. //// NSDate *date2 = nil;
  2450. //
  2451. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2452. // int count=0;
  2453. //
  2454. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2455. // {
  2456. //
  2457. //
  2458. // while (sqlite3_step(statement) == SQLITE_ROW)
  2459. // {
  2460. //
  2461. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2462. //
  2463. // int product_id = sqlite3_column_double(statement, 0);
  2464. //
  2465. //
  2466. //
  2467. //
  2468. //
  2469. // char *description = (char*)sqlite3_column_text(statement, 1);
  2470. // if(description==nil)
  2471. // description= "";
  2472. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2473. //
  2474. // int item_id = sqlite3_column_double(statement, 2);
  2475. //
  2476. // NSDate *date_image = [NSDate date];
  2477. //
  2478. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2479. //
  2480. // printf("image : ");
  2481. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2482. //
  2483. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2484. // // if(url==nil)
  2485. // // url="";
  2486. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2487. //
  2488. // int qty = sqlite3_column_int(statement, 3);
  2489. //
  2490. //
  2491. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2492. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2493. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2494. // item[@"description"]= nsdescription;
  2495. // item[@"img"]= nsurl;
  2496. //
  2497. //
  2498. //
  2499. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2500. // count++;
  2501. //
  2502. // }
  2503. // printf("total time:");
  2504. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2505. //
  2506. // ret[@"count"]= [NSNumber numberWithInt:count];
  2507. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2508. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2509. // ret[@"result"]= [NSNumber numberWithInt:2];
  2510. //
  2511. //
  2512. // appDelegate.wish_count =count;
  2513. //
  2514. // [appDelegate update_count_mark];
  2515. // sqlite3_finalize(statement);
  2516. //
  2517. //
  2518. //
  2519. //
  2520. // }
  2521. //
  2522. // [iSalesDB close_db:db];
  2523. //
  2524. // return ret;
  2525. //}
  2526. +(NSDictionary*) offline_notimpl
  2527. {
  2528. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2529. ret[@"result"]=@"8";
  2530. ret[@"err_msg"]=@"offline mode does not support this function.";
  2531. return ret;
  2532. }
  2533. +(NSDictionary*) offline_home
  2534. {
  2535. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2536. NSString *cachefolder = [paths objectAtIndex:0];
  2537. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2538. NSData* json =nil;
  2539. json=[NSData dataWithContentsOfFile:img_cache];
  2540. if(json==nil)
  2541. return nil;
  2542. NSError *error=nil;
  2543. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2544. return menu;
  2545. }
  2546. +(NSDictionary*) offline_category_menu
  2547. {
  2548. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2549. NSString *cachefolder = [paths objectAtIndex:0];
  2550. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2551. NSData* json =nil;
  2552. json=[NSData dataWithContentsOfFile:img_cache];
  2553. if(json==nil)
  2554. return nil;
  2555. NSError *error=nil;
  2556. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2557. return menu;
  2558. }
  2559. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2560. {
  2561. NSString* offline_command=params[@"offline_Command"];
  2562. NSDictionary* ret=nil;
  2563. if([offline_command isEqualToString:@"model_NIYMAL"])
  2564. {
  2565. NSString* category = params[@"category"];
  2566. ret = [self refresh_model_NIYMAL:category];
  2567. }
  2568. return ret;
  2569. }
  2570. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2571. {
  2572. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2573. [ret setValue:@"2" forKey:@"result"];
  2574. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2575. sqlite3* db= [iSalesDB get_db];
  2576. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2577. [iSalesDB close_db:db];
  2578. [ret setObject:detail1_section forKey:@"detail_1"];
  2579. return ret;
  2580. }
  2581. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2582. {
  2583. //assert(user!=nil);
  2584. // UIApplication * app = [UIApplication sharedApplication];
  2585. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2586. // NSArray* arr1 = [self get_user_all_price_type];
  2587. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2588. // NSSet *set1 = [NSSet setWithArray:arr1];
  2589. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2590. // if(appDelegate.contact_id==nil)
  2591. // set2=[set1 mutableCopy];
  2592. // else
  2593. // [set2 intersectsSet:set1];
  2594. // NSArray *retarr = [set2 allObjects];
  2595. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2596. // sqlite3 *db = [iSalesDB get_db];
  2597. NSString* sqlQuery = nil;
  2598. if(contact_id==nil)
  2599. {
  2600. if(!blogin)
  2601. return nil;
  2602. 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];
  2603. }
  2604. else
  2605. 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];
  2606. sqlite3_stmt * statement;
  2607. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2608. int count=0;
  2609. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2610. {
  2611. while (sqlite3_step(statement) == SQLITE_ROW)
  2612. {
  2613. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2614. char *name = (char*)sqlite3_column_text(statement, 0);
  2615. if(name==nil)
  2616. name="";
  2617. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2618. // double price = sqlite3_column_double(statement, 1);
  2619. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2620. // if(isnull==nil)
  2621. // item[nsname]= @"No Price";
  2622. // else
  2623. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2624. char *price = (char*)sqlite3_column_text(statement, 1);
  2625. if(price!=nil)
  2626. {
  2627. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2628. nsprice=[AESCrypt fastdecrypt:nsprice];
  2629. if(nsprice.length>0)
  2630. {
  2631. double dp= [nsprice doubleValue];
  2632. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2633. }
  2634. }
  2635. else
  2636. {
  2637. item[nsname]= @"No Price";
  2638. }
  2639. // int type= sqlite3_column_int(statement, 2);
  2640. //item[@"type"]=@"price";
  2641. // item[nsname]= nsprice;
  2642. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2643. count++;
  2644. }
  2645. ret[@"count"]= [NSNumber numberWithInt:count];
  2646. sqlite3_finalize(statement);
  2647. }
  2648. // [iSalesDB close_db:db];
  2649. return ret;
  2650. }
  2651. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2652. //{
  2653. // NSArray* arr1 = [self get_user_all_price_type:db];
  2654. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2655. //
  2656. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2657. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2658. // // [set2 intersectsSet:set1];
  2659. // //
  2660. // //
  2661. // // NSArray *retarr = [set2 allObjects];
  2662. //
  2663. // NSString* whereprice=nil;
  2664. // if(contact_id==nil)
  2665. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2666. // else
  2667. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2668. //
  2669. //
  2670. // // sqlite3 *db = [iSalesDB get_db];
  2671. //
  2672. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2673. // sqlite3_stmt * statement;
  2674. //
  2675. //
  2676. // NSNumber* ret = nil;
  2677. // double dprice=DBL_MAX;
  2678. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2679. // {
  2680. //
  2681. //
  2682. // while (sqlite3_step(statement) == SQLITE_ROW)
  2683. // {
  2684. //
  2685. // // double val = sqlite3_column_double(statement, 0);
  2686. // char *price = (char*)sqlite3_column_text(statement, 0);
  2687. // if(price!=nil)
  2688. // {
  2689. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2690. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2691. // if(nsprice.length>0)
  2692. // {
  2693. // double dp= [nsprice doubleValue];
  2694. // if(dp<dprice)
  2695. // dprice=dp;
  2696. // }
  2697. // }
  2698. // }
  2699. //
  2700. //
  2701. //
  2702. //
  2703. // sqlite3_finalize(statement);
  2704. //
  2705. //
  2706. //
  2707. //
  2708. // }
  2709. //
  2710. // // [iSalesDB close_db:db];
  2711. //
  2712. // if(dprice==DBL_MAX)
  2713. // ret= nil;
  2714. // else
  2715. // ret= [NSNumber numberWithDouble:dprice];
  2716. // return ret;
  2717. //}
  2718. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2719. {
  2720. assert(user!=nil);
  2721. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2722. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2723. NSString* whereprice=nil;
  2724. if(contact_id==nil)
  2725. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2726. else
  2727. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2728. // sqlite3 *db = [iSalesDB get_db];
  2729. NSString *productIdCondition = @"1 = 1";
  2730. if (product_id) {
  2731. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2732. }
  2733. NSString *itemIdCondition = @"";
  2734. if (item_id) {
  2735. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2736. }
  2737. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2738. sqlite3_stmt * statement;
  2739. NSNumber* ret = nil;
  2740. double dprice=DBL_MAX;
  2741. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2742. {
  2743. while (sqlite3_step(statement) == SQLITE_ROW)
  2744. {
  2745. // double val = sqlite3_column_double(statement, 0);
  2746. char *price = (char*)sqlite3_column_text(statement, 0);
  2747. if(price!=nil)
  2748. {
  2749. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2750. nsprice=[AESCrypt fastdecrypt:nsprice];
  2751. if(nsprice.length>0)
  2752. {
  2753. double dp= [nsprice doubleValue];
  2754. if(dp<dprice)
  2755. dprice=dp;
  2756. }
  2757. }
  2758. }
  2759. sqlite3_finalize(statement);
  2760. }
  2761. // [iSalesDB close_db:db];
  2762. if(dprice==DBL_MAX)
  2763. ret= nil;
  2764. else
  2765. ret= [NSNumber numberWithDouble:dprice];
  2766. return ret;
  2767. }
  2768. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2769. {
  2770. assert(user!=nil);
  2771. NSArray* ret=nil;
  2772. // sqlite3 *db = [iSalesDB get_db];
  2773. // no customer assigned , use login user contact_id
  2774. // UIApplication * app = [UIApplication sharedApplication];
  2775. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2776. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2777. sqlite3_stmt * statement;
  2778. // int count=0;
  2779. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2780. {
  2781. if (sqlite3_step(statement) == SQLITE_ROW)
  2782. {
  2783. char *val = (char*)sqlite3_column_text(statement, 0);
  2784. if(val==nil)
  2785. val="";
  2786. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2787. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2788. }
  2789. sqlite3_finalize(statement);
  2790. }
  2791. // [iSalesDB close_db:db];
  2792. return ret;
  2793. }
  2794. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2795. {
  2796. // sqlite3 *db = [iSalesDB get_db];
  2797. if(contact_id==nil)
  2798. {
  2799. // no customer assigned , use login user contact_id
  2800. // UIApplication * app = [UIApplication sharedApplication];
  2801. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2802. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2803. sqlite3_stmt * statement;
  2804. // int count=0;
  2805. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2806. {
  2807. if (sqlite3_step(statement) == SQLITE_ROW)
  2808. {
  2809. char *val = (char*)sqlite3_column_text(statement, 0);
  2810. if(val==nil)
  2811. val="";
  2812. contact_id = [[NSString alloc]initWithUTF8String:val];
  2813. }
  2814. sqlite3_finalize(statement);
  2815. }
  2816. if(contact_id.length<=0)
  2817. {
  2818. // [iSalesDB close_db:db];
  2819. return nil;
  2820. }
  2821. }
  2822. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2823. sqlite3_stmt * statement;
  2824. NSArray* ret=nil;
  2825. // int count=0;
  2826. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2827. {
  2828. if (sqlite3_step(statement) == SQLITE_ROW)
  2829. {
  2830. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2831. if(price_type==nil)
  2832. price_type="";
  2833. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2834. if(nsprice_type.length>0)
  2835. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2836. else
  2837. ret=nil;
  2838. }
  2839. sqlite3_finalize(statement);
  2840. }
  2841. // [iSalesDB close_db:db];
  2842. return ret;
  2843. }
  2844. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2845. {
  2846. NSString* ret= nil;
  2847. // sqlite3 *db = [iSalesDB get_db];
  2848. NSString *sqlQuery = nil;
  2849. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2850. // 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;
  2851. // 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
  2852. if(product_id==nil && model_name)
  2853. 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;
  2854. else if (product_id)
  2855. 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
  2856. sqlite3_stmt * statement;
  2857. // int count=0;
  2858. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2859. {
  2860. if (sqlite3_step(statement) == SQLITE_ROW)
  2861. {
  2862. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2863. if(imgurl==nil)
  2864. imgurl="";
  2865. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2866. ret=nsimgurl;
  2867. }
  2868. sqlite3_finalize(statement);
  2869. }
  2870. else
  2871. {
  2872. [ret setValue:@"8" forKey:@"result"];
  2873. }
  2874. // [iSalesDB close_db:db];
  2875. DebugLog(@"data string: %@",ret );
  2876. return ret;
  2877. }
  2878. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2879. {
  2880. NSString* ret= nil;
  2881. sqlite3 *db = [iSalesDB get_db];
  2882. NSString *sqlQuery = nil;
  2883. if(product_id==nil)
  2884. if(upc_code==nil)
  2885. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2886. else
  2887. 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='%@';
  2888. else
  2889. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2890. sqlite3_stmt * statement;
  2891. // int count=0;
  2892. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2893. {
  2894. if (sqlite3_step(statement) == SQLITE_ROW)
  2895. {
  2896. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2897. if(default_category==nil)
  2898. default_category="";
  2899. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2900. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2901. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2902. ret=nsdefault_category;
  2903. }
  2904. sqlite3_finalize(statement);
  2905. }
  2906. else
  2907. {
  2908. [ret setValue:@"8" forKey:@"result"];
  2909. }
  2910. [iSalesDB close_db:db];
  2911. DebugLog(@"data string: %@",ret );
  2912. return ret;
  2913. }
  2914. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2915. NSString *sql = nil;
  2916. if (product_id != nil) {
  2917. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2918. } else {
  2919. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2920. }
  2921. NSString *result_set = [iSalesDB jk_queryText:sql];
  2922. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2923. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2924. return result_set;
  2925. }
  2926. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2927. //{
  2928. //
  2929. //
  2930. // assert(params[@"user"]!=nil);
  2931. //
  2932. //
  2933. //
  2934. // NSString* model_name = [params valueForKey:@"product_name"];
  2935. //
  2936. // NSString* product_id = [params valueForKey:@"product_id"];
  2937. //
  2938. // NSString* category = [params valueForKey:@"category"];
  2939. //
  2940. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2941. // if(category==nil) {
  2942. // category = default_category_id;
  2943. // } else {
  2944. //
  2945. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2946. // // 参数重有多个category id
  2947. // if (arr_0.count > 1) {
  2948. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2949. //
  2950. // // 取交集
  2951. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2952. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2953. // [set_0 intersectSet:set_1];
  2954. //
  2955. // if (set_0.count == 1) {
  2956. // category = (NSString *)[set_0 anyObject];
  2957. // } else {
  2958. // if ([set_0 containsObject:default_category_id]) {
  2959. // category = default_category_id;
  2960. // } else {
  2961. // category = (NSString *)[set_0 anyObject];
  2962. // }
  2963. // }
  2964. // }
  2965. // }
  2966. //
  2967. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2968. //
  2969. //
  2970. //
  2971. //
  2972. //
  2973. //
  2974. // sqlite3 *db = [iSalesDB get_db];
  2975. //
  2976. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2977. //
  2978. //
  2979. // NSString *sqlQuery = nil;
  2980. //
  2981. // if(product_id==nil)
  2982. // 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='%@';
  2983. // else
  2984. //
  2985. // 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=%@;
  2986. //
  2987. //
  2988. // sqlite3_stmt * statement;
  2989. // [ret setValue:@"2" forKey:@"result"];
  2990. // [ret setValue:@"3" forKey:@"detail_section_count"];
  2991. //
  2992. // // int count=0;
  2993. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2994. // {
  2995. //
  2996. //
  2997. // if (sqlite3_step(statement) == SQLITE_ROW)
  2998. // {
  2999. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3000. //
  3001. //
  3002. // char *name = (char*)sqlite3_column_text(statement, 0);
  3003. // if(name==nil)
  3004. // name="";
  3005. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3006. //
  3007. // char *description = (char*)sqlite3_column_text(statement, 1);
  3008. // if(description==nil)
  3009. // description="";
  3010. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3011. //
  3012. //
  3013. // int product_id = sqlite3_column_int(statement, 2);
  3014. //
  3015. //
  3016. // char *color = (char*)sqlite3_column_text(statement, 3);
  3017. // if(color==nil)
  3018. // color="";
  3019. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3020. // //
  3021. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3022. // // if(legcolor==nil)
  3023. // // legcolor="";
  3024. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3025. // //
  3026. // //
  3027. // int availability = sqlite3_column_int(statement, 5);
  3028. // //
  3029. // int incoming_stock = sqlite3_column_int(statement, 6);
  3030. //
  3031. //
  3032. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3033. // if(demension==nil)
  3034. // demension="";
  3035. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3036. //
  3037. //
  3038. //
  3039. // // ,,,,,,,,,
  3040. //
  3041. //
  3042. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3043. // if(seat_height==nil)
  3044. // seat_height="";
  3045. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3046. //
  3047. //
  3048. // char *material = (char*)sqlite3_column_text(statement, 9);
  3049. // if(material==nil)
  3050. // material="";
  3051. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3052. //
  3053. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3054. // if(box_dim==nil)
  3055. // box_dim="";
  3056. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3057. //
  3058. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3059. // if(volume==nil)
  3060. // volume="";
  3061. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3062. //
  3063. // double weight = sqlite3_column_double(statement, 12);
  3064. //
  3065. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3066. // if(model_set==nil)
  3067. // model_set="";
  3068. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3069. //
  3070. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3071. // if(load_ability==nil)
  3072. // load_ability="";
  3073. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3074. //
  3075. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3076. //// if(default_category==nil)
  3077. //// default_category="";
  3078. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3079. //
  3080. //
  3081. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3082. // if(fabric_content==nil)
  3083. // fabric_content="";
  3084. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3085. //
  3086. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3087. // if(assembling==nil)
  3088. // assembling="";
  3089. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3090. //
  3091. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3092. // if(made_in==nil)
  3093. // made_in="";
  3094. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3095. //
  3096. //
  3097. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3098. // if(special_remarks==nil)
  3099. // special_remarks="";
  3100. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3101. //
  3102. //
  3103. // int stockUcom = sqlite3_column_double(statement, 20);
  3104. //
  3105. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3106. // if(product_group==nil)
  3107. // product_group="";
  3108. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3109. //
  3110. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3111. // // if(fashion_selector==nil)
  3112. // // fashion_selector="";
  3113. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3114. //
  3115. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3116. // if(selector_field==nil)
  3117. // selector_field="";
  3118. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3119. //
  3120. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3121. // if(property_field==nil)
  3122. // property_field="";
  3123. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3124. //
  3125. //
  3126. //
  3127. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3128. // if(packaging==nil)
  3129. // packaging="";
  3130. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3131. //
  3132. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3133. //
  3134. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3135. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3136. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3137. //
  3138. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3139. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3140. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3141. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3142. //
  3143. //
  3144. //
  3145. //
  3146. //
  3147. // NSString* Availability=nil;
  3148. // if(availability>0)
  3149. // Availability=[NSString stringWithFormat:@"%d",availability];
  3150. // else
  3151. // Availability = @"Out of Stock";
  3152. //
  3153. // [img_section setValue:Availability forKey:@"Availability"];
  3154. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3155. //
  3156. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3157. //
  3158. //
  3159. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3160. // if(eta==nil)
  3161. // eta="";
  3162. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3163. // if ([nseta isEqualToString:@"null"]) {
  3164. // nseta = @"";
  3165. // }
  3166. // if (availability <= 0) {
  3167. // [img_section setValue:nseta forKey:@"ETA"];
  3168. // }
  3169. //
  3170. //
  3171. // int item_id = sqlite3_column_int(statement, 26);
  3172. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3173. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3174. //
  3175. // NSString* Price=nil;
  3176. // if(appDelegate.bLogin==false)
  3177. // Price=@"Must Sign in.";
  3178. // else
  3179. // {
  3180. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3181. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3182. // if(price==nil)
  3183. // Price=@"No Price.";
  3184. // else
  3185. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3186. // }
  3187. //
  3188. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3189. // [img_section setValue:Price forKey:@"price"];
  3190. // [img_section setValue:nsname forKey:@"model_name"];
  3191. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3192. //
  3193. // if (appDelegate.order_code) { // 离线order code即so#
  3194. //
  3195. // 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];
  3196. // __block int cartQTY = 0;
  3197. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3198. //
  3199. // cartQTY = sqlite3_column_int(stmt, 0);
  3200. //
  3201. // }];
  3202. //
  3203. // if (cartQTY > 0) {
  3204. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3205. // }
  3206. // }
  3207. //
  3208. // [ret setObject:img_section forKey:@"img_section"];
  3209. //
  3210. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3211. // int detail0_item_count=0;
  3212. //
  3213. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3214. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3215. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3216. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3217. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3218. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3219. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3220. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3221. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3222. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3223. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3224. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3225. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3226. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3227. //
  3228. //
  3229. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3230. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3231. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3232. // {
  3233. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3234. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3235. // }
  3236. //
  3237. //
  3238. //
  3239. //
  3240. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3241. // [detail0_section setValue:@"kv" forKey:@"type"];
  3242. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3243. //
  3244. // [ret setObject:detail0_section forKey:@"detail_0"];
  3245. //
  3246. //
  3247. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3248. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3249. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3250. // // [detail1_section setValue:@"content" forKey:@"type"];
  3251. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3252. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3253. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3254. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3255. // [ret setObject:detail1_section forKey:@"detail_1"];
  3256. //
  3257. //
  3258. //
  3259. //
  3260. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3261. // [detail2_section setValue:@"detail" forKey:@"target"];
  3262. // [detail2_section setValue:@"popup" forKey:@"action"];
  3263. // [detail2_section setValue:@"content" forKey:@"type"];
  3264. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3265. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3266. // [detail2_section setValue:@"local" forKey:@"data"];
  3267. // [ret setObject:detail2_section forKey:@"detail_2"];
  3268. // }
  3269. //
  3270. //
  3271. //
  3272. //
  3273. // sqlite3_finalize(statement);
  3274. // }
  3275. // else
  3276. // {
  3277. // [ret setValue:@"8" forKey:@"result"];
  3278. // }
  3279. //// DebugLog(@"count:%d",count);
  3280. //
  3281. //
  3282. // [iSalesDB close_db:db];
  3283. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3284. //
  3285. // return ret;
  3286. //}
  3287. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3288. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3289. NSString* keyword = [params valueForKey:@"keyword"];
  3290. keyword=keyword.lowercaseString;
  3291. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3292. int limit = [[params valueForKey:@"limit"] intValue];
  3293. int offset = [[params valueForKey:@"offset"] intValue];
  3294. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3295. NSString *limit_str = @"";
  3296. if (limited) {
  3297. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3298. }
  3299. sqlite3 *db = [iSalesDB get_db];
  3300. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3301. // UIApplication * app = [UIApplication sharedApplication];
  3302. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3303. NSString *sqlQuery = nil;
  3304. if(exactMatch )
  3305. 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 ;
  3306. else
  3307. 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
  3308. DebugLog(@"offline_search sql:%@",sqlQuery);
  3309. sqlite3_stmt * statement;
  3310. [ret setValue:@"2" forKey:@"result"];
  3311. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3312. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3313. // int count=0;
  3314. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3315. {
  3316. int i=0;
  3317. while (sqlite3_step(statement) == SQLITE_ROW)
  3318. {
  3319. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3320. // char *name = (char*)sqlite3_column_text(statement, 1);
  3321. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3322. char *name = (char*)sqlite3_column_text(statement, 0);
  3323. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3324. char *description = (char*)sqlite3_column_text(statement, 1);
  3325. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3326. int product_id = sqlite3_column_int(statement, 2);
  3327. // char *url = (char*)sqlite3_column_text(statement, 3);
  3328. // if(url==nil)
  3329. // url="";
  3330. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3331. int wid = sqlite3_column_int(statement, 3);
  3332. int closeout = sqlite3_column_int(statement, 4);
  3333. int cid = sqlite3_column_int(statement, 5);
  3334. int wisdelete = sqlite3_column_int(statement, 6);
  3335. int more_color = sqlite3_column_int(statement, 7);
  3336. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3337. if(wid !=0 && wisdelete != 1)
  3338. [item setValue:@"true" forKey:@"wish_exists"];
  3339. else
  3340. [item setValue:@"false" forKey:@"wish_exists"];
  3341. if(closeout==0)
  3342. [item setValue:@"false" forKey:@"is_closeout"];
  3343. else
  3344. [item setValue:@"true" forKey:@"is_closeout"];
  3345. if(cid==0)
  3346. [item setValue:@"false" forKey:@"cart_exists"];
  3347. else
  3348. [item setValue:@"true" forKey:@"cart_exists"];
  3349. if (more_color == 0) {
  3350. [item setObject:@(false) forKey:@"more_color"];
  3351. } else if (more_color == 1) {
  3352. [item setObject:@(true) forKey:@"more_color"];
  3353. }
  3354. [item addEntriesFromDictionary:imgjson];
  3355. // [item setValue:nsurl forKey:@"img"];
  3356. [item setValue:nsname forKey:@"fash_name"];
  3357. [item setValue:nsdescription forKey:@"description"];
  3358. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3359. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3360. i++;
  3361. }
  3362. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3363. [ret setObject:items forKey:@"items"];
  3364. sqlite3_finalize(statement);
  3365. }
  3366. DebugLog(@"count:%d",count);
  3367. [iSalesDB close_db:db];
  3368. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3369. return ret;
  3370. }
  3371. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3372. {
  3373. return [self search:params limited:YES];
  3374. }
  3375. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3376. //{
  3377. // UIApplication * app = [UIApplication sharedApplication];
  3378. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3379. //
  3380. //// [iSalesDB disable_trigger]
  3381. // [iSalesDB disable_trigger];
  3382. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3383. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3384. //
  3385. // [iSalesDB enable_trigger];
  3386. //// [iSalesDB enable_trigger]
  3387. // //
  3388. // // NSString* user = [params valueForKey:@"user"];
  3389. // //
  3390. // // NSString* password = [params valueForKey:@"password"];
  3391. //
  3392. //
  3393. //
  3394. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3395. //
  3396. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3397. //
  3398. //
  3399. //
  3400. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3401. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3402. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3403. //
  3404. // [appDelegate update_count_mark];
  3405. //
  3406. //
  3407. // appDelegate.can_show_price =false;
  3408. // appDelegate.can_see_price =false;
  3409. // appDelegate.can_create_portfolio =false;
  3410. // appDelegate.can_create_order =false;
  3411. //
  3412. //
  3413. // appDelegate.can_cancel_order =false;
  3414. // appDelegate.can_set_cart_price =false;
  3415. // appDelegate.can_delete_order =false;
  3416. // appDelegate.can_submit_order =false;
  3417. // appDelegate.can_set_tearsheet_price =false;
  3418. // appDelegate.can_update_contact_info = false;
  3419. //
  3420. // appDelegate.save_order_logout = false;
  3421. // appDelegate.submit_order_logout = false;
  3422. // appDelegate.alert_sold_in_quantities = false;
  3423. //
  3424. // appDelegate.ipad_perm =nil ;
  3425. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3426. // appDelegate.OrderFilter= nil;
  3427. // [appDelegate SetSo:nil];
  3428. // [appDelegate set_main_button_panel];
  3429. //
  3430. //
  3431. // // sqlite3 *db = [iSalesDB get_db];
  3432. // //
  3433. // //
  3434. // //
  3435. // //
  3436. // //
  3437. // // 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"]];
  3438. // //
  3439. // //
  3440. // //
  3441. // //
  3442. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3443. // // sqlite3_stmt * statement;
  3444. // //
  3445. // //
  3446. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3447. // //
  3448. // //
  3449. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3450. // // {
  3451. // //
  3452. // //
  3453. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3454. // // {
  3455. // //
  3456. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3457. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3458. // //
  3459. // //
  3460. // //
  3461. // // int can_show_price = sqlite3_column_int(statement, 0);
  3462. // // int can_see_price = sqlite3_column_int(statement, 1);
  3463. // //
  3464. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3465. // // if(contact_id==nil)
  3466. // // contact_id="";
  3467. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3468. // //
  3469. // // int user_type = sqlite3_column_int(statement, 3);
  3470. // //
  3471. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3472. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3473. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3474. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3475. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3476. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3477. // // int can_create_order = sqlite3_column_int(statement, 10);
  3478. // //
  3479. // //
  3480. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3481. // // if(mode==nil)
  3482. // // mode="";
  3483. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3484. // //
  3485. // //
  3486. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3487. // // if(username==nil)
  3488. // // username="";
  3489. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3490. // //
  3491. // //
  3492. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3493. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3494. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3495. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3496. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3497. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3498. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3499. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3500. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3501. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3502. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3503. // //
  3504. // // [header setValue:nsusername forKey:@"username"];
  3505. // //
  3506. // //
  3507. // // [ret setObject:header forKey:@"header"];
  3508. // // [ret setValue:nsmode forKey:@"mode"];
  3509. // //
  3510. // //
  3511. // // }
  3512. // //
  3513. // //
  3514. // //
  3515. // // sqlite3_finalize(statement);
  3516. // // }
  3517. // //
  3518. // //
  3519. // //
  3520. // // [iSalesDB close_db:db];
  3521. // //
  3522. // //
  3523. // //
  3524. // //
  3525. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3526. //
  3527. // return ret;
  3528. //}
  3529. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3530. //{
  3531. //
  3532. // UIApplication * app = [UIApplication sharedApplication];
  3533. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3534. //
  3535. // [iSalesDB disable_trigger];
  3536. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3537. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3538. // [iSalesDB enable_trigger];
  3539. //
  3540. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3541. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3542. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3543. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3544. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3545. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3546. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3547. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3548. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3549. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3550. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3551. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3552. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3553. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3554. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3555. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3556. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3557. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3558. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3559. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3560. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3561. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3562. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3563. //
  3564. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3565. //
  3566. // [arr_name addObject:customer_first_name];
  3567. // [arr_name addObject:customer_last_name];
  3568. //
  3569. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3570. //
  3571. // // default ship from
  3572. // 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';";
  3573. //
  3574. // __block NSString *cid = @"";
  3575. // __block NSString *name = @"";
  3576. // __block NSString *ext = @"";
  3577. // __block NSString *contact = @"";
  3578. // __block NSString *email = @"";
  3579. // __block NSString *fax = @"";
  3580. // __block NSString *phone = @"";
  3581. //
  3582. // sqlite3 *db = [iSalesDB get_db];
  3583. //
  3584. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3585. //
  3586. // cid = [self textAtColumn:0 statement:statment];
  3587. // name = [self textAtColumn:1 statement:statment];
  3588. // ext = [self textAtColumn:2 statement:statment];
  3589. // contact = [self textAtColumn:3 statement:statment];
  3590. // email = [self textAtColumn:4 statement:statment];
  3591. // fax = [self textAtColumn:5 statement:statment];
  3592. // phone = [self textAtColumn:6 statement:statment];
  3593. //
  3594. // }];
  3595. //
  3596. // NSString* so_id = [self get_offline_soid:db];
  3597. // if(so_id==nil)
  3598. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3599. //
  3600. // 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];
  3601. //
  3602. //
  3603. //
  3604. // int result =[iSalesDB execSql:sql_neworder db:db];
  3605. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3606. //
  3607. //
  3608. //
  3609. // //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'
  3610. // //soId
  3611. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3612. //
  3613. //
  3614. //
  3615. //
  3616. //
  3617. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3618. // sqlite3_stmt * statement;
  3619. //
  3620. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3621. // {
  3622. // if (sqlite3_step(statement) == SQLITE_ROW)
  3623. // {
  3624. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3625. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3626. //
  3627. // //ret = sqlite3_column_int(statement, 0);
  3628. //
  3629. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3630. // if(soId==nil)
  3631. // soId="";
  3632. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3633. // [ret setValue:nssoId forKey:@"soId"];
  3634. // [ret setValue:nssoId forKey:@"orderCode"];
  3635. //
  3636. // }
  3637. // sqlite3_finalize(statement);
  3638. // }
  3639. //
  3640. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3641. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3642. //
  3643. //
  3644. // [iSalesDB close_db:db];
  3645. //
  3646. // return [RAConvertor dict2data:ret];
  3647. //
  3648. //}
  3649. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3650. {
  3651. //assert(params[@"order_code"]);
  3652. // assert(params[@"order_code"]);
  3653. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3654. NSString* orderCode = [params valueForKey:@"orderCode"];
  3655. NSString* app_order_code= params[@"appDelegate.order_code"];
  3656. // UIApplication * app = [UIApplication sharedApplication];
  3657. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3658. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3659. {
  3660. [iSalesDB disable_trigger];
  3661. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3662. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3663. [iSalesDB enable_trigger];
  3664. }
  3665. sqlite3 *db = [iSalesDB get_db];
  3666. int cart_count=[self query_ordercartcount:orderCode db:db];
  3667. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3668. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3669. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3670. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3671. [iSalesDB close_db:db];
  3672. return [RAConvertor dict2data:ret];
  3673. }
  3674. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3675. {
  3676. assert(params[@"can_create_backorder"]!=nil);
  3677. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3678. sqlite3 *db = [iSalesDB get_db];
  3679. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3680. int count =0;
  3681. if(params[@"count"]!=nil)
  3682. {
  3683. count = [params[@"count"] intValue];
  3684. }
  3685. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3686. NSString* product_id=params[@"product_id"];
  3687. NSString* orderCode=params[@"orderCode"];
  3688. NSString *qty = params[@"qty"];
  3689. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3690. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3691. __block int number_of_outOfStock = 0;
  3692. for(int i=0;i<arr_id.count;i++)
  3693. {
  3694. NSInteger item_qty= count;
  3695. if (qty) {
  3696. item_qty = [qty_arr[i] integerValue];
  3697. }
  3698. if(item_qty==0)
  3699. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3700. // 检查新加Model数量是否大于库存
  3701. if (![params[@"can_create_backorder"] boolValue]) {
  3702. __block BOOL needContinue = NO;
  3703. [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) {
  3704. int availability = sqlite3_column_int(stmt, 0);
  3705. // 库存小于购买量为缺货
  3706. if (availability < item_qty || availability <= 0) {
  3707. number_of_outOfStock++;
  3708. needContinue = YES;
  3709. }
  3710. }];
  3711. if (needContinue) {
  3712. continue;
  3713. }
  3714. }
  3715. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3716. NSString* sql=nil;
  3717. sqlite3_stmt *stmt = nil;
  3718. BOOL shouldStep = NO;
  3719. if(_id<0)
  3720. {
  3721. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3722. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3723. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3724. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3725. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3726. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3727. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3728. shouldStep = YES;
  3729. }
  3730. else
  3731. {
  3732. if (qty) { // wish list move to cart
  3733. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3734. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3735. sqlite3_bind_int(stmt, 1, _id);
  3736. shouldStep = YES;
  3737. } else {
  3738. 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];
  3739. __block BOOL update = YES;
  3740. if (![params[@"can_create_backorder"] boolValue]) {
  3741. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3742. int newQTY = sqlite3_column_int(stmt, 0);
  3743. int availability = sqlite3_column_int(stmt, 1);
  3744. if (newQTY > availability) { // 库存不够
  3745. update = NO;
  3746. number_of_outOfStock++;
  3747. }
  3748. }];
  3749. }
  3750. if (update) {
  3751. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3752. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3753. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3754. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3755. shouldStep = YES;
  3756. }
  3757. }
  3758. }
  3759. if (shouldStep) {
  3760. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3761. [iSalesDB execSql:@"ROLLBACK" db:db];
  3762. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3763. [iSalesDB close_db:db];
  3764. DebugLog(@"add to cart error");
  3765. return [RAConvertor dict2data:ret];
  3766. }
  3767. }
  3768. }
  3769. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3770. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3771. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3772. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3773. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3774. if (![params[@"can_create_backorder"] boolValue]) {
  3775. if (number_of_outOfStock > 0) {
  3776. ret[@"result"]=[NSNumber numberWithInt:8];
  3777. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3778. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3779. }
  3780. }
  3781. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3782. [iSalesDB close_db:db];
  3783. return [RAConvertor dict2data:ret];
  3784. }
  3785. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3786. {
  3787. // UIApplication * app = [UIApplication sharedApplication];
  3788. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3789. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3790. 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];
  3791. sqlite3_stmt * statement;
  3792. int count=0;
  3793. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3794. {
  3795. while (sqlite3_step(statement) == SQLITE_ROW)
  3796. {
  3797. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3798. int bitem_id = sqlite3_column_int(statement, 0);
  3799. int bitem_qty = sqlite3_column_int(statement, 1);
  3800. char *name = (char*)sqlite3_column_text(statement, 2);
  3801. if(name==nil)
  3802. name="";
  3803. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3804. char *description = (char*)sqlite3_column_text(statement, 3);
  3805. if(description==nil)
  3806. description="";
  3807. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3808. double unit_price = sqlite3_column_double(statement, 4);
  3809. int use_unitprice = sqlite3_column_int(statement, 5);
  3810. if(use_unitprice!=1)
  3811. {
  3812. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3813. }
  3814. itemjson[@"model"]=nsname;
  3815. itemjson[@"description"]=nsdescription;
  3816. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3817. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3818. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3819. if(compute)
  3820. {
  3821. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3822. }
  3823. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3824. count++;
  3825. }
  3826. sqlite3_finalize(statement);
  3827. }
  3828. ret[@"count"]=@(count);
  3829. if(count==0)
  3830. return nil;
  3831. else
  3832. return ret;
  3833. }
  3834. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3835. {
  3836. //compute: add part to subtotal;
  3837. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3838. dict_item[@(item_id)]=@"1";
  3839. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3840. double cuft=0;
  3841. double weight=0;
  3842. int carton=0;
  3843. 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];
  3844. sqlite3_stmt * statement;
  3845. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3846. {
  3847. if (sqlite3_step(statement) == SQLITE_ROW)
  3848. {
  3849. double ulength = sqlite3_column_double(statement, 0);
  3850. double uwidth = sqlite3_column_double(statement, 1);
  3851. double uheight = sqlite3_column_double(statement, 2);
  3852. double uweight = sqlite3_column_double(statement, 3);
  3853. double mlength = sqlite3_column_double(statement, 4);
  3854. double mwidth = sqlite3_column_double(statement, 5);
  3855. double mheight = sqlite3_column_double(statement, 6);
  3856. double mweight = sqlite3_column_double(statement, 7);
  3857. double ilength = sqlite3_column_double(statement, 8);
  3858. double iwidth = sqlite3_column_double(statement, 9);
  3859. double iheight = sqlite3_column_double(statement, 10);
  3860. double iweight = sqlite3_column_double(statement, 11);
  3861. // int pcs = sqlite3_column_int(statement,12);
  3862. int mpack = sqlite3_column_int(statement, 13);
  3863. int ipack = sqlite3_column_int(statement, 14);
  3864. double ucbf = sqlite3_column_double(statement, 15);
  3865. // double icbf = sqlite3_column_double(statement, 16);
  3866. // double mcbf = sqlite3_column_double(statement, 17);
  3867. if(ipack==0)
  3868. {
  3869. carton= count/mpack ;
  3870. weight = mweight*carton;
  3871. cuft= carton*(mlength*mwidth*mheight);
  3872. int remain=count%mpack;
  3873. if(remain==0)
  3874. {
  3875. //do nothing;
  3876. }
  3877. else
  3878. {
  3879. carton++;
  3880. weight += uweight*remain;
  3881. cuft += (ulength*uwidth*uheight)*remain;
  3882. }
  3883. }
  3884. else
  3885. {
  3886. carton = count/(mpack*ipack);
  3887. weight = mweight*carton;
  3888. cuft= carton*(mlength*mwidth*mheight);
  3889. int remain=count%(mpack*ipack);
  3890. if(remain==0)
  3891. {
  3892. // do nothing;
  3893. }
  3894. else
  3895. {
  3896. carton++;
  3897. int icarton =remain/ipack;
  3898. int iremain=remain%ipack;
  3899. weight += iweight*icarton;
  3900. cuft += (ilength*iwidth*iheight)*icarton;
  3901. if(iremain==0)
  3902. {
  3903. //do nothing;
  3904. }
  3905. else
  3906. {
  3907. weight += uweight*iremain;
  3908. cuft += (ulength*uwidth*uheight)*iremain;
  3909. }
  3910. }
  3911. }
  3912. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3913. cuft=ucbf*count;
  3914. weight= uweight*count;
  3915. #endif
  3916. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3917. cuft=ucbf*count;
  3918. weight= uweight*count;
  3919. #endif
  3920. }
  3921. sqlite3_finalize(statement);
  3922. }
  3923. if(compute)
  3924. {
  3925. NSArray * arr_count=nil;
  3926. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3927. for(int i=0;i<arr_bundle.count;i++)
  3928. {
  3929. dict_item[arr_bundle[i]]=@"1";
  3930. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3931. cuft+=[bundlejson[@"cuft"] doubleValue];
  3932. weight+=[bundlejson[@"weight"] doubleValue];
  3933. carton+=[bundlejson[@"carton"] intValue];
  3934. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3935. }
  3936. }
  3937. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3938. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3939. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3940. ret[@"items"]=dict_item;
  3941. return ret;
  3942. }
  3943. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3944. {
  3945. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3946. // 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 ];
  3947. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3948. DebugLog(@"offline_login sql:%@",sqlQuery);
  3949. sqlite3_stmt * statement;
  3950. int cart_count=0;
  3951. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3952. if ( dbresult== SQLITE_OK)
  3953. {
  3954. while (sqlite3_step(statement) == SQLITE_ROW)
  3955. {
  3956. int item_id = sqlite3_column_int(statement, 0);
  3957. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3958. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3959. }
  3960. sqlite3_finalize(statement);
  3961. }
  3962. return cart_count;
  3963. }
  3964. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3965. //{
  3966. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3967. // sqlite3 *db = [iSalesDB get_db];
  3968. // UIApplication * app = [UIApplication sharedApplication];
  3969. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3970. //
  3971. // NSString* orderCode=params[@"orderCode"];
  3972. //
  3973. // int sort = [[params objectForKey:@"sort"] intValue];
  3974. // NSString *sort_str = @"";
  3975. // switch (sort) {
  3976. // case 0:{
  3977. // sort_str = @"order by c.modify_time desc";
  3978. // }
  3979. // break;
  3980. // case 1:{
  3981. // sort_str = @"order by c.modify_time asc";
  3982. // }
  3983. // break;
  3984. // case 2:{
  3985. // sort_str = @"order by m.name asc";
  3986. // }
  3987. // break;
  3988. // case 3:{
  3989. // sort_str = @"order by m.name desc";
  3990. // }
  3991. // break;
  3992. // case 4:{
  3993. // sort_str = @"order by m.description asc";
  3994. // }
  3995. // break;
  3996. //
  3997. // default:
  3998. // break;
  3999. //
  4000. // }
  4001. //
  4002. //
  4003. //
  4004. // 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 ];
  4005. //
  4006. //
  4007. //// 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 ];
  4008. //
  4009. //
  4010. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4011. //
  4012. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4013. // sqlite3_stmt * statement;
  4014. //
  4015. //
  4016. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4017. //
  4018. // NSDate *date1 = [NSDate date];
  4019. //
  4020. // int count=0;
  4021. // int cart_count=0;
  4022. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4023. // if ( dbresult== SQLITE_OK)
  4024. // {
  4025. //
  4026. //
  4027. // while (sqlite3_step(statement) == SQLITE_ROW)
  4028. // {
  4029. //// NSDate *row_date = [NSDate date];
  4030. //
  4031. //
  4032. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4033. //
  4034. //
  4035. //
  4036. //
  4037. // int product_id = sqlite3_column_int(statement, 0);
  4038. //
  4039. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4040. //
  4041. // int item_id = sqlite3_column_int(statement, 7);
  4042. //
  4043. // NSString* Price=nil;
  4044. // if(str_price==nil)
  4045. // {
  4046. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4047. //// NSDate *price_date = [NSDate date];
  4048. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4049. //// DebugLog(@"price time interval");
  4050. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4051. //
  4052. // if(price==nil)
  4053. // Price=@"No Price.";
  4054. // else
  4055. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4056. // }
  4057. // else
  4058. // {
  4059. //
  4060. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4061. // }
  4062. //
  4063. //
  4064. // double discount = sqlite3_column_double(statement, 2);
  4065. // int item_count = sqlite3_column_int(statement, 3);
  4066. //
  4067. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4068. // NSString *nsline_note=nil;
  4069. // if(line_note!=nil)
  4070. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4071. //
  4072. //
  4073. // char *name = (char*)sqlite3_column_text(statement, 5);
  4074. // NSString *nsname=nil;
  4075. // if(name!=nil)
  4076. // nsname= [[NSString alloc]initWithUTF8String:name];
  4077. //
  4078. // char *description = (char*)sqlite3_column_text(statement, 6);
  4079. // NSString *nsdescription=nil;
  4080. // if(description!=nil)
  4081. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4082. //
  4083. //
  4084. //
  4085. // int stockUom = sqlite3_column_int(statement, 8);
  4086. // int _id = sqlite3_column_int(statement, 9);
  4087. // int availability = sqlite3_column_int(statement, 10);
  4088. //
  4089. //// NSDate *subtotal_date = [NSDate date];
  4090. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4091. //// DebugLog(@"subtotal_date time interval");
  4092. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4093. //
  4094. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4095. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4096. // int carton=[bsubtotaljson[@"carton"] intValue];
  4097. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4098. //
  4099. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4100. //// NSDate *img_date = [NSDate date];
  4101. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4102. //// DebugLog(@"img_date time interval");
  4103. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4104. //
  4105. // itemjson[@"model"]=nsname;
  4106. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4107. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4108. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4109. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4110. // itemjson[@"check"]=@"true";
  4111. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4112. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4113. // itemjson[@"unit_price"]=Price;
  4114. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4115. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4116. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4117. // itemjson[@"note"]=nsline_note;
  4118. // if (!appDelegate.can_create_backorder) {
  4119. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4120. // }
  4121. //// NSDate *date2 = [NSDate date];
  4122. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4123. //// DebugLog(@"model_bundle time interval");
  4124. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4125. //
  4126. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4127. // count++;
  4128. //
  4129. //// DebugLog(@"row time interval");
  4130. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4131. // }
  4132. //
  4133. //
  4134. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4135. // sqlite3_finalize(statement);
  4136. // }
  4137. //
  4138. //
  4139. // DebugLog(@"request cart total time interval");
  4140. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4141. //
  4142. //
  4143. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4144. //
  4145. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4146. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4147. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4148. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4149. //
  4150. // ret[@"mode"]=@"Regular Mode";
  4151. //
  4152. // [iSalesDB close_db:db];
  4153. //
  4154. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4155. //
  4156. // DebugLog(@"general notes :%@",general_note);
  4157. //
  4158. // ret[@"general_note"]= general_note;
  4159. //
  4160. // return [RAConvertor dict2data:ret];
  4161. //}
  4162. +(NSData*) offline_login :(NSMutableDictionary *) params
  4163. {
  4164. NSString* user = [params valueForKey:@"user"];
  4165. NSString* password = [params valueForKey:@"password"];
  4166. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4167. sqlite3 *db = [iSalesDB get_db];
  4168. 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"]];
  4169. DebugLog(@"offline_login sql:%@",sqlQuery);
  4170. sqlite3_stmt * statement;
  4171. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4172. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4173. {
  4174. if (sqlite3_step(statement) == SQLITE_ROW)
  4175. {
  4176. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4177. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4178. int can_show_price = sqlite3_column_int(statement, 0);
  4179. int can_see_price = sqlite3_column_int(statement, 1);
  4180. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4181. if(contact_id==nil)
  4182. contact_id="";
  4183. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4184. int user_type = sqlite3_column_int(statement, 3);
  4185. int can_cancel_order = sqlite3_column_int(statement, 4);
  4186. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4187. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4188. int can_delete_order = sqlite3_column_int(statement, 7);
  4189. int can_submit_order = sqlite3_column_int(statement, 8);
  4190. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4191. int can_create_order = sqlite3_column_int(statement, 10);
  4192. char *mode = (char*)sqlite3_column_text(statement, 11);
  4193. if(mode==nil)
  4194. mode="";
  4195. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4196. char *username = (char*)sqlite3_column_text(statement, 12);
  4197. if(username==nil)
  4198. username="";
  4199. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4200. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4201. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4202. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4203. [header setValue:nscontact_id forKey:@"contact_id"];
  4204. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4205. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4206. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4207. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4208. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4209. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4210. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4211. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4212. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4213. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4214. [header setValue:nsusername forKey:@"username"];
  4215. NSError* error=nil;
  4216. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4217. [header setValue:statusFilter forKey:@"statusFilter"];
  4218. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4219. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4220. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4221. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4222. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4223. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4224. [ret setObject:header forKey:@"header"];
  4225. [ret setValue:nsmode forKey:@"mode"];
  4226. }
  4227. sqlite3_finalize(statement);
  4228. }
  4229. [iSalesDB close_db:db];
  4230. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4231. return [RAConvertor dict2data:ret];
  4232. }
  4233. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4234. {
  4235. assert(params[@"mode"]!=nil);
  4236. NSString* contactId = [params valueForKey:@"contactId"];
  4237. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4238. sqlite3 *db = [iSalesDB get_db];
  4239. NSString *sqlQuery = nil;
  4240. {
  4241. 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];
  4242. }
  4243. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4244. sqlite3_stmt * statement;
  4245. [ret setValue:@"2" forKey:@"result"];
  4246. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4247. {
  4248. //int i = 0;
  4249. if (sqlite3_step(statement) == SQLITE_ROW)
  4250. {
  4251. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4252. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4253. // int editable = sqlite3_column_int(statement, 0);
  4254. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4255. NSString *nscompany_name =nil;
  4256. if(company_name==nil)
  4257. nscompany_name=@"";
  4258. else
  4259. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4260. char *country = (char*)sqlite3_column_text(statement, 2);
  4261. if(country==nil)
  4262. country="";
  4263. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4264. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4265. // if(addr==nil)
  4266. // addr="";
  4267. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4268. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4269. if(zipcode==nil)
  4270. zipcode="";
  4271. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4272. char *state = (char*)sqlite3_column_text(statement, 5);
  4273. if(state==nil)
  4274. state="";
  4275. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4276. char *city = (char*)sqlite3_column_text(statement, 6);
  4277. if(city==nil)
  4278. city="";
  4279. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4280. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4281. // NSString *nscontact_name = nil;
  4282. // if(contact_name==nil)
  4283. // nscontact_name=@"";
  4284. // else
  4285. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4286. char *phone = (char*)sqlite3_column_text(statement, 8);
  4287. NSString *nsphone = nil;
  4288. if(phone==nil)
  4289. nsphone=@"";
  4290. else
  4291. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4292. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4293. // if(contact_id==nil)
  4294. // contact_id="";
  4295. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4296. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4297. if(addr_1==nil)
  4298. addr_1="";
  4299. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4300. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4301. if(addr_2==nil)
  4302. addr_2="";
  4303. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4304. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4305. if(addr_3==nil)
  4306. addr_3="";
  4307. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4308. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4309. if(addr_4==nil)
  4310. addr_4="";
  4311. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4312. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4313. if(first_name==nil)
  4314. first_name="";
  4315. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4316. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4317. if(last_name==nil)
  4318. last_name="";
  4319. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4320. char *fax = (char*)sqlite3_column_text(statement, 16);
  4321. NSString *nsfax = nil;
  4322. if(fax==nil)
  4323. nsfax=@"";
  4324. else
  4325. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4326. char *email = (char*)sqlite3_column_text(statement, 17);
  4327. NSString *nsemail = nil;
  4328. if(email==nil)
  4329. nsemail=@"";
  4330. else
  4331. nsemail= [[NSString alloc]initWithUTF8String:email];
  4332. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4333. NSString *nsimg_0 = nil;
  4334. if(img_0==nil)
  4335. nsimg_0=@"";
  4336. else
  4337. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4338. [self copy_bcardImg:nsimg_0];
  4339. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4340. NSString *nsimg_1 = nil;
  4341. if(img_1==nil)
  4342. nsimg_1=@"";
  4343. else
  4344. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4345. [self copy_bcardImg:nsimg_1];
  4346. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4347. NSString *nsimg_2 = nil;
  4348. if(img_2==nil)
  4349. nsimg_2=@"";
  4350. else
  4351. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4352. [self copy_bcardImg:nsimg_2];
  4353. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4354. NSString *nsprice_type = nil;
  4355. if(price_type==nil)
  4356. nsprice_type=@"";
  4357. else
  4358. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4359. char *notes = (char*)sqlite3_column_text(statement, 22);
  4360. NSString *nsnotes = nil;
  4361. if(notes==nil)
  4362. nsnotes=@"";
  4363. else
  4364. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4365. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4366. NSString *nssalesrep = nil;
  4367. if(salesrep==nil)
  4368. nssalesrep=@"";
  4369. else
  4370. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4371. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4372. {
  4373. // decrypt
  4374. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4375. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4376. nsphone=[AESCrypt fastdecrypt:nsphone];
  4377. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4378. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4379. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4380. }
  4381. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4382. [arr_name addObject:nsfirst_name];
  4383. [arr_name addObject:nslast_name];
  4384. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4385. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4386. [arr_ext addObject:nsaddr_1];
  4387. [arr_ext addObject:nsaddr_2];
  4388. [arr_ext addObject:nsaddr_3];
  4389. [arr_ext addObject:nsaddr_4];
  4390. [arr_ext addObject:@"\r\n"];
  4391. [arr_ext addObject:nscity];
  4392. [arr_ext addObject:nsstate];
  4393. [arr_ext addObject:nszipcode];
  4394. [arr_ext addObject:nscountry];
  4395. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4396. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4397. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4398. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4399. [item setValue:nscountry forKey:@"customer_country"];
  4400. [item setValue:nsphone forKey:@"customer_phone"];
  4401. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4402. [item setValue:nscompany_name forKey:@"customer_name"];
  4403. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4404. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4405. [item setValue:nsext forKey:@"customer_contact_ext"];
  4406. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4407. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4408. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4409. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4410. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4411. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4412. [item setValue:nslast_name forKey:@"customer_last_name"];
  4413. [item setValue:nscity forKey:@"customer_city"];
  4414. [item setValue:nsstate forKey:@"customer_state"];
  4415. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4416. [item setValue:contactId forKey:@"customer_cid"];
  4417. [item setValue:nscontact_name forKey:@"customer_contact"];
  4418. [item setValue:nsfax forKey:@"customer_fax"];
  4419. [item setValue:nsemail forKey:@"customer_email"];
  4420. [item setValue:contact_type forKey:@"customer_contact_type"];
  4421. [ret setObject:item forKey:@"customerInfo"];
  4422. // i++;
  4423. }
  4424. // UIApplication * app = [UIApplication sharedApplication];
  4425. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4426. [ret setValue:params[@"mode"] forKey:@"mode"];
  4427. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4428. sqlite3_finalize(statement);
  4429. }
  4430. [iSalesDB close_db:db];
  4431. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4432. return ret;
  4433. }
  4434. + (bool) copy_bcardImg:(NSString*) filename
  4435. {
  4436. if(filename.length==0)
  4437. return false;
  4438. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4439. bool ret=false;
  4440. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4441. NSString *cachefolder = [paths objectAtIndex:0];
  4442. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4443. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4444. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4445. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4446. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4447. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4448. //
  4449. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4450. NSFileManager* fileManager = [NSFileManager defaultManager];
  4451. BOOL bdir=NO;
  4452. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4453. {
  4454. NSError *error = nil;
  4455. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4456. {
  4457. ret=false;
  4458. }
  4459. else
  4460. {
  4461. ret=true;
  4462. }
  4463. // NSError *error = nil;
  4464. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4465. //
  4466. // if(!bsuccess)
  4467. // {
  4468. // DebugLog(@"Create offline_createimg folder failed");
  4469. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4470. // return [RAConvertor dict2data:ret];
  4471. // }
  4472. // if(bsuccess)
  4473. // {
  4474. // sqlite3 *db = [self get_db];
  4475. //
  4476. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4477. // [iSalesDB close_db:db];
  4478. // }
  4479. }
  4480. return ret;
  4481. //
  4482. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4483. // if(bsuccess)
  4484. // {
  4485. // NSError *error = nil;
  4486. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4487. // {
  4488. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4489. // }
  4490. // else
  4491. // {
  4492. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4493. //
  4494. // ret[@"img_url_aname"]=filename;
  4495. // ret[@"img_url"]=savedImagePath;
  4496. // }
  4497. // }
  4498. }
  4499. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4500. {
  4501. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4502. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4503. NSString *cachefolder = [paths objectAtIndex:0];
  4504. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4505. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4506. NSFileManager* fileManager = [NSFileManager defaultManager];
  4507. BOOL bdir=YES;
  4508. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4509. {
  4510. NSError *error = nil;
  4511. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4512. if(!bsuccess)
  4513. {
  4514. DebugLog(@"Create offline_createimg folder failed");
  4515. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4516. return [RAConvertor dict2data:ret];
  4517. }
  4518. // if(bsuccess)
  4519. // {
  4520. // sqlite3 *db = [self get_db];
  4521. //
  4522. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4523. // [iSalesDB close_db:db];
  4524. // }
  4525. }
  4526. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4527. {
  4528. NSError *error = nil;
  4529. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4530. if(!bsuccess)
  4531. {
  4532. DebugLog(@"Create img_cache folder failed");
  4533. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4534. return [RAConvertor dict2data:ret];
  4535. }
  4536. // if(bsuccess)
  4537. // {
  4538. // sqlite3 *db = [self get_db];
  4539. //
  4540. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4541. // [iSalesDB close_db:db];
  4542. // }
  4543. }
  4544. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4545. //JEPG格式
  4546. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4547. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4548. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4549. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4550. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4551. if(bsuccess)
  4552. {
  4553. NSError *error = nil;
  4554. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4555. {
  4556. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4557. }
  4558. else
  4559. {
  4560. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4561. ret[@"img_url_aname"]=filename;
  4562. ret[@"img_url"]=filename;
  4563. }
  4564. }
  4565. else
  4566. {
  4567. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4568. }
  4569. return [RAConvertor dict2data:ret];
  4570. }
  4571. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4572. {
  4573. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4574. category = [category substringToIndex:3];
  4575. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4576. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4577. params[@"category"]= category;
  4578. ret[@"params"]= params;
  4579. [ret setValue:@"detail" forKey:@"target"];
  4580. [ret setValue:@"popup" forKey:@"action"];
  4581. [ret setValue:@"content" forKey:@"type"];
  4582. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4583. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4584. [ret setValue:@"true" forKey:@"single_row"];
  4585. [ret setValue:@"true" forKey:@"partial_refresh"];
  4586. // sqlite3 *db = [iSalesDB get_db];
  4587. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4588. NSString *sqlQuery =nil;
  4589. #ifdef BUILD_NPD
  4590. 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 ;
  4591. #else
  4592. 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];
  4593. #endif
  4594. sqlite3_stmt * statement;
  4595. int count = 0;
  4596. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4597. // int count=0;
  4598. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4599. {
  4600. int i=0;
  4601. while (sqlite3_step(statement) == SQLITE_ROW)
  4602. {
  4603. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4604. // char *name = (char*)sqlite3_column_text(statement, 1);
  4605. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4606. char *name = (char*)sqlite3_column_text(statement, 0);
  4607. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4608. int product_id = sqlite3_column_int(statement, 1);
  4609. char *url = (char*)sqlite3_column_text(statement, 2);
  4610. if(url==nil)
  4611. url="";
  4612. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4613. [item setValue:nsurl forKey:@"picture_path"];
  4614. [item setValue:nsname forKey:@"fash_name"];
  4615. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4616. [item setValue:category forKey:@"category"];
  4617. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4618. i++;
  4619. }
  4620. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4621. sqlite3_finalize(statement);
  4622. }
  4623. DebugLog(@"count:%d",count);
  4624. // [iSalesDB close_db:db];
  4625. return ret;
  4626. }
  4627. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4628. {
  4629. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4630. [ret setValue:key forKey:@"key"];
  4631. [ret setValue:value forKey:@"val"];
  4632. [ret setValue:@"price" forKey:@"type"];
  4633. return ret;
  4634. }
  4635. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4636. {
  4637. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4638. [ret setValue:key forKey:@"key"];
  4639. [ret setValue:value forKey:@"val"];
  4640. return ret;
  4641. }
  4642. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4643. {
  4644. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4645. [ret setValue:@"0" forKey:@"img_count"];
  4646. // sqlite3 *db = [iSalesDB get_db];
  4647. 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 ;
  4648. sqlite3_stmt * statement;
  4649. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4650. {
  4651. int i=0;
  4652. if (sqlite3_step(statement) == SQLITE_ROW)
  4653. {
  4654. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4655. // char *name = (char*)sqlite3_column_text(statement, 1);
  4656. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4657. char *value = (char*)sqlite3_column_text(statement, 0);
  4658. if(value==nil)
  4659. value="";
  4660. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4661. char *key = (char*)sqlite3_column_text(statement, 1);
  4662. if(key==nil)
  4663. key="";
  4664. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4665. [item setValue:nsvalue forKey:@"val"];
  4666. [item setValue:nskey forKey:@"key"];
  4667. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4668. [ret setValue:@"1" forKey:@"count"];
  4669. i++;
  4670. }
  4671. sqlite3_finalize(statement);
  4672. }
  4673. // [iSalesDB close_db:db];
  4674. return ret;
  4675. }
  4676. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4677. {
  4678. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4679. [ret setValue:@"0" forKey:@"count"];
  4680. // sqlite3 *db = [iSalesDB get_db];
  4681. 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;
  4682. sqlite3_stmt * statement;
  4683. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4684. {
  4685. int i=0;
  4686. while (sqlite3_step(statement) == SQLITE_ROW)
  4687. {
  4688. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4689. // char *name = (char*)sqlite3_column_text(statement, 1);
  4690. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4691. char *value = (char*)sqlite3_column_text(statement, 0);
  4692. if(value==nil)
  4693. value="";
  4694. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4695. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4696. if(selector_display==nil)
  4697. selector_display="";
  4698. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4699. int product_id = sqlite3_column_int(statement, 2);
  4700. char *category = (char*)sqlite3_column_text(statement, 3);
  4701. if(category==nil)
  4702. category="";
  4703. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4704. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4705. [item setValue:nsvalue forKey:@"title"];
  4706. [item setValue:url forKey:@"pic_url"];
  4707. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4708. [params setValue:@"2" forKey:@"count"];
  4709. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4710. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4711. [param0 setValue:@"product_id" forKey:@"name"];
  4712. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4713. [param1 setValue:nscategory forKey:@"val"];
  4714. [param1 setValue:@"category" forKey:@"name"];
  4715. [params setObject:param0 forKey:@"param_0"];
  4716. [params setObject:param1 forKey:@"param_1"];
  4717. [item setObject:params forKey:@"params"];
  4718. [ret setValue:nsselector_display forKey:@"name"];
  4719. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4720. i++;
  4721. }
  4722. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4723. [ret setValue:@"switch" forKey:@"action"];
  4724. sqlite3_finalize(statement);
  4725. }
  4726. // [iSalesDB close_db:db];
  4727. return ret;
  4728. }
  4729. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4730. {
  4731. // model 在 category search 显示的图片。
  4732. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4733. // sqlite3 *db = [iSalesDB get_db];
  4734. 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];
  4735. sqlite3_stmt * statement;
  4736. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4737. {
  4738. while (sqlite3_step(statement) == SQLITE_ROW)
  4739. {
  4740. char *url = (char*)sqlite3_column_text(statement, 0);
  4741. if(url==nil)
  4742. url="";
  4743. int type = sqlite3_column_int(statement, 1);
  4744. if(type==0)
  4745. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4746. else
  4747. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4748. }
  4749. sqlite3_finalize(statement);
  4750. }
  4751. // [iSalesDB close_db:db];
  4752. return ret;
  4753. }
  4754. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4755. {
  4756. int item_id=-1;
  4757. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4758. sqlite3_stmt * statement;
  4759. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4760. {
  4761. if (sqlite3_step(statement) == SQLITE_ROW)
  4762. {
  4763. item_id = sqlite3_column_int(statement, 0);
  4764. }
  4765. sqlite3_finalize(statement);
  4766. }
  4767. return item_id;
  4768. }
  4769. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4770. {
  4771. // NSString* ret = @"";
  4772. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4773. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4774. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4775. sqlite3_stmt * statement;
  4776. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4777. {
  4778. while (sqlite3_step(statement) == SQLITE_ROW)
  4779. {
  4780. int bitem_id = sqlite3_column_int(statement, 0);
  4781. int bitem_qty = sqlite3_column_int(statement, 1);
  4782. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4783. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4784. }
  4785. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4786. sqlite3_finalize(statement);
  4787. }
  4788. // if(ret==nil)
  4789. // ret=@"";
  4790. *count=arr_count;
  4791. return arr_bundle;
  4792. }
  4793. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4794. {
  4795. // get default sold qty, return -1 if model not found;
  4796. int ret = -1;
  4797. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4798. sqlite3_stmt * statement;
  4799. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4800. {
  4801. if (sqlite3_step(statement) == SQLITE_ROW)
  4802. {
  4803. ret = sqlite3_column_int(statement, 0);
  4804. }
  4805. sqlite3_finalize(statement);
  4806. }
  4807. return ret;
  4808. }
  4809. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4810. {
  4811. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4812. [ret setValue:@"0" forKey:@"img_count"];
  4813. // sqlite3 *db = [iSalesDB get_db];
  4814. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4815. sqlite3_stmt * statement;
  4816. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4817. {
  4818. int i=0;
  4819. while (sqlite3_step(statement) == SQLITE_ROW)
  4820. {
  4821. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4822. // char *name = (char*)sqlite3_column_text(statement, 1);
  4823. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4824. char *url = (char*)sqlite3_column_text(statement, 0);
  4825. if(url==nil)
  4826. url="";
  4827. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4828. [item setValue:nsurl forKey:@"s"];
  4829. [item setValue:nsurl forKey:@"l"];
  4830. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4831. i++;
  4832. }
  4833. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4834. sqlite3_finalize(statement);
  4835. }
  4836. // [iSalesDB close_db:db];
  4837. return ret;
  4838. }
  4839. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4840. // UIApplication * app = [UIApplication sharedApplication];
  4841. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4842. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4843. sqlite3 *db = [iSalesDB get_db];
  4844. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4845. NSString* product_id=params[@"product_id"];
  4846. NSString *item_count_str = params[@"item_count"];
  4847. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4848. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4849. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4850. // NSString *sql = @"";
  4851. for(int i=0;i<arr.count;i++)
  4852. {
  4853. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4854. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4855. __block int cart_count = 0;
  4856. if (!item_count_str) {
  4857. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4858. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4859. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4860. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4861. cart_count = [[model_set_components lastObject] intValue];
  4862. }];
  4863. }
  4864. if(count==0)
  4865. {
  4866. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4867. sqlite3_stmt *stmt;
  4868. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4869. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4870. if (item_count_arr) {
  4871. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4872. } else {
  4873. sqlite3_bind_int(stmt,2,cart_count);
  4874. }
  4875. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4876. [iSalesDB execSql:@"ROLLBACK" db:db];
  4877. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4878. [iSalesDB close_db:db];
  4879. DebugLog(@"add to wishlist error");
  4880. return ret;
  4881. }
  4882. } else {
  4883. int qty = 0;
  4884. if (item_count_arr) {
  4885. qty = [item_count_arr[i] intValue];
  4886. } else {
  4887. qty = cart_count;
  4888. }
  4889. 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]];
  4890. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4891. [iSalesDB execSql:@"ROLLBACK" db:db];
  4892. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4893. [iSalesDB close_db:db];
  4894. DebugLog(@"add to wishlist error");
  4895. return ret;
  4896. }
  4897. }
  4898. }
  4899. // [iSalesDB execSql:sql db:db];
  4900. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4901. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4902. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4903. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4904. [iSalesDB close_db:db];
  4905. ret[@"wish_count"]=@(count);
  4906. ret[@"result"]= [NSNumber numberWithInt:2];
  4907. return ret;
  4908. }
  4909. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4910. // 0 category
  4911. // 1 search
  4912. // 2 itemsearch
  4913. NSData *ret = nil;
  4914. NSDictionary *items = nil;
  4915. switch (from) {
  4916. case 0:{
  4917. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4918. }
  4919. break;
  4920. case 1:{
  4921. items = [[self search:params limited:NO] objectForKey:@"items"];
  4922. }
  4923. break;
  4924. case 2:{
  4925. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4926. }
  4927. break;
  4928. default:
  4929. break;
  4930. }
  4931. if (!items) {
  4932. return ret;
  4933. }
  4934. int count = [[items objectForKey:@"count"] intValue];
  4935. NSMutableString *product_id_str = [@"" mutableCopy];
  4936. for (int i = 0; i < count; i++) {
  4937. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4938. NSDictionary *item = [items objectForKey:key];
  4939. NSString *product_id = [item objectForKey:@"product_id"];
  4940. if (i == 0) {
  4941. [product_id_str appendString:product_id];
  4942. } else {
  4943. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4944. }
  4945. }
  4946. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4947. if ([add_to isEqualToString:@"cart"]) {
  4948. NSString *order_code = [params objectForKey:@"orderCode"];
  4949. if (order_code.length) {
  4950. NSDictionary *newParams = @{
  4951. @"product_id" : product_id_str,
  4952. @"orderCode" : order_code,
  4953. @"can_create_backorder":params[@"can_create_backorder"]
  4954. };
  4955. ret = [self offline_add2cart:newParams.mutableCopy];
  4956. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4957. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4958. ret = [RAConvertor dict2data:retDic];
  4959. }
  4960. } else if([add_to isEqualToString:@"wishlist"]) {
  4961. NSDictionary *newParams = @{
  4962. @"product_id" : product_id_str
  4963. };
  4964. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4965. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4966. ret = [RAConvertor dict2data:retDic];
  4967. } else if([add_to isEqualToString:@"portfolio"]) {
  4968. NSDictionary *newParams = @{
  4969. @"product_id" : product_id_str
  4970. };
  4971. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4972. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4973. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4974. ret = [RAConvertor dict2data:retDic];
  4975. }
  4976. return ret;
  4977. }
  4978. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4979. {
  4980. return [self addAll:params from:0];
  4981. }
  4982. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4983. {
  4984. return [self addAll:params from:1];
  4985. }
  4986. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4987. {
  4988. return [self addAll:params from:2];
  4989. }
  4990. #pragma mark - Jack
  4991. #warning 做SQL操作时转义!!
  4992. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4993. // "val_227" : {
  4994. // "check" : 1,
  4995. // "value" : "US United States",
  4996. // "value_id" : "228"
  4997. // },
  4998. if (!countryCode) {
  4999. countryCode = @"US";
  5000. }
  5001. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5002. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5003. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5004. int code_id = sqlite3_column_int(stmt, 3); // id
  5005. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5006. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5007. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5008. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5009. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5010. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5011. }
  5012. long n = *count;
  5013. *count = n + 1;
  5014. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5015. [container setValue:countryDic forKey:key];
  5016. }] mutableCopy];
  5017. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5018. return ret;
  5019. }
  5020. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5021. countryCode = [self translateSingleQuote:countryCode];
  5022. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5023. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5024. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5025. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5026. if (name == NULL) {
  5027. name = "";
  5028. }
  5029. if (code == NULL) {
  5030. code = "";
  5031. }
  5032. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5033. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5034. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5035. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5036. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5037. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5038. }
  5039. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5040. [container setValue:stateDic forKey:key];
  5041. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5042. DebugLog(@"query all state error: %@",err_msg);
  5043. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5044. // [stateDic setValue:@"Other" forKey:@"value"];
  5045. // [stateDic setValue:@"" forKey:@"value_id"];
  5046. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5047. //
  5048. // if (state_code && [@"" isEqualToString:state_code]) {
  5049. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5050. // }
  5051. //
  5052. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5053. // [container setValue:stateDic forKey:key];
  5054. }] mutableCopy];
  5055. [ret removeObjectForKey:@"result"];
  5056. // failure 可以不用了,一样的
  5057. if (ret.allKeys.count == 0) {
  5058. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5059. [stateDic setValue:@"Other" forKey:@"value"];
  5060. [stateDic setValue:@"" forKey:@"value_id"];
  5061. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5062. if (state_code && [@"" isEqualToString:state_code]) {
  5063. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5064. }
  5065. NSString *key = [NSString stringWithFormat:@"val_0"];
  5066. [ret setValue:stateDic forKey:key];
  5067. }
  5068. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5069. return ret;
  5070. }
  5071. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5072. codeId = [self translateSingleQuote:codeId];
  5073. 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];
  5074. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5075. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5076. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5077. if (name == NULL) {
  5078. name = "";
  5079. }
  5080. if (code == NULL) {
  5081. code = "";
  5082. }
  5083. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5084. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5085. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5086. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5087. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5088. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5089. }
  5090. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5091. [container setValue:stateDic forKey:key];
  5092. }] mutableCopy];
  5093. [ret removeObjectForKey:@"result"];
  5094. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5095. return ret;
  5096. }
  5097. + (NSDictionary *)offline_getPrice {
  5098. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5099. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5100. char *name = (char *) sqlite3_column_text(stmt, 1);
  5101. int type = sqlite3_column_int(stmt, 2);
  5102. int orderBy = sqlite3_column_int(stmt, 3);
  5103. if (name == NULL) {
  5104. name = "";
  5105. }
  5106. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5107. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5108. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5109. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5110. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5111. if (orderBy == 0) {
  5112. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5113. }
  5114. [container setValue:priceDic forKey:key];
  5115. }] mutableCopy];
  5116. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5117. return ret;
  5118. }
  5119. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5120. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5121. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5122. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5123. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5124. // int _id = sqlite3_column_int(stmt, 0);
  5125. NSString *name = [self textAtColumn:1 statement:stmt];
  5126. NSDictionary *typeDic = @{
  5127. @"value_id" : name,
  5128. @"value" : name,
  5129. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5130. };
  5131. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5132. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5133. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5134. }];
  5135. return ret;
  5136. }
  5137. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5138. // 首先从offline_login表中取出sales_code
  5139. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5140. // NSString *user = app.user;
  5141. user = [self translateSingleQuote:user];
  5142. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5143. __block NSString *user_code = @"";
  5144. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5145. char *code = (char *)sqlite3_column_text(stmt, 0);
  5146. if (code == NULL) {
  5147. code = "";
  5148. }
  5149. user_code = [NSString stringWithUTF8String:code];
  5150. }];
  5151. // 再取所有salesRep
  5152. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5153. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5154. // 1 name 2 code 3 salesrep_id
  5155. char *name = (char *)sqlite3_column_text(stmt, 1);
  5156. char *code = (char *)sqlite3_column_text(stmt, 2);
  5157. int salesrep_id = sqlite3_column_int(stmt, 3);
  5158. if (name == NULL) {
  5159. name = "";
  5160. }
  5161. if (code == NULL) {
  5162. code = "";
  5163. }
  5164. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5165. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5166. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5167. // 比较code 相等则check
  5168. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5169. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5170. }
  5171. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5172. }] mutableCopy];
  5173. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5174. return ret;
  5175. }
  5176. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5177. zipcode = [self translateSingleQuote:zipcode];
  5178. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5179. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5180. char *country = (char *)sqlite3_column_text(stmt, 0);
  5181. char *state = (char *)sqlite3_column_text(stmt, 1);
  5182. char *city = (char *)sqlite3_column_text(stmt, 2);
  5183. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5184. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5185. if (country == NULL) {
  5186. country = "";
  5187. }
  5188. if (state == NULL) {
  5189. state = "";
  5190. }
  5191. if (city == NULL) {
  5192. city = "";
  5193. }
  5194. if (country_code == NULL) {
  5195. country_code = "";
  5196. }
  5197. if (state_code == NULL) {
  5198. state_code = "";
  5199. }
  5200. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5201. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5202. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5203. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5204. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5205. }] mutableCopy];
  5206. return ret;
  5207. }
  5208. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5209. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5210. [item setValue:value forKey:valueKey];
  5211. [dic setValue:item forKey:itemKey];
  5212. }
  5213. + (NSString *)countryCodeByid:(NSString *)code_id {
  5214. NSString *ret = nil;
  5215. code_id = [self translateSingleQuote:code_id];
  5216. sqlite3 *db = [iSalesDB get_db];
  5217. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5218. sqlite3_stmt * statement;
  5219. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5220. while (sqlite3_step(statement) == SQLITE_ROW) {
  5221. char *code = (char *)sqlite3_column_text(statement, 0);
  5222. if (code == NULL) {
  5223. code = "";
  5224. }
  5225. ret = [NSString stringWithUTF8String:code];
  5226. }
  5227. sqlite3_finalize(statement);
  5228. }
  5229. [iSalesDB close_db:db];
  5230. return ret;
  5231. }
  5232. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5233. NSString *ret = nil;
  5234. code = [self translateSingleQuote:code];
  5235. sqlite3 *db = [iSalesDB get_db];
  5236. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5237. sqlite3_stmt * statement;
  5238. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5239. while (sqlite3_step(statement) == SQLITE_ROW) {
  5240. char *_id = (char *)sqlite3_column_text(statement, 0);
  5241. if (_id == NULL) {
  5242. _id = "";
  5243. }
  5244. ret = [NSString stringWithFormat:@"%s",_id];
  5245. }
  5246. sqlite3_finalize(statement);
  5247. }
  5248. [iSalesDB close_db:db];
  5249. return ret;
  5250. }
  5251. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5252. NSString *name = nil;
  5253. codeId = [self translateSingleQuote:codeId];
  5254. sqlite3 *db = [iSalesDB get_db];
  5255. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  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 *value = (char *)sqlite3_column_text(statement, 0);
  5260. if (value == NULL) {
  5261. value = "";
  5262. }
  5263. name = [NSString stringWithUTF8String:value];
  5264. }
  5265. sqlite3_finalize(statement);
  5266. }
  5267. [iSalesDB close_db:db];
  5268. return name;
  5269. }
  5270. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5271. NSString *ret = nil;
  5272. sqlite3 *db = [iSalesDB get_db];
  5273. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5274. sqlite3_stmt * statement;
  5275. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5276. while (sqlite3_step(statement) == SQLITE_ROW) {
  5277. char *name = (char *)sqlite3_column_text(statement, 0);
  5278. if (name == NULL) {
  5279. name = "";
  5280. }
  5281. ret = [NSString stringWithUTF8String:name];
  5282. }
  5283. sqlite3_finalize(statement);
  5284. }
  5285. [iSalesDB close_db:db];
  5286. return ret;
  5287. }
  5288. + (NSString *)salesRepCodeById:(NSString *)_id {
  5289. NSString *ret = nil;
  5290. _id = [self translateSingleQuote:_id];
  5291. sqlite3 *db = [iSalesDB get_db];
  5292. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5293. sqlite3_stmt * statement;
  5294. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5295. while (sqlite3_step(statement) == SQLITE_ROW) {
  5296. char *rep = (char *)sqlite3_column_text(statement, 0);
  5297. if (rep == NULL) {
  5298. rep = "";
  5299. }
  5300. ret = [NSString stringWithUTF8String:rep];
  5301. }
  5302. sqlite3_finalize(statement);
  5303. }
  5304. [iSalesDB close_db:db];
  5305. return ret;
  5306. }
  5307. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5308. char *tx = (char *)sqlite3_column_text(stmt, col);
  5309. if (tx == NULL) {
  5310. tx = "";
  5311. }
  5312. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5313. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5314. if (!text) {
  5315. text = @"";
  5316. }
  5317. // 将字符全部为' '的字符串干掉
  5318. int spaceCount = 0;
  5319. for (int i = 0; i < text.length; i++) {
  5320. if ([text characterAtIndex:i] == ' ') {
  5321. spaceCount++;
  5322. }
  5323. }
  5324. if (spaceCount == text.length) {
  5325. text = @"";
  5326. }
  5327. return text;
  5328. }
  5329. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5330. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5331. NSData *data = [NSData dataWithContentsOfFile:path];
  5332. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5333. return ret;
  5334. }
  5335. + (NSString *)textFileName:(NSString *)name {
  5336. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5337. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5338. if (!text) {
  5339. text = @"";
  5340. }
  5341. return text;
  5342. }
  5343. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5344. return [[dic objectForKey:key] mutableCopy];
  5345. }
  5346. + (id)translateSingleQuote:(NSString *)string {
  5347. if ([string isKindOfClass:[NSString class]])
  5348. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5349. return string;
  5350. }
  5351. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5352. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5353. }
  5354. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5355. NSString* ret= nil;
  5356. NSString *sqlQuery = nil;
  5357. // 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
  5358. 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];
  5359. sqlite3_stmt * statement;
  5360. // int count=0;
  5361. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5362. {
  5363. if (sqlite3_step(statement) == SQLITE_ROW)
  5364. {
  5365. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5366. if(imgurl==nil)
  5367. imgurl="";
  5368. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5369. ret=nsimgurl;
  5370. }
  5371. sqlite3_finalize(statement);
  5372. }
  5373. else
  5374. {
  5375. [ret setValue:@"8" forKey:@"result"];
  5376. }
  5377. // [iSalesDB close_db:db];
  5378. // DebugLog(@"data string: %@",ret );
  5379. return ret;
  5380. }
  5381. #pragma mark contact Advanced search
  5382. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5383. {
  5384. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5385. return [RAConvertor dict2data:contactAdvanceDic];
  5386. }
  5387. #pragma mark create new contact
  5388. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5389. assert(params[@"user"]!=nil);
  5390. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5391. NSData *data = [NSData dataWithContentsOfFile:path];
  5392. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5393. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5394. NSString *countryCode = nil;
  5395. NSString *countryCode_id = nil;
  5396. NSString *stateCode = nil;
  5397. NSString *city = nil;
  5398. NSString *zipCode = nil;
  5399. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5400. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5401. NSString *code_id = params[@"country"];
  5402. countryCode_id = code_id;
  5403. countryCode = [self countryCodeByid:code_id];
  5404. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5405. NSString *zip_code = params[@"zipcode"];
  5406. // 剔除全部为空格
  5407. int spaceCount = 0;
  5408. for (int i = 0; i < zip_code.length; i++) {
  5409. if ([zip_code characterAtIndex:i] == ' ') {
  5410. spaceCount++;
  5411. }
  5412. }
  5413. if (spaceCount == zip_code.length) {
  5414. zip_code = @"";
  5415. }
  5416. zipCode = zip_code;
  5417. if (zipCode.length > 0) {
  5418. countryCode_id = params[@"country"];
  5419. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5420. countryCode = [dic valueForKey:@"country_code"];
  5421. if (!countryCode) {
  5422. // countryCode = @"US";
  5423. NSString *code_id = params[@"country"];
  5424. countryCode = [self countryCodeByid:code_id];
  5425. }
  5426. stateCode = [dic valueForKey:@"state_code"];
  5427. if (!stateCode.length) {
  5428. stateCode = params[@"state"];
  5429. }
  5430. city = [dic valueForKey:@"city"];
  5431. if (!city.length) {
  5432. city = params[@"city"];
  5433. }
  5434. // zip code
  5435. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5436. [zipDic setValue:zipCode forKey:@"value"];
  5437. [section_0 setValue:zipDic forKey:@"item_11"];
  5438. } else {
  5439. NSString *code_id = params[@"country"];
  5440. countryCode = [self countryCodeByid:code_id];
  5441. stateCode = params[@"state"];
  5442. city = params[@"city"];
  5443. }
  5444. }
  5445. } else {
  5446. // default: US United States
  5447. countryCode = @"US";
  5448. countryCode_id = @"228";
  5449. }
  5450. // country
  5451. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5452. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5453. // state
  5454. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5455. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5456. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5457. // city
  5458. if (city) {
  5459. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5460. [cityDic setValue:city forKey:@"value"];
  5461. [section_0 setValue:cityDic forKey:@"item_13"];
  5462. }
  5463. // price type
  5464. NSDictionary *priceDic = [self offline_getPrice];
  5465. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5466. // contact type
  5467. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5468. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5469. // Sales Rep
  5470. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5471. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5472. [ret setValue:section_0 forKey:@"section_0"];
  5473. return [RAConvertor dict2data:ret];
  5474. }
  5475. #pragma mark save
  5476. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5477. NSString *addr = nil;
  5478. NSString *contact_name = nil;
  5479. NSString *companyName = [params objectForKey:@"company"];
  5480. if (companyName) {
  5481. companyName = [AESCrypt fastencrypt:companyName];
  5482. } else {
  5483. companyName = @"";
  5484. }
  5485. DebugLog(@"company");
  5486. companyName = [self translateSingleQuote:companyName];
  5487. NSString *addr1 = [params objectForKey:@"address"];
  5488. NSString *addr2 = [params objectForKey:@"address2"];
  5489. NSString *addr3 = [params objectForKey:@"address_3"];
  5490. NSString *addr4 = [params objectForKey:@"address_4"];
  5491. if (!addr2) {
  5492. addr2 = @"";
  5493. }
  5494. if (!addr3) {
  5495. addr3 = @"";
  5496. }
  5497. if (!addr4) {
  5498. addr4 = @"";
  5499. }
  5500. if (!addr1) {
  5501. addr1 = @"";
  5502. }
  5503. DebugLog(@"addr");
  5504. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5505. addr = [AESCrypt fastencrypt:addr];
  5506. addr = [self translateSingleQuote:addr];
  5507. if (addr1 && ![addr1 isEqualToString:@""]) {
  5508. addr1 = [AESCrypt fastencrypt:addr1];
  5509. }
  5510. addr1 = [self translateSingleQuote:addr1];
  5511. addr2 = [self translateSingleQuote:addr2];
  5512. addr3 = [self translateSingleQuote:addr3];
  5513. addr4 = [self translateSingleQuote:addr4];
  5514. NSString *country = [params objectForKey:@"country"];
  5515. if (country) {
  5516. country = [self countryNameByCountryCodeId:country];
  5517. } else {
  5518. country = @"";
  5519. }
  5520. DebugLog(@"country");
  5521. country = [self translateSingleQuote:country];
  5522. NSString *state = [params objectForKey:@"state"];
  5523. if (!state) {
  5524. state = @"";
  5525. }
  5526. DebugLog(@"state");
  5527. state = [self translateSingleQuote:state];
  5528. NSString *city = [params objectForKey:@"city"];
  5529. if (!city) {
  5530. city = @"";
  5531. }
  5532. city = [self translateSingleQuote:city];
  5533. NSString *zipcode = [params objectForKey:@"zipcode"];
  5534. if (!zipcode) {
  5535. zipcode = @"";
  5536. }
  5537. DebugLog(@"zip");
  5538. zipcode = [self translateSingleQuote:zipcode];
  5539. NSString *fistName = [params objectForKey:@"firstname"];
  5540. if (!fistName) {
  5541. fistName = @"";
  5542. }
  5543. NSString *lastName = [params objectForKey:@"lastname"];
  5544. if (!lastName) {
  5545. lastName = @"";
  5546. }
  5547. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5548. DebugLog(@"contact_name");
  5549. contact_name = [self translateSingleQuote:contact_name];
  5550. fistName = [self translateSingleQuote:fistName];
  5551. lastName = [self translateSingleQuote:lastName];
  5552. NSString *phone = [params objectForKey:@"phone"];
  5553. if (phone) {
  5554. phone = [AESCrypt fastencrypt:phone];
  5555. } else {
  5556. phone = @"";
  5557. }
  5558. DebugLog(@"PHONE");
  5559. phone = [self translateSingleQuote:phone];
  5560. NSString *fax = [params objectForKey:@"fax"];
  5561. if (!fax) {
  5562. fax = @"";
  5563. }
  5564. DebugLog(@"FAX");
  5565. fax = [self translateSingleQuote:fax];
  5566. NSString *email = [params objectForKey:@"email"];
  5567. if (!email) {
  5568. email = @"";
  5569. }
  5570. DebugLog(@"EMAIL:%@",email);
  5571. email = [self translateSingleQuote:email];
  5572. NSString *notes = [params objectForKey:@"contact_notes"];
  5573. if (!notes) {
  5574. notes = @"";
  5575. }
  5576. DebugLog(@"NOTE:%@",notes);
  5577. notes = [self translateSingleQuote:notes];
  5578. NSString *price = [params objectForKey:@"price_name"];
  5579. if (price) {
  5580. price = [self priceNameByPriceId:price];
  5581. } else {
  5582. price = @"";
  5583. }
  5584. DebugLog(@"PRICE");
  5585. price = [self translateSingleQuote:price];
  5586. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5587. if (salesRep) {
  5588. salesRep = [self salesRepCodeById:salesRep];
  5589. } else {
  5590. salesRep = @"";
  5591. }
  5592. salesRep = [self translateSingleQuote:salesRep];
  5593. NSString *img = [params objectForKey:@"business_card"];
  5594. NSArray *array = [img componentsSeparatedByString:@","];
  5595. NSString *img_0 = array[0];
  5596. if (!img_0) {
  5597. img_0 = @"";
  5598. }
  5599. img_0 = [self translateSingleQuote:img_0];
  5600. NSString *img_1 = array[1];
  5601. if (!img_1) {
  5602. img_1 = @"";
  5603. }
  5604. img_1 = [self translateSingleQuote:img_1];
  5605. NSString *img_2 = array[2];
  5606. if (!img_2) {
  5607. img_2 = @"";
  5608. }
  5609. img_2 = [self translateSingleQuote:img_2];
  5610. NSString *contact_id = [NSUUID UUID].UUIDString;
  5611. NSString *contact_type = [params objectForKey:@"type_name"];
  5612. if (!contact_type) {
  5613. contact_type = @"";
  5614. }
  5615. contact_type = [self translateSingleQuote:contact_type];
  5616. // 判断更新时是否为customer
  5617. if (update) {
  5618. contact_id = [params objectForKey:@"contact_id"];
  5619. if (!contact_id) {
  5620. contact_id = @"";
  5621. }
  5622. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5623. __block int customer = 0;
  5624. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5625. customer = sqlite3_column_int(stmt, 0);
  5626. }];
  5627. isCustomer = customer ? YES : NO;
  5628. }
  5629. NSMutableDictionary *sync_dic = [params mutableCopy];
  5630. if (isCustomer) {
  5631. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5632. } else {
  5633. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5634. }
  5635. NSString *sync_data = nil;
  5636. NSString *sql = nil;
  5637. if (update){
  5638. contact_id = [params objectForKey:@"contact_id"];
  5639. if (!contact_id) {
  5640. contact_id = @"";
  5641. }
  5642. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5643. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5644. sync_data = [RAConvertor dict2string:sync_dic];
  5645. sync_data = [self translateSingleQuote:sync_data];
  5646. 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];
  5647. } else {
  5648. contact_id = [self translateSingleQuote:contact_id];
  5649. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5650. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5651. sync_data = [RAConvertor dict2string:sync_dic];
  5652. sync_data = [self translateSingleQuote:sync_data];
  5653. 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];
  5654. }
  5655. int result = [iSalesDB execSql:sql];
  5656. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5657. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5658. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5659. }
  5660. #pragma mark save new contact
  5661. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5662. {
  5663. return [self offline_saveContact:params update:NO isCustomer:YES];
  5664. }
  5665. #pragma mark edit contact
  5666. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5667. {
  5668. assert(params[@"user"]!=nil);
  5669. // {
  5670. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5671. // password = 123456;
  5672. // user = EvanK;
  5673. // }
  5674. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5675. NSData *data = [NSData dataWithContentsOfFile:path];
  5676. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5677. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5678. NSString *countryCode = nil;
  5679. NSString *countryCode_id = nil;
  5680. NSString *stateCode = nil;
  5681. /*------contact infor------*/
  5682. __block NSString *country = nil;
  5683. __block NSString *company_name = nil;
  5684. __block NSString *contact_id = params[@"contact_id"];
  5685. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5686. __block NSString *zipcode = nil;
  5687. __block NSString *state = nil; // state_code
  5688. __block NSString *city = nil; //
  5689. __block NSString *firt_name,*last_name;
  5690. __block NSString *phone,*fax,*email;
  5691. __block NSString *notes,*price_type,*sales_rep;
  5692. __block NSString *img_0,*img_1,*img_2;
  5693. __block NSString *contact_type;
  5694. contact_id = [self translateSingleQuote:contact_id];
  5695. 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];
  5696. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5697. country = [self textAtColumn:0 statement:stmt]; // country name
  5698. company_name = [self textAtColumn:1 statement:stmt];
  5699. addr_1 = [self textAtColumn:2 statement:stmt];
  5700. addr_2 = [self textAtColumn:3 statement:stmt];
  5701. addr_3 = [self textAtColumn:4 statement:stmt];
  5702. addr_4 = [self textAtColumn:5 statement:stmt];
  5703. zipcode = [self textAtColumn:6 statement:stmt];
  5704. state = [self textAtColumn:7 statement:stmt]; // state code
  5705. city = [self textAtColumn:8 statement:stmt];
  5706. firt_name = [self textAtColumn:9 statement:stmt];
  5707. last_name = [self textAtColumn:10 statement:stmt];
  5708. phone = [self textAtColumn:11 statement:stmt];
  5709. fax = [self textAtColumn:12 statement:stmt];
  5710. email = [self textAtColumn:13 statement:stmt];
  5711. notes = [self textAtColumn:14 statement:stmt];
  5712. price_type = [self textAtColumn:15 statement:stmt]; // name
  5713. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5714. img_0 = [self textAtColumn:17 statement:stmt];
  5715. img_1 = [self textAtColumn:18 statement:stmt];
  5716. img_2 = [self textAtColumn:19 statement:stmt];
  5717. contact_type = [self textAtColumn:20 statement:stmt];
  5718. }];
  5719. // decrypt
  5720. if (company_name) {
  5721. company_name = [AESCrypt fastdecrypt:company_name];
  5722. }
  5723. if (addr_1) {
  5724. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5725. }
  5726. if (phone) {
  5727. phone = [AESCrypt fastdecrypt:phone];
  5728. }
  5729. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5730. countryCode = [iSalesDB jk_queryText:countrySql];
  5731. stateCode = state;
  5732. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5733. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5734. NSString *code_id = params[@"country"];
  5735. countryCode_id = code_id;
  5736. countryCode = [self countryCodeByid:code_id];
  5737. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5738. NSString *zip_code = params[@"zipcode"];
  5739. // 剔除全部为空格
  5740. int spaceCount = 0;
  5741. for (int i = 0; i < zip_code.length; i++) {
  5742. if ([zip_code characterAtIndex:i] == ' ') {
  5743. spaceCount++;
  5744. }
  5745. }
  5746. if (spaceCount == zip_code.length) {
  5747. zip_code = @"";
  5748. }
  5749. if (zipcode.length > 0) {
  5750. zipcode = zip_code;
  5751. countryCode_id = params[@"country"];
  5752. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5753. countryCode = [dic valueForKey:@"country_code"];
  5754. if (!countryCode) {
  5755. NSString *code_id = params[@"country"];
  5756. countryCode = [self countryCodeByid:code_id];
  5757. }
  5758. stateCode = [dic valueForKey:@"state_code"];
  5759. if (!stateCode.length) {
  5760. stateCode = params[@"state"];
  5761. }
  5762. city = [dic valueForKey:@"city"];
  5763. if (!city.length) {
  5764. city = params[@"city"];
  5765. }
  5766. // zip code
  5767. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5768. [zipDic setValue:zipcode forKey:@"value"];
  5769. [section_0 setValue:zipDic forKey:@"item_8"];
  5770. } else {
  5771. NSString *code_id = params[@"country"];
  5772. countryCode = [self countryCodeByid:code_id];
  5773. stateCode = params[@"state"];
  5774. city = params[@"city"];
  5775. }
  5776. }
  5777. }
  5778. // 0 Country
  5779. // 1 Company Name
  5780. // 2 Contact ID
  5781. // 3 Picture
  5782. // 4 Address 1
  5783. // 5 Address 2
  5784. // 6 Address 3
  5785. // 7 Address 4
  5786. // 8 Zip Code
  5787. // 9 State/Province
  5788. // 10 City
  5789. // 11 Contact First Name
  5790. // 12 Contact Last Name
  5791. // 13 Phone
  5792. // 14 Fax
  5793. // 15 Email
  5794. // 16 Contact Notes
  5795. // 17 Price Type
  5796. // 18 Contact Type
  5797. // 19 Sales Rep
  5798. // country
  5799. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5800. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5801. // company
  5802. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5803. // contact_id
  5804. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5805. // picture
  5806. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5807. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5808. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5809. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5810. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5811. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5812. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5813. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5814. // addr 1 2 3 4
  5815. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5816. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5817. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5818. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5819. // zip code
  5820. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5821. // state
  5822. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5823. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5824. // city
  5825. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5826. // first last
  5827. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5828. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5829. // phone fax email
  5830. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5831. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5832. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5833. // notes
  5834. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5835. // price
  5836. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5837. for (NSString *key in priceDic.allKeys) {
  5838. if ([key containsString:@"val_"]) {
  5839. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5840. if ([dic[@"value"] isEqualToString:price_type]) {
  5841. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5842. [priceDic setValue:dic forKey:key];
  5843. }
  5844. }
  5845. }
  5846. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5847. // Contact Rep
  5848. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5849. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5850. // Sales Rep
  5851. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5852. for (NSString *key in repDic.allKeys) {
  5853. if ([key containsString:@"val_"]) {
  5854. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5855. NSString *value = dic[@"value"];
  5856. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5857. if (code && [code isEqualToString:sales_rep]) {
  5858. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5859. [repDic setValue:dic forKey:key];
  5860. }
  5861. }
  5862. }
  5863. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5864. [ret setValue:section_0 forKey:@"section_0"];
  5865. return [RAConvertor dict2data:ret];
  5866. }
  5867. #pragma mark save contact
  5868. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5869. {
  5870. return [self offline_saveContact:params update:YES isCustomer:YES];
  5871. }
  5872. #pragma mark category
  5873. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5874. if (ck) {
  5875. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5876. for (NSString *key in res.allKeys) {
  5877. if (![key isEqualToString:@"count"]) {
  5878. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5879. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5880. if ([val[@"value"] isEqualToString:ck]) {
  5881. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5882. }
  5883. [res setValue:val forKey:key];
  5884. }
  5885. }
  5886. [dic setValue:res forKey:valueKey];
  5887. }
  5888. }
  5889. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5890. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5891. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5892. NSString* category = [params valueForKey:@"category"];
  5893. if (!category || [category isEqualToString:@""]) {
  5894. category = @"%";
  5895. }
  5896. category = [self translateSingleQuote:category];
  5897. int limit = [[params valueForKey:@"limit"] intValue];
  5898. int offset = [[params valueForKey:@"offset"] intValue];
  5899. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5900. NSString *limit_str = @"";
  5901. if (limited) {
  5902. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5903. }
  5904. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5905. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5906. sqlite3 *db = [iSalesDB get_db];
  5907. // [iSalesDB AddExFunction:db];
  5908. int count;
  5909. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5910. 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];
  5911. double price_min = 0;
  5912. double price_max = 0;
  5913. if ([params.allKeys containsObject:@"alert"]) {
  5914. // alert
  5915. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5916. NSString *alert = params[@"alert"];
  5917. if ([alert isEqualToString:@"Display All"]) {
  5918. alert = [NSString stringWithFormat:@""];
  5919. } else {
  5920. alert = [self translateSingleQuote:alert];
  5921. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5922. }
  5923. // available
  5924. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5925. NSString *available = params[@"available"];
  5926. NSString *available_condition;
  5927. if ([available isEqualToString:@"Display All"]) {
  5928. available_condition = @"";
  5929. } else if ([available isEqualToString:@"Available Now"]) {
  5930. available_condition = @"and availability > 0";
  5931. } else {
  5932. available_condition = @"and availability == 0";
  5933. }
  5934. // best seller
  5935. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5936. NSString *best_seller = @"";
  5937. NSString *order_best_seller = @"m.name asc";
  5938. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5939. best_seller = @"and best_seller > 0";
  5940. order_best_seller = @"m.best_seller desc,m.name asc";
  5941. }
  5942. // price
  5943. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5944. NSString *price = params[@"price"];
  5945. price_min = 0;
  5946. price_max = MAXFLOAT;
  5947. if (params[@"user"] && price != nil) {
  5948. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5949. NSMutableString *priceName = [NSMutableString string];
  5950. for (int i = 0; i < priceTypeArray.count; i++) {
  5951. NSString *pricetype = priceTypeArray[i];
  5952. pricetype = [self translateSingleQuote:pricetype];
  5953. if (i == 0) {
  5954. [priceName appendFormat:@"'%@'",pricetype];
  5955. } else {
  5956. [priceName appendFormat:@",'%@'",pricetype];
  5957. }
  5958. }
  5959. if ([price isEqualToString:@"Display All"]) {
  5960. price = [NSString stringWithFormat:@""];
  5961. } else if([price containsString:@"+"]){
  5962. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5963. price_min = [price doubleValue];
  5964. 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];
  5965. } else {
  5966. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5967. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5968. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5969. 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];
  5970. }
  5971. } else {
  5972. price = @"";
  5973. }
  5974. // sold_by_qty : Sold in quantities of %@
  5975. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5976. NSString *qty = params[@"sold_by_qty"];
  5977. if ([qty isEqualToString:@"Display All"]) {
  5978. qty = @"";
  5979. } else {
  5980. qty = [self translateSingleQuote:qty];
  5981. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5982. }
  5983. // model name;
  5984. NSString* modelname =params[@"modelName"];
  5985. if(modelname.length==0)
  5986. {
  5987. modelname=@"";
  5988. }
  5989. else
  5990. {
  5991. modelname = modelname.lowercaseString;
  5992. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5993. }
  5994. //modelDescription
  5995. NSString* modelDescription =params[@"modelDescription"];
  5996. if(modelDescription.length==0)
  5997. {
  5998. modelDescription=@"";
  5999. }
  6000. else
  6001. {
  6002. modelDescription = modelDescription.lowercaseString;
  6003. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6004. }
  6005. // cate
  6006. // category = [self translateSingleQuote:category];
  6007. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6008. // cate mutiple selection
  6009. NSString *category_id = params[@"category"];
  6010. NSMutableArray *cate_id_array = nil;
  6011. NSMutableString *cateWhere = [NSMutableString string];
  6012. if ([category_id isEqualToString:@""] || !category_id) {
  6013. [cateWhere appendString:@"1 = 1"];
  6014. } else {
  6015. if ([category_id containsString:@","]) {
  6016. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6017. } else {
  6018. cate_id_array = [@[category_id] mutableCopy];
  6019. }
  6020. [cateWhere appendString:@"("];
  6021. for (int i = 0; i < cate_id_array.count; i++) {
  6022. if (i == 0) {
  6023. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6024. } else {
  6025. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6026. }
  6027. }
  6028. [cateWhere appendString:@")"];
  6029. }
  6030. // where bestseller > 0 order by bestseller desc
  6031. // sql query: alert availability(int) best_seller(int) price qty
  6032. 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];
  6033. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6034. }
  6035. DebugLog(@"offline category where: %@",where);
  6036. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6037. if (!params[@"user"]) {
  6038. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6039. }
  6040. [ret setValue:filter forKey:@"filter"];
  6041. DebugLog(@"offline_category sql:%@",sqlQuery);
  6042. sqlite3_stmt * statement;
  6043. [ret setValue:@"2" forKey:@"result"];
  6044. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6045. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6046. // int count=0;
  6047. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6048. {
  6049. int i=0;
  6050. while (sqlite3_step(statement) == SQLITE_ROW)
  6051. {
  6052. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6053. char *name = (char*)sqlite3_column_text(statement, 0);
  6054. if(name==nil)
  6055. name="";
  6056. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6057. char *description = (char*)sqlite3_column_text(statement, 1);
  6058. if(description==nil)
  6059. description="";
  6060. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6061. int product_id = sqlite3_column_int(statement, 2);
  6062. int wid = sqlite3_column_int(statement, 3);
  6063. int closeout = sqlite3_column_int(statement, 4);
  6064. int cid = sqlite3_column_int(statement, 5);
  6065. int wisdelete = sqlite3_column_int(statement, 6);
  6066. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6067. int more_color = sqlite3_column_int(statement, 8);
  6068. // Defaul Category ID
  6069. __block NSString *categoryID = nil;
  6070. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6071. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6072. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6073. if(default_category==nil)
  6074. default_category="";
  6075. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6076. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6077. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6078. categoryID = nsdefault_category;
  6079. }];
  6080. if (!categoryID.length) {
  6081. NSString *cateIDs = params[@"category"];
  6082. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6083. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6084. for (NSString *cateID in requestCategoryArr) {
  6085. BOOL needBreak = NO;
  6086. for (NSString *itemCateIDBox in itemCategoryArr) {
  6087. if (itemCateIDBox.length > 4) {
  6088. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6089. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6090. if ([itemCategoryID isEqualToString:cateID]) {
  6091. needBreak = YES;
  6092. categoryID = itemCategoryID;
  6093. break;
  6094. }
  6095. }
  6096. }
  6097. if (needBreak) {
  6098. break;
  6099. }
  6100. }
  6101. }
  6102. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6103. if(wid !=0 && wisdelete != 1)
  6104. [item setValue:@"true" forKey:@"wish_exists"];
  6105. else
  6106. [item setValue:@"false" forKey:@"wish_exists"];
  6107. if(closeout==0)
  6108. [item setValue:@"false" forKey:@"is_closeout"];
  6109. else
  6110. [item setValue:@"true" forKey:@"is_closeout"];
  6111. if(cid==0)
  6112. [item setValue:@"false" forKey:@"cart_exists"];
  6113. else
  6114. [item setValue:@"true" forKey:@"cart_exists"];
  6115. if (more_color == 0) {
  6116. [item setObject:@(false) forKey:@"more_color"];
  6117. } else if (more_color == 1) {
  6118. [item setObject:@(true) forKey:@"more_color"];
  6119. }
  6120. [item addEntriesFromDictionary:imgjson];
  6121. // [item setValue:nsurl forKey:@"img"];
  6122. [item setValue:nsname forKey:@"name"];
  6123. [item setValue:nsdescription forKey:@"description"];
  6124. if (categoryID) {
  6125. [item setValue:categoryID forKey:@"item_category_id"];
  6126. }
  6127. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6128. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6129. i++;
  6130. }
  6131. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6132. [ret setObject:items forKey:@"items"];
  6133. sqlite3_finalize(statement);
  6134. } else {
  6135. DebugLog(@"nothing...");
  6136. }
  6137. DebugLog(@"count:%d",count);
  6138. [iSalesDB close_db:db];
  6139. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6140. return ret;
  6141. }
  6142. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6143. {
  6144. return [self categoryList:params limited:YES];
  6145. }
  6146. # pragma mark item search
  6147. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6148. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6149. // assert(params[@"order_code"]);
  6150. // params[@"user"]
  6151. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6152. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6153. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6154. // category
  6155. NSDictionary *category_menu = [self offline_category_menu];
  6156. [filter setValue:category_menu forKey:@"category"];
  6157. NSString* where= nil;
  6158. NSString* orderby= @"m.name";
  6159. if (!filterSearch) {
  6160. int covertype = [[params valueForKey:@"covertype"] intValue];
  6161. switch (covertype) {
  6162. case 0:
  6163. {
  6164. where=@"m.category like'%%#005#%%'";
  6165. break;
  6166. }
  6167. case 1:
  6168. {
  6169. where=@"m.alert like '%QS%'";
  6170. break;
  6171. }
  6172. case 2:
  6173. {
  6174. where=@"m.availability>0";
  6175. break;
  6176. }
  6177. case 3:
  6178. {
  6179. where=@"m.best_seller>0";
  6180. orderby=@"m.best_seller desc,m.name asc";
  6181. break;
  6182. }
  6183. default:
  6184. where=@"1=1";
  6185. break;
  6186. }
  6187. }
  6188. int limit = [[params valueForKey:@"limit"] intValue];
  6189. int offset = [[params valueForKey:@"offset"] intValue];
  6190. NSString *limit_str = @"";
  6191. if (limited) {
  6192. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6193. }
  6194. sqlite3 *db = [iSalesDB get_db];
  6195. // [iSalesDB AddExFunction:db];
  6196. int count;
  6197. NSString *sqlQuery = nil;
  6198. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6199. 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];
  6200. double price_min = 0;
  6201. double price_max = 0;
  6202. if (filterSearch) {
  6203. // alert
  6204. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6205. NSString *alert = params[@"alert"];
  6206. if ([alert isEqualToString:@"Display All"]) {
  6207. alert = [NSString stringWithFormat:@""];
  6208. } else {
  6209. alert = [self translateSingleQuote:alert];
  6210. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6211. }
  6212. // available
  6213. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6214. NSString *available = params[@"available"];
  6215. NSString *available_condition;
  6216. if ([available isEqualToString:@"Display All"]) {
  6217. available_condition = @"";
  6218. } else if ([available isEqualToString:@"Available Now"]) {
  6219. available_condition = @"and availability > 0";
  6220. } else {
  6221. available_condition = @"and availability == 0";
  6222. }
  6223. // best seller
  6224. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6225. NSString *best_seller = @"";
  6226. NSString *order_best_seller = @"m.name asc";
  6227. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6228. best_seller = @"and best_seller > 0";
  6229. order_best_seller = @"m.best_seller desc,m.name asc";
  6230. }
  6231. // price
  6232. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6233. NSString *price = params[@"price"];
  6234. price_min = 0;
  6235. price_max = MAXFLOAT;
  6236. if (params[@"user"]) {
  6237. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6238. NSMutableString *priceName = [NSMutableString string];
  6239. for (int i = 0; i < priceTypeArray.count; i++) {
  6240. NSString *pricetype = priceTypeArray[i];
  6241. pricetype = [self translateSingleQuote:pricetype];
  6242. if (i == 0) {
  6243. [priceName appendFormat:@"'%@'",pricetype];
  6244. } else {
  6245. [priceName appendFormat:@",'%@'",pricetype];
  6246. }
  6247. }
  6248. if ([price isEqualToString:@"Display All"]) {
  6249. price = [NSString stringWithFormat:@""];
  6250. } else if([price containsString:@"+"]){
  6251. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6252. price_min = [price doubleValue];
  6253. 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];
  6254. } else {
  6255. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6256. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6257. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6258. 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];
  6259. }
  6260. } else {
  6261. price = @"";
  6262. }
  6263. // sold_by_qty : Sold in quantities of %@
  6264. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6265. NSString *qty = params[@"sold_by_qty"];
  6266. if ([qty isEqualToString:@"Display All"]) {
  6267. qty = @"";
  6268. } else {
  6269. qty = [self translateSingleQuote:qty];
  6270. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6271. }
  6272. // model name;
  6273. NSString* modelname =params[@"modelName"];
  6274. if(modelname.length==0)
  6275. {
  6276. modelname=@"";
  6277. }
  6278. else
  6279. {
  6280. modelname = modelname.lowercaseString;
  6281. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6282. }
  6283. //modelDescription
  6284. NSString* modelDescription =params[@"modelDescription"];
  6285. if(modelDescription.length==0)
  6286. {
  6287. modelDescription=@"";
  6288. }
  6289. else
  6290. {
  6291. modelDescription = modelDescription.lowercaseString;
  6292. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6293. }
  6294. //lower(description) like'%%%@%%'
  6295. // category
  6296. NSString *category_id = params[@"ctgId"];
  6297. NSMutableArray *cate_id_array = nil;
  6298. NSMutableString *cateWhere = [NSMutableString string];
  6299. if ([category_id isEqualToString:@""] || !category_id) {
  6300. [cateWhere appendString:@"1 = 1"];
  6301. } else {
  6302. if ([category_id containsString:@","]) {
  6303. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6304. } else {
  6305. cate_id_array = [@[category_id] mutableCopy];
  6306. }
  6307. /*-----------*/
  6308. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6309. [cateWhere appendString:@"("];
  6310. for (int i = 0; i < cate_id_array.count; i++) {
  6311. for (NSString *key0 in cateDic.allKeys) {
  6312. if ([key0 containsString:@"category_"]) {
  6313. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6314. for (NSString *key1 in category0.allKeys) {
  6315. if ([key1 containsString:@"category_"]) {
  6316. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6317. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6318. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6319. cate_id_array[i] = [category1 objectForKey:@"id"];
  6320. if (i == 0) {
  6321. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6322. } else {
  6323. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6324. }
  6325. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6326. [category0 setValue:category1 forKey:key1];
  6327. [cateDic setValue:category0 forKey:key0];
  6328. }
  6329. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6330. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6331. cate_id_array[i] = [category0 objectForKey:@"id"];
  6332. if (i == 0) {
  6333. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6334. } else {
  6335. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6336. }
  6337. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6338. [cateDic setValue:category0 forKey:key0];
  6339. }
  6340. }
  6341. }
  6342. }
  6343. }
  6344. }
  6345. [cateWhere appendString:@")"];
  6346. [filter setValue:cateDic forKey:@"category"];
  6347. }
  6348. // where bestseller > 0 order by bestseller desc
  6349. // sql query: alert availability(int) best_seller(int) price qty
  6350. 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];
  6351. // count
  6352. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6353. }
  6354. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6355. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6356. if (!params[@"user"]) {
  6357. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6358. }
  6359. [ret setValue:filter forKey:@"filter"];
  6360. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6361. sqlite3_stmt * statement;
  6362. [ret setValue:@"2" forKey:@"result"];
  6363. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6364. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6365. // int count=0;
  6366. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6367. {
  6368. int i=0;
  6369. while (sqlite3_step(statement) == SQLITE_ROW)
  6370. {
  6371. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6372. char *name = (char*)sqlite3_column_text(statement, 0);
  6373. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6374. char *description = (char*)sqlite3_column_text(statement, 1);
  6375. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6376. int product_id = sqlite3_column_int(statement, 2);
  6377. int wid = sqlite3_column_int(statement, 3);
  6378. int closeout = sqlite3_column_int(statement, 4);
  6379. int cid = sqlite3_column_int(statement, 5);
  6380. int wisdelete = sqlite3_column_int(statement, 6);
  6381. int more_color = sqlite3_column_int(statement, 7);
  6382. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6383. if(wid !=0 && wisdelete != 1)
  6384. [item setValue:@"true" forKey:@"wish_exists"];
  6385. else
  6386. [item setValue:@"false" forKey:@"wish_exists"];
  6387. if(closeout==0)
  6388. [item setValue:@"false" forKey:@"is_closeout"];
  6389. else
  6390. [item setValue:@"true" forKey:@"is_closeout"];
  6391. if(cid==0)
  6392. [item setValue:@"false" forKey:@"cart_exists"];
  6393. else
  6394. [item setValue:@"true" forKey:@"cart_exists"];
  6395. if (more_color == 0) {
  6396. [item setObject:@(false) forKey:@"more_color"];
  6397. } else if (more_color == 1) {
  6398. [item setObject:@(true) forKey:@"more_color"];
  6399. }
  6400. [item addEntriesFromDictionary:imgjson];
  6401. // [item setValue:nsurl forKey:@"img"];
  6402. [item setValue:nsname forKey:@"fash_name"];
  6403. [item setValue:nsdescription forKey:@"description"];
  6404. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6405. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6406. i++;
  6407. }
  6408. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6409. [ret setObject:items forKey:@"items"];
  6410. sqlite3_finalize(statement);
  6411. }
  6412. [iSalesDB close_db:db];
  6413. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6414. return ret;
  6415. }
  6416. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6417. {
  6418. return [self itemsearch:params limited:YES];
  6419. }
  6420. #pragma mark order detail
  6421. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6422. if (str1.length == 0) {
  6423. str1 = @"&nbsp";
  6424. }
  6425. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6426. return str;
  6427. }
  6428. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6429. {
  6430. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6431. [fromformatter setDateFormat:from];
  6432. NSDate *date = [fromformatter dateFromString:datetime];
  6433. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6434. [toformatter setDateFormat:to];
  6435. NSString * ret = [toformatter stringFromDate:date];
  6436. return ret;
  6437. }
  6438. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6439. // 把毫秒去掉
  6440. if ([dateTime containsString:@"."]) {
  6441. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6442. }
  6443. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6444. formatter.dateFormat = formate;
  6445. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6446. NSDate *date = [formatter dateFromString:dateTime];
  6447. formatter.dateFormat = newFormate;
  6448. NSString *result = [formatter stringFromDate:date];
  6449. return result ? result : @"";
  6450. }
  6451. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6452. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6453. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6454. }
  6455. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6456. {
  6457. assert(params[@"mode"]!=nil);
  6458. assert(params[@"user"]!=nil);
  6459. DebugLog(@"offline oderdetail params: %@",params);
  6460. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6461. int orderId = [params[@"orderId"] intValue];
  6462. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6463. // decrypt card number and card security code
  6464. // 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 ];
  6465. 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];
  6466. sqlite3 *db = [iSalesDB get_db];
  6467. sqlite3_stmt * statement;
  6468. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6469. NSString *nssoid = @"";
  6470. NSString* orderinfo = @"";
  6471. NSString *moreInfo = @"";
  6472. double handlingFee = 0;
  6473. double payments_and_credist = 0;
  6474. double totalPrice = 0;
  6475. double shippingFee = 0;
  6476. double lift_gate = 0;
  6477. NSString *customer_contact = @"";
  6478. NSString *customer_email = @"";
  6479. NSString *customer_fax = @"";
  6480. NSString *customer_phone = @"";
  6481. NSString *customer_city = @"";
  6482. NSString *customer_state = @"";
  6483. NSString *customer_zipcode = @"";
  6484. NSString *customer_country = @"";
  6485. BOOL must_call = NO;
  6486. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6487. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6488. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6489. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6490. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6491. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6492. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6493. {
  6494. if (sqlite3_step(statement) == SQLITE_ROW)
  6495. {
  6496. // int order_id = sqlite3_column_int(statement, 0);
  6497. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6498. // ret[@"sign_url"] = sign_url;
  6499. section_1 = @{
  6500. @"data":sign_url,
  6501. @"title":@"Signature",
  6502. @"type":@"sign_url"
  6503. }.mutableCopy;
  6504. [ret setObject:section_1 forKey:@"section_1"];
  6505. customer_contact = [self textAtColumn:52 statement:statement];
  6506. customer_email = [self textAtColumn:53 statement:statement];
  6507. customer_phone = [self textAtColumn:54 statement:statement];
  6508. customer_fax = [self textAtColumn:55 statement:statement];
  6509. customer_city = [self textAtColumn:60 statement:statement];
  6510. customer_state = [self textAtColumn:61 statement:statement];
  6511. customer_zipcode = [self textAtColumn:62 statement:statement];
  6512. customer_country = [self textAtColumn:63 statement:statement];
  6513. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6514. int offline_edit=sqlite3_column_int(statement, 56);
  6515. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6516. char *soid = (char*)sqlite3_column_text(statement, 1);
  6517. if(soid==nil)
  6518. soid= "";
  6519. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6520. // so#
  6521. ret[@"so#"] = nssoid;
  6522. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6523. if(poNumber==nil)
  6524. poNumber= "";
  6525. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6526. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6527. if(create_time==nil)
  6528. create_time= "";
  6529. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6530. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6531. int status = sqlite3_column_int(statement, 4);
  6532. int erpStatus = sqlite3_column_int(statement, 49);
  6533. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6534. // status
  6535. if (status > 1 && status != 3) {
  6536. status = erpStatus;
  6537. } else if (status == 3) {
  6538. status = 15;
  6539. }
  6540. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6541. ret[@"order_status"] = nsstatus;
  6542. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6543. if(company_name==nil)
  6544. company_name= "";
  6545. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6546. // company name
  6547. ret[@"company_name"] = nscompany_name;
  6548. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6549. if(customer_contact==nil)
  6550. customer_contact= "";
  6551. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6552. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6553. if(addr_1==nil)
  6554. addr_1="";
  6555. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6556. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6557. if(addr_2==nil)
  6558. addr_2="";
  6559. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6560. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6561. if(addr_3==nil)
  6562. addr_3="";
  6563. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6564. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6565. if(addr_4==nil)
  6566. addr_4="";
  6567. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6568. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6569. [arr_addr addObject:nsaddr_1];
  6570. [arr_addr addObject:nsaddr_2];
  6571. [arr_addr addObject:nsaddr_3];
  6572. [arr_addr addObject:nsaddr_4];
  6573. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6574. [arr_addr addObject:customer_state];
  6575. [arr_addr addObject:customer_zipcode];
  6576. [arr_addr addObject:customer_country];
  6577. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6578. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6579. char *logist = (char*)sqlite3_column_text(statement, 11);
  6580. if(logist==nil)
  6581. logist= "";
  6582. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6583. if (/*status == -11 || */status == 10 || status == 11) {
  6584. nslogist = [self textAtColumn:59 statement:statement];
  6585. };
  6586. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6587. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6588. shipping = @"Shipping To Be Quoted";
  6589. } else {
  6590. shippingFee = sqlite3_column_double(statement, 12);
  6591. }
  6592. // Shipping
  6593. // ret[@"Shipping"] = shipping;
  6594. NSDictionary *shipping_item = @{
  6595. @"title":@"Shipping",
  6596. @"value":shipping
  6597. };
  6598. [price_data setObject:shipping_item forKey:@"item_1"];
  6599. int have_lift_gate = sqlite3_column_int(statement, 17);
  6600. lift_gate = sqlite3_column_double(statement, 13);
  6601. // Liftgate Fee(No loading dock)
  6602. if (!have_lift_gate) {
  6603. lift_gate = 0;
  6604. }
  6605. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6606. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6607. if (sqlite3_column_int(statement, 57)) {
  6608. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6609. liftgate_value = @"Shipping To Be Quoted";
  6610. }
  6611. NSDictionary *liftgate_item = @{
  6612. @"title":@"Liftgate Fee(No loading dock)",
  6613. @"value":liftgate_value
  6614. };
  6615. [price_data setObject:liftgate_item forKey:@"item_2"];
  6616. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6617. // [ret removeObjectForKey:@"Shipping"];
  6618. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6619. // }
  6620. //
  6621. // if (have_lift_gate) {
  6622. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6623. // }
  6624. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6625. if(general_notes==nil)
  6626. general_notes= "";
  6627. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6628. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6629. if(internal_notes==nil)
  6630. internal_notes= "";
  6631. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6632. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6633. if(payment_type==nil)
  6634. payment_type= "";
  6635. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6636. // order info
  6637. orderinfo = [self textFileName:@"order_info.html"];
  6638. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6639. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6640. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6641. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6642. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6643. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6644. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6645. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6646. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6647. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6648. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6649. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6650. NSString *payment = nil;
  6651. // id -> show
  6652. __block NSString *show_pay_type = nspayment_type;
  6653. [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) {
  6654. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6655. if (show_typ_ch != NULL) {
  6656. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6657. }
  6658. }];
  6659. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6660. {
  6661. payment = [self textFileName:@"creditcardpayment.html"];
  6662. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6663. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6664. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6665. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6666. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6667. NSString *card_type = [self textAtColumn:42 statement:statement];
  6668. if (card_type.length > 0) { // 显示星号
  6669. card_type = @"****";
  6670. }
  6671. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6672. if (card_number.length > 0 && card_number.length > 4) {
  6673. for (int i = 0; i < card_number.length - 4; i++) {
  6674. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6675. }
  6676. } else {
  6677. card_number = @"";
  6678. }
  6679. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6680. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6681. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6682. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6683. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6684. card_expiration = @"****";
  6685. }
  6686. NSString *card_city = [self textAtColumn:46 statement:statement];
  6687. NSString *card_state = [self textAtColumn:47 statement:statement];
  6688. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6689. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6690. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6691. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6692. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6693. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6694. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6695. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6696. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6697. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6698. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6699. }
  6700. else
  6701. {
  6702. payment=[self textFileName:@"normalpayment.html"];
  6703. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6704. }
  6705. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6706. ret[@"result"]= [NSNumber numberWithInt:2];
  6707. // more info
  6708. moreInfo = [self textFileName:@"more_info.html"];
  6709. /*****ship to******/
  6710. // ShipToCompany_or_&nbsp
  6711. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6712. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6713. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6714. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6715. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6716. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6717. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6718. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6719. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6720. /*****ship from******/
  6721. // ShipFromCompany_or_&nbsp
  6722. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6723. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6724. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6725. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6726. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6727. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6728. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6729. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6730. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6731. /*****freight to******/
  6732. // FreightBillToCompany_or_&nbsp
  6733. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6734. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6735. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6736. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6737. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6738. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6739. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6740. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6741. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6742. /*****merchandise to******/
  6743. // MerchandiseBillToCompany_or_&nbsp
  6744. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6745. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6746. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6747. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6748. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6749. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6750. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6751. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6752. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6753. /*****return to******/
  6754. // ReturnToCompany_or_&nbsp
  6755. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6756. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6757. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6758. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6759. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6760. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6761. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6762. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6763. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6764. //
  6765. // DebugLog(@"more info : %@",moreInfo);
  6766. // handling fee
  6767. handlingFee = sqlite3_column_double(statement, 33);
  6768. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6769. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6770. if (sqlite3_column_int(statement, 58)) {
  6771. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6772. handling_fee_value = @"Shipping To Be Quoted";
  6773. }
  6774. NSDictionary *handling_fee_item = @{
  6775. @"title":@"Handling Fee",
  6776. @"value":handling_fee_value
  6777. };
  6778. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6779. //
  6780. // customer info
  6781. customerID = [self textAtColumn:36 statement:statement];
  6782. // mode
  6783. ret[@"mode"] = params[@"mode"];
  6784. // model_count
  6785. ret[@"model_count"] = @(0);
  6786. }
  6787. sqlite3_finalize(statement);
  6788. }
  6789. [iSalesDB close_db:db];
  6790. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6791. // "customer_email" = "Shui Hu";
  6792. // "customer_fax" = "";
  6793. // "customer_first_name" = F;
  6794. // "customer_last_name" = L;
  6795. // "customer_name" = ",da He Xiang Dong Liu A";
  6796. // "customer_phone" = "Hey Xuan Feng";
  6797. contactInfo[@"customer_phone"] = customer_phone;
  6798. contactInfo[@"customer_fax"] = customer_fax;
  6799. contactInfo[@"customer_email"] = customer_email;
  6800. NSString *first_name = @"";
  6801. NSString *last_name = @"";
  6802. if ([customer_contact isEqualToString:@""]) {
  6803. } else if ([customer_contact containsString:@" "]) {
  6804. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6805. first_name = [customer_contact substringToIndex:first_space_index];
  6806. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6807. }
  6808. contactInfo[@"customer_first_name"] = first_name;
  6809. contactInfo[@"customer_last_name"] = last_name;
  6810. ret[@"customerInfo"] = contactInfo;
  6811. // models
  6812. if (nssoid) {
  6813. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6814. __block double TotalCuft = 0;
  6815. __block double TotalWeight = 0;
  6816. __block int TotalCarton = 0;
  6817. __block double allItemPrice = 0;
  6818. 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];
  6819. sqlite3 *db1 = [iSalesDB get_db];
  6820. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6821. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6822. int product_id = sqlite3_column_int(stmt, 0);
  6823. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6824. int item_id = sqlite3_column_int(stmt, 7);
  6825. NSString* Price=nil;
  6826. if(str_price==nil)
  6827. {
  6828. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6829. if(price==nil)
  6830. Price=@"No Price.";
  6831. else
  6832. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6833. }
  6834. else
  6835. {
  6836. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6837. }
  6838. double discount = sqlite3_column_double(stmt, 2);
  6839. int item_count = sqlite3_column_int(stmt, 3);
  6840. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6841. NSString *nsline_note=nil;
  6842. if(line_note!=nil)
  6843. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6844. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6845. // NSString *nsname = nil;
  6846. // if(name!=nil)
  6847. // nsname= [[NSString alloc]initWithUTF8String:name];
  6848. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6849. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6850. // NSString *nsdescription=nil;
  6851. // if(description!=nil)
  6852. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6853. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6854. // int stockUom = sqlite3_column_int(stmt, 8);
  6855. // int _id = sqlite3_column_int(stmt, 9);
  6856. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6857. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6858. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6859. int carton=[bsubtotaljson[@"carton"] intValue];
  6860. TotalCuft += cuft;
  6861. TotalWeight += weight;
  6862. TotalCarton += carton;
  6863. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6864. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6865. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6866. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6867. itemjson[@"note"]=nsline_note;
  6868. itemjson[@"origin_price"] = Price;
  6869. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6870. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6871. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6872. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6873. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6874. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6875. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6876. if(itemjson[@"combine"] != nil)
  6877. {
  6878. // int citem=0;
  6879. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6880. for(int bc=0;bc<bcount;bc++)
  6881. {
  6882. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6883. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6884. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6885. subTotal += uprice * modulus * item_count;
  6886. }
  6887. }
  6888. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6889. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6890. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6891. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6892. itemjson[@"type"] = @"order_item";
  6893. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6894. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6895. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6896. allItemPrice += subTotal;
  6897. }];
  6898. section_3[@"data"] = model_data;
  6899. section_3[@"type"] = @"sub_order";
  6900. section_3[@"title"] = @"Models";
  6901. section_3[@"switch"] = @(false);
  6902. ret[@"section_3"] = section_3;
  6903. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6904. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6905. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6906. // payments/Credits
  6907. // payments_and_credist = sqlite3_column_double(statement, 34);
  6908. payments_and_credist = allItemPrice;
  6909. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6910. NSDictionary *sub_total_item = @{
  6911. @"title":@"Sub-Total",
  6912. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6913. };
  6914. [price_data setObject:sub_total_item forKey:@"item_0"];
  6915. // // total
  6916. // totalPrice = sqlite3_column_double(statement, 35);
  6917. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6918. } else {
  6919. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6920. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6921. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6922. // payments/Credits
  6923. payments_and_credist = 0;
  6924. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6925. NSDictionary *sub_total_item = @{
  6926. @"title":@"Sub-Total",
  6927. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6928. };
  6929. [price_data setObject:sub_total_item forKey:@"item_0"];
  6930. }
  6931. // total
  6932. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6933. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6934. NSDictionary *total_item = @{
  6935. @"title":@"Total",
  6936. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6937. };
  6938. [price_data setObject:total_item forKey:@"item_4"];
  6939. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6940. section_4 = @{
  6941. @"data":price_data,
  6942. @"title":@"Price Info",
  6943. @"type":@"price_info"
  6944. }.mutableCopy;
  6945. ret[@"section_4"] = section_4;
  6946. // ret[@"order_info"]= orderinfo;
  6947. section_0 = @{
  6948. @"data":orderinfo,
  6949. @"title":@"Order Info",
  6950. @"type":@"order_info"
  6951. }.mutableCopy;
  6952. ret[@"section_0"] = section_0;
  6953. // ret[@"more_order_info"] = moreInfo;
  6954. section_2 = @{
  6955. @"data":moreInfo,
  6956. @"title":@"More Info",
  6957. @"type":@"more_order_info"
  6958. }.mutableCopy;
  6959. ret[@"section_2"] = section_2;
  6960. ret[@"count"] = @(5);
  6961. return [RAConvertor dict2data:ret];
  6962. }
  6963. #pragma mark order list
  6964. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6965. NSString *nsstatus = @"";
  6966. switch (status) {
  6967. case 0:
  6968. {
  6969. nsstatus=@"Temp Order";
  6970. break;
  6971. }
  6972. case 1:
  6973. {
  6974. nsstatus=@"Saved Order";
  6975. break;
  6976. }
  6977. case 2: {
  6978. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6979. break;
  6980. }
  6981. case 3:
  6982. case 15:
  6983. {
  6984. nsstatus=@"Cancelled";
  6985. break;
  6986. }
  6987. case 10:
  6988. {
  6989. nsstatus=@"Quote Submitted";
  6990. break;
  6991. }
  6992. case 11:
  6993. {
  6994. nsstatus=@"Sales Order Submitted";
  6995. break;
  6996. }
  6997. case 12:
  6998. {
  6999. nsstatus=@"Processing";
  7000. break;
  7001. }
  7002. case 13:
  7003. {
  7004. nsstatus=@"Shipped";
  7005. break;
  7006. }
  7007. case 14:
  7008. {
  7009. nsstatus=@"Closed";
  7010. break;
  7011. }
  7012. case -11:
  7013. {
  7014. nsstatus = @"Ready For Submit";
  7015. break;
  7016. }
  7017. default:
  7018. break;
  7019. }
  7020. return nsstatus;
  7021. }
  7022. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7023. {
  7024. double total = 0;
  7025. __block double payments_and_credist = 0;
  7026. __block double allItemPrice = 0;
  7027. // sqlite3 *db1 = [iSalesDB get_db];
  7028. if (so_id) {
  7029. // 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];
  7030. 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];
  7031. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7032. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7033. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7034. int product_id = sqlite3_column_int(stmt, 0);
  7035. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7036. int discount = sqlite3_column_int(stmt, 2);
  7037. int item_count = sqlite3_column_int(stmt, 3);
  7038. // int item_id = sqlite3_column_int(stmt, 7);
  7039. int item_id = sqlite3_column_int(stmt, 4);
  7040. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7041. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7042. NSString* Price=nil;
  7043. if(str_price==nil)
  7044. {
  7045. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7046. if(price==nil)
  7047. Price=@"No Price.";
  7048. else
  7049. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7050. }
  7051. else
  7052. {
  7053. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7054. }
  7055. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7056. itemjson[@"The unit price"]=Price;
  7057. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7058. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7059. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7060. if(itemjson[@"combine"] != nil)
  7061. {
  7062. // int citem=0;
  7063. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7064. for(int bc=0;bc<bcount;bc++)
  7065. {
  7066. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7067. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7068. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7069. subTotal += uprice * modulus * item_count;
  7070. }
  7071. }
  7072. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7073. allItemPrice += subTotal;
  7074. }];
  7075. payments_and_credist = allItemPrice;
  7076. } else {
  7077. // payments/Credits
  7078. payments_and_credist = 0;
  7079. }
  7080. // lift_gate handlingFee shippingFee
  7081. __block double lift_gate = 0;
  7082. __block double handlingFee = 0;
  7083. __block double shippingFee = 0;
  7084. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7085. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7086. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7087. if (have_lift_gate) {
  7088. lift_gate = sqlite3_column_double(stmt, 1);
  7089. }
  7090. handlingFee = sqlite3_column_double(stmt, 2);
  7091. shippingFee = sqlite3_column_double(stmt, 3);
  7092. }];
  7093. // total
  7094. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7095. if (close) {
  7096. [iSalesDB close_db:db1];
  7097. }
  7098. return total;
  7099. }
  7100. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7101. {
  7102. assert(params[@"user"]!=nil);
  7103. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7104. int limit = [[params valueForKey:@"limit"] intValue];
  7105. int offset = [[params valueForKey:@"offset"] intValue];
  7106. NSString* keyword = [params valueForKey:@"keyWord"];
  7107. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7108. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7109. NSString* where=@"1 = 1";
  7110. if(keyword.length>0)
  7111. 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]];
  7112. if (orderStatus.length > 0) {
  7113. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7114. if (order_status_array.count == 1) {
  7115. int status_value = [[order_status_array firstObject] intValue];
  7116. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7117. if (status_value == 15 || status_value == 3) {
  7118. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7119. } else {
  7120. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7121. }
  7122. } else {
  7123. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7124. }
  7125. } else if (order_status_array.count > 1) {
  7126. for (int i = 0; i < order_status_array.count;i++) {
  7127. NSString *status = order_status_array[i];
  7128. NSString *condition = @" or";
  7129. if (i == 0) {
  7130. condition = @" and (";
  7131. }
  7132. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7133. int status_value = [status intValue];
  7134. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7135. if (status_value == 15 || status_value == 3) {
  7136. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7137. } else {
  7138. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7139. }
  7140. } else {
  7141. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7142. }
  7143. }
  7144. where = [where stringByAppendingString:@" )"];
  7145. }
  7146. }
  7147. 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];
  7148. // DebugLog(@"order list sql: %@",sqlQuery);
  7149. sqlite3 *db = [iSalesDB get_db];
  7150. sqlite3_stmt * statement;
  7151. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7152. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7153. {
  7154. int count=0;
  7155. while (sqlite3_step(statement) == SQLITE_ROW)
  7156. {
  7157. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7158. int order_id = sqlite3_column_double(statement, 0);
  7159. char *soid = (char*)sqlite3_column_text(statement, 1);
  7160. if(soid==nil)
  7161. soid= "";
  7162. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7163. int status = sqlite3_column_double(statement, 2);
  7164. int erpStatus = sqlite3_column_double(statement, 9);
  7165. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7166. if(sales_rep==nil)
  7167. sales_rep= "";
  7168. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7169. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7170. if(create_by==nil)
  7171. create_by= "";
  7172. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7173. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7174. if(company_name==nil)
  7175. company_name= "";
  7176. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7177. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7178. if(create_time==nil)
  7179. create_time= "";
  7180. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7181. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7182. // double total_price = sqlite3_column_double(statement, 7);
  7183. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7184. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7185. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7186. int offline_edit = sqlite3_column_int(statement, 10);
  7187. // ": "JH",
  7188. // "": "$8307.00",
  7189. // "": "MOB1608050001",
  7190. // "": "ArpithaT",
  7191. // "": "1st Stage Property Transformations",
  7192. // "": "JANICE SUTTON",
  7193. // "": 2255,
  7194. // "": "08/02/2016 09:49:18",
  7195. // "": 1,
  7196. // "": "Saved Order"
  7197. // "": "1470384050483",
  7198. // "model_count": "6 / 28"
  7199. item[@"sales_rep"]= nssales_rep;
  7200. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7201. item[@"so#"]= nssoid;
  7202. item[@"create_by"]= nscreate_by;
  7203. item[@"customer_name"]= nscompany_name;
  7204. item[@"customer_contact"] = customer_contact;
  7205. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7206. item[@"purchase_time"]= nscreate_time;
  7207. int statusCode = status;
  7208. if (statusCode == 2) {
  7209. statusCode = erpStatus;
  7210. } else if (statusCode == 3) {
  7211. statusCode = 15;
  7212. }
  7213. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7214. item[@"order_status"]= nsstatus;
  7215. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7216. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7217. // item[@"model_count"]
  7218. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7219. count++;
  7220. }
  7221. ret[@"count"]= [NSNumber numberWithInt:count];
  7222. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7223. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7224. ret[@"result"]= [NSNumber numberWithInt:2];
  7225. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7226. ret[@"time_zone"] = @"PST";
  7227. sqlite3_finalize(statement);
  7228. }
  7229. 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];
  7230. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7231. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7232. [iSalesDB close_db:db];
  7233. return [RAConvertor dict2data:ret];
  7234. }
  7235. #pragma mark update gnotes
  7236. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7237. {
  7238. DebugLog(@"params: %@",params);
  7239. // comments = Meyoyoyoyoyoyoy;
  7240. // orderCode = MOB1608110001;
  7241. // password = 123456;
  7242. // user = EvanK;
  7243. 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]];
  7244. int ret = [iSalesDB execSql:sql];
  7245. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7246. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7247. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7248. // [dic setValue:@"160409" forKey:@"min_ver"];
  7249. return [RAConvertor dict2data:dic];
  7250. }
  7251. #pragma mark move to wishlist
  7252. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7253. {
  7254. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7255. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7256. _id = [NSString stringWithFormat:@"(%@)",_id];
  7257. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7258. sqlite3 *db = [iSalesDB get_db];
  7259. __block NSString *product_id = @"";
  7260. __block NSString *item_count_str = @"";
  7261. // __block NSString *item_id = nil;
  7262. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7263. // product_id = [self textAtColumn:0 statement:stmt];
  7264. int item_count = sqlite3_column_int(stmt, 1);
  7265. // item_id = [self textAtColumn:2 statement:stmt];
  7266. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7267. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7268. if (p_id.length) {
  7269. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7270. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7271. }
  7272. }];
  7273. [iSalesDB close_db:db];
  7274. // 去除第一个,
  7275. if (product_id.length > 1) {
  7276. product_id = [product_id substringFromIndex:1];
  7277. }
  7278. if (item_count_str.length > 1) {
  7279. item_count_str = [item_count_str substringFromIndex:1];
  7280. }
  7281. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7282. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7283. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7284. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7285. sqlite3 *db1 = [iSalesDB get_db];
  7286. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7287. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7288. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7289. // 删除
  7290. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7291. int ret = [iSalesDB execSql:deleteSql db:db1];
  7292. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7293. [iSalesDB close_db:db1];
  7294. return [RAConvertor dict2data:dic];
  7295. }
  7296. #pragma mark cart delete
  7297. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7298. {
  7299. // cartItemId = 548;
  7300. // orderCode = MOB1608110001;
  7301. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7302. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7303. _id = [NSString stringWithFormat:@"(%@)",_id];
  7304. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7305. int ret = [iSalesDB execSql:sql];
  7306. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7307. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7308. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7309. // [dic setValue:@"160409" forKey:@"min_ver"];
  7310. return [RAConvertor dict2data:dic];
  7311. }
  7312. #pragma mark set price
  7313. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7314. {
  7315. DebugLog(@"cart set price params: %@",params);
  7316. // "cartitem_id" = 1;
  7317. // discount = "0.000000";
  7318. // "item_note" = "";
  7319. // price = "269.000000";
  7320. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7321. NSString *notes = [self valueInParams:params key:@"item_note"];
  7322. NSString *discount = [self valueInParams:params key:@"discount"];
  7323. NSString *price = [self valueInParams:params key:@"price"];
  7324. // bool badd_price_changed=false;
  7325. // sqlite3* db=[iSalesDB get_db];
  7326. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7327. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7328. // NSRange range;
  7329. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7330. //
  7331. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7332. // badd_price_changed=true;
  7333. // [iSalesDB close_db:db];
  7334. //
  7335. // if(badd_price_changed)
  7336. // {
  7337. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7338. // }
  7339. //
  7340. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7341. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7342. int ret = [iSalesDB execSql:sql];
  7343. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7344. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7345. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7346. // [dic setValue:@"160409" forKey:@"min_ver"];
  7347. return [RAConvertor dict2data:dic];
  7348. }
  7349. #pragma mark set line notes
  7350. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7351. {
  7352. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7353. NSString *notes = [self valueInParams:params key:@"notes"];
  7354. notes = [self translateSingleQuote:notes];
  7355. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7356. int ret = [iSalesDB execSql:sql];
  7357. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7358. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7359. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7360. // [dic setValue:@"160409" forKey:@"min_ver"];
  7361. return [RAConvertor dict2data:dic];
  7362. }
  7363. #pragma mark set qty
  7364. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7365. {
  7366. assert(params[@"can_create_backorder"]!=nil);
  7367. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7368. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7369. int item_count = [params[@"inputInt"] intValue];
  7370. // 购买检查数量大于库存
  7371. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7372. if (!params[@"can_create_backorder"]) {
  7373. 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];
  7374. __block BOOL out_of_stock = NO;
  7375. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7376. int availability = sqlite3_column_int(stmt, 0);
  7377. if (availability < item_count) {
  7378. out_of_stock = YES;
  7379. }
  7380. }];
  7381. if (out_of_stock) { // 缺货
  7382. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7383. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7384. return [RAConvertor dict2data:dic];
  7385. }
  7386. }
  7387. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7388. int ret = [iSalesDB execSql:sql];
  7389. __block int item_id = 0;
  7390. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7391. item_id = sqlite3_column_int(stmt, 0);
  7392. }];
  7393. sqlite3 *db = [iSalesDB get_db];
  7394. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7395. [iSalesDB close_db:db];
  7396. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7397. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7398. // [dic setValue:@"160409" forKey:@"min_ver"];
  7399. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7400. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7401. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7402. return [RAConvertor dict2data:dic];
  7403. }
  7404. #pragma mark place order
  7405. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7406. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7407. if (aname) {
  7408. [dic setValue:aname forKey:@"aname"];
  7409. }
  7410. if (control) {
  7411. [dic setValue:control forKey:@"control"];
  7412. }
  7413. if (keyboard) {
  7414. [dic setValue:keyboard forKey:@"keyboard"];
  7415. }
  7416. if (name) {
  7417. [dic setValue:name forKey:@"name"];
  7418. }
  7419. if (value) {
  7420. [dic setValue:value forKey:@"value"];
  7421. }
  7422. return dic;
  7423. }
  7424. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7425. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7426. orderCode = [self translateSingleQuote:orderCode];
  7427. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7428. 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];
  7429. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7430. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7431. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7432. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7433. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7434. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7435. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7436. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7437. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7438. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7439. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7440. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7441. NSString *email = [self textAtColumn:11 statement:stmt];
  7442. NSString *phone = [self textAtColumn:12 statement:stmt];
  7443. NSString *fax = [self textAtColumn:13 statement:stmt];
  7444. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7445. NSString *city = [self textAtColumn:15 statement:stmt];
  7446. NSString *state = [self textAtColumn:16 statement:stmt];
  7447. NSString *country = [self textAtColumn:17 statement:stmt];
  7448. NSString *name = [self textAtColumn:18 statement:stmt];
  7449. // contact id
  7450. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7451. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7452. [contact_id_dic setValue:@"text" forKey:@"control"];
  7453. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7454. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7455. [contact_id_dic setValue:@"true" forKey:@"required"];
  7456. [contact_id_dic setValue:contact_id forKey:@"value"];
  7457. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7458. // business card
  7459. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7460. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7461. [business_card_dic setValue:@"img" forKey:@"control"];
  7462. [business_card_dic setValue:@"1" forKey:@"disable"];
  7463. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7464. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7465. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7466. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7467. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7468. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7469. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7470. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7471. // fax
  7472. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7473. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7474. // zipcode
  7475. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7476. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7477. // city
  7478. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7479. [customer_dic setValue:city_dic forKey:@"item_12"];
  7480. // state
  7481. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7482. [customer_dic setValue:state_dic forKey:@"item_13"];
  7483. // country
  7484. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7485. [customer_dic setValue:country_dic forKey:@"item_14"];
  7486. // company name
  7487. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7488. [customer_dic setValue:company_dic forKey:@"item_2"];
  7489. // addr_1
  7490. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7491. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7492. // addr_2
  7493. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7494. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7495. // addr_3
  7496. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7497. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7498. // addr_4
  7499. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7500. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7501. // Contact
  7502. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7503. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7504. // email
  7505. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7506. [customer_dic setValue:email_dic forKey:@"item_8"];
  7507. // phone
  7508. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7509. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7510. // title
  7511. [customer_dic setValue:@"Customer" forKey:@"title"];
  7512. // count
  7513. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7514. }];
  7515. // setting
  7516. NSDictionary *setting = params[@"setting"];
  7517. NSNumber *hide = setting[@"CustomerHide"];
  7518. [customer_dic setValue:hide forKey:@"hide"];
  7519. return customer_dic;
  7520. }
  7521. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7522. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7523. // setting
  7524. NSDictionary *setting = params[@"setting"];
  7525. NSNumber *hide = setting[@"ShipToHide"];
  7526. [dic setValue:hide forKey:@"hide"];
  7527. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7528. orderCode = [self translateSingleQuote:orderCode];
  7529. 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];
  7530. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7531. NSString *cid = [self textAtColumn:0 statement:stmt];
  7532. NSString *name = [self textAtColumn:1 statement:stmt];
  7533. NSString *ext = [self textAtColumn:2 statement:stmt];
  7534. NSString *contact = [self textAtColumn:3 statement:stmt];
  7535. NSString *email = [self textAtColumn:4 statement:stmt];
  7536. NSString *fax = [self textAtColumn:5 statement:stmt];
  7537. NSString *phone = [self textAtColumn:6 statement:stmt];
  7538. // count
  7539. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7540. // title
  7541. [dic setValue:@"Ship To" forKey:@"title"];
  7542. // choose
  7543. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7544. [choose_dic setValue:@"choose" forKey:@"aname"];
  7545. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7546. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7547. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7548. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7549. @"receive_contact" : @"customer_contact",
  7550. @"receive_email" : @"customer_email",
  7551. @"receive_ext" : @"customer_contact_ext",
  7552. @"receive_fax" : @"customer_fax",
  7553. @"receive_name" : @"customer_name",
  7554. @"receive_phone" : @"customer_phone"},
  7555. @"refresh" : [NSNumber numberWithInteger:1],
  7556. @"type" : @"pull"};
  7557. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7558. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7559. @"name" : @"Add new address",
  7560. @"refresh" : [NSNumber numberWithInteger:1],
  7561. @"value" : @"new_addr"
  7562. };
  7563. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7564. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7565. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7566. @"receive_contact" : @"customer_contact",
  7567. @"receive_email" : @"customer_email",
  7568. @"receive_ext" : @"customer_contact_ext",
  7569. @"receive_fax" : @"customer_fax",
  7570. @"receive_name" : @"customer_name",
  7571. @"receive_phone" : @"customer_phone"},
  7572. @"name" : @"select_ship_to",
  7573. @"refresh" : [NSNumber numberWithInteger:1],
  7574. @"value" : @"Sales_Order_Ship_To"};
  7575. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7576. [dic setValue:choose_dic forKey:@"item_0"];
  7577. // contact id
  7578. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7579. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7580. [contact_id_dic setValue:@"true" forKey:@"required"];
  7581. [dic setValue:contact_id_dic forKey:@"item_1"];
  7582. // company name
  7583. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7584. [dic setValue:company_name_dic forKey:@"item_2"];
  7585. // address
  7586. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7587. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7588. if ([required integerValue]) {
  7589. [ext_dic setValue:@"true" forKey:@"required"];
  7590. }
  7591. [dic setValue:ext_dic forKey:@"item_3"];
  7592. // contact
  7593. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7594. [dic setValue:contact_dic forKey:@"item_4"];
  7595. // phone
  7596. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7597. [dic setValue:phone_dic forKey:@"item_5"];
  7598. // fax
  7599. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7600. [dic setValue:fax_dic forKey:@"item_6"];
  7601. // email
  7602. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7603. [dic setValue:email_dic forKey:@"item_7"];
  7604. }];
  7605. return dic;
  7606. }
  7607. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7608. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7609. // setting
  7610. NSDictionary *setting = params[@"setting"];
  7611. NSNumber *hide = setting[@"ShipFromHide"];
  7612. [dic setValue:hide forKey:@"hide"];
  7613. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7614. orderCode = [self translateSingleQuote:orderCode];
  7615. 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];
  7616. 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';";
  7617. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7618. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7619. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7620. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7621. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7622. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7623. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7624. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7625. if (!cid.length) {
  7626. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7627. cid = [self textAtColumn:0 statement:statment];
  7628. name = [self textAtColumn:1 statement:statment];
  7629. ext = [self textAtColumn:2 statement:statment];
  7630. contact = [self textAtColumn:3 statement:statment];
  7631. email = [self textAtColumn:4 statement:statment];
  7632. fax = [self textAtColumn:5 statement:statment];
  7633. phone = [self textAtColumn:6 statement:statment];
  7634. }];
  7635. }
  7636. // count
  7637. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7638. // title
  7639. [dic setValue:@"Ship From" forKey:@"title"];
  7640. // hide
  7641. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7642. // choose
  7643. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7644. [choose_dic setValue:@"choose" forKey:@"aname"];
  7645. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7646. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7647. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7648. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7649. @"sender_contact" : @"customer_contact",
  7650. @"sender_email" : @"customer_email",
  7651. @"sender_ext" : @"customer_contact_ext",
  7652. @"sender_fax" : @"customer_fax",
  7653. @"sender_name" : @"customer_name",
  7654. @"sender_phone" : @"customer_phone"},
  7655. @"name" : @"select_cid",
  7656. @"refresh" : [NSNumber numberWithInteger:0],
  7657. @"value" : @"Sales_Order_Ship_From"};
  7658. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7659. [dic setValue:choose_dic forKey:@"item_0"];
  7660. // contact id
  7661. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7662. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7663. [contact_id_dic setValue:@"true" forKey:@"required"];
  7664. [dic setValue:contact_id_dic forKey:@"item_1"];
  7665. // company name
  7666. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7667. [dic setValue:company_name_dic forKey:@"item_2"];
  7668. // address
  7669. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7670. [dic setValue:ext_dic forKey:@"item_3"];
  7671. // contact
  7672. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7673. [dic setValue:contact_dic forKey:@"item_4"];
  7674. // phone
  7675. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7676. [dic setValue:phone_dic forKey:@"item_5"];
  7677. // fax
  7678. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7679. [dic setValue:fax_dic forKey:@"item_6"];
  7680. // email
  7681. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7682. [dic setValue:email_dic forKey:@"item_7"];
  7683. }];
  7684. return dic;
  7685. }
  7686. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7687. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7688. // setting
  7689. NSDictionary *setting = params[@"setting"];
  7690. NSNumber *hide = setting[@"FreightBillToHide"];
  7691. [dic setValue:hide forKey:@"hide"];
  7692. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7693. orderCode = [self translateSingleQuote:orderCode];
  7694. 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];
  7695. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7696. NSString *cid = [self textAtColumn:0 statement:stmt];
  7697. NSString *name = [self textAtColumn:1 statement:stmt];
  7698. NSString *ext = [self textAtColumn:2 statement:stmt];
  7699. NSString *contact = [self textAtColumn:3 statement:stmt];
  7700. NSString *email = [self textAtColumn:4 statement:stmt];
  7701. NSString *fax = [self textAtColumn:5 statement:stmt];
  7702. NSString *phone = [self textAtColumn:6 statement:stmt];
  7703. // count
  7704. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7705. // title
  7706. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7707. // hide
  7708. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7709. // choose
  7710. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7711. [choose_dic setValue:@"choose" forKey:@"aname"];
  7712. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7713. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7714. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7715. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7716. @"shipping_billto_contact" : @"receive_contact",
  7717. @"shipping_billto_email" : @"receive_email",
  7718. @"shipping_billto_ext" : @"receive_ext",
  7719. @"shipping_billto_fax" : @"receive_fax",
  7720. @"shipping_billto_name" : @"receive_name",
  7721. @"shipping_billto_phone" : @"receive_phone"},
  7722. @"type" : @"pull"};
  7723. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7724. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7725. @"type" : @"pull",
  7726. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7727. @"shipping_billto_contact" : @"customer_contact",
  7728. @"shipping_billto_email" : @"customer_email",
  7729. @"shipping_billto_ext" : @"customer_contact_ext",
  7730. @"shipping_billto_fax" : @"customer_fax",
  7731. @"shipping_billto_name" : @"customer_name",
  7732. @"shipping_billto_phone" : @"customer_phone"}
  7733. };
  7734. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7735. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7736. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7737. @"shipping_billto_contact" : @"sender_contact",
  7738. @"shipping_billto_email" : @"sender_email",
  7739. @"shipping_billto_ext" : @"sender_ext",
  7740. @"shipping_billto_fax" : @"sender_fax",
  7741. @"shipping_billto_name" : @"sender_name",
  7742. @"shipping_billto_phone" : @"sender_phone"},
  7743. @"type" : @"pull"
  7744. };
  7745. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7746. NSDictionary *select_freight_bill_to_dic = @{
  7747. @"aname" : @"Select freight bill to",
  7748. @"name" : @"select_cid",
  7749. @"refresh" : [NSNumber numberWithInteger:0],
  7750. @"value" : @"Sales_Order_Freight_Bill_To",
  7751. @"key_map" : @{
  7752. @"shipping_billto_cid" : @"customer_cid",
  7753. @"shipping_billto_contact" : @"customer_contact",
  7754. @"shipping_billto_email" : @"customer_email",
  7755. @"shipping_billto_ext" : @"customer_contact_ext",
  7756. @"shipping_billto_fax" : @"customer_fax",
  7757. @"shipping_billto_name" : @"customer_name",
  7758. @"shipping_billto_phone" : @"customer_phone"
  7759. }
  7760. };
  7761. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7762. [dic setValue:choose_dic forKey:@"item_0"];
  7763. // contact id
  7764. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7765. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7766. [contact_id_dic setValue:@"true" forKey:@"required"];
  7767. [dic setValue:contact_id_dic forKey:@"item_1"];
  7768. // company name
  7769. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7770. [dic setValue:company_name_dic forKey:@"item_2"];
  7771. // address
  7772. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7773. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7774. if ([ext_required integerValue]) {
  7775. [ext_dic setValue:@"true" forKey:@"required"];
  7776. }
  7777. [dic setValue:ext_dic forKey:@"item_3"];
  7778. // contact
  7779. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7780. [dic setValue:contact_dic forKey:@"item_4"];
  7781. // phone
  7782. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7783. [dic setValue:phone_dic forKey:@"item_5"];
  7784. // fax
  7785. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7786. [dic setValue:fax_dic forKey:@"item_6"];
  7787. // email
  7788. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7789. [dic setValue:email_dic forKey:@"item_7"];
  7790. }];
  7791. return dic;
  7792. }
  7793. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7794. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7795. // setting
  7796. NSDictionary *setting = params[@"setting"];
  7797. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7798. [dic setValue:hide forKey:@"hide"];
  7799. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7800. orderCode = [self translateSingleQuote:orderCode];
  7801. 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];
  7802. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7803. NSString *cid = [self textAtColumn:0 statement:stmt];
  7804. NSString *name = [self textAtColumn:1 statement:stmt];
  7805. NSString *ext = [self textAtColumn:2 statement:stmt];
  7806. NSString *contact = [self textAtColumn:3 statement:stmt];
  7807. NSString *email = [self textAtColumn:4 statement:stmt];
  7808. NSString *fax = [self textAtColumn:5 statement:stmt];
  7809. NSString *phone = [self textAtColumn:6 statement:stmt];
  7810. // count
  7811. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7812. // title
  7813. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7814. // hide
  7815. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7816. // choose
  7817. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7818. [choose_dic setValue:@"choose" forKey:@"aname"];
  7819. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7820. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7821. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7822. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7823. @"billing_contact" : @"receive_contact",
  7824. @"billing_email" : @"receive_email",
  7825. @"billing_ext" : @"receive_ext",
  7826. @"billing_fax" : @"receive_fax",
  7827. @"billing_name" : @"receive_name",
  7828. @"billing_phone" : @"receive_phone"},
  7829. @"type" : @"pull"};
  7830. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7831. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7832. @"type" : @"pull",
  7833. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7834. @"billing_contact" : @"customer_contact",
  7835. @"billing_email" : @"customer_email",
  7836. @"billing_ext" : @"customer_contact_ext",
  7837. @"billing_fax" : @"customer_fax",
  7838. @"billing_name" : @"customer_name",
  7839. @"billing_phone" : @"customer_phone"}
  7840. };
  7841. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7842. NSDictionary *select_bill_to_dic = @{
  7843. @"aname" : @"Select bill to",
  7844. @"name" : @"select_cid",
  7845. @"refresh" : [NSNumber numberWithInteger:0],
  7846. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7847. @"key_map" : @{
  7848. @"billing_cid" : @"customer_cid",
  7849. @"billing_contact" : @"customer_contact",
  7850. @"billing_email" : @"customer_email",
  7851. @"billing_ext" : @"customer_contact_ext",
  7852. @"billing_fax" : @"customer_fax",
  7853. @"billing_name" : @"customer_name",
  7854. @"billing_phone" : @"customer_phone"
  7855. }
  7856. };
  7857. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7858. [dic setValue:choose_dic forKey:@"item_0"];
  7859. // contact id
  7860. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7861. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7862. [contact_id_dic setValue:@"true" forKey:@"required"];
  7863. [dic setValue:contact_id_dic forKey:@"item_1"];
  7864. // company name
  7865. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7866. [dic setValue:company_name_dic forKey:@"item_2"];
  7867. // address
  7868. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7869. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7870. if ([ext_required integerValue]) {
  7871. [ext_dic setValue:@"true" forKey:@"required"];
  7872. }
  7873. [dic setValue:ext_dic forKey:@"item_3"];
  7874. // contact
  7875. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7876. [dic setValue:contact_dic forKey:@"item_4"];
  7877. // phone
  7878. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7879. [dic setValue:phone_dic forKey:@"item_5"];
  7880. // fax
  7881. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7882. [dic setValue:fax_dic forKey:@"item_6"];
  7883. // email
  7884. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7885. [dic setValue:email_dic forKey:@"item_7"];
  7886. }];
  7887. return dic;
  7888. }
  7889. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7890. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7891. // setting
  7892. NSDictionary *setting = params[@"setting"];
  7893. NSNumber *hide = setting[@"ReturnToHide"];
  7894. [dic setValue:hide forKey:@"hide"];
  7895. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7896. orderCode = [self translateSingleQuote:orderCode];
  7897. 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];
  7898. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7899. NSString *cid = [self textAtColumn:0 statement:stmt];
  7900. NSString *name = [self textAtColumn:1 statement:stmt];
  7901. NSString *ext = [self textAtColumn:2 statement:stmt];
  7902. NSString *contact = [self textAtColumn:3 statement:stmt];
  7903. NSString *email = [self textAtColumn:4 statement:stmt];
  7904. NSString *fax = [self textAtColumn:5 statement:stmt];
  7905. NSString *phone = [self textAtColumn:6 statement:stmt];
  7906. // count
  7907. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7908. // title
  7909. [dic setValue:@"Return To" forKey:@"title"];
  7910. // hide
  7911. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7912. // choose
  7913. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7914. [choose_dic setValue:@"choose" forKey:@"aname"];
  7915. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7916. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7917. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7918. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7919. @"returnto_contact" : @"sender_contact",
  7920. @"returnto_email" : @"sender_email",
  7921. @"returnto_ext" : @"sender_ext",
  7922. @"returnto_fax" : @"sender_fax",
  7923. @"returnto_name" : @"sender_name",
  7924. @"returnto_phone" : @"sender_phone"},
  7925. @"type" : @"pull"};
  7926. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7927. NSDictionary *select_return_to_dic = @{
  7928. @"aname" : @"Select return to",
  7929. @"name" : @"select_cid",
  7930. @"refresh" : [NSNumber numberWithInteger:0],
  7931. @"value" : @"Contact_Return_To",
  7932. @"key_map" : @{
  7933. @"returnto_cid" : @"customer_cid",
  7934. @"returnto_contact" : @"customer_contact",
  7935. @"returnto_email" : @"customer_email",
  7936. @"returnto_ext" : @"customer_contact_ext",
  7937. @"returnto_fax" : @"customer_fax",
  7938. @"returnto_name" : @"customer_name",
  7939. @"returnto_phone" : @"customer_phone"
  7940. }
  7941. };
  7942. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7943. [dic setValue:choose_dic forKey:@"item_0"];
  7944. // contact id
  7945. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7946. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7947. [contact_id_dic setValue:@"true" forKey:@"required"];
  7948. [dic setValue:contact_id_dic forKey:@"item_1"];
  7949. // company name
  7950. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7951. [dic setValue:company_name_dic forKey:@"item_2"];
  7952. // address
  7953. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7954. [dic setValue:ext_dic forKey:@"item_3"];
  7955. // contact
  7956. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7957. [dic setValue:contact_dic forKey:@"item_4"];
  7958. // phone
  7959. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7960. [dic setValue:phone_dic forKey:@"item_5"];
  7961. // fax
  7962. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7963. [dic setValue:fax_dic forKey:@"item_6"];
  7964. // email
  7965. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7966. [dic setValue:email_dic forKey:@"item_7"];
  7967. }];
  7968. return dic;
  7969. }
  7970. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  7971. {
  7972. assert(params[@"user"]!=nil);
  7973. assert(params[@"contact_id"]!=nil);
  7974. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7975. orderCode = [self translateSingleQuote:orderCode];
  7976. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7977. __block double TotalCuft = 0;
  7978. __block double TotalWeight = 0;
  7979. __block int TotalCarton = 0;
  7980. __block double payments = 0;
  7981. // setting
  7982. NSDictionary *setting = params[@"setting"];
  7983. NSNumber *hide = setting[@"ModelInformationHide"];
  7984. [dic setValue:hide forKey:@"hide"];
  7985. 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];
  7986. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7987. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7988. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7989. // item id
  7990. int item_id = sqlite3_column_int(stmt, 0);
  7991. // count
  7992. int item_count = sqlite3_column_int(stmt, 1);
  7993. // stockUom
  7994. int stockUom = sqlite3_column_int(stmt, 2);
  7995. // unit price
  7996. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7997. NSString* Price=nil;
  7998. if([str_price isEqualToString:@""])
  7999. {
  8000. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8001. if(price==nil)
  8002. Price=@"No Price.";
  8003. else
  8004. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8005. }
  8006. else
  8007. {
  8008. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8009. }
  8010. // discount
  8011. double discount = sqlite3_column_double(stmt, 4);
  8012. // name
  8013. NSString *name = [self textAtColumn:5 statement:stmt];
  8014. // description
  8015. NSString *description = [self textAtColumn:6 statement:stmt];
  8016. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8017. // line note
  8018. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8019. int avaulability = sqlite3_column_int(stmt, 8);
  8020. // img
  8021. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8022. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8023. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8024. if(combine != nil)
  8025. {
  8026. // int citem=0;
  8027. int bcount=[[combine valueForKey:@"count"] intValue];
  8028. for(int bc=0;bc<bcount;bc++)
  8029. {
  8030. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8031. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8032. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8033. subTotal += uprice * modulus * item_count;
  8034. }
  8035. }
  8036. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8037. [model_dic setValue:@"model" forKey:@"control"];
  8038. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8039. [model_dic setValue:description forKey:@"description"];
  8040. [model_dic setValue:line_note forKey:@"note"];
  8041. [model_dic setValue:img forKey:@"img_url"];
  8042. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8043. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8044. [model_dic setValue:Price forKey:@"unit_price"];
  8045. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8046. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8047. if (combine) {
  8048. [model_dic setValue:combine forKey:@"combine"];
  8049. }
  8050. // well,what under the row is the info for total
  8051. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8052. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8053. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8054. int carton=[bsubtotaljson[@"carton"] intValue];
  8055. TotalCuft += cuft;
  8056. TotalWeight += weight;
  8057. TotalCarton += carton;
  8058. payments += subTotal;
  8059. //---------------------------
  8060. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8061. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8062. }];
  8063. [dic setValue:@"Model Information" forKey:@"title"];
  8064. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8065. return dic;
  8066. }
  8067. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8068. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8069. [dic setValue:@"Remarks Content" forKey:@"title"];
  8070. // setting
  8071. NSDictionary *setting = params[@"setting"];
  8072. NSNumber *hide = setting[@"RemarksContentHide"];
  8073. [dic setValue:hide forKey:@"hide"];
  8074. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8075. orderCode = [self translateSingleQuote:orderCode];
  8076. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  8077. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8078. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8079. int mustCall = sqlite3_column_int(stmt, 1);
  8080. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8081. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8082. NSDictionary *po_dic = @{
  8083. @"aname" : @"PO#",
  8084. @"control" : @"edit",
  8085. @"keyboard" : @"text",
  8086. @"name" : @"poNumber",
  8087. @"value" : poNumber
  8088. };
  8089. NSDictionary *must_call_dic = @{
  8090. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8091. @"control" : @"switch",
  8092. @"name" : @"must_call",
  8093. @"value" : mustCall ? @"true" : @"false"
  8094. };
  8095. NSDictionary *general_notes_dic = @{
  8096. @"aname" : @"General notes",
  8097. @"control" : @"text_view",
  8098. @"keyboard" : @"text",
  8099. @"name" : @"comments",
  8100. @"value" : generalNotes
  8101. };
  8102. // NSDictionary *internal_notes_dic = @{
  8103. // @"aname" : @"Internal notes",
  8104. // @"control" : @"text_view",
  8105. // @"keyboard" : @"text",
  8106. // @"name" : @"internal_notes",
  8107. // @"value" : internalNotes
  8108. // };
  8109. [dic setValue:po_dic forKey:@"item_0"];
  8110. [dic setValue:must_call_dic forKey:@"item_1"];
  8111. [dic setValue:general_notes_dic forKey:@"item_2"];
  8112. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8113. }];
  8114. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8115. return dic;
  8116. }
  8117. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8118. // params
  8119. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8120. orderCode = [self translateSingleQuote:orderCode];
  8121. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8122. // setting
  8123. NSDictionary *setting = params[@"setting"];
  8124. NSNumber *hide = setting[@"OrderTotalHide"];
  8125. [dic setValue:hide forKey:@"hide"];
  8126. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8127. __block double lift_gate_value = 0;
  8128. __block double handling_fee = 0;
  8129. __block double shipping = 0;
  8130. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8131. int lift_gate = sqlite3_column_int(stmt, 0);
  8132. lift_gate_value = sqlite3_column_double(stmt, 1);
  8133. shipping = sqlite3_column_double(stmt, 2);
  8134. handling_fee = sqlite3_column_double(stmt, 3);
  8135. if (!lift_gate) {
  8136. lift_gate_value = 0;
  8137. }
  8138. }];
  8139. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8140. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8141. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8142. double payments = [[total valueForKey:@"payments"] doubleValue];
  8143. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8144. double totalPrice = payments;
  8145. [dic setValue:@"Order Total" forKey:@"title"];
  8146. NSDictionary *payments_dic = @{
  8147. @"align" : @"right",
  8148. @"aname" : @"Payments/Credits",
  8149. @"control" : @"text",
  8150. @"name" : @"paymentsAndCredits",
  8151. @"type" : @"price",
  8152. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8153. };
  8154. [dic setValue:payments_dic forKey:@"item_0"];
  8155. // version 1.71 remove
  8156. // NSDictionary *handling_fee_dic = @{
  8157. // @"align" : @"right",
  8158. // @"aname" : @"Handling Fee",
  8159. // @"control" : @"text",
  8160. // @"name" : @"handling_fee_value",
  8161. // @"required" : @"true",
  8162. // @"type" : @"price",
  8163. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8164. // };
  8165. NSDictionary *shipping_dic = @{
  8166. @"align" : @"right",
  8167. @"aname" : @"Shipping*",
  8168. @"control" : @"text",
  8169. @"name" : @"shipping",
  8170. @"required" : @"true",
  8171. @"type" : @"price",
  8172. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8173. };
  8174. NSDictionary *lift_gate_dic = @{
  8175. @"align" : @"right",
  8176. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8177. @"control" : @"text",
  8178. @"name" : @"lift_gate_value",
  8179. @"required" : @"true",
  8180. @"type" : @"price",
  8181. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8182. };
  8183. int item_count = 1;
  8184. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8185. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8186. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8187. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8188. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8189. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8190. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8191. } else {
  8192. }
  8193. }
  8194. // version 1.71 remove
  8195. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8196. NSDictionary *total_price_dic = @{
  8197. @"align" : @"right",
  8198. @"aname" : @"Total",
  8199. @"control" : @"text",
  8200. @"name" : @"totalPrice",
  8201. @"type" : @"price",
  8202. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8203. };
  8204. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8205. NSDictionary *total_cuft_dic = @{
  8206. @"align" : @"right",
  8207. @"aname" : @"Total Cuft",
  8208. @"control" : @"text",
  8209. @"name" : @"",
  8210. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8211. };
  8212. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8213. NSDictionary *total_weight_dic = @{
  8214. @"align" : @"right",
  8215. @"aname" : @"Total Weight",
  8216. @"control" : @"text",
  8217. @"name" : @"",
  8218. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8219. };
  8220. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8221. NSDictionary *total_carton_dic = @{
  8222. @"align" : @"right",
  8223. @"aname" : @"Total Carton",
  8224. @"control" : @"text",
  8225. @"name" : @"",
  8226. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8227. };
  8228. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8229. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8230. return dic;
  8231. }
  8232. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8233. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8234. orderCode = [self translateSingleQuote:orderCode];
  8235. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8236. // setting
  8237. NSDictionary *setting = params[@"setting"];
  8238. NSNumber *hide = setting[@"SignatureHide"];
  8239. [dic setValue:hide forKey:@"hide"];
  8240. [dic setValue:@"Signature" forKey:@"title"];
  8241. __block NSString *pic_path = @"";
  8242. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8243. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8244. pic_path = [self textAtColumn:0 statement:stmt];
  8245. }];
  8246. NSDictionary *pic_dic = @{
  8247. @"aname" : @"Signature",
  8248. @"avalue" :pic_path,
  8249. @"control" : @"signature",
  8250. @"name" : @"sign_picpath",
  8251. @"value" : pic_path
  8252. };
  8253. [dic setValue:pic_dic forKey:@"item_0"];
  8254. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8255. return dic;
  8256. }
  8257. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8258. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8259. // setting
  8260. NSDictionary *setting = params[@"setting"];
  8261. NSNumber *hide = setting[@"ShippingMethodHide"];
  8262. [dic setValue:hide forKey:@"hide"];
  8263. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8264. orderCode = [self translateSingleQuote:orderCode];
  8265. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8266. __block NSString *logist = @"";
  8267. __block NSString *lift_gate = @"";
  8268. __block int lift_gate_integer = 0;
  8269. __block NSString *logistic_note = @"";
  8270. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8271. logist = [self textAtColumn:0 statement:stmt];
  8272. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8273. logistic_note = [self textAtColumn:2 statement:stmt];
  8274. }];
  8275. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8276. [dic setValue:@"Shipping Method" forKey:@"title"];
  8277. // val_0
  8278. int PERSONAL_PICK_UP_check = 0;
  8279. int USE_MY_CARRIER_check = 0;
  8280. NSString *logistic_note_text = @"";
  8281. int will_call_check = 0;
  8282. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8283. will_call_check = 1;
  8284. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8285. PERSONAL_PICK_UP_check = 1;
  8286. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8287. USE_MY_CARRIER_check = 1;
  8288. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8289. logistic_note = [logistic_note_array firstObject];
  8290. if (logistic_note_array.count > 1) {
  8291. logistic_note_text = [logistic_note_array lastObject];
  8292. }
  8293. }
  8294. }
  8295. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8296. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8297. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8298. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8299. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8300. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8301. NSDictionary *val0_subItem_item0 = @{
  8302. @"aname" : @"Option",
  8303. @"cadedate" : @{
  8304. @"count" : [NSNumber numberWithInteger:2],
  8305. @"val_0" : @{
  8306. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8307. @"refresh" : [NSNumber numberWithInteger:0],
  8308. @"value" : @"PERSONAL PICK UP",
  8309. @"value_id" : @"PERSONAL PICK UP"
  8310. },
  8311. @"val_1" : @{
  8312. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8313. @"refresh" : [NSNumber numberWithInteger:0],
  8314. @"sub_item" : @{
  8315. @"count" : [NSNumber numberWithInteger:1],
  8316. @"item_0" : @{
  8317. @"aname" : @"BOL",
  8318. @"control" : @"edit",
  8319. @"keyboard" : @"text",
  8320. @"name" : @"logist_note_text",
  8321. @"refresh" : [NSNumber numberWithInteger:0],
  8322. @"required" : @"false",
  8323. @"value" : logistic_note_text
  8324. }
  8325. },
  8326. @"value" : @"USE MY CARRIER",
  8327. @"value_id" : @"USE MY CARRIER"
  8328. }
  8329. },
  8330. @"control" : @"enum",
  8331. @"name" : @"logistic_note",
  8332. @"required" : @"true",
  8333. @"single_select" : @"true"
  8334. };
  8335. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8336. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8337. // val_1
  8338. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8339. if([lift_gate isEqualToString:@""]) {
  8340. if (lift_gate_integer == 1) {
  8341. lift_gate = @"true";
  8342. } else {
  8343. lift_gate = @"false";
  8344. }
  8345. }
  8346. int common_carrier_check = 0;
  8347. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8348. common_carrier_check = 1;
  8349. [params setValue:lift_gate forKey:@"lift_gate"];
  8350. }
  8351. NSDictionary *val_1 = @{
  8352. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8353. @"sub_item" : @{
  8354. @"count" : [NSNumber numberWithInteger:1],
  8355. @"item_0" : @{
  8356. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8357. @"control" : @"switch",
  8358. @"name" : @"lift_gate",
  8359. @"refresh" : [NSNumber numberWithInteger:1],
  8360. @"required" : @"true",
  8361. @"value" : lift_gate
  8362. }
  8363. },
  8364. @"value" : @"COMMON CARRIER",
  8365. @"value_id" : @"COMMON CARRIER"
  8366. };
  8367. // cadedate
  8368. NSDictionary *cadedate = @{
  8369. @"count" : [NSNumber numberWithInteger:2],
  8370. @"val_0" : val_0,
  8371. @"val_1" : val_1
  8372. };
  8373. // item_0
  8374. NSMutableDictionary *item_0 = @{
  8375. @"aname" : @"Shipping",
  8376. @"cadedate" : cadedate,
  8377. @"control" : @"enum",
  8378. @"name" : @"logist",
  8379. @"refresh" : [NSNumber numberWithInteger:1],
  8380. @"single_select" : @"true",
  8381. }.mutableCopy;
  8382. NSNumber *required = setting[@"ShippingMethodRequire"];
  8383. if ([required integerValue]) {
  8384. [item_0 setValue:@"true" forKey:@"required"];
  8385. }
  8386. [dic setValue:item_0 forKey:@"item_0"];
  8387. if ([logist isEqualToString:@"WILL CALL"]) {
  8388. [dic removeObjectForKey:@"lift_gate"];
  8389. }
  8390. return dic;
  8391. }
  8392. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8393. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8394. orderCode = [self translateSingleQuote:orderCode];
  8395. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8396. __block int submit_as_integer = 0;
  8397. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8398. submit_as_integer = sqlite3_column_int(stmt, 0);
  8399. }];
  8400. int check11 = 0;
  8401. int check10 = 0;
  8402. if (submit_as_integer == 11) {
  8403. check11 = 1;
  8404. }
  8405. if (submit_as_integer == 10) {
  8406. check10 = 1;
  8407. }
  8408. // section_0
  8409. NSMutableDictionary *dic = @{
  8410. @"count" : @(1),
  8411. @"item_0" : @{
  8412. @"aname" : @"Submit Order As",
  8413. @"cadedate" : @{
  8414. @"count" : @(2),
  8415. @"val_0" : @{
  8416. @"check" : [NSNumber numberWithInteger:check11],
  8417. @"value" : @"Sales Order",
  8418. @"value_id" : @(11)
  8419. },
  8420. @"val_1" : @{
  8421. @"check" : [NSNumber numberWithInteger:check10],
  8422. @"value" : @"Quote",
  8423. @"value_id" : @(10)
  8424. }
  8425. },
  8426. @"control" : @"enum",
  8427. @"name" : @"erpOrderStatus",
  8428. @"required" : @"true",
  8429. @"single_select" : @"true"
  8430. },
  8431. @"title" : @"Order Type"
  8432. }.mutableCopy;
  8433. // setting
  8434. NSDictionary *setting = params[@"setting"];
  8435. NSNumber *hide = setting[@"OrderTypeHide"];
  8436. [dic setValue:hide forKey:@"hide"];
  8437. return dic;
  8438. }
  8439. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8440. countryCode = [self translateSingleQuote:countryCode];
  8441. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8442. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8443. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8444. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8445. if (name == NULL) {
  8446. name = "";
  8447. }
  8448. if (code == NULL) {
  8449. code = "";
  8450. }
  8451. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8452. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8453. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8454. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8455. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8456. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8457. }
  8458. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8459. [container setValue:stateDic forKey:key];
  8460. }] mutableCopy];
  8461. [ret removeObjectForKey:@"result"];
  8462. // failure 可以不用了,一样的
  8463. if (ret.allKeys.count == 0) {
  8464. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8465. [stateDic setValue:@"Other" forKey:@"value"];
  8466. [stateDic setValue:@"" forKey:@"value_id"];
  8467. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8468. if (state_code && [@"" isEqualToString:state_code]) {
  8469. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8470. }
  8471. NSString *key = [NSString stringWithFormat:@"val_0"];
  8472. [ret setValue:stateDic forKey:key];
  8473. }
  8474. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8475. return ret;
  8476. }
  8477. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8478. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8479. orderCode = [self translateSingleQuote:orderCode];
  8480. 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];
  8481. __block NSString *payType = @"";
  8482. __block NSString *firstName = @"";
  8483. __block NSString *lastName = @"";
  8484. __block NSString *addr1 = @"";
  8485. __block NSString *addr2 = @"";
  8486. __block NSString *zipcode = @"";
  8487. __block NSString *cardType = @"";
  8488. __block NSString *cardNumber = @"";
  8489. __block NSString *securityCode = @"";
  8490. __block NSString *month = @"";
  8491. __block NSString *year = @"";
  8492. __block NSString *city = @"";
  8493. __block NSString *state = @"";
  8494. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8495. payType = [self textAtColumn:0 statement:stmt];
  8496. firstName = [self textAtColumn:1 statement:stmt];
  8497. lastName = [self textAtColumn:2 statement:stmt];
  8498. addr1 = [self textAtColumn:3 statement:stmt];
  8499. addr2 = [self textAtColumn:4 statement:stmt];
  8500. zipcode = [self textAtColumn:5 statement:stmt];
  8501. cardType = [self textAtColumn:6 statement:stmt];
  8502. cardNumber = [self textAtColumn:7 statement:stmt];
  8503. securityCode = [self textAtColumn:8 statement:stmt];
  8504. month = [self textAtColumn:9 statement:stmt];
  8505. year = [self textAtColumn:10 statement:stmt];
  8506. city = [self textAtColumn:11 statement:stmt];
  8507. state = [self textAtColumn:12 statement:stmt];
  8508. }];
  8509. NSString *required = @"true";
  8510. // setting
  8511. NSDictionary *setting = params[@"setting"];
  8512. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8513. if ([requiredNumber integerValue]) {
  8514. required = @"true";
  8515. } else {
  8516. required = @"false";
  8517. }
  8518. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8519. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8520. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8521. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8522. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8523. NSString *type = [self textAtColumn:1 statement:stmt];
  8524. NSMutableDictionary *val = @{
  8525. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8526. @"value" : type,
  8527. @"value_id" : type_id
  8528. }.mutableCopy;
  8529. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  8530. NSDictionary *sub_item = @{
  8531. @"count" : @(3),
  8532. @"item_0" : @{
  8533. @"aname" : @"choose",
  8534. @"control" : @"multi_action",
  8535. @"count" : @(1),
  8536. @"item_0" : @{
  8537. @"aname" : @"Same as customer",
  8538. @"key_map" : @{
  8539. @"credit_card_address1" : @"customer_address1",
  8540. @"credit_card_address2" : @"customer_address2",
  8541. @"credit_card_city" : @"customer_city",
  8542. @"credit_card_first_name" : @"customer_first_name",
  8543. @"credit_card_last_name" : @"customer_last_name",
  8544. @"credit_card_state" : @"customer_state",
  8545. @"credit_card_zipcode" : @"customer_zipcode"
  8546. },
  8547. @"type" : @"pull"
  8548. }
  8549. },
  8550. @"item_1" : @{
  8551. @"aname" : @"",
  8552. @"color" : @"red",
  8553. @"control" : @"text",
  8554. @"name" : @"",
  8555. @"value" : @"USA Credit cards only"
  8556. },
  8557. @"item_2" : @{
  8558. @"aname" : @"Fill",
  8559. @"cadedate" : @{
  8560. @"count" : @(2),
  8561. @"val_0" : @{
  8562. @"check" : @(1),
  8563. @"sub_item" : @{
  8564. @"count" : @(11),
  8565. @"item_0" : @{
  8566. @"aname" : @"Type",
  8567. @"cadedate" : @{
  8568. @"count" : @(2),
  8569. @"val_0" : @{
  8570. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8571. @"value" : @"VISA",
  8572. @"value_id" : @"VISA"/*@(0)*/
  8573. },
  8574. @"val_1" : @{
  8575. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8576. @"value" : @"MASTER CARD",
  8577. @"value_id" : @"MASTER CARD"/*@(1)*/
  8578. }
  8579. },
  8580. @"control" : @"enum",
  8581. @"name" : @"credit_card_type",
  8582. @"required" : @"true",
  8583. @"single_select" : @"true"
  8584. },
  8585. @"item_1" : @{
  8586. @"aname" : @"Number",
  8587. @"control" : @"edit",
  8588. @"keyboard" : @"int",
  8589. @"length" : @"16",
  8590. @"name" : @"credit_card_number",
  8591. @"required" : @"true",
  8592. @"value" : cardNumber
  8593. },
  8594. @"item_10" : @{
  8595. @"aname" : @"State",
  8596. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8597. @"control" : @"enum",
  8598. @"enum" : @"true",
  8599. @"name" : @"credit_card_state",
  8600. @"required" : @"true",
  8601. @"single_select" : @"true"
  8602. },
  8603. @"item_2" : @{
  8604. @"aname" : @"Expiration Date",
  8605. @"control" : @"monthpicker",
  8606. @"name" : @"credit_card_expiration",
  8607. @"required" : @"true",
  8608. @"type" : @"date",
  8609. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8610. },
  8611. @"item_3" : @{
  8612. @"aname" : @"Security Code",
  8613. @"control" : @"edit",
  8614. @"keyboard" : @"int",
  8615. @"length" : @"3",
  8616. @"name" : @"credit_card_security_code",
  8617. @"required" : @"true",
  8618. @"value" : securityCode
  8619. },
  8620. @"item_4" : @{
  8621. @"aname" : @"First Name",
  8622. @"control" : @"edit",
  8623. @"keyboard" : @"text",
  8624. @"name" : @"credit_card_first_name",
  8625. @"required" : @"true",
  8626. @"value" : firstName
  8627. },
  8628. @"item_5" : @{
  8629. @"aname" : @"Last Name",
  8630. @"control" : @"edit",
  8631. @"keyboard" : @"text",
  8632. @"name" : @"credit_card_last_name",
  8633. @"required" : @"true",
  8634. @"value" : lastName
  8635. },
  8636. @"item_6" : @{
  8637. @"aname" : @"Address 1",
  8638. @"control" : @"edit",
  8639. @"keyboard" : @"text",
  8640. @"name" : @"credit_card_address1",
  8641. @"required" : @"true",
  8642. @"value" : addr1
  8643. },
  8644. @"item_7" : @{
  8645. @"aname" : @"Address 2",
  8646. @"control" : @"edit",
  8647. @"keyboard" : @"text",
  8648. @"name" : @"credit_card_address2",
  8649. @"value" : addr2
  8650. },
  8651. @"item_8" : @{
  8652. @"aname" : @"zip code",
  8653. @"control" : @"edit",
  8654. @"keyboard" : @"text",
  8655. @"name" : @"credit_card_zipcode",
  8656. @"required" : @"true",
  8657. @"value" : zipcode
  8658. },
  8659. @"item_9" : @{
  8660. @"aname" : @"City",
  8661. @"control" : @"edit",
  8662. @"keyboard" : @"text",
  8663. @"name" : @"credit_card_city",
  8664. @"required" : @"true",
  8665. @"value" : city
  8666. }
  8667. },
  8668. @"value" : @"Fill Now",
  8669. @"value_id" : @""
  8670. },
  8671. @"val_1" : @{
  8672. @"check" : @(0),
  8673. @"value" : @"Fill Later",
  8674. @"value_id" : @""
  8675. }
  8676. },
  8677. @"control" : @"enum",
  8678. @"name" : @"",
  8679. @"single_select" : @"true"
  8680. }
  8681. };
  8682. [val setObject:sub_item forKey:@"sub_item"];
  8683. }
  8684. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8685. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8686. }];
  8687. // "section_2"
  8688. NSMutableDictionary *dic = @{
  8689. @"count" : @(1),
  8690. @"item_0" : @{
  8691. @"aname" : @"Payment",
  8692. @"required" : required,
  8693. @"cadedate" : cadedate,
  8694. // @{
  8695. // @"count" : @(6),
  8696. // @"val_3" : @{
  8697. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8698. // @"value" : @"Check",
  8699. // @"value_id" : @"Check"
  8700. // },
  8701. // @"val_2" : @{
  8702. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8703. // @"value" : @"Cash",
  8704. // @"value_id" : @"Cash"
  8705. // },
  8706. // @"val_1" : @{
  8707. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8708. // @"value" : @"NET 60",
  8709. // @"value_id" : @"NET 30"
  8710. // },
  8711. // @"val_4" : @{
  8712. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8713. // @"value" : @"Wire Transfer",
  8714. // @"value_id" : @"Wire Transfer"
  8715. // },
  8716. // @"val_0" : @{
  8717. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8718. // @"sub_item" : @{
  8719. // @"count" : @(3),
  8720. // @"item_0" : @{
  8721. // @"aname" : @"choose",
  8722. // @"control" : @"multi_action",
  8723. // @"count" : @(1),
  8724. // @"item_0" : @{
  8725. // @"aname" : @"Same as customer",
  8726. // @"key_map" : @{
  8727. // @"credit_card_address1" : @"customer_address1",
  8728. // @"credit_card_address2" : @"customer_address2",
  8729. // @"credit_card_city" : @"customer_city",
  8730. // @"credit_card_first_name" : @"customer_first_name",
  8731. // @"credit_card_last_name" : @"customer_last_name",
  8732. // @"credit_card_state" : @"customer_state",
  8733. // @"credit_card_zipcode" : @"customer_zipcode"
  8734. // },
  8735. // @"type" : @"pull"
  8736. // }
  8737. // },
  8738. // @"item_1" : @{
  8739. // @"aname" : @"",
  8740. // @"color" : @"red",
  8741. // @"control" : @"text",
  8742. // @"name" : @"",
  8743. // @"value" : @"USA Credit cards only"
  8744. // },
  8745. // @"item_2" : @{
  8746. // @"aname" : @"Fill",
  8747. // @"cadedate" : @{
  8748. // @"count" : @(2),
  8749. // @"val_0" : @{
  8750. // @"check" : @(1),
  8751. // @"sub_item" : @{
  8752. // @"count" : @(11),
  8753. // @"item_0" : @{
  8754. // @"aname" : @"Type",
  8755. // @"cadedate" : @{
  8756. // @"count" : @(2),
  8757. // @"val_0" : @{
  8758. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8759. // @"value" : @"VISA",
  8760. // @"value_id" : @(0)
  8761. // },
  8762. // @"val_1" : @{
  8763. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8764. // @"value" : @"MASTER CARD",
  8765. // @"value_id" : @(1)
  8766. // }
  8767. // },
  8768. // @"control" : @"enum",
  8769. // @"name" : @"credit_card_type",
  8770. // @"required" : @"true",
  8771. // @"single_select" : @"true"
  8772. // },
  8773. // @"item_1" : @{
  8774. // @"aname" : @"Number",
  8775. // @"control" : @"edit",
  8776. // @"keyboard" : @"int",
  8777. // @"length" : @"16",
  8778. // @"name" : @"credit_card_number",
  8779. // @"required" : @"true",
  8780. // @"value" : cardNumber
  8781. // },
  8782. // @"item_10" : @{
  8783. // @"aname" : @"State",
  8784. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8785. // @"control" : @"enum",
  8786. // @"enum" : @"true",
  8787. // @"name" : @"credit_card_state",
  8788. // @"required" : @"true",
  8789. // @"single_select" : @"true"
  8790. // },
  8791. // @"item_2" : @{
  8792. // @"aname" : @"Expiration Date",
  8793. // @"control" : @"monthpicker",
  8794. // @"name" : @"credit_card_expiration",
  8795. // @"required" : @"true",
  8796. // @"type" : @"date",
  8797. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8798. // },
  8799. // @"item_3" : @{
  8800. // @"aname" : @"Security Code",
  8801. // @"control" : @"edit",
  8802. // @"keyboard" : @"int",
  8803. // @"length" : @"3",
  8804. // @"name" : @"credit_card_security_code",
  8805. // @"required" : @"true",
  8806. // @"value" : securityCode
  8807. // },
  8808. // @"item_4" : @{
  8809. // @"aname" : @"First Name",
  8810. // @"control" : @"edit",
  8811. // @"keyboard" : @"text",
  8812. // @"name" : @"credit_card_first_name",
  8813. // @"required" : @"true",
  8814. // @"value" : firstName
  8815. // },
  8816. // @"item_5" : @{
  8817. // @"aname" : @"Last Name",
  8818. // @"control" : @"edit",
  8819. // @"keyboard" : @"text",
  8820. // @"name" : @"credit_card_last_name",
  8821. // @"required" : @"true",
  8822. // @"value" : lastName
  8823. // },
  8824. // @"item_6" : @{
  8825. // @"aname" : @"Address 1",
  8826. // @"control" : @"edit",
  8827. // @"keyboard" : @"text",
  8828. // @"name" : @"credit_card_address1",
  8829. // @"required" : @"true",
  8830. // @"value" : addr1
  8831. // },
  8832. // @"item_7" : @{
  8833. // @"aname" : @"Address 2",
  8834. // @"control" : @"edit",
  8835. // @"keyboard" : @"text",
  8836. // @"name" : @"credit_card_address2",
  8837. // @"value" : addr2
  8838. // },
  8839. // @"item_8" : @{
  8840. // @"aname" : @"zip code",
  8841. // @"control" : @"edit",
  8842. // @"keyboard" : @"text",
  8843. // @"name" : @"credit_card_zipcode",
  8844. // @"required" : @"true",
  8845. // @"value" : zipcode
  8846. // },
  8847. // @"item_9" : @{
  8848. // @"aname" : @"City",
  8849. // @"control" : @"edit",
  8850. // @"keyboard" : @"text",
  8851. // @"name" : @"credit_card_city",
  8852. // @"required" : @"true",
  8853. // @"value" : city
  8854. // }
  8855. // },
  8856. // @"value" : @"Fill Now",
  8857. // @"value_id" : @""
  8858. // },
  8859. // @"val_1" : @{
  8860. // @"check" : @(0),
  8861. // @"value" : @"Fill Later",
  8862. // @"value_id" : @""
  8863. // }
  8864. // },
  8865. // @"control" : @"enum",
  8866. // @"name" : @"",
  8867. // @"single_select" : @"true"
  8868. // }
  8869. // },
  8870. // @"value" : @"Visa/Master",
  8871. // @"value_id" : @"Credit Card"
  8872. // },
  8873. // @"val_5" : @{
  8874. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8875. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8876. // @"value_id" : @"FOLLOW EXISTING"
  8877. // }
  8878. // },
  8879. @"control" : @"enum",
  8880. @"name" : @"paymentType",
  8881. @"single_select" : @"true"
  8882. },
  8883. @"title" : @"Payment Information"
  8884. }.mutableCopy;
  8885. NSNumber *hide = setting[@"PaymentInformationHide"];
  8886. [dic setValue:hide forKey:@"hide"];
  8887. return dic;
  8888. }
  8889. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8890. // params
  8891. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8892. orderCode = [self translateSingleQuote:orderCode];
  8893. // 缺货检查
  8894. 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];
  8895. __block BOOL outOfStock = NO;
  8896. sqlite3 *database = db;
  8897. if (!db) {
  8898. database = [iSalesDB get_db];
  8899. }
  8900. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8901. int availability = sqlite3_column_int(stmt, 0);
  8902. int item_qty = sqlite3_column_int(stmt, 1);
  8903. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8904. }];
  8905. if (!db) {
  8906. [iSalesDB close_db:database];
  8907. }
  8908. return outOfStock;
  8909. }
  8910. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8911. {
  8912. assert(params[@"user"]!=nil);
  8913. assert(params[@"contact_id"]!=nil);
  8914. assert(params[@"can_create_backorder"]!=nil);
  8915. sqlite3 *db = [iSalesDB get_db];
  8916. // params
  8917. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8918. orderCode = [self translateSingleQuote:orderCode];
  8919. // 缺货检查
  8920. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8921. if (![params[@"can_create_backorder"] boolValue]) {
  8922. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8923. if (out_of_stock) {
  8924. [iSalesDB close_db:db];
  8925. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8926. [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"];
  8927. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8928. return [RAConvertor dict2data:resultDic];
  8929. }
  8930. }
  8931. // UISetting
  8932. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8933. NSString *cachefolder = [paths objectAtIndex:0];
  8934. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8935. NSData* json =nil;
  8936. json=[NSData dataWithContentsOfFile:img_cache];
  8937. NSError *error=nil;
  8938. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8939. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8940. [params setObject:setting forKey:@"setting"];
  8941. int section_count = 0;
  8942. // 0 Order Type 1 Shipping Method 2 Payment Information
  8943. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8944. // 0 Order Type
  8945. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8946. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8947. [ret setValue:order_type_dic forKey:key0];
  8948. // 1 Shipping Method
  8949. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8950. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8951. [ret setValue:shipping_method_dic forKey:key1];
  8952. // 2 Payment Information
  8953. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8954. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8955. [ret setValue:payment_info_dic forKey:key2];
  8956. // 3 Customer
  8957. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8958. NSDictionary *customer_dic = [self customerDic:params db:db];
  8959. [ret setValue:customer_dic forKey:key3];
  8960. // 4 Ship To
  8961. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8962. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8963. [ret setValue:ship_to_dic forKey:key4];
  8964. // 5 Ship From
  8965. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8966. if (![shipFromDisable integerValue]) {
  8967. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8968. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8969. [ret setValue:ship_from_dic forKey:key5];
  8970. }
  8971. // 6 Freight Bill To
  8972. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8973. if (![freightBillToDisable integerValue]) {
  8974. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8975. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8976. [ret setValue:freight_bill_to forKey:key6];
  8977. }
  8978. // 7 Merchandise Bill To
  8979. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8980. if (![merchandiseBillToDisable integerValue]) {
  8981. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8982. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8983. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8984. }
  8985. // 8 Return To
  8986. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8987. if (![returnToDisable integerValue]) {
  8988. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8989. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8990. [ret setValue:return_to_dic forKey:key8];
  8991. }
  8992. // 9 Model Information
  8993. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8994. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8995. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8996. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8997. [ret setValue:model_info_dic forKey:key9];
  8998. // 10 Remarks Content
  8999. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9000. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9001. [ret setValue:remarks_content_dic forKey:key10];
  9002. // 11 Order Total
  9003. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9004. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9005. [ret setValue:order_total_dic forKey:key11];
  9006. // 12 Signature
  9007. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9008. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9009. [ret setValue:sign_dic forKey:key12];
  9010. [ret setValue:@(section_count) forKey:@"section_count"];
  9011. [iSalesDB close_db:db];
  9012. return [RAConvertor dict2data:ret];
  9013. // return nil;
  9014. }
  9015. #pragma mark addr editor
  9016. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9017. NSMutableDictionary *new_item = [item mutableCopy];
  9018. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9019. return new_item;
  9020. }
  9021. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9022. {
  9023. // "is_subaction" = true;
  9024. // orderCode = MOB1608240002;
  9025. // password = 123456;
  9026. // "subaction_tag" = 1;
  9027. // user = EvanK;
  9028. // {
  9029. // "is_subaction" = true;
  9030. // orderCode = MOB1608240002;
  9031. // password = 123456;
  9032. // "refresh_trigger" = zipcode;
  9033. // "subaction_tag" = 1;
  9034. // user = EvanK;
  9035. // }
  9036. NSString *country_code = nil;
  9037. NSString *zipCode = nil;
  9038. NSString *stateCode = nil;
  9039. NSString *city = nil;
  9040. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9041. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9042. NSString *code_id = params[@"country"];
  9043. country_code = [self countryCodeByid:code_id];
  9044. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9045. NSString *zip_code = params[@"zipcode"];
  9046. // 剔除全部为空格
  9047. int spaceCount = 0;
  9048. for (int i = 0; i < zip_code.length; i++) {
  9049. if ([zip_code characterAtIndex:i] == ' ') {
  9050. spaceCount++;
  9051. }
  9052. }
  9053. if (spaceCount == zip_code.length) {
  9054. zip_code = @"";
  9055. }
  9056. zipCode = zip_code;
  9057. if (zipCode.length > 0) {
  9058. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9059. country_code = [dic valueForKey:@"country_code"];
  9060. if (!country_code) {
  9061. // country_code = @"US";
  9062. NSString *code_id = params[@"country"];
  9063. country_code = [self countryCodeByid:code_id];
  9064. }
  9065. stateCode = [dic valueForKey:@"state_code"];
  9066. if(!stateCode.length) {
  9067. stateCode = params[@"state"];
  9068. }
  9069. city = [dic valueForKey:@"city"];
  9070. if (!city.length) {
  9071. city = params[@"city"];
  9072. }
  9073. // zip code
  9074. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9075. // [zipDic setValue:zipCode forKey:@"value"];
  9076. // [section_0 setValue:zipDic forKey:@"item_11"];
  9077. } else {
  9078. NSString *code_id = params[@"country"];
  9079. country_code = [self countryCodeByid:code_id];
  9080. stateCode = params[@"state"];
  9081. city = params[@"city"];
  9082. }
  9083. }
  9084. }
  9085. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9086. // [ret removeObjectForKey:@"up_params"];
  9087. [ret setObject:@"New Address" forKey:@"title"];
  9088. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9089. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9090. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9091. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9092. // [country_dic removeObjectForKey:@"refresh"];
  9093. // [country_dic removeObjectForKey:@"restore"];
  9094. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9095. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9096. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9097. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9098. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9099. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9100. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9101. // [zip_code_dic removeObjectForKey:@"refresh"];
  9102. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9103. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9104. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9105. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9106. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9107. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9108. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9109. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9110. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9111. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9112. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9113. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9114. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9115. // country
  9116. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9117. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9118. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9119. // state
  9120. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9121. // NSDictionary *tmpDic = @{
  9122. // @"value" : @"Other",
  9123. // @"value_id" : @"",
  9124. // @"check" : [NSNumber numberWithInteger:0]
  9125. // };
  9126. //
  9127. // for (int i = 0; i < allState.allKeys.count; i++) {
  9128. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9129. //
  9130. // NSDictionary *tmp = allState[key];
  9131. // [allState setValue:tmpDic forKey:key];
  9132. // tmpDic = tmp;
  9133. // }
  9134. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9135. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9136. [ret setValue:new_section_0 forKey:@"section_0"];
  9137. return [RAConvertor dict2data:ret];
  9138. }
  9139. #pragma mark save addr
  9140. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9141. {
  9142. // NSString *companyName = [self valueInParams:params key:@"company"];
  9143. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9144. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9145. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9146. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9147. // NSString *countryId = [self valueInParams:params key:@"country"];
  9148. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9149. // NSString *city = [self valueInParams:params key:@"city"];
  9150. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9151. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9152. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9153. // NSString *phone = [self valueInParams:params key:@"phone"];
  9154. // NSString *fax = [self valueInParams:params key:@"fax"];
  9155. // NSString *email = [self valueInParams:params key:@"email"];
  9156. return [self offline_saveContact:params update:NO isCustomer:NO];
  9157. }
  9158. #pragma mark cancel order
  9159. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9160. {
  9161. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9162. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9163. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9164. if (order_id.length) {
  9165. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9166. }
  9167. int ret = [iSalesDB execSql:sql];
  9168. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9169. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9170. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9171. // [dic setValue:@"160409" forKey:@"min_ver"];
  9172. return [RAConvertor dict2data:dic];
  9173. }
  9174. #pragma mark sign order
  9175. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9176. {
  9177. //参考 offline_saveBusinesscard
  9178. DebugLog(@"sign order params: %@",params);
  9179. // orderCode = MOB1608240002;
  9180. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9181. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9182. orderCode = [self translateSingleQuote:orderCode];
  9183. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9184. picPath = [self translateSingleQuote:picPath];
  9185. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9186. int ret = [iSalesDB execSql:sql];
  9187. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9188. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9189. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9190. // [dic setValue:@"160409" forKey:@"min_ver"];
  9191. return [RAConvertor dict2data:dic];
  9192. }
  9193. #pragma mark save order
  9194. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9195. NSString *ret = [self valueInParams:params key:key];
  9196. if (translate) {
  9197. ret = [self translateSingleQuote:ret];
  9198. }
  9199. return ret;
  9200. }
  9201. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9202. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9203. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9204. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9205. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9206. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9207. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9208. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9209. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9210. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9211. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9212. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9213. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9214. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9215. if (![total_price isEqualToString:@""]) {
  9216. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9217. } else {
  9218. total_price = @"";
  9219. }
  9220. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9221. if ([paymentsAndCredits isEqualToString:@""]) {
  9222. paymentsAndCredits = @"";
  9223. } else {
  9224. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9225. }
  9226. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9227. if ([handling_fee_value isEqualToString:@""]) {
  9228. handling_fee_value = @"";
  9229. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9230. handling_fee_value = @"";
  9231. } else {
  9232. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9233. }
  9234. NSString *handling_fee_placeholder = handling_fee_value;
  9235. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9236. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9237. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9238. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9239. NSString *lift_gate_placeholder = @"";
  9240. if ([lift_gate_value isEqualToString:@""]) {
  9241. lift_gate_placeholder = @"";
  9242. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9243. lift_gate_placeholder = @"";
  9244. } else {
  9245. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9246. }
  9247. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9248. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9249. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9250. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9251. 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];
  9252. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9253. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9254. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9255. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9256. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9257. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9258. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9259. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9260. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9261. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9262. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9263. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9264. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9265. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9266. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9267. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9268. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9269. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9270. if (!number_nil) {
  9271. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9272. }
  9273. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9274. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9275. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9276. if (!security_code_nil) {
  9277. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9278. }
  9279. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9280. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9281. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9282. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9283. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9284. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9285. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9286. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9287. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9288. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9289. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9290. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9291. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9292. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9293. NSString *contact_id = customer_cid;
  9294. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9295. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9296. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9297. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9298. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9299. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9300. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9301. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9302. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9303. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9304. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9305. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9306. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9307. //
  9308. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9309. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9310. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9311. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9312. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9313. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9314. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9315. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9316. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9317. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9318. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9319. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9320. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9321. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9322. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9323. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9324. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9325. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9326. 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];
  9327. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9328. if (receive_cid.length) {
  9329. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9330. }
  9331. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9332. if (receive_name.length) {
  9333. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9334. }
  9335. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9336. if (receive_ext.length) {
  9337. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9338. }
  9339. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9340. if (receive_contact.length) {
  9341. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9342. }
  9343. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9344. if (receive_phone.length) {
  9345. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9346. }
  9347. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9348. if (receive_email.length) {
  9349. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9350. }
  9351. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9352. if (receive_fax.length) {
  9353. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9354. }
  9355. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9356. if (sender_cid.length) {
  9357. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9358. }
  9359. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9360. if (sender_name.length) {
  9361. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9362. }
  9363. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9364. if (sender_ext.length) {
  9365. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9366. }
  9367. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9368. if(sender_contact.length) {
  9369. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9370. }
  9371. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9372. if (sender_phone.length) {
  9373. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9374. }
  9375. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9376. if (sender_fax.length) {
  9377. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9378. }
  9379. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9380. if (sender_email.length) {
  9381. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9382. }
  9383. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9384. if (shipping_billto_cid.length) {
  9385. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9386. }
  9387. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9388. if (shipping_billto_name.length) {
  9389. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9390. }
  9391. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9392. if (shipping_billto_ext.length) {
  9393. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9394. }
  9395. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9396. if (shipping_billto_contact.length) {
  9397. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9398. }
  9399. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9400. if (shipping_billto_phone.length) {
  9401. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9402. }
  9403. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9404. if (shipping_billto_fax.length) {
  9405. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9406. }
  9407. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9408. if (shipping_billto_email.length) {
  9409. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9410. }
  9411. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9412. if (billing_cid.length) {
  9413. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9414. }
  9415. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9416. if (billing_name.length) {
  9417. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9418. }
  9419. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9420. if (billing_ext.length) {
  9421. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9422. }
  9423. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9424. if (billing_contact.length) {
  9425. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9426. }
  9427. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9428. if (billing_phone.length) {
  9429. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9430. }
  9431. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9432. if (billing_fax.length) {
  9433. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9434. }
  9435. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9436. if (billing_email.length) {
  9437. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9438. }
  9439. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9440. if (returnto_cid.length) {
  9441. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9442. }
  9443. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9444. if (returnto_name.length) {
  9445. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9446. }
  9447. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9448. if (returnto_ext.length) {
  9449. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9450. }
  9451. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9452. if (returnto_contact.length) {
  9453. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9454. }
  9455. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9456. if (returnto_phone.length) {
  9457. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9458. }
  9459. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9460. if (returnto_fax.length) {
  9461. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9462. }
  9463. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9464. if (returnto_email.length) {
  9465. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9466. }
  9467. NSString *order_status = @"status = 1,";
  9468. if (submit) {
  9469. order_status = @"status = -11,";
  9470. }
  9471. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9472. NSString *sync_sql = @"";
  9473. if (submit) {
  9474. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9475. [param setValue:sales_rep forKey:@"sales_rep"];
  9476. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9477. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9478. }
  9479. 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];
  9480. DebugLog(@"save order contactSql: %@",contactSql);
  9481. DebugLog(@"save order orderSql: %@",orderSql);
  9482. // int contact_ret = [iSalesDB execSql:contactSql];
  9483. int order_ret = [iSalesDB execSql:orderSql];
  9484. int ret = order_ret;
  9485. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9486. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9487. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9488. // [dic setValue:@"160409" forKey:@"min_ver"];
  9489. [dic setObject:so_id forKey:@"so#"];
  9490. return [RAConvertor dict2data:dic];
  9491. }
  9492. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9493. {
  9494. // id foo = nil;
  9495. // NSMutableArray *a = @[].mutableCopy;
  9496. // [a addObject:foo];
  9497. return [self saveorder:param submit:NO];
  9498. }
  9499. #pragma mark add to cart by name
  9500. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  9501. {
  9502. NSString *orderCode = [params objectForKey:@"orderCode"];
  9503. NSDictionary *newParams = @{
  9504. @"product_id" : params[@"product_id_string"],
  9505. @"orderCode" : orderCode,
  9506. @"can_create_backorder":params[@"can_create_backorder"]
  9507. };
  9508. return [self offline_add2cart:[newParams mutableCopy]];
  9509. }
  9510. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  9511. {
  9512. NSString *orderCode = [params objectForKey:@"orderCode"];
  9513. NSString *upccode = [params objectForKey:@"upc_code"];
  9514. // product_name = [self translateSingleQuote:product_name];
  9515. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9516. // bool search_upc = [params[@"search_upc"] boolValue];
  9517. sqlite3 *db = [iSalesDB get_db];
  9518. __block NSMutableString *product_id_string = [NSMutableString string];
  9519. // NSString *name = [product_name_array objectAtIndex:i];
  9520. NSString *sql =nil;
  9521. 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];
  9522. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9523. int product_id = sqlite3_column_int(stmt, 0);
  9524. [product_id_string appendFormat:@"%d",product_id];
  9525. }];
  9526. if (!orderCode) {
  9527. orderCode = @"";
  9528. }
  9529. NSDictionary *newParams = @{
  9530. @"product_id" : product_id_string,
  9531. @"orderCode" : orderCode,
  9532. @"can_create_backorder":params[@"can_create_backorder"]
  9533. };
  9534. [iSalesDB close_db:db];
  9535. return [self offline_add2cart:[newParams mutableCopy]];
  9536. }
  9537. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9538. {
  9539. NSString *orderCode = [params objectForKey:@"orderCode"];
  9540. NSString *product_name = [params objectForKey:@"product_name"];
  9541. product_name = [self translateSingleQuote:product_name];
  9542. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9543. bool search_upc = [params[@"search_upc"] boolValue];
  9544. sqlite3 *db = [iSalesDB get_db];
  9545. __block NSMutableString *product_id_string = [NSMutableString string];
  9546. for (int i = 0; i < product_name_array.count; i++) {
  9547. NSString *name = [product_name_array objectAtIndex:i];
  9548. NSString *sql =nil;
  9549. if(search_upc)
  9550. 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];
  9551. else
  9552. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9553. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9554. int product_id = sqlite3_column_int(stmt, 0);
  9555. if (i == product_name_array.count - 1) {
  9556. [product_id_string appendFormat:@"%d",product_id];
  9557. } else {
  9558. [product_id_string appendFormat:@"%d,",product_id];
  9559. }
  9560. }];
  9561. }
  9562. if (!orderCode) {
  9563. orderCode = @"";
  9564. }
  9565. NSDictionary *newParams = @{
  9566. @"product_id" : product_id_string,
  9567. @"orderCode" : orderCode,
  9568. @"can_create_backorder":params[@"can_create_backorder"]
  9569. };
  9570. [iSalesDB close_db:db];
  9571. return [self offline_add2cart:[newParams mutableCopy]];
  9572. }
  9573. #pragma mark reset order
  9574. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9575. {
  9576. // UIApplication * app = [UIApplication sharedApplication];
  9577. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9578. [iSalesDB disable_trigger];
  9579. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9580. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9581. [iSalesDB enable_trigger];
  9582. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9583. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9584. return [RAConvertor dict2data:dic];
  9585. }
  9586. #pragma mark submit order
  9587. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9588. {
  9589. return [self saveorder:params submit:YES];
  9590. }
  9591. #pragma mark copy order
  9592. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9593. {
  9594. NSMutableDictionary *ret = @{}.mutableCopy;
  9595. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9596. sqlite3 *db = [iSalesDB get_db];
  9597. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9598. // 首先查看联系人是否active
  9599. 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];
  9600. __block int customer_is_active = 1;
  9601. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9602. customer_is_active = sqlite3_column_int(stmt, 0);
  9603. }];
  9604. if (!customer_is_active) {
  9605. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9606. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9607. [iSalesDB close_db:db];
  9608. return [RAConvertor dict2data:ret];
  9609. }
  9610. // new order
  9611. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9612. NSString *new_order_code = [self get_offline_soid:db];
  9613. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9614. user = [self translateSingleQuote:user];
  9615. 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
  9616. __block int result = 1;
  9617. result = [iSalesDB execSql:insert_order_sql db:db];
  9618. if (!result) {
  9619. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9620. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9621. [iSalesDB close_db:db];
  9622. return [RAConvertor dict2data:ret];
  9623. }
  9624. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9625. // __block NSString *product_id = @"";
  9626. __weak typeof(self) weakSelf = self;
  9627. 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];
  9628. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9629. int is_active = sqlite3_column_int(stmt, 1);
  9630. if (is_active) {
  9631. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9632. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9633. 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];
  9634. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9635. }
  9636. }];
  9637. // product_id = [product_id substringFromIndex:1];
  9638. //
  9639. // [self offline_add2cart:@{}.mutableCopy];
  9640. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9641. [iSalesDB close_db:db];
  9642. if (result) {
  9643. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9644. } else {
  9645. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9646. }
  9647. [ret setObject:new_order_code forKey:@"so_id"];
  9648. return [RAConvertor dict2data:ret];
  9649. }
  9650. #pragma mark move wish list to cart
  9651. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9652. assert(params[@"can_create_backorder"]!=nil);
  9653. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9654. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9655. NSString *collectId = params[@"collectId"];
  9656. 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];
  9657. __block NSString *product_id = @"";
  9658. __block NSString *qty = @"";
  9659. __block int number_of_outOfStock = 0;
  9660. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9661. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9662. int productId = sqlite3_column_int(stmt, 0);
  9663. int item_qty = sqlite3_column_int(stmt, 1);
  9664. int availability = sqlite3_column_int(stmt, 2);
  9665. int _id = sqlite3_column_int(stmt, 3);
  9666. if (![params[@"can_create_backorder"] boolValue]) {
  9667. // 库存小于购买量为缺货
  9668. if (availability >= item_qty) {
  9669. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9670. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9671. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9672. } else {
  9673. number_of_outOfStock++;
  9674. }
  9675. } else {
  9676. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9677. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9678. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9679. }
  9680. }];
  9681. NSMutableDictionary *retDic = nil;
  9682. if (![params[@"can_create_backorder"] boolValue]) {
  9683. if (delete_collectId.count == 0) {
  9684. retDic = [NSMutableDictionary dictionary];
  9685. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9686. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9687. return [RAConvertor dict2data:retDic];
  9688. }
  9689. }
  9690. if (product_id.length > 1) {
  9691. product_id = [product_id substringFromIndex:1];
  9692. }
  9693. if (qty.length > 1) {
  9694. qty = [qty substringFromIndex:1];
  9695. }
  9696. NSString *orderCode = params[@"orderCode"];
  9697. if (!orderCode) {
  9698. orderCode = @"";
  9699. }
  9700. NSDictionary *newParams = @{
  9701. @"product_id" : product_id,
  9702. @"orderCode" : orderCode,
  9703. @"qty" : qty,
  9704. @"can_create_backorder":params[@"can_create_backorder"]
  9705. };
  9706. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9707. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9708. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9709. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9710. NSInteger ret = [wish_return[@"result"] intValue];
  9711. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9712. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9713. }
  9714. if (![params[@"can_create_backorder"] boolValue]) {
  9715. if (number_of_outOfStock > 0) {
  9716. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9717. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9718. }
  9719. }
  9720. return [RAConvertor dict2data:retDic];
  9721. }
  9722. #pragma mark - portfolio
  9723. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9724. // assert(params[@"contact_id"]!=nil);
  9725. assert(params[@"user"]!=nil);
  9726. assert(params[@"can_see_price"]!=nil);
  9727. int sort = [[params valueForKey:@"sort"] intValue];
  9728. int offset = [[params valueForKey:@"offset"] intValue];
  9729. int limit = [[params valueForKey:@"limit"] intValue];
  9730. NSString *orderBy = @"";
  9731. switch (sort) {
  9732. case 0:{
  9733. orderBy = @"p.modify_time desc";
  9734. }
  9735. break;
  9736. case 1:{
  9737. orderBy = @"modify_time asc";
  9738. }
  9739. break;
  9740. case 2:{
  9741. orderBy = @"p.name asc";
  9742. }
  9743. break;
  9744. case 3:{
  9745. orderBy = @"p.name desc";
  9746. }
  9747. break;
  9748. case 4:{
  9749. orderBy = @"p.description asc";
  9750. }
  9751. break;
  9752. case 5: {
  9753. orderBy = @"m.default_category asc";
  9754. }
  9755. default:
  9756. break;
  9757. }
  9758. // 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];
  9759. 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];
  9760. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9761. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9762. sqlite3 *db = [iSalesDB get_db];
  9763. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9764. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9765. int product_id = sqlite3_column_int(stmt, 0);
  9766. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9767. NSString *name = [self textAtColumn:1 statement:stmt];
  9768. NSString *description = [self textAtColumn:2 statement:stmt];
  9769. double price = sqlite3_column_double(stmt, 3);
  9770. double discount = sqlite3_column_double(stmt,4);
  9771. int qty = sqlite3_column_int(stmt, 5);
  9772. int percentage = sqlite3_column_int(stmt, 6);
  9773. int item_id = sqlite3_column_int(stmt, 7);
  9774. // int fashion_id = sqlite3_column_int(stmt, 8);
  9775. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9776. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9777. double percent = sqlite3_column_double(stmt, 11);
  9778. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9779. if ([price_null isEqualToString:@"null"]) {
  9780. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9781. }
  9782. NSMutableDictionary *item = @{
  9783. @"linenotes": line_note,
  9784. @"check": @(1),
  9785. @"product_id": product_id_string,
  9786. @"available_qty": @(qty),
  9787. @"available_percent" : @(percent),
  9788. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9789. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9790. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9791. @"img": img_path,
  9792. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9793. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9794. }.mutableCopy;
  9795. if (percentage) {
  9796. [item removeObjectForKey:@"available_qty"];
  9797. } else {
  9798. [item removeObjectForKey:@"available_percent"];
  9799. }
  9800. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9801. if ([qty_null isEqualToString:@"null"]) {
  9802. [item removeObjectForKey:@"available_qty"];
  9803. }
  9804. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9805. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9806. }];
  9807. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9808. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9809. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9810. }
  9811. [iSalesDB close_db:db];
  9812. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9813. [dic setValue:@"" forKey:@"email_content"];
  9814. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9815. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9816. return [RAConvertor dict2data:dic];
  9817. }
  9818. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9819. __block int qty = 0;
  9820. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9821. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9822. qty = sqlite3_column_int(stmt, 0);
  9823. }];
  9824. return qty;
  9825. }
  9826. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9827. {
  9828. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9829. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9830. // NSMutableDictionary * values = params[@"replaceValue"];
  9831. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9832. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9833. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9834. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9835. NSString *pdf_path = @"";
  9836. if (direct) {
  9837. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9838. } else {
  9839. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9840. }
  9841. NSString *create_user = [self valueInParams:params key:@"user"];
  9842. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9843. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9844. // model info
  9845. // 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];
  9846. // V1.90 more color
  9847. 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];
  9848. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9849. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9850. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9851. sqlite3 *db = [iSalesDB get_db];
  9852. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9853. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9854. int product_id = sqlite3_column_int(stmt, 0);
  9855. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9856. double price = sqlite3_column_double(stmt, 1);
  9857. double discount = sqlite3_column_double(stmt,2);
  9858. int qty = sqlite3_column_int(stmt, 3);
  9859. int percentage = sqlite3_column_int(stmt, 4);
  9860. int item_id = sqlite3_column_int(stmt, 5);
  9861. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9862. double percent = sqlite3_column_double(stmt, 7);
  9863. int more_color = sqlite3_column_int(stmt, 8);
  9864. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9865. /* if ([price_null isEqualToString:@"null"]) {
  9866. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9867. }
  9868. */
  9869. [product_ids_string appendFormat:@"%@,",product_id_string];
  9870. // Regular Price
  9871. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9872. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9873. // QTY
  9874. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9875. if ([qty_null isEqualToString:@"null"]) {
  9876. // 查available
  9877. qty = [self model_QTY:product_id_string db:db];
  9878. }
  9879. if (percentage) {
  9880. qty = qty * percent / 100;
  9881. }
  9882. // Special Price
  9883. if ([price_null isEqualToString:@"null"]) {
  9884. // price = regular price
  9885. price = [regular_price_str doubleValue];
  9886. }
  9887. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9888. if (![discount_null isEqualToString:@"null"]) {
  9889. price = price * (1 - discount / 100.0);
  9890. }
  9891. NSMutableDictionary *item = @{
  9892. @"line_note": line_note,
  9893. @"product_id": product_id_string,
  9894. @"available_qty": @(qty),
  9895. @"more_color":@(more_color),
  9896. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9897. @"regular_price" : regular_price_str,
  9898. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9899. }.mutableCopy;
  9900. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9901. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9902. }];
  9903. [iSalesDB close_db:db];
  9904. if (product_ids_string.length > 0) {
  9905. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9906. }
  9907. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9908. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9909. return [RAConvertor dict2data:resultDictionary];
  9910. }
  9911. NSString *model_info = [RAConvertor dict2string:dic];
  9912. // 创建PDF
  9913. // 在preview情况下保存,则不需要新建了
  9914. if (direct) {
  9915. NSMutableDictionary *tear_sheet_params = params;
  9916. // if (tear_sheet_id) {
  9917. // tear_sheet_params = values;
  9918. // }
  9919. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9920. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9921. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9922. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9923. resultDictionary = pdfInfo.mutableCopy;
  9924. } else { // 创建PDF失败
  9925. return pdfData;
  9926. }
  9927. } else {
  9928. // pdf_path 就是本地路径
  9929. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9930. }
  9931. // 将文件移动到PDF Cache文件夹
  9932. NSString *newPath = [pdf_path lastPathComponent];
  9933. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9934. NSString *cachefolder = [paths objectAtIndex:0];
  9935. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9936. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9937. NSFileManager *fileManager = [NSFileManager defaultManager];
  9938. NSError *error = nil;
  9939. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9940. if (error) { // 移动文件失败
  9941. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9942. return [RAConvertor dict2data:resultDictionary];
  9943. }
  9944. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9945. pdf_path = [newPath lastPathComponent];
  9946. // 保存信息
  9947. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9948. NSString *off_params = [RAConvertor dict2string:params];
  9949. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9950. pdf_path = [self translateSingleQuote:pdf_path];
  9951. create_user = [self translateSingleQuote:create_user];
  9952. tear_note = [self translateSingleQuote:tear_note];
  9953. tear_name = [self translateSingleQuote:tear_name];
  9954. model_info = [self translateSingleQuote:model_info];
  9955. configureParams = [self translateSingleQuote:configureParams];
  9956. off_params = [self translateSingleQuote:off_params];
  9957. 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];
  9958. if (tear_sheet_id) {
  9959. int _id = [tear_sheet_id intValue];
  9960. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9961. }
  9962. int result = [iSalesDB execSql:save_pdf_sql];
  9963. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9964. //
  9965. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9966. if (remove_Item) {
  9967. // portfolioId
  9968. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9969. }
  9970. return [RAConvertor dict2data:resultDictionary];
  9971. }
  9972. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9973. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  9974. }
  9975. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9976. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  9977. }
  9978. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9979. int offset = [[params valueForKey:@"offset"] intValue];
  9980. int limit = [[params valueForKey:@"limit"] intValue];
  9981. NSString *keyword = [params valueForKey:@"keyWord"];
  9982. NSString *where = @"where is_delete is null or is_delete = 0";
  9983. if (keyword.length) {
  9984. keyword = [self translateSingleQuote:keyword];
  9985. 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];
  9986. }
  9987. 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
  9988. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9989. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9990. NSString *cachefolder = [paths objectAtIndex:0];
  9991. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9992. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9993. NSString *name = [self textAtColumn:0 statement:stmt];
  9994. NSString *note = [self textAtColumn:1 statement:stmt];
  9995. NSString *time = [self textAtColumn:2 statement:stmt];
  9996. NSString *user = [self textAtColumn:3 statement:stmt];
  9997. NSString *path = [self textAtColumn:4 statement:stmt];
  9998. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9999. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10000. path = [pdfFolder stringByAppendingPathComponent:path];
  10001. BOOL bdir=NO;
  10002. NSFileManager* fileManager = [NSFileManager defaultManager];
  10003. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10004. {
  10005. //pdf文件不存在
  10006. path=nil;
  10007. }
  10008. time = [self changeDateTimeFormate:time];
  10009. time = [time stringByAppendingString:@" PST"];
  10010. int sheet_id = sqlite3_column_int(stmt, 5);
  10011. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10012. item[@"tearsheetsId"]=@(sheet_id);
  10013. item[@"pdf_path"]=path;
  10014. item[@"create_time"]=time;
  10015. item[@"create_user"]=user;
  10016. item[@"tear_note"]=note;
  10017. item[@"tear_name"]=name;
  10018. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10019. item[@"model_info"]=model_info;
  10020. item[@"off_params"]=off_params;
  10021. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10022. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10023. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10024. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10025. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10026. }];
  10027. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10028. return [RAConvertor dict2data:dic];
  10029. }
  10030. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10031. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10032. // NSString *user = [params objectForKey:@"user"];
  10033. // 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];
  10034. 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];
  10035. int result = [iSalesDB execSql:sql];
  10036. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10037. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10038. if (result == RESULT_TRUE) {
  10039. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10040. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10041. sqlite3 *db = [iSalesDB get_db];
  10042. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10043. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10044. [iSalesDB close_db:db];
  10045. }
  10046. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10047. return [RAConvertor dict2data:dic];
  10048. }
  10049. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10050. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10051. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10052. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10053. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10054. NSDictionary *company_item = @{
  10055. @"control": @"text",
  10056. @"keyboard": @"text",
  10057. @"name": @"company_name",
  10058. @"value": COMPANY_FULL_NAME,
  10059. @"aname": @"Company Name"
  10060. };
  10061. [section_0 setObject:company_item forKey:@"item_0"];
  10062. [dic setObject:section_0 forKey:@"section_0"];
  10063. // Regurlar Price
  10064. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10065. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10066. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10067. __block long val_count = 0;
  10068. NSString *sql = @"select name,type,order_by from price order by order_by";
  10069. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10070. NSString *name = [self textAtColumn:0 statement:stmt];
  10071. int type = sqlite3_column_int(stmt, 1);
  10072. int order_by = sqlite3_column_int(stmt, 2);
  10073. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10074. NSDictionary *price_dic = @{
  10075. @"value" : name,
  10076. @"value_id" : [NSNumber numberWithInteger:type],
  10077. @"check" : order_by == 0 ? @(1) : @(0)
  10078. };
  10079. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10080. val_count = ++(*count);
  10081. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10082. val_count = 0;
  10083. }];
  10084. [cadedate setObject:@{@"value" : @"None",
  10085. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10086. val_count++;
  10087. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10088. [price setObject:cadedate forKey:@"cadedate"];
  10089. [section1 setObject:price forKey:@"item_2"];
  10090. [dic setObject:section1 forKey:@"section_1"];
  10091. return [RAConvertor dict2data:dic];
  10092. }
  10093. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10094. NSString *product_id = [params objectForKey:@"fashionId"];
  10095. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10096. __block int result = RESULT_TRUE;
  10097. __block int qty = 0;
  10098. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10099. qty = sqlite3_column_int(stmt, 0);
  10100. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10101. result = RESULT_FALSE;
  10102. }];
  10103. NSMutableDictionary *dic = @{
  10104. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10105. @"mode" : @"Regular Mode",
  10106. @"quantity_available" : @(qty),
  10107. @"result" : @(result),
  10108. }.mutableCopy;
  10109. return [RAConvertor dict2data:dic];
  10110. }
  10111. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10112. NSString *item_ids = [params objectForKey:@"item_id"];
  10113. NSString *line_notes = [params objectForKey:@"notes"];
  10114. NSString *price_str = [params objectForKey:@"price"];
  10115. NSString *discount_str = [params objectForKey:@"discount"];
  10116. NSString *percent = [params objectForKey:@"available_percent"];
  10117. NSString *qty = [params objectForKey:@"available_qty"];
  10118. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10119. int dot = 0;
  10120. if (line_notes) {
  10121. line_notes = [self translateSingleQuote:line_notes];
  10122. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10123. sql = [sql stringByAppendingString:line_notes];
  10124. dot = 1;
  10125. } else {
  10126. line_notes = @"";
  10127. }
  10128. if (price_str) {
  10129. if (dot) {
  10130. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10131. } else {
  10132. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10133. dot = 1;
  10134. }
  10135. sql = [sql stringByAppendingString:price_str];
  10136. } else {
  10137. price_str = @"";
  10138. }
  10139. if (discount_str) {
  10140. if (dot) {
  10141. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10142. } else {
  10143. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10144. dot = 1;
  10145. }
  10146. sql = [sql stringByAppendingString:discount_str];
  10147. } else {
  10148. discount_str = @"";
  10149. }
  10150. if (percent) {
  10151. if (dot) {
  10152. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10153. } else {
  10154. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10155. dot = 1;
  10156. }
  10157. sql = [sql stringByAppendingString:percent];
  10158. } else {
  10159. percent = @"";
  10160. }
  10161. if (qty) {
  10162. if (dot) {
  10163. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10164. } else {
  10165. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10166. dot = 1;
  10167. }
  10168. sql = [sql stringByAppendingString:qty];
  10169. } else {
  10170. qty = @"";
  10171. }
  10172. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10173. sql = [sql stringByAppendingString:where];
  10174. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10175. int result = [iSalesDB execSql:sql];
  10176. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10177. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10178. return [RAConvertor dict2data:dic];
  10179. }
  10180. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10181. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10182. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10183. sqlite3 *db = [iSalesDB get_db];
  10184. 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];
  10185. int result = [iSalesDB execSql:sql db:db];
  10186. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10187. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10188. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10189. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10190. [iSalesDB close_db:db];
  10191. return [RAConvertor dict2data:dic];
  10192. }
  10193. + (void)offline_removePDFWithName:(NSString *)name {
  10194. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10195. NSString *cachefolder = [paths objectAtIndex:0];
  10196. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10197. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10198. NSFileManager *fileManager = [NSFileManager defaultManager];
  10199. [fileManager removeItemAtPath:path error:nil];
  10200. }
  10201. + (void)offline_clear_PDFCache {
  10202. NSFileManager *fileManager = [NSFileManager defaultManager];
  10203. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10204. NSString *cachefolder = [paths objectAtIndex:0];
  10205. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10206. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10207. for (NSString *path in pdf_files) {
  10208. [self offline_removePDFWithName:[path lastPathComponent]];
  10209. }
  10210. }
  10211. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10212. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10213. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10214. NSString *user = [params objectForKey:@"user"];
  10215. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10216. if (![create_user isEqualToString:user]) {
  10217. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10218. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10219. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10220. return [RAConvertor dict2data:dic];
  10221. }
  10222. 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]];
  10223. __block int is_local = 0;
  10224. __block NSString *path = @"";
  10225. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10226. is_local = sqlite3_column_int(stmt, 0);
  10227. path = [self textAtColumn:1 statement:stmt];
  10228. }];
  10229. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10230. if (is_local == 1) {
  10231. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10232. // 删除文件
  10233. [self offline_removePDFWithName:path];
  10234. }
  10235. int result = [iSalesDB execSql:sql];
  10236. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10237. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10238. return [RAConvertor dict2data:dic];
  10239. }
  10240. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10241. {
  10242. // assert(add_params[@"contact_id"]!=nil);
  10243. assert(add_params[@"user"]!=nil);
  10244. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10245. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10246. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10247. NSString* where=@"1=1";
  10248. if (ver!=nil) {
  10249. where=@"is_dirty=1";
  10250. }
  10251. 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];
  10252. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10253. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10254. sqlite3 *db = [iSalesDB get_db];
  10255. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10256. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10257. NSInteger _id = sqlite3_column_int(stmt, 0);
  10258. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10259. NSString *name = [self textAtColumn:2 statement:stmt];
  10260. NSString *desc = [self textAtColumn:3 statement:stmt];
  10261. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10262. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10263. NSInteger qty = sqlite3_column_int(stmt, 6);
  10264. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10265. double percent = sqlite3_column_double(stmt, 8);
  10266. double price = sqlite3_column_double(stmt, 9);
  10267. double discount = sqlite3_column_double(stmt, 10);
  10268. NSString *img = [self textAtColumn:11 statement:stmt];
  10269. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10270. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10271. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10272. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10273. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10274. if ([price_null isEqualToString:@"null"]) {
  10275. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10276. }
  10277. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10278. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10279. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10280. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10281. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10282. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10283. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10284. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10285. if ([qty_null isEqualToString:@"null"]) {
  10286. // [item setValue:@"null" forKey:@"available_qty"];
  10287. } else {
  10288. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10289. }
  10290. if ([is_percent_null isEqualToString:@"null"]) {
  10291. // [item setValue:@"null" forKey:@"percentage"];
  10292. } else {
  10293. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10294. }
  10295. if ([percent_null isEqualToString:@"null"]) {
  10296. // [item setValue:@"null" forKey:@"percent"];
  10297. } else {
  10298. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10299. }
  10300. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10301. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10302. [item setValue:name forKey:@"name"];
  10303. [item setValue:desc forKey:@"description"];
  10304. [item setValue:img forKey:@"img"];
  10305. [item setValue:line_note forKey:@"line_note"];
  10306. [item setValue:create_time forKey:@"createtime"];
  10307. [item setValue:modify_time forKey:@"modifytime"];
  10308. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10309. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10310. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10311. }];
  10312. [iSalesDB close_db:db];
  10313. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10314. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10315. }
  10316. return ret;
  10317. }
  10318. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10319. {
  10320. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10321. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10322. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10323. NSString* where=@"1=1";
  10324. if (ver!=nil) {
  10325. where=@"is_dirty=1";
  10326. }
  10327. 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];
  10328. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10329. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10330. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10331. NSInteger _id = sqlite3_column_int(stmt, 0);
  10332. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10333. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10334. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10335. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10336. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10337. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10338. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10339. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10340. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10341. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10342. NSString *uuid = [NSUUID UUID].UUIDString;
  10343. int is_delete = sqlite3_column_int(stmt, 11);
  10344. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10345. int is_local = sqlite3_column_int(stmt, 12);
  10346. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10347. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10348. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10349. }
  10350. [item setObject:pdf_path forKey:@"pdf_path"];
  10351. [item setObject:create_user forKey:@"create_user"];
  10352. [item setObject:tear_note forKey:@"tear_note"];
  10353. [item setObject:tear_name forKey:@"tear_name"];
  10354. [item setObject:model_info forKey:@"model_info"];
  10355. [item setObject:createtime forKey:@"createtime"];
  10356. [item setObject:modifytime forKey:@"modifytime"];
  10357. [item setObject:urlParams forKey:@"urlParams"];
  10358. [item setObject:off_params forKey:@"off_params"];
  10359. [item setObject:uuid forKey:@"pdf_token"];
  10360. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10361. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10362. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10363. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10364. } else {
  10365. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10366. }
  10367. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10368. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10369. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10370. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10371. }];
  10372. return ret;
  10373. }
  10374. #pragma mark 2020
  10375. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10376. {
  10377. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10378. sqlite3 *db = [iSalesDB get_db];
  10379. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10380. if (contactType) {
  10381. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10382. } else {
  10383. contactType = @"1 = 1";
  10384. }
  10385. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10386. DebugLog(@"offline contact list keyword: %@",keyword);
  10387. // advanced search
  10388. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10389. if (contact_name) {
  10390. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10391. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10392. } else {
  10393. contact_name = @"";
  10394. }
  10395. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10396. if (customer_phone) {
  10397. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10398. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10399. } else {
  10400. customer_phone = @"";
  10401. }
  10402. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10403. if (customer_fax) {
  10404. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10405. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10406. } else {
  10407. customer_fax = @"";
  10408. }
  10409. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10410. if (customer_zipcode) {
  10411. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10412. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10413. } else {
  10414. customer_zipcode = @"";
  10415. }
  10416. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10417. if (customer_sales_rep) {
  10418. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10419. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10420. } else {
  10421. customer_sales_rep = @"";
  10422. }
  10423. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10424. if (customer_state) {
  10425. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10426. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10427. } else {
  10428. customer_state = @"";
  10429. }
  10430. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10431. if (customer_name) {
  10432. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10433. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10434. } else {
  10435. customer_name = @"";
  10436. }
  10437. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10438. if (customer_country) {
  10439. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10440. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10441. } else {
  10442. customer_country = @"";
  10443. }
  10444. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10445. if (customer_cid) {
  10446. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10447. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10448. } else {
  10449. customer_cid = @"";
  10450. }
  10451. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10452. if (customer_city) {
  10453. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10454. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  10455. } else {
  10456. customer_city = @"";
  10457. }
  10458. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  10459. if (customer_address) {
  10460. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10461. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  10462. } else {
  10463. customer_address = @"";
  10464. }
  10465. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  10466. if (customer_email) {
  10467. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10468. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  10469. } else {
  10470. customer_email = @"";
  10471. }
  10472. NSString *price_name = [params valueForKey:@"price_name"];
  10473. if (price_name) {
  10474. if ([price_name containsString:@","]) {
  10475. // 首先从 price表中查处name
  10476. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  10477. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  10478. for (int i = 1;i < pArray.count;i++) {
  10479. NSString *p = pArray[i];
  10480. [mutablePStr appendFormat:@" or type = %@ ",p];
  10481. }
  10482. [mutablePStr appendString:@";"];
  10483. __block NSMutableArray *price_name_array = [NSMutableArray array];
  10484. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10485. char *name = (char *)sqlite3_column_text(stmt, 0);
  10486. if (!name)
  10487. name = "";
  10488. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  10489. }];
  10490. // 再根据name 拼sql
  10491. NSMutableString *mutable_price_name = [NSMutableString string];
  10492. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  10493. for (int i = 1; i < price_name_array.count; i++) {
  10494. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  10495. }
  10496. [mutable_price_name appendString:@")"];
  10497. price_name = mutable_price_name;
  10498. } else {
  10499. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  10500. if ([price_name isEqualToString:@""]) {
  10501. price_name = @"";
  10502. } else {
  10503. __block NSString *price;
  10504. price_name = [self translateSingleQuote:price_name];
  10505. [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) {
  10506. char *p = (char *)sqlite3_column_text(stmt, 0);
  10507. if (p == NULL) {
  10508. p = "";
  10509. }
  10510. price = [NSString stringWithUTF8String:p];
  10511. }];
  10512. if ([price isEqualToString:@""]) {
  10513. price_name = @"";
  10514. } else {
  10515. price = [self translateSingleQuote:price];
  10516. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  10517. }
  10518. }
  10519. }
  10520. } else {
  10521. price_name = @"";
  10522. }
  10523. int limit = [[params valueForKey:@"limit"] intValue];
  10524. int offset = [[params valueForKey:@"offset"] intValue];
  10525. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10526. 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];
  10527. 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];
  10528. // int result= [iSalesDB AddExFunction:db];
  10529. int count =0;
  10530. NSString *sqlQuery = nil;
  10531. if(keyword.length==0)
  10532. {
  10533. // 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];
  10534. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  10535. sqlQuery = sql;
  10536. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  10537. }
  10538. else
  10539. {
  10540. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  10541. keyword = keyword.lowercaseString;
  10542. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10543. 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];
  10544. 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]];
  10545. }
  10546. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10547. sqlite3_stmt * statement;
  10548. [ret setValue:@"2" forKey:@"result"];
  10549. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  10550. // 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";
  10551. int i = 0;
  10552. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10553. {
  10554. while (sqlite3_step(statement) == SQLITE_ROW)
  10555. {
  10556. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10557. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10558. int editable = sqlite3_column_int(statement, 0);
  10559. char *company_name = (char*)sqlite3_column_text(statement, 1);
  10560. NSString *nscompany_name =nil;
  10561. if(company_name==nil)
  10562. nscompany_name=@"";
  10563. else
  10564. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  10565. char *country = (char*)sqlite3_column_text(statement, 2);
  10566. if(country==nil)
  10567. country="";
  10568. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10569. // char *addr = (char*)sqlite3_column_text(statement, 3);
  10570. // if(addr==nil)
  10571. // addr="";
  10572. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10573. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10574. if(zipcode==nil)
  10575. zipcode="";
  10576. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10577. char *state = (char*)sqlite3_column_text(statement, 5);
  10578. if(state==nil)
  10579. state="";
  10580. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10581. char *city = (char*)sqlite3_column_text(statement, 6);
  10582. if(city==nil)
  10583. city="";
  10584. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10585. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10586. // NSString *nscontact_name = nil;
  10587. // if(contact_name==nil)
  10588. // nscontact_name=@"";
  10589. // else
  10590. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10591. char *phone = (char*)sqlite3_column_text(statement, 8);
  10592. NSString *nsphone = nil;
  10593. if(phone==nil)
  10594. nsphone=@"";
  10595. else
  10596. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10597. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10598. if(contact_id==nil)
  10599. contact_id="";
  10600. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10601. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10602. if(addr_1==nil)
  10603. addr_1="";
  10604. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10605. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10606. if(addr_2==nil)
  10607. addr_2="";
  10608. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10609. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10610. if(addr_3==nil)
  10611. addr_3="";
  10612. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10613. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10614. if(addr_4==nil)
  10615. addr_4="";
  10616. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10617. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10618. if(first_name==nil)
  10619. first_name="";
  10620. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10621. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10622. if(last_name==nil)
  10623. last_name="";
  10624. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10625. char *fax = (char*)sqlite3_column_text(statement, 16);
  10626. NSString *nsfax = nil;
  10627. if(fax==nil)
  10628. nsfax=@"";
  10629. else
  10630. {
  10631. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10632. if(nsfax.length>0)
  10633. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10634. }
  10635. char *email = (char*)sqlite3_column_text(statement, 17);
  10636. NSString *nsemail = nil;
  10637. if(email==nil)
  10638. nsemail=@"";
  10639. else
  10640. {
  10641. nsemail= [[NSString alloc]initWithUTF8String:email];
  10642. if(nsemail.length>0)
  10643. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10644. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10645. }
  10646. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10647. [arr_name addObject:nsfirst_name];
  10648. [arr_name addObject:nslast_name];
  10649. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10650. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10651. {
  10652. // decrypt
  10653. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10654. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10655. nsphone=[AESCrypt fastdecrypt:nsphone];
  10656. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10657. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10658. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10659. }
  10660. [arr_addr addObject:nscompany_name];
  10661. [arr_addr addObject:nscontact_name];
  10662. [arr_addr addObject:@"<br>"];
  10663. [arr_addr addObject:nsaddr_1];
  10664. [arr_addr addObject:nsaddr_2];
  10665. [arr_addr addObject:nsaddr_3];
  10666. [arr_addr addObject:nsaddr_4];
  10667. //[arr_addr addObject:nsaddr];
  10668. [arr_addr addObject:nszipcode];
  10669. [arr_addr addObject:nscity];
  10670. [arr_addr addObject:nsstate];
  10671. [arr_addr addObject:nscountry];
  10672. [arr_addr addObject:@"<br>"];
  10673. [arr_addr addObject:nsphone];
  10674. [arr_addr addObject:nsfax];
  10675. [arr_addr addObject:nsemail];
  10676. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10677. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10678. [item setValue:name forKey:@"name"];
  10679. [item setValue:nscontact_id forKey:@"contact_id"];
  10680. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10681. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10682. i++;
  10683. }
  10684. sqlite3_finalize(statement);
  10685. }
  10686. [iSalesDB close_db:db];
  10687. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10688. dispatch_async(dispatch_get_main_queue(), ^{
  10689. UIApplication * app = [UIApplication sharedApplication];
  10690. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10691. [ret setValue:appDelegate.mode forKey:@"mode"];
  10692. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10693. result(ret);
  10694. });
  10695. return ;
  10696. });
  10697. }
  10698. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10699. //{
  10700. //
  10701. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10702. //
  10703. // NSString* contactId = [params valueForKey:@"contactId"];
  10704. //
  10705. //
  10706. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10707. //
  10708. //
  10709. //
  10710. //
  10711. //
  10712. //
  10713. // sqlite3 *db = [iSalesDB get_db];
  10714. //
  10715. //
  10716. //
  10717. //
  10718. //
  10719. // NSString *sqlQuery = nil;
  10720. //
  10721. //
  10722. // {
  10723. // 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];
  10724. //
  10725. // }
  10726. //
  10727. //
  10728. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10729. // sqlite3_stmt * statement;
  10730. //
  10731. //
  10732. // [ret setValue:@"2" forKey:@"result"];
  10733. //
  10734. //
  10735. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10736. // {
  10737. //
  10738. // //int i = 0;
  10739. // if (sqlite3_step(statement) == SQLITE_ROW)
  10740. // {
  10741. //
  10742. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10743. //
  10744. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10745. //
  10746. //
  10747. // // int editable = sqlite3_column_int(statement, 0);
  10748. //
  10749. //
  10750. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10751. // NSString *nscompany_name =nil;
  10752. // if(company_name==nil)
  10753. // nscompany_name=@"";
  10754. // else
  10755. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10756. //
  10757. //
  10758. // char *country = (char*)sqlite3_column_text(statement, 2);
  10759. // if(country==nil)
  10760. // country="";
  10761. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10762. //
  10763. //
  10764. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10765. // // if(addr==nil)
  10766. // // addr="";
  10767. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10768. //
  10769. //
  10770. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10771. // if(zipcode==nil)
  10772. // zipcode="";
  10773. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10774. //
  10775. //
  10776. // char *state = (char*)sqlite3_column_text(statement, 5);
  10777. // if(state==nil)
  10778. // state="";
  10779. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10780. //
  10781. // char *city = (char*)sqlite3_column_text(statement, 6);
  10782. // if(city==nil)
  10783. // city="";
  10784. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10785. //
  10786. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10787. // // NSString *nscontact_name = nil;
  10788. // // if(contact_name==nil)
  10789. // // nscontact_name=@"";
  10790. // // else
  10791. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10792. //
  10793. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10794. // NSString *nsphone = nil;
  10795. // if(phone==nil)
  10796. // nsphone=@"";
  10797. // else
  10798. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10799. //
  10800. //
  10801. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10802. // // if(contact_id==nil)
  10803. // // contact_id="";
  10804. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10805. //
  10806. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10807. // if(addr_1==nil)
  10808. // addr_1="";
  10809. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10810. //
  10811. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10812. // if(addr_2==nil)
  10813. // addr_2="";
  10814. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10815. //
  10816. //
  10817. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10818. // if(addr_3==nil)
  10819. // addr_3="";
  10820. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10821. //
  10822. //
  10823. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10824. // if(addr_4==nil)
  10825. // addr_4="";
  10826. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10827. //
  10828. //
  10829. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10830. // if(first_name==nil)
  10831. // first_name="";
  10832. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10833. //
  10834. //
  10835. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10836. // if(last_name==nil)
  10837. // last_name="";
  10838. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10839. //
  10840. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10841. // NSString *nsfax = nil;
  10842. // if(fax==nil)
  10843. // nsfax=@"";
  10844. // else
  10845. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10846. //
  10847. // char *email = (char*)sqlite3_column_text(statement, 17);
  10848. // NSString *nsemail = nil;
  10849. // if(email==nil)
  10850. // nsemail=@"";
  10851. // else
  10852. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10853. //
  10854. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10855. // NSString *nsimg_0 = nil;
  10856. // if(img_0==nil)
  10857. // nsimg_0=@"";
  10858. // else
  10859. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10860. //
  10861. // [self copy_bcardImg:nsimg_0];
  10862. //
  10863. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10864. // NSString *nsimg_1 = nil;
  10865. // if(img_1==nil)
  10866. // nsimg_1=@"";
  10867. // else
  10868. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10869. // [self copy_bcardImg:nsimg_1];
  10870. //
  10871. //
  10872. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10873. // NSString *nsimg_2 = nil;
  10874. // if(img_2==nil)
  10875. // nsimg_2=@"";
  10876. // else
  10877. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10878. // [self copy_bcardImg:nsimg_2];
  10879. //
  10880. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10881. // NSString *nsprice_type = nil;
  10882. // if(price_type==nil)
  10883. // nsprice_type=@"";
  10884. // else
  10885. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10886. //
  10887. //
  10888. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10889. // NSString *nsnotes = nil;
  10890. // if(notes==nil)
  10891. // nsnotes=@"";
  10892. // else
  10893. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10894. //
  10895. //
  10896. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10897. // NSString *nssalesrep = nil;
  10898. // if(salesrep==nil)
  10899. // nssalesrep=@"";
  10900. // else
  10901. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10902. //
  10903. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10904. //
  10905. //
  10906. // {
  10907. // // decrypt
  10908. //
  10909. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10910. //
  10911. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10912. //
  10913. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10914. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10915. //
  10916. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10917. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10918. //
  10919. // }
  10920. //
  10921. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10922. //
  10923. // [arr_name addObject:nsfirst_name];
  10924. // [arr_name addObject:nslast_name];
  10925. //
  10926. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10927. //
  10928. //
  10929. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10930. //
  10931. // [arr_ext addObject:nsaddr_1];
  10932. // [arr_ext addObject:nsaddr_2];
  10933. // [arr_ext addObject:nsaddr_3];
  10934. // [arr_ext addObject:nsaddr_4];
  10935. // [arr_ext addObject:@"\r\n"];
  10936. //
  10937. // [arr_ext addObject:nscity];
  10938. // [arr_ext addObject:nsstate];
  10939. // [arr_ext addObject:nszipcode];
  10940. // [arr_ext addObject:nscountry];
  10941. //
  10942. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10943. //
  10944. //
  10945. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10946. //
  10947. //
  10948. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10949. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10950. // [item setValue:nscountry forKey:@"customer_country"];
  10951. // [item setValue:nsphone forKey:@"customer_phone"];
  10952. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10953. // [item setValue:nscompany_name forKey:@"customer_name"];
  10954. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10955. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10956. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10957. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10958. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10959. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10960. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10961. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  10962. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  10963. // [item setValue:nslast_name forKey:@"customer_last_name"];
  10964. // [item setValue:nscity forKey:@"customer_city"];
  10965. // [item setValue:nsstate forKey:@"customer_state"];
  10966. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  10967. // [item setValue:contactId forKey:@"customer_cid"];
  10968. // [item setValue:nscontact_name forKey:@"customer_contact"];
  10969. // [item setValue:nsfax forKey:@"customer_fax"];
  10970. // [item setValue:nsemail forKey:@"customer_email"];
  10971. // [item setValue:contact_type forKey:@"customer_contact_type"];
  10972. //
  10973. //
  10974. // [ret setObject:item forKey:@"customerInfo"];
  10975. // // i++;
  10976. //
  10977. //
  10978. //
  10979. // }
  10980. //
  10981. //
  10982. //
  10983. //
  10984. // sqlite3_finalize(statement);
  10985. // }
  10986. //
  10987. //
  10988. //
  10989. // [iSalesDB close_db:db];
  10990. //
  10991. //
  10992. //
  10993. //
  10994. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10995. // dispatch_async(dispatch_get_main_queue(), ^{
  10996. // UIApplication * app = [UIApplication sharedApplication];
  10997. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10998. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10999. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11000. // result(ret);
  11001. // });
  11002. //
  11003. // return ;
  11004. // });
  11005. //}
  11006. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11007. {
  11008. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11009. NSString* order_code= appDelegate.order_code;
  11010. NSString* user = appDelegate.user;
  11011. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11012. NSString* category = [params valueForKey:@"category"];
  11013. if (!category || [category isEqualToString:@""]) {
  11014. category = @"%";
  11015. }
  11016. category = [self translateSingleQuote:category];
  11017. int limit = [[params valueForKey:@"limit"] intValue];
  11018. int offset = [[params valueForKey:@"offset"] intValue];
  11019. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11020. NSString *limit_str = @"";
  11021. if (limited) {
  11022. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11023. }
  11024. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11025. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11026. sqlite3 *db = [iSalesDB get_db];
  11027. // [iSalesDB AddExFunction:db];
  11028. int count;
  11029. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11030. 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];
  11031. double price_min = 0;
  11032. double price_max = 0;
  11033. if ([params.allKeys containsObject:@"alert"]) {
  11034. // alert
  11035. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11036. NSString *alert = params[@"alert"];
  11037. if ([alert isEqualToString:@"Display All"]) {
  11038. alert = [NSString stringWithFormat:@""];
  11039. } else {
  11040. alert = [self translateSingleQuote:alert];
  11041. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11042. }
  11043. // available
  11044. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11045. NSString *available = params[@"available"];
  11046. NSString *available_condition;
  11047. if ([available isEqualToString:@"Display All"]) {
  11048. available_condition = @"";
  11049. } else if ([available isEqualToString:@"Available Now"]) {
  11050. available_condition = @"and availability > 0";
  11051. } else {
  11052. available_condition = @"and availability == 0";
  11053. }
  11054. // best seller
  11055. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11056. NSString *best_seller = @"";
  11057. NSString *order_best_seller = @"m.name asc";
  11058. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11059. best_seller = @"and best_seller > 0";
  11060. order_best_seller = @"m.best_seller desc,m.name asc";
  11061. }
  11062. // price
  11063. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11064. NSString *price = params[@"price"];
  11065. price_min = 0;
  11066. price_max = MAXFLOAT;
  11067. if (user && price != nil) {
  11068. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11069. NSMutableString *priceName = [NSMutableString string];
  11070. for (int i = 0; i < priceTypeArray.count; i++) {
  11071. NSString *pricetype = priceTypeArray[i];
  11072. pricetype = [self translateSingleQuote:pricetype];
  11073. if (i == 0) {
  11074. [priceName appendFormat:@"'%@'",pricetype];
  11075. } else {
  11076. [priceName appendFormat:@",'%@'",pricetype];
  11077. }
  11078. }
  11079. if ([price isEqualToString:@"Display All"]) {
  11080. price = [NSString stringWithFormat:@""];
  11081. } else if([price containsString:@"+"]){
  11082. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11083. price_min = [price doubleValue];
  11084. 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];
  11085. } else {
  11086. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11087. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11088. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11089. 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];
  11090. }
  11091. } else {
  11092. price = @"";
  11093. }
  11094. // sold_by_qty : Sold in quantities of %@
  11095. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11096. NSString *qty = params[@"sold_by_qty"];
  11097. if ([qty isEqualToString:@"Display All"]) {
  11098. qty = @"";
  11099. } else {
  11100. qty = [self translateSingleQuote:qty];
  11101. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11102. }
  11103. // cate
  11104. // category = [self translateSingleQuote:category];
  11105. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11106. // cate mutiple selection
  11107. NSString *category_id = params[@"category"];
  11108. NSMutableArray *cate_id_array = nil;
  11109. NSMutableString *cateWhere = [NSMutableString string];
  11110. if ([category_id isEqualToString:@""] || !category_id) {
  11111. [cateWhere appendString:@"1 = 1"];
  11112. } else {
  11113. if ([category_id containsString:@","]) {
  11114. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11115. } else {
  11116. cate_id_array = [@[category_id] mutableCopy];
  11117. }
  11118. [cateWhere appendString:@"("];
  11119. for (int i = 0; i < cate_id_array.count; i++) {
  11120. if (i == 0) {
  11121. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11122. } else {
  11123. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11124. }
  11125. }
  11126. [cateWhere appendString:@")"];
  11127. }
  11128. // where bestseller > 0 order by bestseller desc
  11129. // sql query: alert availability(int) best_seller(int) price qty
  11130. 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];
  11131. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11132. }
  11133. DebugLog(@"offline category where: %@",where);
  11134. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11135. if (!user) {
  11136. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11137. }
  11138. [ret setValue:filter forKey:@"filter"];
  11139. DebugLog(@"offline_category sql:%@",sqlQuery);
  11140. sqlite3_stmt * statement;
  11141. [ret setValue:@"2" forKey:@"result"];
  11142. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11143. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11144. // int count=0;
  11145. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11146. {
  11147. int i=0;
  11148. while (sqlite3_step(statement) == SQLITE_ROW)
  11149. {
  11150. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11151. char *name = (char*)sqlite3_column_text(statement, 0);
  11152. if(name==nil)
  11153. name="";
  11154. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11155. char *description = (char*)sqlite3_column_text(statement, 1);
  11156. if(description==nil)
  11157. description="";
  11158. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11159. int product_id = sqlite3_column_int(statement, 2);
  11160. int wid = sqlite3_column_int(statement, 3);
  11161. int closeout = sqlite3_column_int(statement, 4);
  11162. int cid = sqlite3_column_int(statement, 5);
  11163. int wisdelete = sqlite3_column_int(statement, 6);
  11164. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11165. int more_color = sqlite3_column_int(statement, 8);
  11166. // Defaul Category ID
  11167. __block NSString *categoryID = nil;
  11168. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11169. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11170. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11171. if(default_category==nil)
  11172. default_category="";
  11173. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11174. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11175. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11176. categoryID = nsdefault_category;
  11177. }];
  11178. if (!categoryID.length) {
  11179. NSString *cateIDs = params[@"category"];
  11180. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11181. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11182. for (NSString *cateID in requestCategoryArr) {
  11183. BOOL needBreak = NO;
  11184. for (NSString *itemCateIDBox in itemCategoryArr) {
  11185. if (itemCateIDBox.length > 4) {
  11186. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11187. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11188. if ([itemCategoryID isEqualToString:cateID]) {
  11189. needBreak = YES;
  11190. categoryID = itemCategoryID;
  11191. break;
  11192. }
  11193. }
  11194. }
  11195. if (needBreak) {
  11196. break;
  11197. }
  11198. }
  11199. }
  11200. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11201. if(wid !=0 && wisdelete != 1)
  11202. [item setValue:@"true" forKey:@"wish_exists"];
  11203. else
  11204. [item setValue:@"false" forKey:@"wish_exists"];
  11205. if(closeout==0)
  11206. [item setValue:@"false" forKey:@"is_closeout"];
  11207. else
  11208. [item setValue:@"true" forKey:@"is_closeout"];
  11209. if(cid==0)
  11210. [item setValue:@"false" forKey:@"cart_exists"];
  11211. else
  11212. [item setValue:@"true" forKey:@"cart_exists"];
  11213. if (more_color == 0) {
  11214. [item setObject:@(false) forKey:@"more_color"];
  11215. } else if (more_color == 1) {
  11216. [item setObject:@(true) forKey:@"more_color"];
  11217. }
  11218. [item addEntriesFromDictionary:imgjson];
  11219. // [item setValue:nsurl forKey:@"img"];
  11220. [item setValue:nsname forKey:@"name"];
  11221. [item setValue:nsdescription forKey:@"description"];
  11222. if (categoryID) {
  11223. [item setValue:categoryID forKey:@"item_category_id"];
  11224. }
  11225. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11226. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11227. i++;
  11228. }
  11229. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11230. [ret setObject:items forKey:@"items"];
  11231. sqlite3_finalize(statement);
  11232. } else {
  11233. DebugLog(@"nothing...");
  11234. }
  11235. DebugLog(@"count:%d",count);
  11236. [iSalesDB close_db:db];
  11237. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11238. dispatch_async(dispatch_get_main_queue(), ^{
  11239. // UIApplication * app = [UIApplication sharedApplication];
  11240. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11241. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11242. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11243. result(ret);
  11244. });
  11245. });
  11246. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11247. }
  11248. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11249. {
  11250. [self categoryList:params limited:YES completionHandler:result];
  11251. }
  11252. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11253. {
  11254. UIApplication * app = [UIApplication sharedApplication];
  11255. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11256. NSString* order_code = appDelegate.order_code;
  11257. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11258. NSString* keyword = [params valueForKey:@"keyword"];
  11259. keyword=keyword.lowercaseString;
  11260. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11261. int limit = [[params valueForKey:@"limit"] intValue];
  11262. int offset = [[params valueForKey:@"offset"] intValue];
  11263. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11264. NSString *limit_str = @"";
  11265. if (limited) {
  11266. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11267. }
  11268. sqlite3 *db = [iSalesDB get_db];
  11269. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11270. NSString *sqlQuery = nil;
  11271. if(exactMatch )
  11272. 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 ;
  11273. else
  11274. 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
  11275. DebugLog(@"offline_search sql:%@",sqlQuery);
  11276. sqlite3_stmt * statement;
  11277. [ret setValue:@"2" forKey:@"result"];
  11278. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11279. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11280. // int count=0;
  11281. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11282. {
  11283. int i=0;
  11284. while (sqlite3_step(statement) == SQLITE_ROW)
  11285. {
  11286. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11287. // char *name = (char*)sqlite3_column_text(statement, 1);
  11288. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11289. char *name = (char*)sqlite3_column_text(statement, 0);
  11290. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11291. char *description = (char*)sqlite3_column_text(statement, 1);
  11292. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11293. int product_id = sqlite3_column_int(statement, 2);
  11294. // char *url = (char*)sqlite3_column_text(statement, 3);
  11295. // if(url==nil)
  11296. // url="";
  11297. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11298. int wid = sqlite3_column_int(statement, 3);
  11299. int closeout = sqlite3_column_int(statement, 4);
  11300. int cid = sqlite3_column_int(statement, 5);
  11301. int wisdelete = sqlite3_column_int(statement, 6);
  11302. int more_color = sqlite3_column_int(statement, 7);
  11303. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11304. if(wid !=0 && wisdelete != 1)
  11305. [item setValue:@"true" forKey:@"wish_exists"];
  11306. else
  11307. [item setValue:@"false" forKey:@"wish_exists"];
  11308. if(closeout==0)
  11309. [item setValue:@"false" forKey:@"is_closeout"];
  11310. else
  11311. [item setValue:@"true" forKey:@"is_closeout"];
  11312. if(cid==0)
  11313. [item setValue:@"false" forKey:@"cart_exists"];
  11314. else
  11315. [item setValue:@"true" forKey:@"cart_exists"];
  11316. if (more_color == 0) {
  11317. [item setObject:@(false) forKey:@"more_color"];
  11318. } else if (more_color == 1) {
  11319. [item setObject:@(true) forKey:@"more_color"];
  11320. }
  11321. [item addEntriesFromDictionary:imgjson];
  11322. // [item setValue:nsurl forKey:@"img"];
  11323. [item setValue:nsname forKey:@"fash_name"];
  11324. [item setValue:nsdescription forKey:@"description"];
  11325. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11326. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11327. i++;
  11328. }
  11329. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11330. [ret setObject:items forKey:@"items"];
  11331. sqlite3_finalize(statement);
  11332. }
  11333. DebugLog(@"count:%d",count);
  11334. [iSalesDB close_db:db];
  11335. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11336. dispatch_async(dispatch_get_main_queue(), ^{
  11337. result(ret);
  11338. });
  11339. });
  11340. }
  11341. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11342. {
  11343. [self search:params limited:YES completionHandler:result];
  11344. }
  11345. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11346. //{
  11347. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11348. // NSString* order_code = appDelegate.order_code;
  11349. // NSString* user = appDelegate.user;
  11350. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11351. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11352. //
  11353. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11354. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11355. //
  11356. // // category
  11357. // NSDictionary *category_menu = [self offline_category_menu];
  11358. // [filter setValue:category_menu forKey:@"category"];
  11359. //
  11360. // NSString* where= nil;
  11361. // NSString* orderby= @"m.name";
  11362. // if (!filterSearch) {
  11363. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11364. //
  11365. // switch (covertype) {
  11366. // case 0:
  11367. // {
  11368. // where=@"m.category like'%%#005#%%'";
  11369. // break;
  11370. // }
  11371. // case 1:
  11372. // {
  11373. // where=@"m.alert like '%QS%'";
  11374. // break;
  11375. // }
  11376. // case 2:
  11377. // {
  11378. // where=@"m.availability>0";
  11379. // break;
  11380. // }
  11381. // case 3:
  11382. // {
  11383. // where=@"m.best_seller>0";
  11384. // orderby=@"m.best_seller desc,m.name asc";
  11385. // break;
  11386. // }
  11387. // default:
  11388. // where=@"1=1";
  11389. // break;
  11390. // }
  11391. //
  11392. // }
  11393. //
  11394. //
  11395. // int limit = [[params valueForKey:@"limit"] intValue];
  11396. // int offset = [[params valueForKey:@"offset"] intValue];
  11397. //
  11398. // NSString *limit_str = @"";
  11399. // if (limited) {
  11400. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11401. // }
  11402. //
  11403. //
  11404. // sqlite3 *db = [iSalesDB get_db];
  11405. // // [iSalesDB AddExFunction:db];
  11406. //
  11407. // int count;
  11408. //
  11409. // NSString *sqlQuery = nil;
  11410. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11411. // 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];
  11412. //
  11413. //
  11414. // double price_min = 0;
  11415. // double price_max = 0;
  11416. // if (filterSearch) {
  11417. // // alert
  11418. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11419. // NSString *alert = params[@"alert"];
  11420. // if ([alert isEqualToString:@"Display All"]) {
  11421. // alert = [NSString stringWithFormat:@""];
  11422. // } else {
  11423. // alert = [self translateSingleQuote:alert];
  11424. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11425. // }
  11426. //
  11427. // // available
  11428. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11429. // NSString *available = params[@"available"];
  11430. // NSString *available_condition;
  11431. // if ([available isEqualToString:@"Display All"]) {
  11432. // available_condition = @"";
  11433. // } else if ([available isEqualToString:@"Available Now"]) {
  11434. // available_condition = @"and availability > 0";
  11435. // } else {
  11436. // available_condition = @"and availability == 0";
  11437. // }
  11438. //
  11439. // // best seller
  11440. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11441. // NSString *best_seller = @"";
  11442. // NSString *order_best_seller = @"m.name asc";
  11443. //
  11444. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11445. // best_seller = @"and best_seller > 0";
  11446. // order_best_seller = @"m.best_seller desc,m.name asc";
  11447. // }
  11448. //
  11449. // // price
  11450. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11451. // NSString *price = params[@"price"];
  11452. // price_min = 0;
  11453. // price_max = MAXFLOAT;
  11454. // if (user) {
  11455. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11456. // NSMutableString *priceName = [NSMutableString string];
  11457. // for (int i = 0; i < priceTypeArray.count; i++) {
  11458. // NSString *pricetype = priceTypeArray[i];
  11459. // pricetype = [self translateSingleQuote:pricetype];
  11460. // if (i == 0) {
  11461. // [priceName appendFormat:@"'%@'",pricetype];
  11462. // } else {
  11463. // [priceName appendFormat:@",'%@'",pricetype];
  11464. // }
  11465. // }
  11466. //
  11467. // if ([price isEqualToString:@"Display All"]) {
  11468. // price = [NSString stringWithFormat:@""];
  11469. // } else if([price containsString:@"+"]){
  11470. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11471. // price_min = [price doubleValue];
  11472. // 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];
  11473. // } else {
  11474. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11475. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  11476. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  11477. // 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];
  11478. // }
  11479. // } else {
  11480. // price = @"";
  11481. // }
  11482. //
  11483. // // sold_by_qty : Sold in quantities of %@
  11484. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11485. // NSString *qty = params[@"sold_by_qty"];
  11486. // if ([qty isEqualToString:@"Display All"]) {
  11487. // qty = @"";
  11488. // } else {
  11489. // qty = [self translateSingleQuote:qty];
  11490. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11491. // }
  11492. //
  11493. // // category
  11494. // NSString *category_id = params[@"ctgId"];
  11495. // NSMutableArray *cate_id_array = nil;
  11496. // NSMutableString *cateWhere = [NSMutableString string];
  11497. //
  11498. // if ([category_id isEqualToString:@""] || !category_id) {
  11499. // [cateWhere appendString:@"1 = 1"];
  11500. // } else {
  11501. //
  11502. // if ([category_id containsString:@","]) {
  11503. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11504. // } else {
  11505. // cate_id_array = [@[category_id] mutableCopy];
  11506. // }
  11507. // /*-----------*/
  11508. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  11509. //
  11510. // [cateWhere appendString:@"("];
  11511. // for (int i = 0; i < cate_id_array.count; i++) {
  11512. //
  11513. // for (NSString *key0 in cateDic.allKeys) {
  11514. //
  11515. // if ([key0 containsString:@"category_"]) {
  11516. //
  11517. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  11518. //
  11519. // for (NSString *key1 in category0.allKeys) {
  11520. //
  11521. // if ([key1 containsString:@"category_"]) {
  11522. //
  11523. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  11524. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  11525. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11526. //
  11527. // cate_id_array[i] = [category1 objectForKey:@"id"];
  11528. // if (i == 0) {
  11529. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11530. // } else {
  11531. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11532. // }
  11533. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11534. // [category0 setValue:category1 forKey:key1];
  11535. // [cateDic setValue:category0 forKey:key0];
  11536. //
  11537. // }
  11538. //
  11539. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  11540. //
  11541. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11542. //
  11543. // cate_id_array[i] = [category0 objectForKey:@"id"];
  11544. // if (i == 0) {
  11545. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11546. // } else {
  11547. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11548. // }
  11549. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11550. // [cateDic setValue:category0 forKey:key0];
  11551. //
  11552. // }
  11553. //
  11554. // }
  11555. //
  11556. // }
  11557. //
  11558. // }
  11559. //
  11560. // }
  11561. //
  11562. // }
  11563. // [cateWhere appendString:@")"];
  11564. //
  11565. // [filter setValue:cateDic forKey:@"category"];
  11566. // }
  11567. //
  11568. // // where bestseller > 0 order by bestseller desc
  11569. // // sql query: alert availability(int) best_seller(int) price qty
  11570. //
  11571. // 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];
  11572. //
  11573. //
  11574. // // count
  11575. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11576. //
  11577. // }
  11578. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11579. //
  11580. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11581. //
  11582. //
  11583. //
  11584. //
  11585. // if (!user) {
  11586. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11587. // }
  11588. //
  11589. // [ret setValue:filter forKey:@"filter"];
  11590. //
  11591. //
  11592. //
  11593. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11594. //
  11595. // sqlite3_stmt * statement;
  11596. // [ret setValue:@"2" forKey:@"result"];
  11597. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11598. //
  11599. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11600. // // int count=0;
  11601. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11602. // {
  11603. //
  11604. // int i=0;
  11605. // while (sqlite3_step(statement) == SQLITE_ROW)
  11606. // {
  11607. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11608. //
  11609. //
  11610. // char *name = (char*)sqlite3_column_text(statement, 0);
  11611. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11612. //
  11613. // char *description = (char*)sqlite3_column_text(statement, 1);
  11614. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11615. //
  11616. //
  11617. //
  11618. // int product_id = sqlite3_column_int(statement, 2);
  11619. //
  11620. //
  11621. // int wid = sqlite3_column_int(statement, 3);
  11622. // int closeout = sqlite3_column_int(statement, 4);
  11623. // int cid = sqlite3_column_int(statement, 5);
  11624. // int wisdelete = sqlite3_column_int(statement, 6);
  11625. // int more_color = sqlite3_column_int(statement, 7);
  11626. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11627. //
  11628. // if(wid !=0 && wisdelete != 1)
  11629. // [item setValue:@"true" forKey:@"wish_exists"];
  11630. // else
  11631. // [item setValue:@"false" forKey:@"wish_exists"];
  11632. //
  11633. // if(closeout==0)
  11634. // [item setValue:@"false" forKey:@"is_closeout"];
  11635. // else
  11636. // [item setValue:@"true" forKey:@"is_closeout"];
  11637. //
  11638. // if(cid==0)
  11639. // [item setValue:@"false" forKey:@"cart_exists"];
  11640. // else
  11641. // [item setValue:@"true" forKey:@"cart_exists"];
  11642. //
  11643. // if (more_color == 0) {
  11644. // [item setObject:@(false) forKey:@"more_color"];
  11645. // } else if (more_color == 1) {
  11646. // [item setObject:@(true) forKey:@"more_color"];
  11647. // }
  11648. //
  11649. // [item addEntriesFromDictionary:imgjson];
  11650. //
  11651. //
  11652. // // [item setValue:nsurl forKey:@"img"];
  11653. // [item setValue:nsname forKey:@"fash_name"];
  11654. // [item setValue:nsdescription forKey:@"description"];
  11655. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11656. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11657. // i++;
  11658. // }
  11659. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11660. // [ret setObject:items forKey:@"items"];
  11661. // sqlite3_finalize(statement);
  11662. // }
  11663. //
  11664. // [iSalesDB close_db:db];
  11665. //
  11666. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11667. //
  11668. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11669. // dispatch_async(dispatch_get_main_queue(), ^{
  11670. //
  11671. // result(ret);
  11672. // });
  11673. //
  11674. // });
  11675. //
  11676. //
  11677. //
  11678. //}
  11679. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11680. //{
  11681. // [self itemsearch:params limited:YES completionHandler:result];
  11682. //}
  11683. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11684. {
  11685. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11686. NSString* offline_command=params[@"offline_Command"];
  11687. NSMutableDictionary* ret=nil;
  11688. if([offline_command isEqualToString:@"model_NIYMAL"])
  11689. {
  11690. NSString* category = params[@"category"];
  11691. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11692. }
  11693. dispatch_async(dispatch_get_main_queue(), ^{
  11694. result(ret);
  11695. });
  11696. });
  11697. }
  11698. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11699. {
  11700. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11701. int sort = [[params objectForKey:@"sort"] intValue];
  11702. NSString *sort_str = @"";
  11703. switch (sort) {
  11704. case 0:{
  11705. sort_str = @"order by w.modify_time desc";
  11706. }
  11707. break;
  11708. case 1:{
  11709. sort_str = @"order by w.modify_time asc";
  11710. }
  11711. break;
  11712. case 2:{
  11713. sort_str = @"order by m.name asc";
  11714. }
  11715. break;
  11716. case 3:{
  11717. sort_str = @"order by m.name desc";
  11718. }
  11719. break;
  11720. case 4:{
  11721. sort_str = @"order by m.description asc";
  11722. }
  11723. break;
  11724. default:
  11725. break;
  11726. }
  11727. // NSString* user = appDelegate.user;
  11728. sqlite3 *db = [iSalesDB get_db];
  11729. // order by w.create_time
  11730. 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];
  11731. // 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];
  11732. sqlite3_stmt * statement;
  11733. NSDate *date1 = [NSDate date];
  11734. // NSDate *date2 = nil;
  11735. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11736. int count=0;
  11737. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11738. {
  11739. while (sqlite3_step(statement) == SQLITE_ROW)
  11740. {
  11741. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11742. int product_id = sqlite3_column_double(statement, 0);
  11743. char *description = (char*)sqlite3_column_text(statement, 1);
  11744. if(description==nil)
  11745. description= "";
  11746. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11747. int item_id = sqlite3_column_double(statement, 2);
  11748. NSDate *date_image = [NSDate date];
  11749. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11750. printf("image : ");
  11751. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11752. // char *url = (char*)sqlite3_column_text(statement, 3);
  11753. // if(url==nil)
  11754. // url="";
  11755. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11756. int qty = sqlite3_column_int(statement, 3);
  11757. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11758. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11759. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11760. item[@"description"]= nsdescription;
  11761. item[@"img"]= nsurl;
  11762. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11763. count++;
  11764. }
  11765. printf("total time:");
  11766. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11767. ret[@"count"]= [NSNumber numberWithInt:count];
  11768. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11769. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11770. ret[@"result"]= [NSNumber numberWithInt:2];
  11771. sqlite3_finalize(statement);
  11772. }
  11773. [iSalesDB close_db:db];
  11774. dispatch_async(dispatch_get_main_queue(), ^{
  11775. UIApplication * app = [UIApplication sharedApplication];
  11776. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11777. appDelegate.wish_count =count;
  11778. [appDelegate update_count_mark];
  11779. result(ret);
  11780. });
  11781. });
  11782. }
  11783. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11784. {
  11785. // UIApplication * app = [UIApplication sharedApplication];
  11786. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11787. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11788. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11789. sqlite3 *db = [iSalesDB get_db];
  11790. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11791. NSString* product_id=params[@"product_id"];
  11792. NSString *item_count_str = params[@"item_count"];
  11793. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11794. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11795. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11796. // NSString *sql = @"";
  11797. for(int i=0;i<arr.count;i++)
  11798. {
  11799. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11800. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11801. __block int cart_count = 0;
  11802. if (!item_count_str) {
  11803. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11804. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11805. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11806. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11807. cart_count = [[model_set_components lastObject] intValue];
  11808. }];
  11809. }
  11810. if(count==0)
  11811. {
  11812. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11813. sqlite3_stmt *stmt;
  11814. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11815. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11816. if (item_count_arr) {
  11817. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11818. } else {
  11819. sqlite3_bind_int(stmt,2,cart_count);
  11820. }
  11821. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11822. [iSalesDB execSql:@"ROLLBACK" db:db];
  11823. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11824. [iSalesDB close_db:db];
  11825. DebugLog(@"add to wishlist error");
  11826. dispatch_async(dispatch_get_main_queue(), ^{
  11827. result(ret);
  11828. });
  11829. }
  11830. } else {
  11831. int qty = 0;
  11832. if (item_count_arr) {
  11833. qty = [item_count_arr[i] intValue];
  11834. } else {
  11835. qty = cart_count;
  11836. }
  11837. 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]];
  11838. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11839. [iSalesDB execSql:@"ROLLBACK" db:db];
  11840. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11841. [iSalesDB close_db:db];
  11842. DebugLog(@"add to wishlist error");
  11843. dispatch_async(dispatch_get_main_queue(), ^{
  11844. result(ret);
  11845. });
  11846. }
  11847. }
  11848. }
  11849. // [iSalesDB execSql:sql db:db];
  11850. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11851. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11852. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11853. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11854. [iSalesDB close_db:db];
  11855. ret[@"result"]= [NSNumber numberWithInt:2];
  11856. dispatch_async(dispatch_get_main_queue(), ^{
  11857. UIApplication * app = [UIApplication sharedApplication];
  11858. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11859. appDelegate.wish_count =count;
  11860. [appDelegate update_count_mark];
  11861. result(ret);
  11862. });
  11863. });
  11864. }
  11865. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11866. {
  11867. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11868. bool bLogin = appDelegate.bLogin;
  11869. __block NSString* contact_id = appDelegate.contact_id;
  11870. __block NSString* user = appDelegate.user;
  11871. __block NSString* order_code = appDelegate.order_code;
  11872. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11873. NSString* model_name = [params valueForKey:@"product_name"];
  11874. NSString* product_id = [params valueForKey:@"product_id"];
  11875. NSString* upc_code = [params valueForKey:@"upc_code"];
  11876. NSString* category = [params valueForKey:@"category"];
  11877. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11878. if(category==nil) {
  11879. category = default_category_id;
  11880. } else {
  11881. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11882. // 参数重有多个category id
  11883. if (arr_0.count > 1) {
  11884. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11885. // 取交集
  11886. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11887. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11888. [set_0 intersectSet:set_1];
  11889. if (set_0.count == 1) {
  11890. category = (NSString *)[set_0 anyObject];
  11891. } else {
  11892. if ([set_0 containsObject:default_category_id]) {
  11893. category = default_category_id;
  11894. } else {
  11895. category = (NSString *)[set_0 anyObject];
  11896. }
  11897. }
  11898. }
  11899. }
  11900. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11901. sqlite3 *db = [iSalesDB get_db];
  11902. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11903. NSString *sqlQuery = nil;
  11904. if(product_id==nil)
  11905. if(model_name==nil)
  11906. {
  11907. 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='%@';
  11908. }
  11909. else
  11910. {
  11911. 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='%@';
  11912. }
  11913. else
  11914. 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=%@;
  11915. sqlite3_stmt * statement;
  11916. [ret setValue:@"2" forKey:@"result"];
  11917. [ret setValue:@"3" forKey:@"detail_section_count"];
  11918. // int count=0;
  11919. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11920. {
  11921. if (sqlite3_step(statement) == SQLITE_ROW)
  11922. {
  11923. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11924. char *name = (char*)sqlite3_column_text(statement, 0);
  11925. if(name==nil)
  11926. name="";
  11927. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11928. char *description = (char*)sqlite3_column_text(statement, 1);
  11929. if(description==nil)
  11930. description="";
  11931. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11932. int product_id = sqlite3_column_int(statement, 2);
  11933. char *color = (char*)sqlite3_column_text(statement, 3);
  11934. if(color==nil)
  11935. color="";
  11936. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11937. //
  11938. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11939. // if(legcolor==nil)
  11940. // legcolor="";
  11941. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11942. //
  11943. //
  11944. int availability = sqlite3_column_int(statement, 5);
  11945. //
  11946. int incoming_stock = sqlite3_column_int(statement, 6);
  11947. char *demension = (char*)sqlite3_column_text(statement, 7);
  11948. if(demension==nil)
  11949. demension="";
  11950. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11951. // ,,,,,,,,,
  11952. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11953. if(seat_height==nil)
  11954. seat_height="";
  11955. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11956. char *material = (char*)sqlite3_column_text(statement, 9);
  11957. if(material==nil)
  11958. material="";
  11959. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11960. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11961. if(box_dim==nil)
  11962. box_dim="";
  11963. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11964. char *volume = (char*)sqlite3_column_text(statement, 11);
  11965. if(volume==nil)
  11966. volume="";
  11967. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11968. double weight = sqlite3_column_double(statement, 12);
  11969. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11970. if(model_set==nil)
  11971. model_set="";
  11972. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11973. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11974. if(load_ability==nil)
  11975. load_ability="";
  11976. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11977. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11978. // if(default_category==nil)
  11979. // default_category="";
  11980. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11981. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11982. if(fabric_content==nil)
  11983. fabric_content="";
  11984. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11985. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11986. if(assembling==nil)
  11987. assembling="";
  11988. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11989. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11990. if(made_in==nil)
  11991. made_in="";
  11992. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11993. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11994. if(special_remarks==nil)
  11995. special_remarks="";
  11996. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11997. int stockUcom = sqlite3_column_double(statement, 20);
  11998. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11999. if(product_group==nil)
  12000. product_group="";
  12001. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12002. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12003. // if(fashion_selector==nil)
  12004. // fashion_selector="";
  12005. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12006. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12007. if(selector_field==nil)
  12008. selector_field="";
  12009. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12010. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12011. if(property_field==nil)
  12012. property_field="";
  12013. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12014. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12015. if(packaging==nil)
  12016. packaging="";
  12017. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12018. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12019. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12020. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12021. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12022. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12023. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12024. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12025. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12026. NSString* Availability=nil;
  12027. if(availability>0)
  12028. Availability=[NSString stringWithFormat:@"%d",availability];
  12029. else
  12030. Availability = @"Out of Stock";
  12031. [img_section setValue:Availability forKey:@"Availability"];
  12032. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12033. char *eta = (char*)sqlite3_column_text(statement, 25);
  12034. if(eta==nil)
  12035. eta="";
  12036. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12037. if ([nseta isEqualToString:@"null"]) {
  12038. nseta = @"";
  12039. }
  12040. if (availability <= 0) {
  12041. [img_section setValue:nseta forKey:@"ETA"];
  12042. }
  12043. int item_id = sqlite3_column_int(statement, 26);
  12044. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12045. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12046. NSString* Price=nil;
  12047. if(bLogin==false)
  12048. Price=@"Must Sign in.";
  12049. else
  12050. {
  12051. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12052. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12053. if(price==nil)
  12054. Price=@"No Price.";
  12055. else
  12056. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12057. }
  12058. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12059. [img_section setValue:Price forKey:@"price"];
  12060. [img_section setValue:nsname forKey:@"model_name"];
  12061. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12062. if (order_code) { // 离线order code即so#
  12063. 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];
  12064. __block int cartQTY = 0;
  12065. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12066. cartQTY = sqlite3_column_int(stmt, 0);
  12067. }];
  12068. if (cartQTY > 0) {
  12069. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12070. }
  12071. }
  12072. [ret setObject:img_section forKey:@"img_section"];
  12073. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12074. int detail0_item_count=0;
  12075. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12076. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12077. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12078. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12079. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12080. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12081. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12082. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12083. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12084. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12085. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12086. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12087. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12088. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12089. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12090. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12091. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12092. {
  12093. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12094. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12095. }
  12096. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12097. [detail0_section setValue:@"kv" forKey:@"type"];
  12098. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12099. [ret setObject:detail0_section forKey:@"detail_0"];
  12100. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12101. // [detail1_section setValue:@"detail" forKey:@"target"];
  12102. // [detail1_section setValue:@"popup" forKey:@"action"];
  12103. // [detail1_section setValue:@"content" forKey:@"type"];
  12104. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12105. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12106. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12107. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12108. [ret setObject:detail1_section forKey:@"detail_1"];
  12109. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12110. [detail2_section setValue:@"detail" forKey:@"target"];
  12111. [detail2_section setValue:@"popup" forKey:@"action"];
  12112. [detail2_section setValue:@"content" forKey:@"type"];
  12113. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12114. [detail2_section setValue:@"true" forKey:@"single_row"];
  12115. [detail2_section setValue:@"local" forKey:@"data"];
  12116. [ret setObject:detail2_section forKey:@"detail_2"];
  12117. }
  12118. sqlite3_finalize(statement);
  12119. }
  12120. else
  12121. {
  12122. [ret setValue:@"8" forKey:@"result"];
  12123. }
  12124. // DebugLog(@"count:%d",count);
  12125. [iSalesDB close_db:db];
  12126. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12127. dispatch_async(dispatch_get_main_queue(), ^{
  12128. result(ret);
  12129. });
  12130. });
  12131. }
  12132. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12133. //{
  12134. //
  12135. // UIApplication * app = [UIApplication sharedApplication];
  12136. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12137. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12138. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12139. // sqlite3 *db = [iSalesDB get_db];
  12140. // NSString* collectId=params[@"collectId"];
  12141. //
  12142. //
  12143. //
  12144. //
  12145. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12146. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12147. // [iSalesDB execSql:sqlQuery db:db];
  12148. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12149. // [iSalesDB close_db:db];
  12150. //
  12151. //
  12152. // ret[@"result"]= [NSNumber numberWithInt:2];
  12153. // dispatch_async(dispatch_get_main_queue(), ^{
  12154. //
  12155. // appDelegate.wish_count =count;
  12156. //
  12157. // [appDelegate update_count_mark];
  12158. // result(ret);
  12159. // });
  12160. //
  12161. // });
  12162. //}
  12163. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12164. {
  12165. UIApplication * app = [UIApplication sharedApplication];
  12166. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12167. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12168. [iSalesDB disable_trigger];
  12169. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12170. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12171. [iSalesDB enable_trigger];
  12172. dispatch_async(dispatch_get_main_queue(), ^{
  12173. //
  12174. // NSString* user = [params valueForKey:@"user"];
  12175. //
  12176. // NSString* password = [params valueForKey:@"password"];
  12177. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12178. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12179. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12180. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12181. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12182. [appDelegate update_count_mark];
  12183. appDelegate.can_show_price =false;
  12184. appDelegate.can_see_price =false;
  12185. appDelegate.can_create_portfolio =false;
  12186. appDelegate.can_create_order =false;
  12187. appDelegate.can_cancel_order =false;
  12188. appDelegate.can_set_cart_price =false;
  12189. appDelegate.can_delete_order =false;
  12190. appDelegate.can_submit_order =false;
  12191. appDelegate.can_set_tearsheet_price =false;
  12192. appDelegate.can_update_contact_info = false;
  12193. appDelegate.save_order_logout = false;
  12194. appDelegate.submit_order_logout = false;
  12195. appDelegate.alert_sold_in_quantities = false;
  12196. appDelegate.ipad_perm =nil ;
  12197. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12198. appDelegate.OrderFilter= nil;
  12199. [appDelegate SetSo:nil];
  12200. [appDelegate set_main_button_panel];
  12201. result(ret);
  12202. });
  12203. });
  12204. }
  12205. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12206. {
  12207. // iSalesDB.
  12208. // UIApplication * app = [UIApplication sharedApplication];
  12209. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12210. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12211. [iSalesDB disable_trigger];
  12212. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12213. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12214. [iSalesDB enable_trigger];
  12215. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12216. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12217. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12218. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12219. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12220. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12221. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12222. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12223. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12224. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12225. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12226. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12227. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12228. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12229. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12230. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12231. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12232. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12233. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12234. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12235. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12236. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12237. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12238. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12239. [arr_name addObject:customer_first_name];
  12240. [arr_name addObject:customer_last_name];
  12241. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12242. // default ship from
  12243. 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';";
  12244. __block NSString *cid = @"";
  12245. __block NSString *name = @"";
  12246. __block NSString *ext = @"";
  12247. __block NSString *contact = @"";
  12248. __block NSString *email = @"";
  12249. __block NSString *fax = @"";
  12250. __block NSString *phone = @"";
  12251. sqlite3 *db = [iSalesDB get_db];
  12252. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12253. cid = [self textAtColumn:0 statement:statment];
  12254. name = [self textAtColumn:1 statement:statment];
  12255. ext = [self textAtColumn:2 statement:statment];
  12256. contact = [self textAtColumn:3 statement:statment];
  12257. email = [self textAtColumn:4 statement:statment];
  12258. fax = [self textAtColumn:5 statement:statment];
  12259. phone = [self textAtColumn:6 statement:statment];
  12260. }];
  12261. NSString* so_id = [self get_offline_soid:db];
  12262. if(so_id==nil)
  12263. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12264. 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];
  12265. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12266. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12267. //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'
  12268. //soId
  12269. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12270. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12271. sqlite3_stmt * statement;
  12272. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12273. {
  12274. if (sqlite3_step(statement) == SQLITE_ROW)
  12275. {
  12276. // char *name = (char*)sqlite3_column_text(statement, 1);
  12277. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12278. //ret = sqlite3_column_int(statement, 0);
  12279. char *soId = (char*)sqlite3_column_text(statement, 0);
  12280. if(soId==nil)
  12281. soId="";
  12282. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12283. [ret setValue:nssoId forKey:@"soId"];
  12284. [ret setValue:nssoId forKey:@"orderCode"];
  12285. }
  12286. sqlite3_finalize(statement);
  12287. }
  12288. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12289. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12290. [iSalesDB close_db:db];
  12291. dispatch_async(dispatch_get_main_queue(), ^{
  12292. result(ret);
  12293. });
  12294. });
  12295. }
  12296. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12297. {
  12298. UIApplication * app = [UIApplication sharedApplication];
  12299. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12300. bool can_create_backorder= appDelegate.can_create_backorder;
  12301. NSString* user = appDelegate.user;
  12302. NSString* contact_id = appDelegate.contact_id;
  12303. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12304. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12305. sqlite3 *db = [iSalesDB get_db];
  12306. NSString* orderCode=params[@"orderCode"];
  12307. int sort = [[params objectForKey:@"sort"] intValue];
  12308. NSString *sort_str = @"";
  12309. switch (sort) {
  12310. case 0:{
  12311. sort_str = @"order by c.modify_time desc";
  12312. }
  12313. break;
  12314. case 1:{
  12315. sort_str = @"order by c.modify_time asc";
  12316. }
  12317. break;
  12318. case 2:{
  12319. sort_str = @"order by m.name asc";
  12320. }
  12321. break;
  12322. case 3:{
  12323. sort_str = @"order by m.name desc";
  12324. }
  12325. break;
  12326. case 4:{
  12327. sort_str = @"order by m.description asc";
  12328. }
  12329. break;
  12330. default:
  12331. break;
  12332. }
  12333. 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 ];
  12334. // 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 ];
  12335. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12336. DebugLog(@"offline_login sql:%@",sqlQuery);
  12337. sqlite3_stmt * statement;
  12338. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12339. NSDate *date1 = [NSDate date];
  12340. int count=0;
  12341. int cart_count=0;
  12342. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12343. if ( dbresult== SQLITE_OK)
  12344. {
  12345. while (sqlite3_step(statement) == SQLITE_ROW)
  12346. {
  12347. // NSDate *row_date = [NSDate date];
  12348. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12349. int product_id = sqlite3_column_int(statement, 0);
  12350. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12351. int item_id = sqlite3_column_int(statement, 7);
  12352. NSString* Price=nil;
  12353. if(str_price==nil)
  12354. {
  12355. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12356. // NSDate *price_date = [NSDate date];
  12357. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12358. // DebugLog(@"price time interval");
  12359. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12360. if(price==nil)
  12361. Price=@"No Price.";
  12362. else
  12363. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12364. }
  12365. else
  12366. {
  12367. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12368. }
  12369. double discount = sqlite3_column_double(statement, 2);
  12370. int item_count = sqlite3_column_int(statement, 3);
  12371. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12372. NSString *nsline_note=nil;
  12373. if(line_note!=nil)
  12374. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12375. char *name = (char*)sqlite3_column_text(statement, 5);
  12376. NSString *nsname=nil;
  12377. if(name!=nil)
  12378. nsname= [[NSString alloc]initWithUTF8String:name];
  12379. char *description = (char*)sqlite3_column_text(statement, 6);
  12380. NSString *nsdescription=nil;
  12381. if(description!=nil)
  12382. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12383. int stockUom = sqlite3_column_int(statement, 8);
  12384. int _id = sqlite3_column_int(statement, 9);
  12385. int availability = sqlite3_column_int(statement, 10);
  12386. // NSDate *subtotal_date = [NSDate date];
  12387. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12388. // DebugLog(@"subtotal_date time interval");
  12389. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12390. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12391. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12392. int carton=[bsubtotaljson[@"carton"] intValue];
  12393. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12394. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12395. // NSDate *img_date = [NSDate date];
  12396. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12397. // DebugLog(@"img_date time interval");
  12398. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12399. itemjson[@"model"]=nsname;
  12400. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12401. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12402. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12403. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12404. itemjson[@"check"]=@"true";
  12405. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12406. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12407. itemjson[@"unit_price"]=Price;
  12408. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12409. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12410. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12411. itemjson[@"note"]=nsline_note;
  12412. if (!can_create_backorder) {
  12413. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12414. }
  12415. // NSDate *date2 = [NSDate date];
  12416. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12417. // DebugLog(@"model_bundle time interval");
  12418. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12419. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12420. count++;
  12421. // DebugLog(@"row time interval");
  12422. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12423. }
  12424. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12425. sqlite3_finalize(statement);
  12426. }
  12427. DebugLog(@"request cart total time interval");
  12428. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12429. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12430. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12431. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12432. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12433. ret[@"count"]=[NSNumber numberWithInt:count ];
  12434. ret[@"mode"]=@"Regular Mode";
  12435. [iSalesDB close_db:db];
  12436. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12437. DebugLog(@"general notes :%@",general_note);
  12438. ret[@"general_note"]= general_note;
  12439. dispatch_async(dispatch_get_main_queue(), ^{
  12440. result(ret);
  12441. });
  12442. });
  12443. }
  12444. @end