OLDataProvider.m 697 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. //#import "UIProgressView+AFNetworking.h"
  15. //#import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "RASingleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. #import "ActiveViewController.h"
  24. #import "RANetwork.h"
  25. @interface OLDataProvider ()
  26. @end
  27. @implementation OLDataProvider
  28. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  29. {
  30. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  31. NSString* ret=@"No Price.";
  32. sqlite3_stmt * statement;
  33. // int count=0;
  34. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  35. {
  36. if (sqlite3_step(statement) == SQLITE_ROW)
  37. {
  38. char *price = (char*)sqlite3_column_text(statement, 0);
  39. if(price==nil)
  40. price="";
  41. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  42. double dp= [nsprice doubleValue];
  43. ret=[NSString stringWithFormat:@"%.2f",dp];
  44. }
  45. sqlite3_finalize(statement);
  46. }
  47. return ret;
  48. // // [iSalesDB close_db:db];
  49. //
  50. // return nil;
  51. //// if(dprice==DBL_MAX)
  52. //// ret= nil;
  53. //// else
  54. //// ret= [NSNumber numberWithDouble:dprice];
  55. //// return ret;
  56. }
  57. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  58. {
  59. NSString* ret= nil;
  60. NSString *sqlQuery = nil;
  61. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  62. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  63. sqlite3_stmt * statement;
  64. // int count=0;
  65. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  66. {
  67. if (sqlite3_step(statement) == SQLITE_ROW)
  68. {
  69. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  70. if(imgurl==nil)
  71. imgurl="";
  72. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  73. ret=nsimgurl;
  74. }
  75. sqlite3_finalize(statement);
  76. }
  77. else
  78. {
  79. ret=@"";
  80. }
  81. // [iSalesDB close_db:db];
  82. // DebugLog(@"data string: %@",ret );
  83. return ret;
  84. }
  85. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  86. {
  87. // assert(params[@"contact_id"]!=nil);
  88. assert(params[@"user"]!=nil);
  89. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  90. //生成portfolio pdf需要的数据
  91. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  92. data[@"npd_url"]=COMPANY_WEB;
  93. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  94. [formatter setDateFormat:@"MMMM yyyy"];
  95. NSString* date = [formatter stringFromDate:[NSDate date]];
  96. data[@"create_month"]=date;
  97. data[@"company_name"]=COMPANY_FULL_NAME;
  98. data[@"catalog_name"]=params[@"catalog_name"];
  99. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  100. data[@"TOTAL_PAGE"]=0;
  101. data[@"CURRENT_PAGE"]=0;
  102. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  103. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  104. bool show_stockout=[params[@"show_stockout"] boolValue];
  105. NSString *sql = nil;
  106. // v1.90 more color
  107. NSString *group_by = @"";
  108. if(values==nil)
  109. {
  110. if (show_group_by) {
  111. group_by = @"group by b.product_group ";
  112. }
  113. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id %@ order by a.name",params[@"product_ids"],params[@"product_ids"],group_by];
  114. }
  115. else
  116. {
  117. if (show_group_by) {
  118. group_by = @"group by product_group ";
  119. }
  120. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  121. }
  122. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  123. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  124. sqlite3 *db = [iSalesDB get_db];
  125. // NSDictionary *resultDic =
  126. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  127. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  128. int product_id = sqlite3_column_int(stmt, 0);
  129. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  130. NSString *name = [self textAtColumn:1 statement:stmt];
  131. NSString *description = [self textAtColumn:2 statement:stmt];
  132. double price = sqlite3_column_double(stmt, 3);
  133. NSString *s_price = [self textAtColumn:3 statement:stmt];
  134. double discount = sqlite3_column_double(stmt,4);
  135. int qty = sqlite3_column_int(stmt, 5);
  136. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  137. int is_percentage = sqlite3_column_int(stmt, 6);
  138. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  139. int item_id = sqlite3_column_int(stmt, 7);
  140. // int fashion_id = sqlite3_column_int(stmt, 8);
  141. NSString *line_note = [self textAtColumn:9 statement:stmt];
  142. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  143. double percent = sqlite3_column_double(stmt, 10);
  144. // int portfolio_id = sqlite3_column_int(stmt, 11);
  145. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  146. int availability = sqlite3_column_int(stmt, 12);
  147. NSString *color = [self textAtColumn:13 statement:stmt];
  148. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  149. NSString *demension = [self textAtColumn:15 statement:stmt];
  150. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  151. NSString *material = [self textAtColumn:17 statement:stmt];
  152. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  153. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  154. double volume = sqlite3_column_double(stmt, 19);
  155. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  156. double weight = sqlite3_column_double(stmt, 20);
  157. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  158. int model_set = sqlite3_column_int(stmt, 21);
  159. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  160. double load_ability = sqlite3_column_double(stmt, 22);
  161. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  162. NSString *assembling = [self textAtColumn:24 statement:stmt];
  163. NSString *made_in = [self textAtColumn:25 statement:stmt];
  164. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  165. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  166. float bprice=0;
  167. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  168. for(int b=0;b< [bundle[@"count"] intValue];b++)
  169. {
  170. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  171. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  172. }
  173. if(gprice!=nil)
  174. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  175. if(s_percent.length==0||is_percentage==0)
  176. {
  177. percent=100.0;
  178. if([s_qty isEqualToString:@"null"])
  179. qty=availability;
  180. }
  181. else
  182. {
  183. qty=availability;
  184. }
  185. qty=qty*percent/100+0.5;
  186. for(int i=0;i<[values[@"count"] intValue];i++)
  187. {
  188. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  189. if([valueitem[@"product_id"] intValue]==product_id)
  190. {
  191. line_note=valueitem[@"line_note"];
  192. qty=[valueitem[@"available_qty"] intValue];
  193. if([[valueitem allKeys] containsObject:@"available_qty"])
  194. s_qty=[NSString stringWithFormat:@"%d",qty];
  195. else
  196. s_qty=@"null";
  197. if(valueitem[@"regular_price"]!=nil)
  198. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  199. s_price=valueitem[@"special_price"];
  200. price=[valueitem[@"special_price"] floatValue];
  201. discount=0;
  202. // v1.90
  203. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  204. }
  205. }
  206. if(is_percentage==0&&qty==0&&!show_stockout)
  207. {
  208. return;
  209. }
  210. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  211. {
  212. return;
  213. }
  214. NSString* set_price=@"";
  215. if([params[@"entered_price"] boolValue])
  216. {
  217. if (s_price==nil ) {
  218. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  219. }
  220. else
  221. {
  222. if(price*(1-discount/100.0)!=gprice.floatValue)
  223. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  224. }
  225. }
  226. NSString* get_price=@"";
  227. {
  228. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  229. // DebugLog(@"price time interval");
  230. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  231. if(gprice==nil)
  232. get_price=@"Price:No Price.";
  233. else
  234. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  235. }
  236. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  237. // if ([qty_null isEqualToString:@"null"]) {
  238. // qty=availability;
  239. // }
  240. //
  241. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  242. // if ([qty_percent_null isEqualToString:@"null"]) {
  243. // percentage=1;
  244. // }
  245. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  246. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  247. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  248. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  249. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  250. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  251. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  252. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  253. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  254. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  255. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  256. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  257. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  258. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  259. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  260. if([params[@"availability"] boolValue]==false)
  261. str_availability=@"";
  262. else
  263. {
  264. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  265. str_availability=@"<b>Availability:</b> In Production";
  266. }
  267. if([params[@"color"] boolValue]==false || color.length==0 )
  268. str_color=@"";
  269. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  270. str_model_set=@"";
  271. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  272. str_legcolor=@"";
  273. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  274. str_demension=@"";
  275. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  276. str_seat_height=@"";
  277. if([params[@"material"] boolValue]==false || material.length==0 )
  278. str_meterial=@"";
  279. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  280. str_box_dim=@"";
  281. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  282. str_volume=@"";
  283. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  284. str_weight=@"";
  285. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  286. str_load_ability=@"";
  287. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  288. str_fabric_content=@"";
  289. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  290. str_assembling=@"";
  291. if([params[@"made"] boolValue]==false || made_in.length==0 )
  292. str_made_in=@"";
  293. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  294. str_line_note=@"";
  295. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  296. [arr_detail addObject:str_availability];
  297. [arr_detail addObject:str_color];
  298. [arr_detail addObject:str_model_set];
  299. [arr_detail addObject:str_legcolor];
  300. [arr_detail addObject:str_demension];
  301. [arr_detail addObject:str_seat_height];
  302. [arr_detail addObject:str_meterial];
  303. [arr_detail addObject:str_box_dim];
  304. [arr_detail addObject:str_volume];
  305. [arr_detail addObject:str_weight];
  306. [arr_detail addObject:str_load_ability];
  307. [arr_detail addObject:str_fabric_content];
  308. [arr_detail addObject:str_assembling];
  309. [arr_detail addObject:str_made_in];
  310. [arr_detail addObject:str_line_note];
  311. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  312. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  313. //model image;
  314. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  315. // qr image
  316. NSString* qrpath=nil;
  317. if([params[@"show_barcode"] boolValue])
  318. {
  319. NSString* temp = NSTemporaryDirectory();
  320. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  321. qrpath=[temp stringByAppendingPathComponent:filename];
  322. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  323. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  324. }
  325. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  326. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  327. item[@"img"]=img_path;
  328. item[@"img_qr"]=qrpath;
  329. item[@"special_price"]=set_price;
  330. item[@"price"]=get_price;
  331. item[@"name"]=name;
  332. item[@"description"]=description;
  333. item[@"detail"]=detail;
  334. item[@"img"]=img_path;
  335. if (show_group_by && more_color_int != 0) {
  336. item[@"more_color"] = more_color_path;
  337. }
  338. // @{
  339. // //@"linenotes": line_note,
  340. //// @"product_id": product_id_string,
  341. //// @"available_qty": @(qty),
  342. //// @"available_percent" : @(percent),
  343. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  344. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  345. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"": ,
  352. // @"":
  353. // }.mutableCopy;
  354. // if (percentage) {
  355. // [item removeObjectForKey:@"available_qty"];
  356. // } else {
  357. // [item removeObjectForKey:@"available_percent"];
  358. // }
  359. //
  360. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  361. // if ([qty_null isEqualToString:@"null"]) {
  362. // [item removeObjectForKey:@"available_qty"];
  363. // }
  364. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  365. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  366. }];
  367. [iSalesDB close_db:db];
  368. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  369. // [dic setValue:@"" forKey:@"email_content"];
  370. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  371. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  372. //
  373. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  374. // cell[@"count"]=[NSNumber numberWithInt:10];
  375. grid[@"cell0"]=dic;
  376. data[@"grid0"]=grid;
  377. return data;
  378. }
  379. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  380. {
  381. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  382. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  383. NSString *cache_folder=[paths objectAtIndex:0];
  384. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  385. BOOL bdir=NO;
  386. NSFileManager* fileManager = [NSFileManager defaultManager];
  387. if(PDF_DEBUG)
  388. {
  389. NSData *data = [NSData dataWithContentsOfFile:default_path];
  390. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  391. return ret;
  392. }
  393. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  394. {
  395. NSError * error=nil;
  396. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  397. {
  398. return nil;
  399. }
  400. }
  401. NSData *data = [NSData dataWithContentsOfFile:template_path];
  402. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  403. return ret;
  404. }
  405. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  406. {
  407. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  408. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  409. DebugLog(@"offline_login sql:%@",sqlQuery);
  410. sqlite3_stmt * statement;
  411. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  412. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  413. {
  414. if (sqlite3_step(statement) == SQLITE_ROW)
  415. {
  416. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  417. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  418. // int can_show_price = sqlite3_column_int(statement, 0);
  419. // int can_see_price = sqlite3_column_int(statement, 1);
  420. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  421. // if(contact_id==nil)
  422. // contact_id="";
  423. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  424. // int user_type = sqlite3_column_int(statement, 3);
  425. // int can_cancel_order = sqlite3_column_int(statement, 4);
  426. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  427. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  428. // int can_delete_order = sqlite3_column_int(statement, 7);
  429. // int can_submit_order = sqlite3_column_int(statement, 8);
  430. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  431. // int can_create_order = sqlite3_column_int(statement, 10);
  432. // char *mode = (char*)sqlite3_column_text(statement, 11);
  433. // if(mode==nil)
  434. // mode="";
  435. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  436. // char *username = (char*)sqlite3_column_text(statement, 12);
  437. // if(username==nil)
  438. // username="";
  439. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  440. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  441. char *first_name = (char*)sqlite3_column_text(statement, 14);
  442. if(first_name==nil)
  443. first_name="";
  444. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  445. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  446. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  447. // [header setValue:nscontact_id forKey:@"contact_id"];
  448. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  449. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  450. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  451. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  452. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  454. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  455. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  456. //
  457. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  458. //
  459. // [header setValue:nsusername forKey:@"username"];
  460. //
  461. // NSError* error=nil;
  462. //
  463. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  464. // [header setValue:statusFilter forKey:@"statusFilter"];
  465. //
  466. //
  467. //
  468. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  469. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  470. //
  471. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  472. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  473. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  474. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  475. //
  476. // [ret setObject:header forKey:@"header"];
  477. [ret setValue:nsfirst_name forKey:@"first_name"];
  478. }
  479. sqlite3_finalize(statement);
  480. }
  481. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  482. return ret;
  483. }
  484. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  485. {
  486. assert(user!=nil);
  487. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  488. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  489. data[@"npd_url"]=COMPANY_WEB;
  490. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  491. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  492. NSString* date = [formatter stringFromDate:[NSDate date]];
  493. data[@"print_date"]=date;
  494. data[@"company_name"]=COMPANY_FULL_NAME;
  495. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  496. data[@"TOTAL_PAGE"]=0;
  497. data[@"CURRENT_PAGE"]=0;
  498. // [formatter setDateFormat:@"MM/dd/yyyy"];
  499. // date = [formatter stringFromDate:[NSDate date]];
  500. // data[@"creat_date"]=date;
  501. NSString* temp = NSTemporaryDirectory();
  502. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  503. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  504. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  505. // bool bwrite=
  506. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  507. data[@"img_barcode"]=qrpath;
  508. data[@"so_id"]=order_code;
  509. data[@"company"]=COMPANY_FULL_NAME;
  510. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  511. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  512. sqlite3 *db = [iSalesDB get_db];
  513. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  514. sqlite3_stmt * statement;
  515. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  516. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  517. row0[@"count"]=[NSNumber numberWithInt:1];
  518. datasource[@"row0"]=row0;
  519. NSString* print_status=@"";
  520. NSString* order_type=@"Offline Quote";
  521. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  522. {
  523. if (sqlite3_step(statement) == SQLITE_ROW)
  524. {
  525. int erpOrderStatus = sqlite3_column_int(statement, 0);
  526. int status = sqlite3_column_int(statement, 26);
  527. if(status==2)
  528. {
  529. switch (erpOrderStatus) {
  530. case 10:
  531. // order_type=@"Quoted Order";
  532. print_status=@"Quote";
  533. break;
  534. case 11:
  535. print_status=@"Booked";
  536. break;
  537. case 12:
  538. print_status=@"Picked";
  539. break;
  540. case 13:
  541. print_status=@"Shipped";
  542. break;
  543. case 14:
  544. print_status=@"Closed";
  545. break;
  546. case 15:
  547. print_status=@"Canceled";
  548. break;
  549. case 16:
  550. print_status=@"All";
  551. break;
  552. case 18:
  553. print_status=@"Processing";
  554. break;
  555. default:
  556. break;
  557. }
  558. }
  559. else{
  560. switch (status) {
  561. case 0:
  562. {
  563. print_status=@"Quote";
  564. break;
  565. }
  566. case 1:
  567. {
  568. print_status=@"Saved";
  569. break;
  570. }
  571. case 3:
  572. {
  573. //canceled
  574. print_status=@"Canceled";
  575. break;
  576. }
  577. default:
  578. break;
  579. }
  580. }
  581. NSString* sender_name = [self textAtColumn:1 statement:statement];
  582. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  583. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  584. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  585. NSString* sender_email = [self textAtColumn:5 statement:statement];
  586. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  587. NSString* billing_name = [self textAtColumn:7 statement:statement];
  588. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  589. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  590. if(billing_phone.length>0)
  591. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  592. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  593. if(billing_fax.length>0)
  594. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  595. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  596. [billto_arr0 addObject:billing_phone];
  597. [billto_arr0 addObject:billing_fax];
  598. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  599. NSString* billing_email = [self textAtColumn:11 statement:statement];
  600. if(billing_email.length>0)
  601. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  602. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  603. if(billing_contact.length>0)
  604. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  605. NSString* receive_name = [self textAtColumn:12 statement:statement];
  606. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  607. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  608. if(receive_phone.length>0)
  609. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  610. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  611. if(receive_fax.length>0)
  612. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  613. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  614. [receive_arr0 addObject:receive_phone];
  615. [receive_arr0 addObject:receive_fax];
  616. NSString* receive_email = [self textAtColumn:16 statement:statement];
  617. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  618. if(receive_email.length>0)
  619. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  620. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  621. if(receive_contact.length>0)
  622. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  623. NSString* poNumber = [self textAtColumn:17 statement:statement];
  624. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  625. NSString* carrier = [self textAtColumn:19 statement:statement];
  626. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  627. NSString* terms = [self textAtColumn:21 statement:statement];
  628. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  629. NSString* port_destination = [self textAtColumn:23 statement:statement];
  630. NSString* etd = [self textAtColumn:24 statement:statement];
  631. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  632. if(sign_picpath.length==0)
  633. sign_picpath=nil;
  634. NSString* create_date = [self textAtColumn:27 statement:statement];
  635. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  636. NSString* general_notes=[self textAtColumn:33 statement:statement];
  637. NSString *customerID = [self textAtColumn:34 statement:statement];
  638. if (customerID.length == 0) {
  639. customerID = nil;
  640. }
  641. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  642. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  643. if(general_notes.length>0)
  644. {
  645. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  646. row2[@"item_0"]=@{@"g_notes":general_notes };
  647. row2[@"count"]=[NSNumber numberWithInt:1];
  648. datasource[@"row2"]=row2;
  649. }
  650. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  651. [billto_arr addObject:billing_ext];
  652. [billto_arr addObject:billing_pf];
  653. [billto_arr addObject:billing_contact];
  654. [billto_arr addObject:billing_email];
  655. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  656. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  657. [receive_arr addObject:receive_ext];
  658. [receive_arr addObject:receive_pf];
  659. [receive_arr addObject:receive_contact];
  660. [receive_arr addObject:receive_email];
  661. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  662. float shipping = sqlite3_column_double(statement, 28);
  663. float lift_gate_value = sqlite3_column_double(statement, 29);
  664. float handling_fee_value = sqlite3_column_double(statement, 30);
  665. data[@"company"]=sender_name;
  666. data[@"shipfrom_addr"]=sender_ext;
  667. data[@"shipfrom_phone"]=sender_phone;
  668. data[@"shipfrom_fax"]=sender_fax;
  669. data[@"shipfrom_email"]=sender_email;
  670. data[@"warehouse_name"]=warehouse_name;
  671. data[@"bill_to_name"]=billing_name;
  672. data[@"bill_to_ext"]=billto;
  673. data[@"ship_to_name"]=receive_name;
  674. data[@"ship_to_ext"]=receive;
  675. data[@"po_no"]=poNumber;
  676. data[@"rep"]=sales_rep;
  677. data[@"e_schdule"]=etd;
  678. data[@"sales_terms"]=sales_terms;
  679. data[@"port_destination"]=port_destination;
  680. data[@"terms"]=terms;
  681. data[@"carrier"]=carrier;
  682. data[@"vendor_no"]=vendor_no;
  683. data[@"customer_sign"]=sign_picpath;
  684. data[@"print_status"]=print_status;
  685. data[@"create_date"]=create_date;
  686. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  687. sqlite3_stmt * statement_cart;
  688. // NSDate *date1 = [NSDate date];
  689. int count=0;
  690. // int cart_count=0;
  691. float credits=0;
  692. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  693. if ( dbresult== SQLITE_OK)
  694. {
  695. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  696. int row1count=0;
  697. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  698. {
  699. // NSDate *row_date = [NSDate date];
  700. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  701. // int product_id = sqlite3_column_int(statement_cart, 0);
  702. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  703. int item_id = sqlite3_column_int(statement_cart, 7);
  704. double discount = sqlite3_column_double(statement_cart, 2);
  705. NSString* Price=nil;
  706. if(str_price==nil)
  707. {
  708. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  709. // NSDate *price_date = [NSDate date];
  710. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  711. // DebugLog(@"price time interval");
  712. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  713. if(price==nil)
  714. Price=@"No Price.";
  715. else
  716. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  717. }
  718. else
  719. {
  720. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  721. }
  722. float dunitprice=0;
  723. int item_count = sqlite3_column_int(statement_cart, 3);
  724. NSString* Amount=@"";
  725. if(![Price isEqualToString:@"No Price."])
  726. {
  727. dunitprice=[Price floatValue];
  728. dunitprice=dunitprice* (1-discount/100.0);
  729. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  730. credits+=dunitprice*item_count;
  731. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  732. }
  733. else
  734. {
  735. Price=@"";
  736. }
  737. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  738. // NSString *nsline_note=nil;
  739. // if(line_note!=nil)
  740. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  741. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  742. NSString *nsname=nil;
  743. if(name!=nil)
  744. nsname= [[NSString alloc]initWithUTF8String:name];
  745. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  746. NSString *nsdescription=nil;
  747. if(description!=nil)
  748. nsdescription= [[NSString alloc]initWithUTF8String:description];
  749. // int stockUom = sqlite3_column_int(statement_cart, 8);
  750. // int _id = sqlite3_column_int(statement_cart, 9);
  751. // NSDate *subtotal_date = [NSDate date];
  752. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  753. // DebugLog(@"subtotal_date time interval");
  754. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  755. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  756. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  757. // int carton=[bsubtotaljson[@"carton"] intValue];
  758. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  759. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  760. // NSDate *img_date = [NSDate date];
  761. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  762. // DebugLog(@"img_date time interval");
  763. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  764. itemjson[@"model"]=nsname;
  765. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  766. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  767. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  768. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  769. // itemjson[@"check"]=@"true";
  770. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  771. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  772. itemjson[@"unit_price"]=Price;
  773. itemjson[@"amount"]=Amount;
  774. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  775. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  776. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  777. // itemjson[@"note"]=nsline_note;
  778. // NSDate *date2 = [NSDate date];
  779. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  780. // DebugLog(@"model_bundle time interval");
  781. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  782. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  783. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  784. row1count++;
  785. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  786. for(int c=0;c<[combine[@"count"] intValue];c++)
  787. {
  788. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  789. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  790. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  791. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  792. credits+=combine_amount;
  793. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  794. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  795. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  796. row1count++;
  797. }
  798. count++;
  799. // DebugLog(@"row time interval");
  800. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  801. }
  802. row1[@"count"]=[NSNumber numberWithInt:row1count];
  803. datasource[@"row1"]=row1;
  804. data[@"table_model"]=datasource;
  805. sqlite3_finalize(statement_cart);
  806. }
  807. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  808. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  809. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  810. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  811. // if(credits>0)
  812. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  813. // else
  814. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  815. if(shipping>0)
  816. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  817. else
  818. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  819. if(lift_gate_value>0)
  820. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  821. else
  822. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  823. if(handling_fee_value>0)
  824. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  825. else
  826. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  827. float total = credits+shipping+lift_gate_value+handling_fee_value;
  828. if(total>0)
  829. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  830. else
  831. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  832. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  833. fee[@"row0"]=fee_row0;
  834. data[@"table_fee"]=fee;
  835. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  836. // ret[@"sign_url"] = sign_url;
  837. //
  838. // customer_contact = [self textAtColumn:52 statement:statement];
  839. // customer_email = [self textAtColumn:53 statement:statement];
  840. // customer_phone = [self textAtColumn:54 statement:statement];
  841. // customer_fax = [self textAtColumn:55 statement:statement];
  842. //
  843. //
  844. // int offline_edit=sqlite3_column_int(statement, 56);
  845. //
  846. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  847. //
  848. // char *soid = (char*)sqlite3_column_text(statement, 1);
  849. // if(soid==nil)
  850. // soid= "";
  851. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  852. // // so#
  853. // ret[@"so#"] = nssoid;
  854. //
  855. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  856. // if(poNumber==nil)
  857. // poNumber= "";
  858. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  859. //
  860. //
  861. //
  862. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  863. // if(create_time==nil)
  864. // create_time= "";
  865. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  866. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  867. //
  868. // int status = sqlite3_column_int(statement, 4);
  869. // int erpStatus = sqlite3_column_int(statement, 49);
  870. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  871. //
  872. // // status
  873. // if (status > 1 && status != 3) {
  874. // status = erpStatus;
  875. // } else if (status == 3) {
  876. // status = 15;
  877. // }
  878. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  879. // ret[@"order_status"] = nsstatus;
  880. //
  881. //
  882. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  883. // if(company_name==nil)
  884. // company_name= "";
  885. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  886. // // company name
  887. // ret[@"company_name"] = nscompany_name;
  888. //
  889. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  890. // if(customer_contact==nil)
  891. // customer_contact= "";
  892. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  893. //
  894. //
  895. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  896. // if(addr_1==nil)
  897. // addr_1="";
  898. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  899. //
  900. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  901. // if(addr_2==nil)
  902. // addr_2="";
  903. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  904. //
  905. //
  906. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  907. // if(addr_3==nil)
  908. // addr_3="";
  909. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  910. //
  911. //
  912. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  913. // if(addr_4==nil)
  914. // addr_4="";
  915. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  916. //
  917. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  918. // [arr_addr addObject:nsaddr_1];
  919. // [arr_addr addObject:nsaddr_2];
  920. // [arr_addr addObject:nsaddr_3];
  921. // [arr_addr addObject:nsaddr_4];
  922. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  923. //
  924. //
  925. // char *logist = (char*)sqlite3_column_text(statement, 11);
  926. // if(logist==nil)
  927. // logist= "";
  928. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  929. // if (status == -11 || status == 10 || status == 11) {
  930. // nslogist = [self textAtColumn:59 statement:statement];
  931. // };
  932. //
  933. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  934. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  935. // shipping = @"Shipping To Be Quoted";
  936. // } else {
  937. // shippingFee = sqlite3_column_double(statement, 12);
  938. // }
  939. //
  940. // // Shipping
  941. // ret[@"Shipping"] = shipping;
  942. //
  943. // int have_lift_gate = sqlite3_column_int(statement, 17);
  944. // lift_gate = sqlite3_column_double(statement, 13);
  945. // // Liftgate Fee(No loading dock)
  946. // if (!have_lift_gate) {
  947. // lift_gate = 0;
  948. // }
  949. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  950. // if (sqlite3_column_int(statement, 57)) {
  951. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  952. // }
  953. //
  954. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  955. // if(general_notes==nil)
  956. // general_notes= "";
  957. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  958. //
  959. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  960. // if(internal_notes==nil)
  961. // internal_notes= "";
  962. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  963. //
  964. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  965. // if(payment_type==nil)
  966. // payment_type= "";
  967. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  968. //
  969. //
  970. //
  971. // // order info
  972. // orderinfo = [self textFileName:@"order_info.html"];
  973. //
  974. //
  975. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  978. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  979. //
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  987. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  988. //
  989. // NSString *payment = nil;
  990. // if([nspayment_type isEqualToString:@"Credit Card"])
  991. // {
  992. // payment = [self textFileName:@"creditcardpayment.html"];
  993. //
  994. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  995. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  996. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  997. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  998. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  999. // NSString *card_type = [self textAtColumn:42 statement:statement];
  1000. // if (card_type.length > 0) { // 显示星号
  1001. // card_type = @"****";
  1002. // }
  1003. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1004. // if (card_number.length > 0 && card_number.length > 4) {
  1005. // for (int i = 0; i < card_number.length - 4; i++) {
  1006. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1007. // }
  1008. // } else {
  1009. // card_number = @"";
  1010. // }
  1011. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1012. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1013. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1014. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1015. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1016. // card_expiration = @"****";
  1017. // }
  1018. //
  1019. //
  1020. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1021. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1022. //
  1023. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1024. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1025. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1026. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1027. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1028. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1029. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1030. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1031. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1032. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1033. //
  1034. //
  1035. // }
  1036. // else
  1037. // {
  1038. // payment=[self textFileName:@"normalpayment.html"];
  1039. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1040. // }
  1041. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1042. //
  1043. // ret[@"result"]= [NSNumber numberWithInt:2];
  1044. //
  1045. // // more info
  1046. // moreInfo = [self textFileName:@"more_info.html"];
  1047. //
  1048. //
  1049. // // ShipToCompany_or_&nbsp
  1050. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1051. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1052. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1054. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1056. //
  1057. //
  1058. // // ShipFromCompany_or_&nbsp
  1059. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1060. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1061. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1063. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1065. //
  1066. //
  1067. // // FreightBillToCompany_or_&nbsp
  1068. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1069. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1070. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1071. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1072. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1074. //
  1075. //
  1076. // // MerchandiseBillToCompany_or_&nbsp
  1077. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1078. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1079. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1080. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1081. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1082. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1083. //
  1084. //
  1085. // // ReturnToCompany_or_&nbsp
  1086. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1087. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1088. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1089. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1090. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1091. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1092. // //
  1093. //
  1094. // // DebugLog(@"more info : %@",moreInfo);
  1095. //
  1096. // // handling fee
  1097. // handlingFee = sqlite3_column_double(statement, 33);
  1098. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1099. // if (sqlite3_column_int(statement, 58)) {
  1100. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1101. // }
  1102. // //
  1103. //
  1104. // // customer info
  1105. // customerID = [self textAtColumn:36 statement:statement];
  1106. //
  1107. // // mode
  1108. // ret[@"mode"] = appDelegate.mode;
  1109. //
  1110. // // model_count
  1111. // ret[@"model_count"] = @(0);
  1112. }
  1113. sqlite3_finalize(statement);
  1114. }
  1115. [iSalesDB close_db:db];
  1116. data[@"order_type"]=order_type;
  1117. /*
  1118. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1119. // "customer_email" = "Shui Hu";
  1120. // "customer_fax" = "";
  1121. // "customer_first_name" = F;
  1122. // "customer_last_name" = L;
  1123. // "customer_name" = ",da He Xiang Dong Liu A";
  1124. // "customer_phone" = "Hey Xuan Feng";
  1125. contactInfo[@"customer_phone"] = customer_phone;
  1126. contactInfo[@"customer_fax"] = customer_fax;
  1127. contactInfo[@"customer_email"] = customer_email;
  1128. NSString *first_name = @"";
  1129. NSString *last_name = @"";
  1130. if ([customer_contact isEqualToString:@""]) {
  1131. } else if ([customer_contact containsString:@" "]) {
  1132. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1133. first_name = [customer_contact substringToIndex:first_space_index];
  1134. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1135. }
  1136. contactInfo[@"customer_first_name"] = first_name;
  1137. contactInfo[@"customer_last_name"] = last_name;
  1138. ret[@"customerInfo"] = contactInfo;
  1139. // models
  1140. if (nssoid) {
  1141. __block double TotalCuft = 0;
  1142. __block double TotalWeight = 0;
  1143. __block int TotalCarton = 0;
  1144. __block double allItemPrice = 0;
  1145. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  1146. sqlite3 *db1 = [iSalesDB get_db];
  1147. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1148. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1149. int product_id = sqlite3_column_int(stmt, 0);
  1150. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1151. int item_id = sqlite3_column_int(stmt, 7);
  1152. NSString* Price=nil;
  1153. if(str_price==nil)
  1154. {
  1155. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1156. if(price==nil)
  1157. Price=@"No Price.";
  1158. else
  1159. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1160. }
  1161. else
  1162. {
  1163. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1164. }
  1165. double discount = sqlite3_column_double(stmt, 2);
  1166. int item_count = sqlite3_column_int(stmt, 3);
  1167. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1168. NSString *nsline_note=nil;
  1169. if(line_note!=nil)
  1170. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1171. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1172. // NSString *nsname = nil;
  1173. // if(name!=nil)
  1174. // nsname= [[NSString alloc]initWithUTF8String:name];
  1175. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1176. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1177. // NSString *nsdescription=nil;
  1178. // if(description!=nil)
  1179. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1180. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1181. // int stockUom = sqlite3_column_int(stmt, 8);
  1182. // int _id = sqlite3_column_int(stmt, 9);
  1183. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1184. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1185. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1186. int carton=[bsubtotaljson[@"carton"] intValue];
  1187. TotalCuft += cuft;
  1188. TotalWeight += weight;
  1189. TotalCarton += carton;
  1190. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1191. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1192. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1193. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1194. itemjson[@"note"]=nsline_note;
  1195. itemjson[@"origin_price"] = Price;
  1196. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1197. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1198. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1199. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1200. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1201. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1202. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1203. if(itemjson[@"combine"] != nil)
  1204. {
  1205. // int citem=0;
  1206. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1207. for(int bc=0;bc<bcount;bc++)
  1208. {
  1209. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1210. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1211. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1212. subTotal += uprice * modulus * item_count;
  1213. }
  1214. }
  1215. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1216. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1217. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1218. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1219. allItemPrice += subTotal;
  1220. }];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1223. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1224. // payments/Credits
  1225. // payments_and_credist = sqlite3_column_double(statement, 34);
  1226. payments_and_credist = allItemPrice;
  1227. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1228. // // total
  1229. // totalPrice = sqlite3_column_double(statement, 35);
  1230. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1231. } else {
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1234. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1235. // payments/Credits
  1236. payments_and_credist = 0;
  1237. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1238. }
  1239. // total
  1240. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1241. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1242. ret[@"order_info"]= orderinfo;
  1243. ret[@"more_order_info"] = moreInfo;
  1244. return [RAConvertor dict2data:ret];
  1245. */
  1246. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1247. return data;
  1248. }
  1249. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1250. {
  1251. assert(params[@"user"]!=nil);
  1252. NSString* template_file=nil;
  1253. switch([params[@"thumb"] intValue])
  1254. {
  1255. case 0:
  1256. template_file= @"so.json";
  1257. break;
  1258. case 1:
  1259. template_file= @"so_thumb.json";
  1260. break;
  1261. default:
  1262. template_file= @"so.json";
  1263. }
  1264. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1265. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1266. if(data[@"table_model"][@"row2"]==nil)
  1267. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1268. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1269. DebugLog(@"%@",file);
  1270. // return nil;
  1271. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1272. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1273. if (file) {
  1274. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1275. [dic setObject:file forKey:@"pdf_path"];
  1276. dic[@"isLocalFile"]=@"true";
  1277. return [RAConvertor dict2data:dic];
  1278. }
  1279. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1280. dic[@"isLocalFile"]=@"true";
  1281. return [RAConvertor dict2data:dic];
  1282. }
  1283. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1284. {
  1285. // assert(params[@"contact_id"]!=nil);
  1286. // assert(params[@"user"]!=nil);
  1287. if(params[@"user"]==nil)
  1288. {
  1289. AppDelegate *appDelegate = nil;
  1290. //some UI methods ej
  1291. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1292. if(appDelegate.user!=nil)
  1293. [params setValue:appDelegate.user forKey:@"user"];
  1294. }
  1295. NSString* template_file=nil;
  1296. switch([params[@"pdf_style"] intValue])
  1297. {
  1298. case 0:
  1299. template_file= @"portfolio_2x3.json";
  1300. break;
  1301. case 1:
  1302. template_file= @"portfolio_3x2.json";
  1303. break;
  1304. default:
  1305. template_file= @"portfolio_3x2.json";
  1306. }
  1307. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1308. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1309. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1310. DebugLog(@"%@",file);
  1311. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1312. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1313. if (file) {
  1314. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1315. [dic setObject:file forKey:@"pdf_path"];
  1316. dic[@"isLocalFile"]=@"true";
  1317. return [RAConvertor dict2data:dic];
  1318. }
  1319. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1320. dic[@"isLocalFile"]=@"true";
  1321. return [RAConvertor dict2data:dic];
  1322. }
  1323. +(NSString*) get_offline_soid:(sqlite3*)db
  1324. {
  1325. NSString* soid=nil;
  1326. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1327. [formatter setDateFormat:@"yyMMdd"];
  1328. NSString* date = [formatter stringFromDate:[NSDate date]];
  1329. for(int i=1;i<999;i++)
  1330. {
  1331. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1332. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1333. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1334. if(count==0)
  1335. return soid;
  1336. }
  1337. return nil;
  1338. }
  1339. +(NSArray*) enumOfflineOrder
  1340. {
  1341. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1342. sqlite3 *db = [iSalesDB get_db];
  1343. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1344. sqlite3_stmt * statement;
  1345. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1346. if ( dbresult== SQLITE_OK)
  1347. {
  1348. while (sqlite3_step(statement) == SQLITE_ROW)
  1349. {
  1350. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1351. NSString* nsso_id=nil;
  1352. if(so_id!=nil)
  1353. {
  1354. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1355. }
  1356. [ret addObject:nsso_id];
  1357. }
  1358. sqlite3_finalize(statement);
  1359. }
  1360. [iSalesDB close_db:db];
  1361. return ret;
  1362. }
  1363. //+(void) uploadFile:(NSString*) file
  1364. //{
  1365. // NSData* data = [NSData dataWithContentsOfFile: file];
  1366. // UIApplication * app = [UIApplication sharedApplication];
  1367. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1368. //
  1369. //
  1370. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1371. //
  1372. //
  1373. //
  1374. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1375. //
  1376. // if(appDelegate.user!=nil)
  1377. // [params setValue:appDelegate.user forKey:@"user"];
  1378. // // if(appDelegate.contact_id!=nil)
  1379. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1380. // if(appDelegate.password!=nil)
  1381. // [params setValue:appDelegate.password forKey:@"password"];
  1382. //
  1383. //
  1384. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1385. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1386. // } error:nil];
  1387. //
  1388. //
  1389. //
  1390. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1391. //
  1392. // NSProgress *progress = nil;
  1393. //
  1394. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1395. //
  1396. //
  1397. //
  1398. // if (error) {
  1399. //
  1400. // NSString* err_msg = [error localizedDescription];
  1401. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1402. //
  1403. //
  1404. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1405. //
  1406. // DebugLog(@"data string: %@",str);
  1407. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1408. //
  1409. // } else {
  1410. // DebugLog(@"response ");
  1411. //
  1412. //
  1413. //
  1414. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1415. //
  1416. // // 再将NSData转为字符串
  1417. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1418. //
  1419. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1420. //
  1421. // DebugLog(@"data string: %@",jsonStr);
  1422. //
  1423. // NSDictionary* json = responseObject;
  1424. //
  1425. //
  1426. // if([[json valueForKey:@"result"] intValue]==2)
  1427. // {
  1428. //// NSString* img_url_down = json[@"img_url_aname"];
  1429. //// NSString* img_url_up = json[@"img_url"];
  1430. //
  1431. //
  1432. // }
  1433. // else
  1434. // {
  1435. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1436. // }
  1437. //
  1438. //
  1439. // }
  1440. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1441. // }];
  1442. //
  1443. //
  1444. //
  1445. //
  1446. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1447. // //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. //
  1454. // [uploadTask resume];
  1455. //
  1456. //}
  1457. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1458. //{
  1459. //
  1460. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1461. // NSString *documents = [paths objectAtIndex:0];
  1462. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1463. //
  1464. //
  1465. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1466. //
  1467. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1468. //
  1469. //
  1470. //
  1471. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1472. //
  1473. //
  1474. //
  1475. //
  1476. //
  1477. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1478. //
  1479. //
  1480. //
  1481. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1482. //
  1483. //
  1484. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1485. //
  1486. //
  1487. //
  1488. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1489. //
  1490. //
  1491. //
  1492. //
  1493. // NSMutableDictionary* orderlist = nil;
  1494. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1495. //
  1496. //
  1497. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1498. //
  1499. //
  1500. // orderlist = [NSMutableDictionary new];
  1501. //
  1502. //// [orderlist addObject:appDelegate.order_code];
  1503. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1504. //
  1505. // }
  1506. // else
  1507. // {
  1508. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1509. //
  1510. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1511. //
  1512. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1513. // }
  1514. //
  1515. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1516. // orderitem[@"create_by"] = appDelegate.user;
  1517. // orderitem[@"price"] = upparams[@"total_price"];
  1518. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1519. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1520. // orderitem[@"order_status"] = @"Saved Order";
  1521. // orderitem[@"order_code"] = appDelegate.order_code;
  1522. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1523. //
  1524. //
  1525. // int count = [orderlist[@"count"] intValue];
  1526. //
  1527. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1528. //
  1529. // newlist[@"item_0"]=orderitem;
  1530. // int newcount = 1;
  1531. // for(int i=0;i<count;i++)
  1532. // {
  1533. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1534. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1535. // continue;
  1536. // else
  1537. // {
  1538. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1539. // newcount++;
  1540. // }
  1541. // }
  1542. // newlist[@"count"] = @(newcount);
  1543. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1544. //
  1545. // return;
  1546. //}
  1547. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1548. {
  1549. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1550. // NSString *documents = [paths objectAtIndex:0];
  1551. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1552. NSString *userdir = [OLDataProvider getUserPath];
  1553. NSMutableDictionary* orderlist = nil;
  1554. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1555. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1556. // order list 为空,不用维护
  1557. #ifdef RA_NOTIFICATION
  1558. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1559. #else
  1560. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1561. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1562. #endif
  1563. return;
  1564. // [orderlist addObject:appDelegate.order_code];
  1565. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1566. }
  1567. else
  1568. {
  1569. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1570. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1571. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1572. }
  1573. //==================== order list 删除
  1574. int count = [orderlist[@"count"] intValue];
  1575. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1576. NSMutableDictionary* submititem =nil;
  1577. int newcount = 0;
  1578. for(int i=0;i<count;i++)
  1579. {
  1580. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1581. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1582. {
  1583. submititem = item;
  1584. continue;
  1585. }
  1586. else
  1587. {
  1588. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1589. newcount++;
  1590. }
  1591. }
  1592. newlist[@"count"] = @(newcount);
  1593. [RAUtils dicttofile:orderlistPath dict:newlist];
  1594. //////////////////---=============================== submitlist 添加
  1595. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1596. NSMutableDictionary* submitlist = nil;
  1597. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1598. submitlist = [NSMutableDictionary new];
  1599. }
  1600. else
  1601. {
  1602. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1603. }
  1604. int submitcount =[submitlist[@"count"] intValue];
  1605. submititem[@"order_status"] = soid;//@"Submitted Order";
  1606. submititem[@"customer_name"] = company;//@"Submitted Order";
  1607. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1608. submitlist[@"count"] = @(submitcount+1);
  1609. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1610. #ifdef RA_NOTIFICATION
  1611. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1612. #else
  1613. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1614. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1615. #endif
  1616. }
  1617. +(void) save2submitScanOrder1
  1618. {
  1619. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1620. // NSString *documents = [paths objectAtIndex:0];
  1621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1622. NSString *userdir = [OLDataProvider getUserPath];
  1623. NSMutableDictionary* orderlist = nil;
  1624. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1625. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1626. // order list 为空,不用维护
  1627. #ifdef RA_NOTIFICATION
  1628. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1629. #else
  1630. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1631. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1632. #endif
  1633. return;
  1634. // [orderlist addObject:appDelegate.order_code];
  1635. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1636. }
  1637. else
  1638. {
  1639. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1640. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1641. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1642. }
  1643. int count = [orderlist[@"count"] intValue];
  1644. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1645. int newcount = 0;
  1646. for(int i=0;i<count;i++)
  1647. {
  1648. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1649. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1650. continue;
  1651. else
  1652. {
  1653. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1654. newcount++;
  1655. }
  1656. }
  1657. newlist[@"count"] = @(newcount);
  1658. [RAUtils dicttofile:orderlistPath dict:newlist];
  1659. #ifdef RA_NOTIFICATION
  1660. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1661. #else
  1662. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1663. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1664. #endif
  1665. }
  1666. +(void) createScanTempFolder
  1667. {
  1668. NSString* scanFilePath = [self getScanTempPath];
  1669. BOOL bdir=YES;
  1670. NSFileManager* fileManager = [NSFileManager defaultManager];
  1671. if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1672. {
  1673. NSError *error = nil;
  1674. bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1675. if(!bsuccess)
  1676. DebugLog(@"Create cache folder failed");
  1677. // if(bsuccess)
  1678. // {
  1679. // sqlite3 *db = [self get_db];
  1680. //
  1681. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1682. // [iSalesDB close_db:db];
  1683. // }
  1684. }
  1685. }
  1686. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1687. {
  1688. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1689. NSString *documents = [paths objectAtIndex:0];
  1690. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1691. NSString* servername = addressDic[@"name"];
  1692. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1693. NSString* user = appDelegate.user;
  1694. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1695. orderpath = [orderpath stringByAppendingPathComponent:user];
  1696. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1697. BOOL bdir=YES;
  1698. NSFileManager* fileManager = [NSFileManager defaultManager];
  1699. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1700. {
  1701. NSError *error = nil;
  1702. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1703. if(!bsuccess)
  1704. DebugLog(@"Create cache folder failed");
  1705. // if(bsuccess)
  1706. // {
  1707. // sqlite3 *db = [self get_db];
  1708. //
  1709. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1710. // [iSalesDB close_db:db];
  1711. // }
  1712. }
  1713. return orderpath;
  1714. }
  1715. +(NSString*) getScanOrderPath
  1716. {
  1717. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1718. NSString *documents = [paths objectAtIndex:0];
  1719. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1720. NSString* servername = addressDic[@"name"];
  1721. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1722. NSString* user = appDelegate.user;
  1723. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1724. orderpath = [orderpath stringByAppendingPathComponent:user];
  1725. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1726. BOOL bdir=YES;
  1727. NSFileManager* fileManager = [NSFileManager defaultManager];
  1728. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1729. {
  1730. NSError *error = nil;
  1731. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1732. if(!bsuccess)
  1733. DebugLog(@"Create cache folder failed");
  1734. // if(bsuccess)
  1735. // {
  1736. // sqlite3 *db = [self get_db];
  1737. //
  1738. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1739. // [iSalesDB close_db:db];
  1740. // }
  1741. }
  1742. return orderpath;
  1743. }
  1744. +(NSString*) getScanTempPath
  1745. {
  1746. if(RASingleton.sharedInstance.scan_temp_code == nil)
  1747. RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1748. return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1749. }
  1750. +(void) moveScanTemp2Order
  1751. {
  1752. NSString *orderdir = [OLDataProvider getScanOrderPath];
  1753. NSError *error = nil;
  1754. NSString* tempdir = [OLDataProvider getScanTempPath];
  1755. // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1756. // for(int i=0;i<arr_files.count;i++)
  1757. // {
  1758. // NSString* file=arr_files[i];
  1759. //
  1760. //
  1761. // }
  1762. [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1763. }
  1764. +(NSString*) getUserPath
  1765. {
  1766. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1767. NSString *documents = [paths objectAtIndex:0];
  1768. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1769. NSString* servername = addressDic[@"name"];
  1770. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1771. NSString* user = appDelegate.user;
  1772. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1773. userpath = [userpath stringByAppendingPathComponent:user];
  1774. return userpath;
  1775. }
  1776. +(NSString*) getScanPath
  1777. {
  1778. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1779. if(appDelegate.order_code!=nil)
  1780. {
  1781. return [self getScanOrderPath];
  1782. }
  1783. else
  1784. {
  1785. [self createScanTempFolder];
  1786. return [self getScanTempPath];
  1787. }
  1788. }
  1789. +(NSString*) createScanTempCode
  1790. {
  1791. return [[NSUUID new] UUIDString];
  1792. }
  1793. +(NSString*) scanTemplatePath:(NSString*)file
  1794. {
  1795. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1796. NSString *documents = [paths objectAtIndex:0];
  1797. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1798. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1799. return templatefile;
  1800. }
  1801. +(void) realguestLogin
  1802. {
  1803. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1804. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1805. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1806. }
  1807. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1808. {
  1809. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1810. NSString *documents = [paths objectAtIndex:0];
  1811. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1812. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1813. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1814. return [dict mutableCopy];
  1815. }
  1816. +(void) deleteSubmittedOrder:(NSString*) order_code
  1817. {
  1818. NSString *userdir = [OLDataProvider getUserPath];
  1819. if(order_code.length==0)
  1820. return;
  1821. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1822. // NSString *documents = [paths objectAtIndex:0];
  1823. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1824. if([appDelegate.order_code isEqualToString:order_code])
  1825. {
  1826. //要删除的订单是当前打开的订单;
  1827. appDelegate.order_code = nil;
  1828. appDelegate.customerInfo = nil;
  1829. RASingleton.sharedInstance.scan_cart = nil;
  1830. appDelegate.contact_id = nil;
  1831. // [appDelegate updateScanButton:false];
  1832. [appDelegate update_count_mark];
  1833. }
  1834. //删除订单目录
  1835. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1836. //维护订单列表
  1837. NSMutableDictionary* orderlist = nil;
  1838. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1839. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1840. // order list 为空,不用维护
  1841. #ifdef RA_NOTIFICATION
  1842. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1843. #else
  1844. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1845. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1846. #endif
  1847. return;
  1848. // [orderlist addObject:appDelegate.order_code];
  1849. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1850. }
  1851. else
  1852. {
  1853. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1854. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1855. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1856. }
  1857. int count = [orderlist[@"count"] intValue];
  1858. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1859. int newcount = 0;
  1860. for(int i=0;i<count;i++)
  1861. {
  1862. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1863. if([item[@"order_code"] isEqualToString: order_code ])
  1864. continue;
  1865. else
  1866. {
  1867. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1868. newcount++;
  1869. }
  1870. }
  1871. newlist[@"count"] = @(newcount);
  1872. [RAUtils dicttofile:orderlistPath dict:newlist];
  1873. #ifdef RA_NOTIFICATION
  1874. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1875. #else
  1876. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1877. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1878. #endif
  1879. }
  1880. +(void) updateScanOrder:(NSString*) order_code
  1881. {
  1882. NSString *userdir = [OLDataProvider getUserPath];
  1883. if(order_code.length==0)
  1884. return;
  1885. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1886. // NSString *documents = [paths objectAtIndex:0];
  1887. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1888. NSMutableDictionary* orderlist = nil;
  1889. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1890. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1891. // order list 为空,不用维护
  1892. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1893. return;
  1894. // [orderlist addObject:appDelegate.order_code];
  1895. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1896. }
  1897. else
  1898. {
  1899. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1900. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1901. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1902. }
  1903. int count = [orderlist[@"count"] intValue];
  1904. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1905. float p =0;
  1906. float s =0;
  1907. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1908. {
  1909. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1910. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1911. int mcount = [cartitem[@"count"] intValue];
  1912. double msurcharge =0;
  1913. if(RASingleton.sharedInstance.price_type==1)
  1914. {
  1915. if([cartitem[@"special_price"] boolValue])
  1916. {
  1917. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price2"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1918. }
  1919. else
  1920. {
  1921. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price1"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1922. }
  1923. }
  1924. s+=msurcharge;
  1925. }
  1926. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1927. int order_qty=0;
  1928. for(int c=0;c<[section[@"count"] intValue];c++)
  1929. {
  1930. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1931. order_qty+=[item[@"count"] intValue];
  1932. }
  1933. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1934. // int newcount = 0;
  1935. for(int i=0;i<count;i++)
  1936. {
  1937. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1938. if([item[@"order_code"] isEqualToString: order_code ])
  1939. {
  1940. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1941. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1942. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1943. }
  1944. }
  1945. // newlist[@"count"] = @(newcount);
  1946. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1947. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1948. }
  1949. +(void) deleteScanOrder:(NSString*) order_code
  1950. {
  1951. NSString *userdir = [OLDataProvider getUserPath];
  1952. if(order_code.length==0)
  1953. return;
  1954. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1955. // NSString *documents = [paths objectAtIndex:0];
  1956. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1957. if([appDelegate.order_code isEqualToString:order_code])
  1958. {
  1959. //要删除的订单是当前打开的订单;
  1960. appDelegate.order_code = nil;
  1961. appDelegate.customerInfo = nil;
  1962. RASingleton.sharedInstance.scan_cart = nil;
  1963. appDelegate.contact_id = nil;
  1964. // [appDelegate updateScanButton:false];
  1965. [appDelegate update_count_mark];
  1966. }
  1967. //删除订单目录
  1968. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1969. //维护订单列表
  1970. NSMutableDictionary* orderlist = nil;
  1971. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1972. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1973. // order list 为空,不用维护
  1974. #ifdef RA_NOTIFICATION
  1975. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1976. #else
  1977. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1978. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1979. #endif
  1980. return;
  1981. // [orderlist addObject:appDelegate.order_code];
  1982. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1983. }
  1984. else
  1985. {
  1986. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1987. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1988. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1989. }
  1990. int count = [orderlist[@"count"] intValue];
  1991. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1992. int newcount = 0;
  1993. for(int i=0;i<count;i++)
  1994. {
  1995. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1996. if([item[@"order_code"] isEqualToString: order_code ])
  1997. continue;
  1998. else
  1999. {
  2000. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2001. newcount++;
  2002. }
  2003. }
  2004. newlist[@"count"] = @(newcount);
  2005. [RAUtils dicttofile:orderlistPath dict:newlist];
  2006. #ifdef RA_NOTIFICATION
  2007. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2008. #else
  2009. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2010. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2011. #endif
  2012. }
  2013. +(void) saveScanCart:(NSMutableDictionary*) cart
  2014. {
  2015. if(cart==nil)
  2016. return;
  2017. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2018. // NSString *documents = [paths objectAtIndex:0];
  2019. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2020. if(RASingleton.sharedInstance.price_type==0)
  2021. {
  2022. NSMutableDictionary * section = cart[@"section_0"];
  2023. int count = [section[@"count"] intValue];
  2024. for(int i=0;i<count;i++)
  2025. {
  2026. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2027. [item removeObjectForKey:@"discount"];
  2028. [item removeObjectForKey:@"note"];
  2029. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2030. }
  2031. cart[@"section_0"]=section;
  2032. }
  2033. NSString *orderdir = [self getScanPath];
  2034. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2035. [RAUtils dicttofile:cartpath dict:cart];
  2036. }
  2037. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2038. {
  2039. assert(add_params[@"user"]!=nil);
  2040. // assert(add_params[@"contact_id"]!=nil);
  2041. // assert(add_params[@"password"]!=nil);
  2042. NSString* serial= [[NSUUID UUID] UUIDString];
  2043. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2044. NSString *cachefolder = [paths objectAtIndex:0];
  2045. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2046. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2047. NSFileManager* fileManager = [NSFileManager defaultManager];
  2048. BOOL bdir=YES;
  2049. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2050. {
  2051. NSError *error = nil;
  2052. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2053. if(!bsuccess)
  2054. DebugLog(@"Create UPLOAD folder failed");
  2055. }
  2056. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2057. ret[@"contact"]=[self prepareContact:serial];
  2058. ret[@"wishlist"]=[self prepareWishlist:serial];
  2059. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2060. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2061. ret[@"view_portfolio"] = [self preparePDF:serial];
  2062. NSString* str= [RAConvertor dict2string:ret];
  2063. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2064. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2065. NSError *error=nil;
  2066. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2067. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2068. ZipArchive* zip = [[ZipArchive alloc] init];
  2069. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2070. for(int i=0;i<arr_files.count;i++)
  2071. {
  2072. NSString* file=arr_files[i];
  2073. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2074. }
  2075. if( ![zip CloseZipFile2] )
  2076. {
  2077. zippath = @"";
  2078. }
  2079. ret[@"file"]=zippath;
  2080. return ret;
  2081. }
  2082. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2083. {
  2084. if(filename.length==0)
  2085. return false;
  2086. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2087. bool ret=false;
  2088. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2089. NSString *cachefolder = [paths objectAtIndex:0];
  2090. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2091. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2092. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2093. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2094. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2095. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2096. NSFileManager* fileManager = [NSFileManager defaultManager];
  2097. BOOL bdir=NO;
  2098. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2099. {
  2100. NSError *error = nil;
  2101. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2102. {
  2103. ret=false;
  2104. }
  2105. else
  2106. {
  2107. ret=true;
  2108. }
  2109. }
  2110. return ret;
  2111. }
  2112. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2113. {
  2114. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2115. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2116. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2117. NSString* where=@" 1=1";
  2118. if (ver!=nil) {
  2119. where=@"is_dirty=1";
  2120. }
  2121. sqlite3 *db = [iSalesDB get_db];
  2122. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2123. sqlite3_stmt * statement;
  2124. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2125. int count=0;
  2126. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2127. if ( dbresult== SQLITE_OK)
  2128. {
  2129. while (sqlite3_step(statement) == SQLITE_ROW)
  2130. {
  2131. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2132. int _id = sqlite3_column_int(statement, 0);
  2133. int product_id = sqlite3_column_int(statement, 1);
  2134. int item_id = sqlite3_column_int(statement, 2);
  2135. int qty = sqlite3_column_int(statement, 3);
  2136. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2137. NSString* nscreate_time=nil;
  2138. if(create_time!=nil)
  2139. {
  2140. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2141. }
  2142. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2143. NSString* nsmodify_time=nil;
  2144. if(modify_time!=nil)
  2145. {
  2146. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2147. }
  2148. int is_delete = sqlite3_column_int(statement, 6);
  2149. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2150. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2151. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2152. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2153. itemjson[@"create_time"]=nscreate_time;
  2154. itemjson[@"modify_time"]=nsmodify_time;
  2155. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2156. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2157. count++;
  2158. }
  2159. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2160. sqlite3_finalize(statement);
  2161. }
  2162. ret[@"count"]=[NSNumber numberWithInt:count ];
  2163. [iSalesDB close_db:db];
  2164. return ret;
  2165. }
  2166. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2167. {
  2168. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2169. // UIApplication * app = [UIApplication sharedApplication];
  2170. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2171. assert(add_params[@"user"]!=nil);
  2172. // assert(add_params[@"password"]!=nil);
  2173. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  2174. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  2175. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2176. DebugLog(@"offline_login sql:%@",sqlQuery);
  2177. sqlite3_stmt * statement;
  2178. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2179. int count=0;
  2180. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2181. if ( dbresult== SQLITE_OK)
  2182. {
  2183. while (sqlite3_step(statement) == SQLITE_ROW)
  2184. {
  2185. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2186. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2187. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2188. int product_id = sqlite3_column_int(statement, 0);
  2189. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2190. int item_id = sqlite3_column_int(statement, 7);
  2191. NSString* Price=nil;
  2192. if(str_price==nil)
  2193. {
  2194. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2195. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2196. if(price==nil)
  2197. Price=@"No Price.";
  2198. else
  2199. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2200. }
  2201. else
  2202. {
  2203. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2204. }
  2205. double discount = sqlite3_column_double(statement, 2);
  2206. int item_count = sqlite3_column_int(statement, 3);
  2207. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2208. NSString *nsline_note=nil;
  2209. if(line_note!=nil)
  2210. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2211. char *name = (char*)sqlite3_column_text(statement, 5);
  2212. NSString *nsname=nil;
  2213. if(name!=nil)
  2214. nsname= [[NSString alloc]initWithUTF8String:name];
  2215. char *description = (char*)sqlite3_column_text(statement, 6);
  2216. NSString *nsdescription=nil;
  2217. if(description!=nil)
  2218. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2219. // int stockUom = sqlite3_column_int(statement, 8);
  2220. // int _id = sqlite3_column_int(statement, 9);
  2221. //
  2222. //
  2223. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2224. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2225. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2226. // int carton=[bsubtotaljson[@"carton"] intValue];
  2227. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2228. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2229. itemjson[@"model"]=nsname;
  2230. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2231. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2232. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2233. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2234. // itemjson[@"check"]=@"true";
  2235. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2236. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2237. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2238. itemjson[@"unit_price"]=Price;
  2239. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2240. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2241. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2242. itemjson[@"note"]=nsline_note;
  2243. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2244. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2245. count++;
  2246. }
  2247. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2248. sqlite3_finalize(statement);
  2249. }
  2250. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2251. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2252. return ret;
  2253. }
  2254. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2255. {
  2256. assert(add_params[@"user"]!=nil);
  2257. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2258. sqlite3 *db = [iSalesDB get_db];
  2259. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2260. // for(int i=0;i<arr_soid.count;i++)
  2261. sqlite3_stmt * statement;
  2262. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2263. //int count=0;
  2264. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2265. if ( dbresult== SQLITE_OK)
  2266. {
  2267. int count=0;
  2268. while (sqlite3_step(statement) == SQLITE_ROW)
  2269. {
  2270. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2271. int _id = sqlite3_column_int(statement, 0);
  2272. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2273. NSString* nssync_data=nil;
  2274. if(sync_data!=nil)
  2275. {
  2276. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2277. }
  2278. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2279. NSString* nsimg_1=nil;
  2280. if(img_1!=nil)
  2281. {
  2282. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2283. [self copy_upImg:serial file:nsimg_1];
  2284. }
  2285. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2286. NSString* nsso_no=nil;
  2287. if(so_no!=nil)
  2288. {
  2289. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2290. }
  2291. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2292. itemjson[@"order_type"]=@"submit order";
  2293. else
  2294. itemjson[@"order_type"]=@"archive order";
  2295. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2296. itemjson[@"json_data"]= nssync_data;
  2297. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2298. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2299. count++;
  2300. }
  2301. ret[@"count"]=[NSNumber numberWithInt:count ];
  2302. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2303. }
  2304. sqlite3_finalize(statement);
  2305. [iSalesDB close_db:db];
  2306. return ret;
  2307. }
  2308. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2309. {
  2310. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2311. sqlite3 *db = [iSalesDB get_db];
  2312. // UIApplication * app = [UIApplication sharedApplication];
  2313. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2314. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  2315. sqlite3_stmt * statement;
  2316. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2317. int count=0;
  2318. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2319. if ( dbresult== SQLITE_OK)
  2320. {
  2321. while (sqlite3_step(statement) == SQLITE_ROW)
  2322. {
  2323. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2324. int _id = sqlite3_column_int(statement, 0);
  2325. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2326. NSString* nsimg_0=nil;
  2327. if(img_0!=nil)
  2328. {
  2329. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2330. [self copy_upImg:serial file:nsimg_0];
  2331. }
  2332. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2333. NSString* nsimg_1=nil;
  2334. if(img_1!=nil)
  2335. {
  2336. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2337. [self copy_upImg:serial file:nsimg_1];
  2338. }
  2339. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2340. NSString* nsimg_2=nil;
  2341. if(img_2!=nil)
  2342. {
  2343. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2344. [self copy_upImg:serial file:nsimg_2];
  2345. }
  2346. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2347. NSString* nssync_data=nil;
  2348. if(sync_data!=nil)
  2349. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2350. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2351. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2352. itemjson[@"json_data"]= nssync_data;
  2353. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2354. count++;
  2355. }
  2356. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2357. sqlite3_finalize(statement);
  2358. }
  2359. ret[@"count"]=[NSNumber numberWithInt:count ];
  2360. [iSalesDB close_db:db];
  2361. return ret;
  2362. }
  2363. +(bool) check_offlinedata
  2364. {
  2365. // UIApplication * app = [UIApplication sharedApplication];
  2366. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2367. sqlite3 *db = [iSalesDB get_db];
  2368. NSString * where=@"1=1";
  2369. // if(appDelegate.user!=nil)
  2370. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2371. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2372. [iSalesDB close_db:db];
  2373. if(count==0)
  2374. {
  2375. return false;
  2376. }
  2377. return true;
  2378. //
  2379. //[iSalesDB close_db:db];
  2380. }
  2381. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2382. {
  2383. // UIApplication * app = [UIApplication sharedApplication];
  2384. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2385. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2386. sqlite3 *db = [iSalesDB get_db];
  2387. NSString* collectId=params[@"collectId"];
  2388. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2389. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2390. [iSalesDB execSql:sqlQuery db:db];
  2391. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2392. [iSalesDB close_db:db];
  2393. // appDelegate.wish_count =count;
  2394. //
  2395. // [appDelegate update_count_mark];
  2396. ret[@"result"]= [NSNumber numberWithInt:2];
  2397. ret[@"wish_count"]=@(count);
  2398. return ret;
  2399. }
  2400. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2401. //{
  2402. //
  2403. // UIApplication * app = [UIApplication sharedApplication];
  2404. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2405. //
  2406. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2407. // sqlite3 *db = [iSalesDB get_db];
  2408. // NSString* product_id=params[@"product_id"];
  2409. //
  2410. //
  2411. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2412. //
  2413. //
  2414. // for(int i=0;i<arr.count;i++)
  2415. // {
  2416. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2417. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2418. // if(count==0)
  2419. // {
  2420. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2421. // [iSalesDB execSql:sqlQuery db:db];
  2422. // }
  2423. // }
  2424. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2425. // [iSalesDB close_db:db];
  2426. //
  2427. // appDelegate.wish_count =count;
  2428. //
  2429. // [appDelegate update_count_mark];
  2430. // ret[@"result"]= [NSNumber numberWithInt:2];
  2431. // return ret;
  2432. // //
  2433. // //return ret;
  2434. //}
  2435. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2436. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2437. DebugLog(@"time interval: %lf",interval);
  2438. }
  2439. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2440. //{
  2441. // UIApplication * app = [UIApplication sharedApplication];
  2442. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2443. //
  2444. // int sort = [[params objectForKey:@"sort"] intValue];
  2445. // NSString *sort_str = @"";
  2446. // switch (sort) {
  2447. // case 0:{
  2448. // sort_str = @"order by w.modify_time desc";
  2449. // }
  2450. // break;
  2451. // case 1:{
  2452. // sort_str = @"order by w.modify_time asc";
  2453. // }
  2454. // break;
  2455. // case 2:{
  2456. // sort_str = @"order by m.name asc";
  2457. // }
  2458. // break;
  2459. // case 3:{
  2460. // sort_str = @"order by m.name desc";
  2461. // }
  2462. // break;
  2463. // case 4:{
  2464. // sort_str = @"order by m.description asc";
  2465. // }
  2466. // break;
  2467. //
  2468. // default:
  2469. // break;
  2470. //
  2471. // }
  2472. //
  2473. //
  2474. //// NSString* user = appDelegate.user;
  2475. //
  2476. // sqlite3 *db = [iSalesDB get_db];
  2477. //
  2478. // // order by w.create_time
  2479. // NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  2480. //
  2481. //// NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  2482. //
  2483. //
  2484. // sqlite3_stmt * statement;
  2485. //
  2486. // NSDate *date1 = [NSDate date];
  2487. //// NSDate *date2 = nil;
  2488. //
  2489. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2490. // int count=0;
  2491. //
  2492. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2493. // {
  2494. //
  2495. //
  2496. // while (sqlite3_step(statement) == SQLITE_ROW)
  2497. // {
  2498. //
  2499. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2500. //
  2501. // int product_id = sqlite3_column_double(statement, 0);
  2502. //
  2503. //
  2504. //
  2505. //
  2506. //
  2507. // char *description = (char*)sqlite3_column_text(statement, 1);
  2508. // if(description==nil)
  2509. // description= "";
  2510. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2511. //
  2512. // int item_id = sqlite3_column_double(statement, 2);
  2513. //
  2514. // NSDate *date_image = [NSDate date];
  2515. //
  2516. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2517. //
  2518. // printf("image : ");
  2519. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2520. //
  2521. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2522. // // if(url==nil)
  2523. // // url="";
  2524. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2525. //
  2526. // int qty = sqlite3_column_int(statement, 3);
  2527. //
  2528. //
  2529. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2530. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2531. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2532. // item[@"description"]= nsdescription;
  2533. // item[@"img"]= nsurl;
  2534. //
  2535. //
  2536. //
  2537. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2538. // count++;
  2539. //
  2540. // }
  2541. // printf("total time:");
  2542. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2543. //
  2544. // ret[@"count"]= [NSNumber numberWithInt:count];
  2545. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2546. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2547. // ret[@"result"]= [NSNumber numberWithInt:2];
  2548. //
  2549. //
  2550. // appDelegate.wish_count =count;
  2551. //
  2552. // [appDelegate update_count_mark];
  2553. // sqlite3_finalize(statement);
  2554. //
  2555. //
  2556. //
  2557. //
  2558. // }
  2559. //
  2560. // [iSalesDB close_db:db];
  2561. //
  2562. // return ret;
  2563. //}
  2564. +(NSDictionary*) offline_notimpl
  2565. {
  2566. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2567. ret[@"result"]=@"8";
  2568. ret[@"err_msg"]=@"offline mode does not support this function.";
  2569. return ret;
  2570. }
  2571. +(NSDictionary*) offline_home
  2572. {
  2573. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2574. NSString *cachefolder = [paths objectAtIndex:0];
  2575. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2576. NSData* json =nil;
  2577. json=[NSData dataWithContentsOfFile:img_cache];
  2578. if(json==nil)
  2579. return nil;
  2580. NSError *error=nil;
  2581. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2582. return menu;
  2583. }
  2584. +(NSDictionary*) offline_category_menu
  2585. {
  2586. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2587. NSString *cachefolder = [paths objectAtIndex:0];
  2588. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2589. NSData* json =nil;
  2590. json=[NSData dataWithContentsOfFile:img_cache];
  2591. if(json==nil)
  2592. return nil;
  2593. NSError *error=nil;
  2594. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2595. return menu;
  2596. }
  2597. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2598. {
  2599. NSString* offline_command=params[@"offline_Command"];
  2600. NSDictionary* ret=nil;
  2601. if([offline_command isEqualToString:@"model_NIYMAL"])
  2602. {
  2603. NSString* category = params[@"category"];
  2604. ret = [self refresh_model_NIYMAL:category];
  2605. }
  2606. return ret;
  2607. }
  2608. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2609. {
  2610. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2611. [ret setValue:@"2" forKey:@"result"];
  2612. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2613. sqlite3* db= [iSalesDB get_db];
  2614. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2615. [iSalesDB close_db:db];
  2616. [ret setObject:detail1_section forKey:@"detail_1"];
  2617. return ret;
  2618. }
  2619. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2620. {
  2621. //assert(user!=nil);
  2622. // UIApplication * app = [UIApplication sharedApplication];
  2623. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2624. // NSArray* arr1 = [self get_user_all_price_type];
  2625. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2626. // NSSet *set1 = [NSSet setWithArray:arr1];
  2627. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2628. // if(appDelegate.contact_id==nil)
  2629. // set2=[set1 mutableCopy];
  2630. // else
  2631. // [set2 intersectsSet:set1];
  2632. // NSArray *retarr = [set2 allObjects];
  2633. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2634. // sqlite3 *db = [iSalesDB get_db];
  2635. NSString* sqlQuery = nil;
  2636. if(contact_id==nil)
  2637. {
  2638. if(!blogin)
  2639. return nil;
  2640. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  2641. }
  2642. else
  2643. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  2644. sqlite3_stmt * statement;
  2645. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2646. int count=0;
  2647. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2648. {
  2649. while (sqlite3_step(statement) == SQLITE_ROW)
  2650. {
  2651. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2652. char *name = (char*)sqlite3_column_text(statement, 0);
  2653. if(name==nil)
  2654. name="";
  2655. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2656. // double price = sqlite3_column_double(statement, 1);
  2657. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2658. // if(isnull==nil)
  2659. // item[nsname]= @"No Price";
  2660. // else
  2661. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2662. char *price = (char*)sqlite3_column_text(statement, 1);
  2663. if(price!=nil)
  2664. {
  2665. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2666. nsprice=[AESCrypt fastdecrypt:nsprice];
  2667. if(nsprice.length>0)
  2668. {
  2669. double dp= [nsprice doubleValue];
  2670. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2671. }
  2672. }
  2673. else
  2674. {
  2675. item[nsname]= @"No Price";
  2676. }
  2677. // int type= sqlite3_column_int(statement, 2);
  2678. //item[@"type"]=@"price";
  2679. // item[nsname]= nsprice;
  2680. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2681. count++;
  2682. }
  2683. ret[@"count"]= [NSNumber numberWithInt:count];
  2684. sqlite3_finalize(statement);
  2685. }
  2686. // [iSalesDB close_db:db];
  2687. return ret;
  2688. }
  2689. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2690. //{
  2691. // NSArray* arr1 = [self get_user_all_price_type:db];
  2692. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2693. //
  2694. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2695. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2696. // // [set2 intersectsSet:set1];
  2697. // //
  2698. // //
  2699. // // NSArray *retarr = [set2 allObjects];
  2700. //
  2701. // NSString* whereprice=nil;
  2702. // if(contact_id==nil)
  2703. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2704. // else
  2705. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2706. //
  2707. //
  2708. // // sqlite3 *db = [iSalesDB get_db];
  2709. //
  2710. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2711. // sqlite3_stmt * statement;
  2712. //
  2713. //
  2714. // NSNumber* ret = nil;
  2715. // double dprice=DBL_MAX;
  2716. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2717. // {
  2718. //
  2719. //
  2720. // while (sqlite3_step(statement) == SQLITE_ROW)
  2721. // {
  2722. //
  2723. // // double val = sqlite3_column_double(statement, 0);
  2724. // char *price = (char*)sqlite3_column_text(statement, 0);
  2725. // if(price!=nil)
  2726. // {
  2727. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2728. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2729. // if(nsprice.length>0)
  2730. // {
  2731. // double dp= [nsprice doubleValue];
  2732. // if(dp<dprice)
  2733. // dprice=dp;
  2734. // }
  2735. // }
  2736. // }
  2737. //
  2738. //
  2739. //
  2740. //
  2741. // sqlite3_finalize(statement);
  2742. //
  2743. //
  2744. //
  2745. //
  2746. // }
  2747. //
  2748. // // [iSalesDB close_db:db];
  2749. //
  2750. // if(dprice==DBL_MAX)
  2751. // ret= nil;
  2752. // else
  2753. // ret= [NSNumber numberWithDouble:dprice];
  2754. // return ret;
  2755. //}
  2756. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2757. {
  2758. assert(user!=nil);
  2759. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2760. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2761. NSString* whereprice=nil;
  2762. if(contact_id==nil)
  2763. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2764. else
  2765. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2766. // sqlite3 *db = [iSalesDB get_db];
  2767. NSString *productIdCondition = @"1 = 1";
  2768. if (product_id) {
  2769. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2770. }
  2771. NSString *itemIdCondition = @"";
  2772. if (item_id) {
  2773. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2774. }
  2775. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2776. sqlite3_stmt * statement;
  2777. NSNumber* ret = nil;
  2778. double dprice=DBL_MAX;
  2779. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2780. {
  2781. while (sqlite3_step(statement) == SQLITE_ROW)
  2782. {
  2783. // double val = sqlite3_column_double(statement, 0);
  2784. char *price = (char*)sqlite3_column_text(statement, 0);
  2785. if(price!=nil)
  2786. {
  2787. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2788. nsprice=[AESCrypt fastdecrypt:nsprice];
  2789. if(nsprice.length>0)
  2790. {
  2791. double dp= [nsprice doubleValue];
  2792. if(dp<dprice)
  2793. dprice=dp;
  2794. }
  2795. }
  2796. }
  2797. sqlite3_finalize(statement);
  2798. }
  2799. // [iSalesDB close_db:db];
  2800. if(dprice==DBL_MAX)
  2801. ret= nil;
  2802. else
  2803. ret= [NSNumber numberWithDouble:dprice];
  2804. return ret;
  2805. }
  2806. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2807. {
  2808. assert(user!=nil);
  2809. NSArray* ret=nil;
  2810. // sqlite3 *db = [iSalesDB get_db];
  2811. // no customer assigned , use login user contact_id
  2812. // UIApplication * app = [UIApplication sharedApplication];
  2813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2814. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2815. sqlite3_stmt * statement;
  2816. // int count=0;
  2817. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2818. {
  2819. if (sqlite3_step(statement) == SQLITE_ROW)
  2820. {
  2821. char *val = (char*)sqlite3_column_text(statement, 0);
  2822. if(val==nil)
  2823. val="";
  2824. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2825. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2826. }
  2827. sqlite3_finalize(statement);
  2828. }
  2829. // [iSalesDB close_db:db];
  2830. return ret;
  2831. }
  2832. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2833. {
  2834. // sqlite3 *db = [iSalesDB get_db];
  2835. if(contact_id==nil)
  2836. {
  2837. // no customer assigned , use login user contact_id
  2838. // UIApplication * app = [UIApplication sharedApplication];
  2839. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2840. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2841. sqlite3_stmt * statement;
  2842. // int count=0;
  2843. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2844. {
  2845. if (sqlite3_step(statement) == SQLITE_ROW)
  2846. {
  2847. char *val = (char*)sqlite3_column_text(statement, 0);
  2848. if(val==nil)
  2849. val="";
  2850. contact_id = [[NSString alloc]initWithUTF8String:val];
  2851. }
  2852. sqlite3_finalize(statement);
  2853. }
  2854. if(contact_id.length<=0)
  2855. {
  2856. // [iSalesDB close_db:db];
  2857. return nil;
  2858. }
  2859. }
  2860. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2861. sqlite3_stmt * statement;
  2862. NSArray* ret=nil;
  2863. // int count=0;
  2864. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2865. {
  2866. if (sqlite3_step(statement) == SQLITE_ROW)
  2867. {
  2868. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2869. if(price_type==nil)
  2870. price_type="";
  2871. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2872. if(nsprice_type.length>0)
  2873. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2874. else
  2875. ret=nil;
  2876. }
  2877. sqlite3_finalize(statement);
  2878. }
  2879. // [iSalesDB close_db:db];
  2880. return ret;
  2881. }
  2882. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2883. {
  2884. NSString* ret= nil;
  2885. // sqlite3 *db = [iSalesDB get_db];
  2886. NSString *sqlQuery = nil;
  2887. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2888. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2889. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2890. if(product_id==nil && model_name)
  2891. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2892. else if (product_id)
  2893. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2894. sqlite3_stmt * statement;
  2895. // int count=0;
  2896. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2897. {
  2898. if (sqlite3_step(statement) == SQLITE_ROW)
  2899. {
  2900. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2901. if(imgurl==nil)
  2902. imgurl="";
  2903. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2904. ret=nsimgurl;
  2905. }
  2906. sqlite3_finalize(statement);
  2907. }
  2908. else
  2909. {
  2910. [ret setValue:@"8" forKey:@"result"];
  2911. }
  2912. // [iSalesDB close_db:db];
  2913. DebugLog(@"data string: %@",ret );
  2914. return ret;
  2915. }
  2916. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2917. {
  2918. NSString* ret= nil;
  2919. sqlite3 *db = [iSalesDB get_db];
  2920. NSString *sqlQuery = nil;
  2921. if(product_id==nil)
  2922. if(upc_code==nil)
  2923. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2924. else
  2925. sqlQuery = [NSString stringWithFormat:@"select default_category from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upc_code,upc_code,upc_code,upc_code];// select default_category from model where name='%@';
  2926. else
  2927. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2928. sqlite3_stmt * statement;
  2929. // int count=0;
  2930. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2931. {
  2932. if (sqlite3_step(statement) == SQLITE_ROW)
  2933. {
  2934. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2935. if(default_category==nil)
  2936. default_category="";
  2937. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2938. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2939. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2940. ret=nsdefault_category;
  2941. }
  2942. sqlite3_finalize(statement);
  2943. }
  2944. else
  2945. {
  2946. [ret setValue:@"8" forKey:@"result"];
  2947. }
  2948. [iSalesDB close_db:db];
  2949. DebugLog(@"data string: %@",ret );
  2950. return ret;
  2951. }
  2952. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2953. NSString *sql = nil;
  2954. if (product_id != nil) {
  2955. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2956. } else {
  2957. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2958. }
  2959. NSString *result_set = [iSalesDB jk_queryText:sql];
  2960. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2961. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2962. return result_set;
  2963. }
  2964. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2965. //{
  2966. //
  2967. //
  2968. // assert(params[@"user"]!=nil);
  2969. //
  2970. //
  2971. //
  2972. // NSString* model_name = [params valueForKey:@"product_name"];
  2973. //
  2974. // NSString* product_id = [params valueForKey:@"product_id"];
  2975. //
  2976. // NSString* category = [params valueForKey:@"category"];
  2977. //
  2978. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2979. // if(category==nil) {
  2980. // category = default_category_id;
  2981. // } else {
  2982. //
  2983. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2984. // // 参数重有多个category id
  2985. // if (arr_0.count > 1) {
  2986. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2987. //
  2988. // // 取交集
  2989. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2990. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2991. // [set_0 intersectSet:set_1];
  2992. //
  2993. // if (set_0.count == 1) {
  2994. // category = (NSString *)[set_0 anyObject];
  2995. // } else {
  2996. // if ([set_0 containsObject:default_category_id]) {
  2997. // category = default_category_id;
  2998. // } else {
  2999. // category = (NSString *)[set_0 anyObject];
  3000. // }
  3001. // }
  3002. // }
  3003. // }
  3004. //
  3005. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3006. //
  3007. //
  3008. //
  3009. //
  3010. //
  3011. //
  3012. // sqlite3 *db = [iSalesDB get_db];
  3013. //
  3014. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3015. //
  3016. //
  3017. // NSString *sqlQuery = nil;
  3018. //
  3019. // if(product_id==nil)
  3020. // sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  3021. // else
  3022. //
  3023. // sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  3024. //
  3025. //
  3026. // sqlite3_stmt * statement;
  3027. // [ret setValue:@"2" forKey:@"result"];
  3028. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3029. //
  3030. // // int count=0;
  3031. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3032. // {
  3033. //
  3034. //
  3035. // if (sqlite3_step(statement) == SQLITE_ROW)
  3036. // {
  3037. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3038. //
  3039. //
  3040. // char *name = (char*)sqlite3_column_text(statement, 0);
  3041. // if(name==nil)
  3042. // name="";
  3043. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3044. //
  3045. // char *description = (char*)sqlite3_column_text(statement, 1);
  3046. // if(description==nil)
  3047. // description="";
  3048. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3049. //
  3050. //
  3051. // int product_id = sqlite3_column_int(statement, 2);
  3052. //
  3053. //
  3054. // char *color = (char*)sqlite3_column_text(statement, 3);
  3055. // if(color==nil)
  3056. // color="";
  3057. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3058. // //
  3059. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3060. // // if(legcolor==nil)
  3061. // // legcolor="";
  3062. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3063. // //
  3064. // //
  3065. // int availability = sqlite3_column_int(statement, 5);
  3066. // //
  3067. // int incoming_stock = sqlite3_column_int(statement, 6);
  3068. //
  3069. //
  3070. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3071. // if(demension==nil)
  3072. // demension="";
  3073. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3074. //
  3075. //
  3076. //
  3077. // // ,,,,,,,,,
  3078. //
  3079. //
  3080. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3081. // if(seat_height==nil)
  3082. // seat_height="";
  3083. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3084. //
  3085. //
  3086. // char *material = (char*)sqlite3_column_text(statement, 9);
  3087. // if(material==nil)
  3088. // material="";
  3089. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3090. //
  3091. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3092. // if(box_dim==nil)
  3093. // box_dim="";
  3094. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3095. //
  3096. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3097. // if(volume==nil)
  3098. // volume="";
  3099. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3100. //
  3101. // double weight = sqlite3_column_double(statement, 12);
  3102. //
  3103. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3104. // if(model_set==nil)
  3105. // model_set="";
  3106. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3107. //
  3108. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3109. // if(load_ability==nil)
  3110. // load_ability="";
  3111. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3112. //
  3113. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3114. //// if(default_category==nil)
  3115. //// default_category="";
  3116. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3117. //
  3118. //
  3119. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3120. // if(fabric_content==nil)
  3121. // fabric_content="";
  3122. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3123. //
  3124. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3125. // if(assembling==nil)
  3126. // assembling="";
  3127. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3128. //
  3129. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3130. // if(made_in==nil)
  3131. // made_in="";
  3132. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3133. //
  3134. //
  3135. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3136. // if(special_remarks==nil)
  3137. // special_remarks="";
  3138. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3139. //
  3140. //
  3141. // int stockUcom = sqlite3_column_double(statement, 20);
  3142. //
  3143. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3144. // if(product_group==nil)
  3145. // product_group="";
  3146. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3147. //
  3148. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3149. // // if(fashion_selector==nil)
  3150. // // fashion_selector="";
  3151. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3152. //
  3153. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3154. // if(selector_field==nil)
  3155. // selector_field="";
  3156. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3157. //
  3158. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3159. // if(property_field==nil)
  3160. // property_field="";
  3161. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3162. //
  3163. //
  3164. //
  3165. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3166. // if(packaging==nil)
  3167. // packaging="";
  3168. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3169. //
  3170. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3171. //
  3172. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3173. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3174. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3175. //
  3176. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3177. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3178. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3179. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3180. //
  3181. //
  3182. //
  3183. //
  3184. //
  3185. // NSString* Availability=nil;
  3186. // if(availability>0)
  3187. // Availability=[NSString stringWithFormat:@"%d",availability];
  3188. // else
  3189. // Availability = @"Out of Stock";
  3190. //
  3191. // [img_section setValue:Availability forKey:@"Availability"];
  3192. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3193. //
  3194. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3195. //
  3196. //
  3197. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3198. // if(eta==nil)
  3199. // eta="";
  3200. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3201. // if ([nseta isEqualToString:@"null"]) {
  3202. // nseta = @"";
  3203. // }
  3204. // if (availability <= 0) {
  3205. // [img_section setValue:nseta forKey:@"ETA"];
  3206. // }
  3207. //
  3208. //
  3209. // int item_id = sqlite3_column_int(statement, 26);
  3210. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3211. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3212. //
  3213. // NSString* Price=nil;
  3214. // if(appDelegate.bLogin==false)
  3215. // Price=@"Must Sign in.";
  3216. // else
  3217. // {
  3218. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3219. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3220. // if(price==nil)
  3221. // Price=@"No Price.";
  3222. // else
  3223. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3224. // }
  3225. //
  3226. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3227. // [img_section setValue:Price forKey:@"price"];
  3228. // [img_section setValue:nsname forKey:@"model_name"];
  3229. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3230. //
  3231. // if (appDelegate.order_code) { // 离线order code即so#
  3232. //
  3233. // NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  3234. // __block int cartQTY = 0;
  3235. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3236. //
  3237. // cartQTY = sqlite3_column_int(stmt, 0);
  3238. //
  3239. // }];
  3240. //
  3241. // if (cartQTY > 0) {
  3242. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3243. // }
  3244. // }
  3245. //
  3246. // [ret setObject:img_section forKey:@"img_section"];
  3247. //
  3248. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3249. // int detail0_item_count=0;
  3250. //
  3251. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3252. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3253. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3254. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3255. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3256. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3257. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3258. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3259. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3260. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3261. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3262. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3263. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3264. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3265. //
  3266. //
  3267. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3268. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3269. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3270. // {
  3271. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3272. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3273. // }
  3274. //
  3275. //
  3276. //
  3277. //
  3278. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3279. // [detail0_section setValue:@"kv" forKey:@"type"];
  3280. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3281. //
  3282. // [ret setObject:detail0_section forKey:@"detail_0"];
  3283. //
  3284. //
  3285. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3286. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3287. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3288. // // [detail1_section setValue:@"content" forKey:@"type"];
  3289. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3290. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3291. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3292. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3293. // [ret setObject:detail1_section forKey:@"detail_1"];
  3294. //
  3295. //
  3296. //
  3297. //
  3298. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3299. // [detail2_section setValue:@"detail" forKey:@"target"];
  3300. // [detail2_section setValue:@"popup" forKey:@"action"];
  3301. // [detail2_section setValue:@"content" forKey:@"type"];
  3302. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3303. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3304. // [detail2_section setValue:@"local" forKey:@"data"];
  3305. // [ret setObject:detail2_section forKey:@"detail_2"];
  3306. // }
  3307. //
  3308. //
  3309. //
  3310. //
  3311. // sqlite3_finalize(statement);
  3312. // }
  3313. // else
  3314. // {
  3315. // [ret setValue:@"8" forKey:@"result"];
  3316. // }
  3317. //// DebugLog(@"count:%d",count);
  3318. //
  3319. //
  3320. // [iSalesDB close_db:db];
  3321. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3322. //
  3323. // return ret;
  3324. //}
  3325. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3326. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3327. NSString* keyword = [params valueForKey:@"keyword"];
  3328. keyword=keyword.lowercaseString;
  3329. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3330. int limit = [[params valueForKey:@"limit"] intValue];
  3331. int offset = [[params valueForKey:@"offset"] intValue];
  3332. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3333. NSString *limit_str = @"";
  3334. if (limited) {
  3335. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3336. }
  3337. sqlite3 *db = [iSalesDB get_db];
  3338. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3339. // UIApplication * app = [UIApplication sharedApplication];
  3340. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3341. NSString *sqlQuery = nil;
  3342. if(exactMatch )
  3343. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",params[@"orderCode"], keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  3344. else
  3345. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",params[@"orderCode"], keyword,keyword,keyword,keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  3346. DebugLog(@"offline_search sql:%@",sqlQuery);
  3347. sqlite3_stmt * statement;
  3348. [ret setValue:@"2" forKey:@"result"];
  3349. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3350. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3351. // int count=0;
  3352. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3353. {
  3354. int i=0;
  3355. while (sqlite3_step(statement) == SQLITE_ROW)
  3356. {
  3357. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3358. // char *name = (char*)sqlite3_column_text(statement, 1);
  3359. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3360. char *name = (char*)sqlite3_column_text(statement, 0);
  3361. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3362. char *description = (char*)sqlite3_column_text(statement, 1);
  3363. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3364. int product_id = sqlite3_column_int(statement, 2);
  3365. // char *url = (char*)sqlite3_column_text(statement, 3);
  3366. // if(url==nil)
  3367. // url="";
  3368. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3369. int wid = sqlite3_column_int(statement, 3);
  3370. int closeout = sqlite3_column_int(statement, 4);
  3371. int cid = sqlite3_column_int(statement, 5);
  3372. int wisdelete = sqlite3_column_int(statement, 6);
  3373. int more_color = sqlite3_column_int(statement, 7);
  3374. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3375. if(wid !=0 && wisdelete != 1)
  3376. [item setValue:@"true" forKey:@"wish_exists"];
  3377. else
  3378. [item setValue:@"false" forKey:@"wish_exists"];
  3379. if(closeout==0)
  3380. [item setValue:@"false" forKey:@"is_closeout"];
  3381. else
  3382. [item setValue:@"true" forKey:@"is_closeout"];
  3383. if(cid==0)
  3384. [item setValue:@"false" forKey:@"cart_exists"];
  3385. else
  3386. [item setValue:@"true" forKey:@"cart_exists"];
  3387. if (more_color == 0) {
  3388. [item setObject:@(false) forKey:@"more_color"];
  3389. } else if (more_color == 1) {
  3390. [item setObject:@(true) forKey:@"more_color"];
  3391. }
  3392. [item addEntriesFromDictionary:imgjson];
  3393. // [item setValue:nsurl forKey:@"img"];
  3394. [item setValue:nsname forKey:@"fash_name"];
  3395. [item setValue:nsdescription forKey:@"description"];
  3396. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3397. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3398. i++;
  3399. }
  3400. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3401. [ret setObject:items forKey:@"items"];
  3402. sqlite3_finalize(statement);
  3403. }
  3404. DebugLog(@"count:%d",count);
  3405. [iSalesDB close_db:db];
  3406. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3407. return ret;
  3408. }
  3409. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3410. {
  3411. return [self search:params limited:YES];
  3412. }
  3413. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3414. //{
  3415. // UIApplication * app = [UIApplication sharedApplication];
  3416. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3417. //
  3418. //// [iSalesDB disable_trigger]
  3419. // [iSalesDB disable_trigger];
  3420. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3421. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3422. //
  3423. // [iSalesDB enable_trigger];
  3424. //// [iSalesDB enable_trigger]
  3425. // //
  3426. // // NSString* user = [params valueForKey:@"user"];
  3427. // //
  3428. // // NSString* password = [params valueForKey:@"password"];
  3429. //
  3430. //
  3431. //
  3432. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3433. //
  3434. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3435. //
  3436. //
  3437. //
  3438. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3439. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3440. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3441. //
  3442. // [appDelegate update_count_mark];
  3443. //
  3444. //
  3445. // appDelegate.can_show_price =false;
  3446. // appDelegate.can_see_price =false;
  3447. // appDelegate.can_create_portfolio =false;
  3448. // appDelegate.can_create_order =false;
  3449. //
  3450. //
  3451. // appDelegate.can_cancel_order =false;
  3452. // appDelegate.can_set_cart_price =false;
  3453. // appDelegate.can_delete_order =false;
  3454. // appDelegate.can_submit_order =false;
  3455. // appDelegate.can_set_tearsheet_price =false;
  3456. // appDelegate.can_update_contact_info = false;
  3457. //
  3458. // appDelegate.save_order_logout = false;
  3459. // appDelegate.submit_order_logout = false;
  3460. // appDelegate.alert_sold_in_quantities = false;
  3461. //
  3462. // appDelegate.ipad_perm =nil ;
  3463. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3464. // appDelegate.OrderFilter= nil;
  3465. // [appDelegate SetSo:nil];
  3466. // [appDelegate set_main_button_panel];
  3467. //
  3468. //
  3469. // // sqlite3 *db = [iSalesDB get_db];
  3470. // //
  3471. // //
  3472. // //
  3473. // //
  3474. // //
  3475. // // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  3476. // //
  3477. // //
  3478. // //
  3479. // //
  3480. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3481. // // sqlite3_stmt * statement;
  3482. // //
  3483. // //
  3484. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3485. // //
  3486. // //
  3487. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3488. // // {
  3489. // //
  3490. // //
  3491. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3492. // // {
  3493. // //
  3494. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3495. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3496. // //
  3497. // //
  3498. // //
  3499. // // int can_show_price = sqlite3_column_int(statement, 0);
  3500. // // int can_see_price = sqlite3_column_int(statement, 1);
  3501. // //
  3502. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3503. // // if(contact_id==nil)
  3504. // // contact_id="";
  3505. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3506. // //
  3507. // // int user_type = sqlite3_column_int(statement, 3);
  3508. // //
  3509. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3510. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3511. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3512. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3513. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3514. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3515. // // int can_create_order = sqlite3_column_int(statement, 10);
  3516. // //
  3517. // //
  3518. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3519. // // if(mode==nil)
  3520. // // mode="";
  3521. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3522. // //
  3523. // //
  3524. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3525. // // if(username==nil)
  3526. // // username="";
  3527. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3528. // //
  3529. // //
  3530. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3531. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3532. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3533. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3534. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3535. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3536. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3537. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3538. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3539. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3540. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3541. // //
  3542. // // [header setValue:nsusername forKey:@"username"];
  3543. // //
  3544. // //
  3545. // // [ret setObject:header forKey:@"header"];
  3546. // // [ret setValue:nsmode forKey:@"mode"];
  3547. // //
  3548. // //
  3549. // // }
  3550. // //
  3551. // //
  3552. // //
  3553. // // sqlite3_finalize(statement);
  3554. // // }
  3555. // //
  3556. // //
  3557. // //
  3558. // // [iSalesDB close_db:db];
  3559. // //
  3560. // //
  3561. // //
  3562. // //
  3563. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3564. //
  3565. // return ret;
  3566. //}
  3567. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3568. //{
  3569. //
  3570. // UIApplication * app = [UIApplication sharedApplication];
  3571. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3572. //
  3573. // [iSalesDB disable_trigger];
  3574. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3575. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3576. // [iSalesDB enable_trigger];
  3577. //
  3578. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3579. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3580. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3581. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3582. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3583. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3584. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3585. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3586. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3587. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3588. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3589. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3590. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3591. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3592. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3593. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3594. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3595. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3596. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3597. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3598. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3599. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3600. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3601. //
  3602. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3603. //
  3604. // [arr_name addObject:customer_first_name];
  3605. // [arr_name addObject:customer_last_name];
  3606. //
  3607. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3608. //
  3609. // // default ship from
  3610. // NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  3611. //
  3612. // __block NSString *cid = @"";
  3613. // __block NSString *name = @"";
  3614. // __block NSString *ext = @"";
  3615. // __block NSString *contact = @"";
  3616. // __block NSString *email = @"";
  3617. // __block NSString *fax = @"";
  3618. // __block NSString *phone = @"";
  3619. //
  3620. // sqlite3 *db = [iSalesDB get_db];
  3621. //
  3622. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3623. //
  3624. // cid = [self textAtColumn:0 statement:statment];
  3625. // name = [self textAtColumn:1 statement:statment];
  3626. // ext = [self textAtColumn:2 statement:statment];
  3627. // contact = [self textAtColumn:3 statement:statment];
  3628. // email = [self textAtColumn:4 statement:statment];
  3629. // fax = [self textAtColumn:5 statement:statment];
  3630. // phone = [self textAtColumn:6 statement:statment];
  3631. //
  3632. // }];
  3633. //
  3634. // NSString* so_id = [self get_offline_soid:db];
  3635. // if(so_id==nil)
  3636. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3637. //
  3638. // NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  3639. //
  3640. //
  3641. //
  3642. // int result =[iSalesDB execSql:sql_neworder db:db];
  3643. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3644. //
  3645. //
  3646. //
  3647. // //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  3648. // //soId
  3649. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3650. //
  3651. //
  3652. //
  3653. //
  3654. //
  3655. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3656. // sqlite3_stmt * statement;
  3657. //
  3658. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3659. // {
  3660. // if (sqlite3_step(statement) == SQLITE_ROW)
  3661. // {
  3662. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3663. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3664. //
  3665. // //ret = sqlite3_column_int(statement, 0);
  3666. //
  3667. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3668. // if(soId==nil)
  3669. // soId="";
  3670. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3671. // [ret setValue:nssoId forKey:@"soId"];
  3672. // [ret setValue:nssoId forKey:@"orderCode"];
  3673. //
  3674. // }
  3675. // sqlite3_finalize(statement);
  3676. // }
  3677. //
  3678. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3679. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3680. //
  3681. //
  3682. // [iSalesDB close_db:db];
  3683. //
  3684. // return [RAConvertor dict2data:ret];
  3685. //
  3686. //}
  3687. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3688. {
  3689. //assert(params[@"order_code"]);
  3690. // assert(params[@"order_code"]);
  3691. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3692. NSString* orderCode = [params valueForKey:@"orderCode"];
  3693. NSString* app_order_code= params[@"appDelegate.order_code"];
  3694. // UIApplication * app = [UIApplication sharedApplication];
  3695. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3696. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3697. {
  3698. [iSalesDB disable_trigger];
  3699. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3700. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3701. [iSalesDB enable_trigger];
  3702. }
  3703. sqlite3 *db = [iSalesDB get_db];
  3704. int cart_count=[self query_ordercartcount:orderCode db:db];
  3705. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3706. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3707. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3708. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3709. [iSalesDB close_db:db];
  3710. return [RAConvertor dict2data:ret];
  3711. }
  3712. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3713. {
  3714. assert(params[@"can_create_backorder"]!=nil);
  3715. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3716. sqlite3 *db = [iSalesDB get_db];
  3717. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3718. int count =0;
  3719. if(params[@"count"]!=nil)
  3720. {
  3721. count = [params[@"count"] intValue];
  3722. }
  3723. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3724. NSString* product_id=params[@"product_id"];
  3725. NSString* orderCode=params[@"orderCode"];
  3726. NSString *qty = params[@"qty"];
  3727. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3728. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3729. __block int number_of_outOfStock = 0;
  3730. for(int i=0;i<arr_id.count;i++)
  3731. {
  3732. NSInteger item_qty= count;
  3733. if (qty) {
  3734. item_qty = [qty_arr[i] integerValue];
  3735. }
  3736. if(item_qty==0)
  3737. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3738. // 检查新加Model数量是否大于库存
  3739. if (![params[@"can_create_backorder"] boolValue]) {
  3740. __block BOOL needContinue = NO;
  3741. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3742. int availability = sqlite3_column_int(stmt, 0);
  3743. // 库存小于购买量为缺货
  3744. if (availability < item_qty || availability <= 0) {
  3745. number_of_outOfStock++;
  3746. needContinue = YES;
  3747. }
  3748. }];
  3749. if (needContinue) {
  3750. continue;
  3751. }
  3752. }
  3753. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3754. NSString* sql=nil;
  3755. sqlite3_stmt *stmt = nil;
  3756. BOOL shouldStep = NO;
  3757. if(_id<0)
  3758. {
  3759. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3760. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3761. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3762. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3763. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3764. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3765. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3766. shouldStep = YES;
  3767. }
  3768. else
  3769. {
  3770. if (qty) { // wish list move to cart
  3771. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3772. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3773. sqlite3_bind_int(stmt, 1, _id);
  3774. shouldStep = YES;
  3775. } else {
  3776. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  3777. __block BOOL update = YES;
  3778. if (![params[@"can_create_backorder"] boolValue]) {
  3779. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3780. int newQTY = sqlite3_column_int(stmt, 0);
  3781. int availability = sqlite3_column_int(stmt, 1);
  3782. if (newQTY > availability) { // 库存不够
  3783. update = NO;
  3784. number_of_outOfStock++;
  3785. }
  3786. }];
  3787. }
  3788. if (update) {
  3789. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3790. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3791. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3792. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3793. shouldStep = YES;
  3794. }
  3795. }
  3796. }
  3797. if (shouldStep) {
  3798. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3799. [iSalesDB execSql:@"ROLLBACK" db:db];
  3800. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3801. [iSalesDB close_db:db];
  3802. DebugLog(@"add to cart error");
  3803. return [RAConvertor dict2data:ret];
  3804. }
  3805. }
  3806. }
  3807. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3808. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3809. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3810. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3811. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3812. if (![params[@"can_create_backorder"] boolValue]) {
  3813. if (number_of_outOfStock > 0) {
  3814. ret[@"result"]=[NSNumber numberWithInt:8];
  3815. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3816. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3817. }
  3818. }
  3819. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3820. [iSalesDB close_db:db];
  3821. return [RAConvertor dict2data:ret];
  3822. }
  3823. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3824. {
  3825. // UIApplication * app = [UIApplication sharedApplication];
  3826. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3827. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3828. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  3829. sqlite3_stmt * statement;
  3830. int count=0;
  3831. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3832. {
  3833. while (sqlite3_step(statement) == SQLITE_ROW)
  3834. {
  3835. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3836. int bitem_id = sqlite3_column_int(statement, 0);
  3837. int bitem_qty = sqlite3_column_int(statement, 1);
  3838. char *name = (char*)sqlite3_column_text(statement, 2);
  3839. if(name==nil)
  3840. name="";
  3841. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3842. char *description = (char*)sqlite3_column_text(statement, 3);
  3843. if(description==nil)
  3844. description="";
  3845. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3846. double unit_price = sqlite3_column_double(statement, 4);
  3847. int use_unitprice = sqlite3_column_int(statement, 5);
  3848. if(use_unitprice!=1)
  3849. {
  3850. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3851. }
  3852. itemjson[@"model"]=nsname;
  3853. itemjson[@"description"]=nsdescription;
  3854. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3855. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3856. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3857. if(compute)
  3858. {
  3859. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3860. }
  3861. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3862. count++;
  3863. }
  3864. sqlite3_finalize(statement);
  3865. }
  3866. ret[@"count"]=@(count);
  3867. if(count==0)
  3868. return nil;
  3869. else
  3870. return ret;
  3871. }
  3872. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3873. {
  3874. //compute: add part to subtotal;
  3875. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3876. dict_item[@(item_id)]=@"1";
  3877. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3878. double cuft=0;
  3879. double weight=0;
  3880. int carton=0;
  3881. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  3882. sqlite3_stmt * statement;
  3883. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3884. {
  3885. if (sqlite3_step(statement) == SQLITE_ROW)
  3886. {
  3887. double ulength = sqlite3_column_double(statement, 0);
  3888. double uwidth = sqlite3_column_double(statement, 1);
  3889. double uheight = sqlite3_column_double(statement, 2);
  3890. double uweight = sqlite3_column_double(statement, 3);
  3891. double mlength = sqlite3_column_double(statement, 4);
  3892. double mwidth = sqlite3_column_double(statement, 5);
  3893. double mheight = sqlite3_column_double(statement, 6);
  3894. double mweight = sqlite3_column_double(statement, 7);
  3895. double ilength = sqlite3_column_double(statement, 8);
  3896. double iwidth = sqlite3_column_double(statement, 9);
  3897. double iheight = sqlite3_column_double(statement, 10);
  3898. double iweight = sqlite3_column_double(statement, 11);
  3899. // int pcs = sqlite3_column_int(statement,12);
  3900. int mpack = sqlite3_column_int(statement, 13);
  3901. int ipack = sqlite3_column_int(statement, 14);
  3902. double ucbf = sqlite3_column_double(statement, 15);
  3903. // double icbf = sqlite3_column_double(statement, 16);
  3904. // double mcbf = sqlite3_column_double(statement, 17);
  3905. if(ipack==0)
  3906. {
  3907. carton= count/mpack ;
  3908. weight = mweight*carton;
  3909. cuft= carton*(mlength*mwidth*mheight);
  3910. int remain=count%mpack;
  3911. if(remain==0)
  3912. {
  3913. //do nothing;
  3914. }
  3915. else
  3916. {
  3917. carton++;
  3918. weight += uweight*remain;
  3919. cuft += (ulength*uwidth*uheight)*remain;
  3920. }
  3921. }
  3922. else
  3923. {
  3924. carton = count/(mpack*ipack);
  3925. weight = mweight*carton;
  3926. cuft= carton*(mlength*mwidth*mheight);
  3927. int remain=count%(mpack*ipack);
  3928. if(remain==0)
  3929. {
  3930. // do nothing;
  3931. }
  3932. else
  3933. {
  3934. carton++;
  3935. int icarton =remain/ipack;
  3936. int iremain=remain%ipack;
  3937. weight += iweight*icarton;
  3938. cuft += (ilength*iwidth*iheight)*icarton;
  3939. if(iremain==0)
  3940. {
  3941. //do nothing;
  3942. }
  3943. else
  3944. {
  3945. weight += uweight*iremain;
  3946. cuft += (ulength*uwidth*uheight)*iremain;
  3947. }
  3948. }
  3949. }
  3950. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3951. cuft=ucbf*count;
  3952. weight= uweight*count;
  3953. #endif
  3954. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3955. cuft=ucbf*count;
  3956. weight= uweight*count;
  3957. #endif
  3958. }
  3959. sqlite3_finalize(statement);
  3960. }
  3961. if(compute)
  3962. {
  3963. NSArray * arr_count=nil;
  3964. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3965. for(int i=0;i<arr_bundle.count;i++)
  3966. {
  3967. dict_item[arr_bundle[i]]=@"1";
  3968. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3969. cuft+=[bundlejson[@"cuft"] doubleValue];
  3970. weight+=[bundlejson[@"weight"] doubleValue];
  3971. carton+=[bundlejson[@"carton"] intValue];
  3972. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3973. }
  3974. }
  3975. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3976. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3977. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3978. ret[@"items"]=dict_item;
  3979. return ret;
  3980. }
  3981. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3982. {
  3983. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3984. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  3985. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3986. DebugLog(@"offline_login sql:%@",sqlQuery);
  3987. sqlite3_stmt * statement;
  3988. int cart_count=0;
  3989. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3990. if ( dbresult== SQLITE_OK)
  3991. {
  3992. while (sqlite3_step(statement) == SQLITE_ROW)
  3993. {
  3994. int item_id = sqlite3_column_int(statement, 0);
  3995. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3996. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3997. }
  3998. sqlite3_finalize(statement);
  3999. }
  4000. return cart_count;
  4001. }
  4002. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4003. //{
  4004. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4005. // sqlite3 *db = [iSalesDB get_db];
  4006. // UIApplication * app = [UIApplication sharedApplication];
  4007. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4008. //
  4009. // NSString* orderCode=params[@"orderCode"];
  4010. //
  4011. // int sort = [[params objectForKey:@"sort"] intValue];
  4012. // NSString *sort_str = @"";
  4013. // switch (sort) {
  4014. // case 0:{
  4015. // sort_str = @"order by c.modify_time desc";
  4016. // }
  4017. // break;
  4018. // case 1:{
  4019. // sort_str = @"order by c.modify_time asc";
  4020. // }
  4021. // break;
  4022. // case 2:{
  4023. // sort_str = @"order by m.name asc";
  4024. // }
  4025. // break;
  4026. // case 3:{
  4027. // sort_str = @"order by m.name desc";
  4028. // }
  4029. // break;
  4030. // case 4:{
  4031. // sort_str = @"order by m.description asc";
  4032. // }
  4033. // break;
  4034. //
  4035. // default:
  4036. // break;
  4037. //
  4038. // }
  4039. //
  4040. //
  4041. //
  4042. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  4043. //
  4044. //
  4045. //// NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  4046. //
  4047. //
  4048. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4049. //
  4050. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4051. // sqlite3_stmt * statement;
  4052. //
  4053. //
  4054. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4055. //
  4056. // NSDate *date1 = [NSDate date];
  4057. //
  4058. // int count=0;
  4059. // int cart_count=0;
  4060. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4061. // if ( dbresult== SQLITE_OK)
  4062. // {
  4063. //
  4064. //
  4065. // while (sqlite3_step(statement) == SQLITE_ROW)
  4066. // {
  4067. //// NSDate *row_date = [NSDate date];
  4068. //
  4069. //
  4070. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4071. //
  4072. //
  4073. //
  4074. //
  4075. // int product_id = sqlite3_column_int(statement, 0);
  4076. //
  4077. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4078. //
  4079. // int item_id = sqlite3_column_int(statement, 7);
  4080. //
  4081. // NSString* Price=nil;
  4082. // if(str_price==nil)
  4083. // {
  4084. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4085. //// NSDate *price_date = [NSDate date];
  4086. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4087. //// DebugLog(@"price time interval");
  4088. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4089. //
  4090. // if(price==nil)
  4091. // Price=@"No Price.";
  4092. // else
  4093. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4094. // }
  4095. // else
  4096. // {
  4097. //
  4098. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4099. // }
  4100. //
  4101. //
  4102. // double discount = sqlite3_column_double(statement, 2);
  4103. // int item_count = sqlite3_column_int(statement, 3);
  4104. //
  4105. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4106. // NSString *nsline_note=nil;
  4107. // if(line_note!=nil)
  4108. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4109. //
  4110. //
  4111. // char *name = (char*)sqlite3_column_text(statement, 5);
  4112. // NSString *nsname=nil;
  4113. // if(name!=nil)
  4114. // nsname= [[NSString alloc]initWithUTF8String:name];
  4115. //
  4116. // char *description = (char*)sqlite3_column_text(statement, 6);
  4117. // NSString *nsdescription=nil;
  4118. // if(description!=nil)
  4119. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4120. //
  4121. //
  4122. //
  4123. // int stockUom = sqlite3_column_int(statement, 8);
  4124. // int _id = sqlite3_column_int(statement, 9);
  4125. // int availability = sqlite3_column_int(statement, 10);
  4126. //
  4127. //// NSDate *subtotal_date = [NSDate date];
  4128. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4129. //// DebugLog(@"subtotal_date time interval");
  4130. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4131. //
  4132. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4133. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4134. // int carton=[bsubtotaljson[@"carton"] intValue];
  4135. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4136. //
  4137. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4138. //// NSDate *img_date = [NSDate date];
  4139. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4140. //// DebugLog(@"img_date time interval");
  4141. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4142. //
  4143. // itemjson[@"model"]=nsname;
  4144. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4145. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4146. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4147. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4148. // itemjson[@"check"]=@"true";
  4149. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4150. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4151. // itemjson[@"unit_price"]=Price;
  4152. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4153. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4154. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4155. // itemjson[@"note"]=nsline_note;
  4156. // if (!appDelegate.can_create_backorder) {
  4157. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4158. // }
  4159. //// NSDate *date2 = [NSDate date];
  4160. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4161. //// DebugLog(@"model_bundle time interval");
  4162. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4163. //
  4164. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4165. // count++;
  4166. //
  4167. //// DebugLog(@"row time interval");
  4168. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4169. // }
  4170. //
  4171. //
  4172. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4173. // sqlite3_finalize(statement);
  4174. // }
  4175. //
  4176. //
  4177. // DebugLog(@"request cart total time interval");
  4178. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4179. //
  4180. //
  4181. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4182. //
  4183. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4184. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4185. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4186. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4187. //
  4188. // ret[@"mode"]=@"Regular Mode";
  4189. //
  4190. // [iSalesDB close_db:db];
  4191. //
  4192. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4193. //
  4194. // DebugLog(@"general notes :%@",general_note);
  4195. //
  4196. // ret[@"general_note"]= general_note;
  4197. //
  4198. // return [RAConvertor dict2data:ret];
  4199. //}
  4200. +(NSData*) offline_login :(NSMutableDictionary *) params
  4201. {
  4202. NSString* user = [params valueForKey:@"user"];
  4203. NSString* password = [params valueForKey:@"password"];
  4204. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4205. sqlite3 *db = [iSalesDB get_db];
  4206. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  4207. DebugLog(@"offline_login sql:%@",sqlQuery);
  4208. sqlite3_stmt * statement;
  4209. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4210. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4211. {
  4212. if (sqlite3_step(statement) == SQLITE_ROW)
  4213. {
  4214. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4215. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4216. int can_show_price = sqlite3_column_int(statement, 0);
  4217. int can_see_price = sqlite3_column_int(statement, 1);
  4218. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4219. if(contact_id==nil)
  4220. contact_id="";
  4221. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4222. int user_type = sqlite3_column_int(statement, 3);
  4223. int can_cancel_order = sqlite3_column_int(statement, 4);
  4224. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4225. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4226. int can_delete_order = sqlite3_column_int(statement, 7);
  4227. int can_submit_order = sqlite3_column_int(statement, 8);
  4228. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4229. int can_create_order = sqlite3_column_int(statement, 10);
  4230. char *mode = (char*)sqlite3_column_text(statement, 11);
  4231. if(mode==nil)
  4232. mode="";
  4233. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4234. char *username = (char*)sqlite3_column_text(statement, 12);
  4235. if(username==nil)
  4236. username="";
  4237. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4238. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4239. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4240. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4241. [header setValue:nscontact_id forKey:@"contact_id"];
  4242. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4243. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4244. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4245. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4246. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4247. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4248. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4249. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4250. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4251. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4252. [header setValue:nsusername forKey:@"username"];
  4253. NSError* error=nil;
  4254. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4255. [header setValue:statusFilter forKey:@"statusFilter"];
  4256. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4257. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4258. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4259. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4260. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4261. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4262. [ret setObject:header forKey:@"header"];
  4263. [ret setValue:nsmode forKey:@"mode"];
  4264. }
  4265. sqlite3_finalize(statement);
  4266. }
  4267. [iSalesDB close_db:db];
  4268. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4269. return [RAConvertor dict2data:ret];
  4270. }
  4271. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4272. {
  4273. assert(params[@"mode"]!=nil);
  4274. NSString* contactId = [params valueForKey:@"contactId"];
  4275. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4276. sqlite3 *db = [iSalesDB get_db];
  4277. NSString *sqlQuery = nil;
  4278. {
  4279. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type from offline_contact where contact_id='%@'",contactId];
  4280. }
  4281. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4282. sqlite3_stmt * statement;
  4283. [ret setValue:@"2" forKey:@"result"];
  4284. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4285. {
  4286. //int i = 0;
  4287. if (sqlite3_step(statement) == SQLITE_ROW)
  4288. {
  4289. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4290. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4291. // int editable = sqlite3_column_int(statement, 0);
  4292. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4293. NSString *nscompany_name =nil;
  4294. if(company_name==nil)
  4295. nscompany_name=@"";
  4296. else
  4297. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4298. char *country = (char*)sqlite3_column_text(statement, 2);
  4299. if(country==nil)
  4300. country="";
  4301. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4302. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4303. // if(addr==nil)
  4304. // addr="";
  4305. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4306. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4307. if(zipcode==nil)
  4308. zipcode="";
  4309. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4310. char *state = (char*)sqlite3_column_text(statement, 5);
  4311. if(state==nil)
  4312. state="";
  4313. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4314. char *city = (char*)sqlite3_column_text(statement, 6);
  4315. if(city==nil)
  4316. city="";
  4317. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4318. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4319. // NSString *nscontact_name = nil;
  4320. // if(contact_name==nil)
  4321. // nscontact_name=@"";
  4322. // else
  4323. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4324. char *phone = (char*)sqlite3_column_text(statement, 8);
  4325. NSString *nsphone = nil;
  4326. if(phone==nil)
  4327. nsphone=@"";
  4328. else
  4329. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4330. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4331. // if(contact_id==nil)
  4332. // contact_id="";
  4333. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4334. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4335. if(addr_1==nil)
  4336. addr_1="";
  4337. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4338. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4339. if(addr_2==nil)
  4340. addr_2="";
  4341. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4342. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4343. if(addr_3==nil)
  4344. addr_3="";
  4345. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4346. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4347. if(addr_4==nil)
  4348. addr_4="";
  4349. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4350. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4351. if(first_name==nil)
  4352. first_name="";
  4353. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4354. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4355. if(last_name==nil)
  4356. last_name="";
  4357. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4358. char *fax = (char*)sqlite3_column_text(statement, 16);
  4359. NSString *nsfax = nil;
  4360. if(fax==nil)
  4361. nsfax=@"";
  4362. else
  4363. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4364. char *email = (char*)sqlite3_column_text(statement, 17);
  4365. NSString *nsemail = nil;
  4366. if(email==nil)
  4367. nsemail=@"";
  4368. else
  4369. nsemail= [[NSString alloc]initWithUTF8String:email];
  4370. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4371. NSString *nsimg_0 = nil;
  4372. if(img_0==nil)
  4373. nsimg_0=@"";
  4374. else
  4375. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4376. [self copy_bcardImg:nsimg_0];
  4377. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4378. NSString *nsimg_1 = nil;
  4379. if(img_1==nil)
  4380. nsimg_1=@"";
  4381. else
  4382. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4383. [self copy_bcardImg:nsimg_1];
  4384. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4385. NSString *nsimg_2 = nil;
  4386. if(img_2==nil)
  4387. nsimg_2=@"";
  4388. else
  4389. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4390. [self copy_bcardImg:nsimg_2];
  4391. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4392. NSString *nsprice_type = nil;
  4393. if(price_type==nil)
  4394. nsprice_type=@"";
  4395. else
  4396. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4397. char *notes = (char*)sqlite3_column_text(statement, 22);
  4398. NSString *nsnotes = nil;
  4399. if(notes==nil)
  4400. nsnotes=@"";
  4401. else
  4402. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4403. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4404. NSString *nssalesrep = nil;
  4405. if(salesrep==nil)
  4406. nssalesrep=@"";
  4407. else
  4408. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4409. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4410. {
  4411. // decrypt
  4412. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4413. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4414. nsphone=[AESCrypt fastdecrypt:nsphone];
  4415. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4416. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4417. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4418. }
  4419. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4420. [arr_name addObject:nsfirst_name];
  4421. [arr_name addObject:nslast_name];
  4422. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4423. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4424. [arr_ext addObject:nsaddr_1];
  4425. [arr_ext addObject:nsaddr_2];
  4426. [arr_ext addObject:nsaddr_3];
  4427. [arr_ext addObject:nsaddr_4];
  4428. [arr_ext addObject:@"\r\n"];
  4429. [arr_ext addObject:nscity];
  4430. [arr_ext addObject:nsstate];
  4431. [arr_ext addObject:nszipcode];
  4432. [arr_ext addObject:nscountry];
  4433. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4434. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4435. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4436. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4437. [item setValue:nscountry forKey:@"customer_country"];
  4438. [item setValue:nsphone forKey:@"customer_phone"];
  4439. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4440. [item setValue:nscompany_name forKey:@"customer_name"];
  4441. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4442. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4443. [item setValue:nsext forKey:@"customer_contact_ext"];
  4444. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4445. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4446. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4447. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4448. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4449. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4450. [item setValue:nslast_name forKey:@"customer_last_name"];
  4451. [item setValue:nscity forKey:@"customer_city"];
  4452. [item setValue:nsstate forKey:@"customer_state"];
  4453. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4454. [item setValue:contactId forKey:@"customer_cid"];
  4455. [item setValue:nscontact_name forKey:@"customer_contact"];
  4456. [item setValue:nsfax forKey:@"customer_fax"];
  4457. [item setValue:nsemail forKey:@"customer_email"];
  4458. [item setValue:contact_type forKey:@"customer_contact_type"];
  4459. [ret setObject:item forKey:@"customerInfo"];
  4460. // i++;
  4461. }
  4462. // UIApplication * app = [UIApplication sharedApplication];
  4463. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4464. [ret setValue:params[@"mode"] forKey:@"mode"];
  4465. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4466. sqlite3_finalize(statement);
  4467. }
  4468. [iSalesDB close_db:db];
  4469. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4470. return ret;
  4471. }
  4472. + (bool) copy_bcardImg:(NSString*) filename
  4473. {
  4474. if(filename.length==0)
  4475. return false;
  4476. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4477. bool ret=false;
  4478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4479. NSString *cachefolder = [paths objectAtIndex:0];
  4480. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4481. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4482. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4483. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4484. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4485. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4486. //
  4487. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4488. NSFileManager* fileManager = [NSFileManager defaultManager];
  4489. BOOL bdir=NO;
  4490. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4491. {
  4492. NSError *error = nil;
  4493. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4494. {
  4495. ret=false;
  4496. }
  4497. else
  4498. {
  4499. ret=true;
  4500. }
  4501. // NSError *error = nil;
  4502. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4503. //
  4504. // if(!bsuccess)
  4505. // {
  4506. // DebugLog(@"Create offline_createimg folder failed");
  4507. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4508. // return [RAConvertor dict2data:ret];
  4509. // }
  4510. // if(bsuccess)
  4511. // {
  4512. // sqlite3 *db = [self get_db];
  4513. //
  4514. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4515. // [iSalesDB close_db:db];
  4516. // }
  4517. }
  4518. return ret;
  4519. //
  4520. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4521. // if(bsuccess)
  4522. // {
  4523. // NSError *error = nil;
  4524. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4525. // {
  4526. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4527. // }
  4528. // else
  4529. // {
  4530. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4531. //
  4532. // ret[@"img_url_aname"]=filename;
  4533. // ret[@"img_url"]=savedImagePath;
  4534. // }
  4535. // }
  4536. }
  4537. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4538. {
  4539. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4540. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4541. NSString *cachefolder = [paths objectAtIndex:0];
  4542. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4543. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4544. NSFileManager* fileManager = [NSFileManager defaultManager];
  4545. BOOL bdir=YES;
  4546. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4547. {
  4548. NSError *error = nil;
  4549. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4550. if(!bsuccess)
  4551. {
  4552. DebugLog(@"Create offline_createimg folder failed");
  4553. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4554. return [RAConvertor dict2data:ret];
  4555. }
  4556. // if(bsuccess)
  4557. // {
  4558. // sqlite3 *db = [self get_db];
  4559. //
  4560. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4561. // [iSalesDB close_db:db];
  4562. // }
  4563. }
  4564. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4565. {
  4566. NSError *error = nil;
  4567. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4568. if(!bsuccess)
  4569. {
  4570. DebugLog(@"Create img_cache folder failed");
  4571. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4572. return [RAConvertor dict2data:ret];
  4573. }
  4574. // if(bsuccess)
  4575. // {
  4576. // sqlite3 *db = [self get_db];
  4577. //
  4578. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4579. // [iSalesDB close_db:db];
  4580. // }
  4581. }
  4582. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4583. //JEPG格式
  4584. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4585. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4586. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4587. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4588. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4589. if(bsuccess)
  4590. {
  4591. NSError *error = nil;
  4592. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4593. {
  4594. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4595. }
  4596. else
  4597. {
  4598. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4599. ret[@"img_url_aname"]=filename;
  4600. ret[@"img_url"]=filename;
  4601. }
  4602. }
  4603. else
  4604. {
  4605. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4606. }
  4607. return [RAConvertor dict2data:ret];
  4608. }
  4609. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4610. {
  4611. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4612. category = [category substringToIndex:3];
  4613. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4614. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4615. params[@"category"]= category;
  4616. ret[@"params"]= params;
  4617. [ret setValue:@"detail" forKey:@"target"];
  4618. [ret setValue:@"popup" forKey:@"action"];
  4619. [ret setValue:@"content" forKey:@"type"];
  4620. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4621. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4622. [ret setValue:@"true" forKey:@"single_row"];
  4623. [ret setValue:@"true" forKey:@"partial_refresh"];
  4624. // sqlite3 *db = [iSalesDB get_db];
  4625. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4626. NSString *sqlQuery =nil;
  4627. #ifdef BUILD_NPD
  4628. sqlQuery=[NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  4629. #else
  4630. sqlQuery=[NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY];
  4631. #endif
  4632. sqlite3_stmt * statement;
  4633. int count = 0;
  4634. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4635. // int count=0;
  4636. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4637. {
  4638. int i=0;
  4639. while (sqlite3_step(statement) == SQLITE_ROW)
  4640. {
  4641. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4642. // char *name = (char*)sqlite3_column_text(statement, 1);
  4643. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4644. char *name = (char*)sqlite3_column_text(statement, 0);
  4645. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4646. int product_id = sqlite3_column_int(statement, 1);
  4647. char *url = (char*)sqlite3_column_text(statement, 2);
  4648. if(url==nil)
  4649. url="";
  4650. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4651. [item setValue:nsurl forKey:@"picture_path"];
  4652. [item setValue:nsname forKey:@"fash_name"];
  4653. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4654. [item setValue:category forKey:@"category"];
  4655. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4656. i++;
  4657. }
  4658. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4659. sqlite3_finalize(statement);
  4660. }
  4661. DebugLog(@"count:%d",count);
  4662. // [iSalesDB close_db:db];
  4663. return ret;
  4664. }
  4665. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4666. {
  4667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4668. [ret setValue:key forKey:@"key"];
  4669. [ret setValue:value forKey:@"val"];
  4670. [ret setValue:@"price" forKey:@"type"];
  4671. return ret;
  4672. }
  4673. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4674. {
  4675. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4676. [ret setValue:key forKey:@"key"];
  4677. [ret setValue:value forKey:@"val"];
  4678. return ret;
  4679. }
  4680. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4681. {
  4682. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4683. [ret setValue:@"0" forKey:@"img_count"];
  4684. // sqlite3 *db = [iSalesDB get_db];
  4685. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  4686. sqlite3_stmt * statement;
  4687. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4688. {
  4689. int i=0;
  4690. if (sqlite3_step(statement) == SQLITE_ROW)
  4691. {
  4692. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4693. // char *name = (char*)sqlite3_column_text(statement, 1);
  4694. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4695. char *value = (char*)sqlite3_column_text(statement, 0);
  4696. if(value==nil)
  4697. value="";
  4698. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4699. char *key = (char*)sqlite3_column_text(statement, 1);
  4700. if(key==nil)
  4701. key="";
  4702. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4703. [item setValue:nsvalue forKey:@"val"];
  4704. [item setValue:nskey forKey:@"key"];
  4705. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4706. [ret setValue:@"1" forKey:@"count"];
  4707. i++;
  4708. }
  4709. sqlite3_finalize(statement);
  4710. }
  4711. // [iSalesDB close_db:db];
  4712. return ret;
  4713. }
  4714. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4715. {
  4716. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4717. [ret setValue:@"0" forKey:@"count"];
  4718. // sqlite3 *db = [iSalesDB get_db];
  4719. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  4720. sqlite3_stmt * statement;
  4721. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4722. {
  4723. int i=0;
  4724. while (sqlite3_step(statement) == SQLITE_ROW)
  4725. {
  4726. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4727. // char *name = (char*)sqlite3_column_text(statement, 1);
  4728. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4729. char *value = (char*)sqlite3_column_text(statement, 0);
  4730. if(value==nil)
  4731. value="";
  4732. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4733. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4734. if(selector_display==nil)
  4735. selector_display="";
  4736. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4737. int product_id = sqlite3_column_int(statement, 2);
  4738. char *category = (char*)sqlite3_column_text(statement, 3);
  4739. if(category==nil)
  4740. category="";
  4741. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4742. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4743. [item setValue:nsvalue forKey:@"title"];
  4744. [item setValue:url forKey:@"pic_url"];
  4745. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4746. [params setValue:@"2" forKey:@"count"];
  4747. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4748. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4749. [param0 setValue:@"product_id" forKey:@"name"];
  4750. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4751. [param1 setValue:nscategory forKey:@"val"];
  4752. [param1 setValue:@"category" forKey:@"name"];
  4753. [params setObject:param0 forKey:@"param_0"];
  4754. [params setObject:param1 forKey:@"param_1"];
  4755. [item setObject:params forKey:@"params"];
  4756. [ret setValue:nsselector_display forKey:@"name"];
  4757. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4758. i++;
  4759. }
  4760. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4761. [ret setValue:@"switch" forKey:@"action"];
  4762. sqlite3_finalize(statement);
  4763. }
  4764. // [iSalesDB close_db:db];
  4765. return ret;
  4766. }
  4767. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4768. {
  4769. // model 在 category search 显示的图片。
  4770. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4771. // sqlite3 *db = [iSalesDB get_db];
  4772. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  4773. sqlite3_stmt * statement;
  4774. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4775. {
  4776. while (sqlite3_step(statement) == SQLITE_ROW)
  4777. {
  4778. char *url = (char*)sqlite3_column_text(statement, 0);
  4779. if(url==nil)
  4780. url="";
  4781. int type = sqlite3_column_int(statement, 1);
  4782. if(type==0)
  4783. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4784. else
  4785. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4786. }
  4787. sqlite3_finalize(statement);
  4788. }
  4789. // [iSalesDB close_db:db];
  4790. return ret;
  4791. }
  4792. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4793. {
  4794. int item_id=-1;
  4795. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4796. sqlite3_stmt * statement;
  4797. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4798. {
  4799. if (sqlite3_step(statement) == SQLITE_ROW)
  4800. {
  4801. item_id = sqlite3_column_int(statement, 0);
  4802. }
  4803. sqlite3_finalize(statement);
  4804. }
  4805. return item_id;
  4806. }
  4807. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4808. {
  4809. // NSString* ret = @"";
  4810. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4811. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4812. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4813. sqlite3_stmt * statement;
  4814. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4815. {
  4816. while (sqlite3_step(statement) == SQLITE_ROW)
  4817. {
  4818. int bitem_id = sqlite3_column_int(statement, 0);
  4819. int bitem_qty = sqlite3_column_int(statement, 1);
  4820. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4821. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4822. }
  4823. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4824. sqlite3_finalize(statement);
  4825. }
  4826. // if(ret==nil)
  4827. // ret=@"";
  4828. *count=arr_count;
  4829. return arr_bundle;
  4830. }
  4831. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4832. {
  4833. // get default sold qty, return -1 if model not found;
  4834. int ret = -1;
  4835. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4836. sqlite3_stmt * statement;
  4837. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4838. {
  4839. if (sqlite3_step(statement) == SQLITE_ROW)
  4840. {
  4841. ret = sqlite3_column_int(statement, 0);
  4842. }
  4843. sqlite3_finalize(statement);
  4844. }
  4845. return ret;
  4846. }
  4847. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4848. {
  4849. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4850. [ret setValue:@"0" forKey:@"img_count"];
  4851. // sqlite3 *db = [iSalesDB get_db];
  4852. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4853. sqlite3_stmt * statement;
  4854. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4855. {
  4856. int i=0;
  4857. while (sqlite3_step(statement) == SQLITE_ROW)
  4858. {
  4859. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4860. // char *name = (char*)sqlite3_column_text(statement, 1);
  4861. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4862. char *url = (char*)sqlite3_column_text(statement, 0);
  4863. if(url==nil)
  4864. url="";
  4865. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4866. [item setValue:nsurl forKey:@"s"];
  4867. [item setValue:nsurl forKey:@"l"];
  4868. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4869. i++;
  4870. }
  4871. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4872. sqlite3_finalize(statement);
  4873. }
  4874. // [iSalesDB close_db:db];
  4875. return ret;
  4876. }
  4877. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4878. // UIApplication * app = [UIApplication sharedApplication];
  4879. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4880. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4881. sqlite3 *db = [iSalesDB get_db];
  4882. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4883. NSString* product_id=params[@"product_id"];
  4884. NSString *item_count_str = params[@"item_count"];
  4885. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4886. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4887. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4888. // NSString *sql = @"";
  4889. for(int i=0;i<arr.count;i++)
  4890. {
  4891. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4892. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4893. __block int cart_count = 0;
  4894. if (!item_count_str) {
  4895. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4896. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4897. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4898. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4899. cart_count = [[model_set_components lastObject] intValue];
  4900. }];
  4901. }
  4902. if(count==0)
  4903. {
  4904. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4905. sqlite3_stmt *stmt;
  4906. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4907. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4908. if (item_count_arr) {
  4909. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4910. } else {
  4911. sqlite3_bind_int(stmt,2,cart_count);
  4912. }
  4913. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4914. [iSalesDB execSql:@"ROLLBACK" db:db];
  4915. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4916. [iSalesDB close_db:db];
  4917. DebugLog(@"add to wishlist error");
  4918. return ret;
  4919. }
  4920. } else {
  4921. int qty = 0;
  4922. if (item_count_arr) {
  4923. qty = [item_count_arr[i] intValue];
  4924. } else {
  4925. qty = cart_count;
  4926. }
  4927. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  4928. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4929. [iSalesDB execSql:@"ROLLBACK" db:db];
  4930. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4931. [iSalesDB close_db:db];
  4932. DebugLog(@"add to wishlist error");
  4933. return ret;
  4934. }
  4935. }
  4936. }
  4937. // [iSalesDB execSql:sql db:db];
  4938. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4939. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4940. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4941. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4942. [iSalesDB close_db:db];
  4943. ret[@"wish_count"]=@(count);
  4944. ret[@"result"]= [NSNumber numberWithInt:2];
  4945. return ret;
  4946. }
  4947. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4948. // 0 category
  4949. // 1 search
  4950. // 2 itemsearch
  4951. NSData *ret = nil;
  4952. NSDictionary *items = nil;
  4953. switch (from) {
  4954. case 0:{
  4955. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4956. }
  4957. break;
  4958. case 1:{
  4959. items = [[self search:params limited:NO] objectForKey:@"items"];
  4960. }
  4961. break;
  4962. case 2:{
  4963. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4964. }
  4965. break;
  4966. default:
  4967. break;
  4968. }
  4969. if (!items) {
  4970. return ret;
  4971. }
  4972. int count = [[items objectForKey:@"count"] intValue];
  4973. NSMutableString *product_id_str = [@"" mutableCopy];
  4974. for (int i = 0; i < count; i++) {
  4975. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4976. NSDictionary *item = [items objectForKey:key];
  4977. NSString *product_id = [item objectForKey:@"product_id"];
  4978. if (i == 0) {
  4979. [product_id_str appendString:product_id];
  4980. } else {
  4981. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4982. }
  4983. }
  4984. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4985. if ([add_to isEqualToString:@"cart"]) {
  4986. NSString *order_code = [params objectForKey:@"orderCode"];
  4987. if (order_code.length) {
  4988. NSDictionary *newParams = @{
  4989. @"product_id" : product_id_str,
  4990. @"orderCode" : order_code,
  4991. @"can_create_backorder":params[@"can_create_backorder"]
  4992. };
  4993. ret = [self offline_add2cart:newParams.mutableCopy];
  4994. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4995. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4996. ret = [RAConvertor dict2data:retDic];
  4997. }
  4998. } else if([add_to isEqualToString:@"wishlist"]) {
  4999. NSDictionary *newParams = @{
  5000. @"product_id" : product_id_str
  5001. };
  5002. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5003. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5004. ret = [RAConvertor dict2data:retDic];
  5005. } else if([add_to isEqualToString:@"portfolio"]) {
  5006. NSDictionary *newParams = @{
  5007. @"product_id" : product_id_str
  5008. };
  5009. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5010. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5011. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5012. ret = [RAConvertor dict2data:retDic];
  5013. }
  5014. return ret;
  5015. }
  5016. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5017. {
  5018. return [self addAll:params from:0];
  5019. }
  5020. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5021. {
  5022. return [self addAll:params from:1];
  5023. }
  5024. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5025. {
  5026. return [self addAll:params from:2];
  5027. }
  5028. #pragma mark - Jack
  5029. #warning 做SQL操作时转义!!
  5030. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5031. // "val_227" : {
  5032. // "check" : 1,
  5033. // "value" : "US United States",
  5034. // "value_id" : "228"
  5035. // },
  5036. if (!countryCode) {
  5037. countryCode = @"US";
  5038. }
  5039. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5040. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5041. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5042. int code_id = sqlite3_column_int(stmt, 3); // id
  5043. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5044. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5045. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5046. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5047. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5048. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5049. }
  5050. long n = *count;
  5051. *count = n + 1;
  5052. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5053. [container setValue:countryDic forKey:key];
  5054. }] mutableCopy];
  5055. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5056. return ret;
  5057. }
  5058. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5059. countryCode = [self translateSingleQuote:countryCode];
  5060. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5061. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5062. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5063. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5064. if (name == NULL) {
  5065. name = "";
  5066. }
  5067. if (code == NULL) {
  5068. code = "";
  5069. }
  5070. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5071. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5072. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5073. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5074. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5075. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5076. }
  5077. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5078. [container setValue:stateDic forKey:key];
  5079. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5080. DebugLog(@"query all state error: %@",err_msg);
  5081. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5082. // [stateDic setValue:@"Other" forKey:@"value"];
  5083. // [stateDic setValue:@"" forKey:@"value_id"];
  5084. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5085. //
  5086. // if (state_code && [@"" isEqualToString:state_code]) {
  5087. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5088. // }
  5089. //
  5090. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5091. // [container setValue:stateDic forKey:key];
  5092. }] mutableCopy];
  5093. [ret removeObjectForKey:@"result"];
  5094. // failure 可以不用了,一样的
  5095. if (ret.allKeys.count == 0) {
  5096. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5097. [stateDic setValue:@"Other" forKey:@"value"];
  5098. [stateDic setValue:@"" forKey:@"value_id"];
  5099. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5100. if (state_code && [@"" isEqualToString:state_code]) {
  5101. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5102. }
  5103. NSString *key = [NSString stringWithFormat:@"val_0"];
  5104. [ret setValue:stateDic forKey:key];
  5105. }
  5106. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5107. return ret;
  5108. }
  5109. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5110. codeId = [self translateSingleQuote:codeId];
  5111. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  5112. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5113. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5114. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5115. if (name == NULL) {
  5116. name = "";
  5117. }
  5118. if (code == NULL) {
  5119. code = "";
  5120. }
  5121. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5122. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5123. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5124. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5125. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5126. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5127. }
  5128. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5129. [container setValue:stateDic forKey:key];
  5130. }] mutableCopy];
  5131. [ret removeObjectForKey:@"result"];
  5132. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5133. return ret;
  5134. }
  5135. + (NSDictionary *)offline_getPrice {
  5136. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5137. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5138. char *name = (char *) sqlite3_column_text(stmt, 1);
  5139. int type = sqlite3_column_int(stmt, 2);
  5140. int orderBy = sqlite3_column_int(stmt, 3);
  5141. if (name == NULL) {
  5142. name = "";
  5143. }
  5144. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5145. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5146. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5147. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5148. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5149. if (orderBy == 0) {
  5150. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5151. }
  5152. [container setValue:priceDic forKey:key];
  5153. }] mutableCopy];
  5154. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5155. return ret;
  5156. }
  5157. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5158. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5159. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5160. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5161. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5162. // int _id = sqlite3_column_int(stmt, 0);
  5163. NSString *name = [self textAtColumn:1 statement:stmt];
  5164. NSDictionary *typeDic = @{
  5165. @"value_id" : name,
  5166. @"value" : name,
  5167. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5168. };
  5169. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5170. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5171. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5172. }];
  5173. return ret;
  5174. }
  5175. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5176. // 首先从offline_login表中取出sales_code
  5177. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5178. // NSString *user = app.user;
  5179. user = [self translateSingleQuote:user];
  5180. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5181. __block NSString *user_code = @"";
  5182. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5183. char *code = (char *)sqlite3_column_text(stmt, 0);
  5184. if (code == NULL) {
  5185. code = "";
  5186. }
  5187. user_code = [NSString stringWithUTF8String:code];
  5188. }];
  5189. // 再取所有salesRep
  5190. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5191. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5192. // 1 name 2 code 3 salesrep_id
  5193. char *name = (char *)sqlite3_column_text(stmt, 1);
  5194. char *code = (char *)sqlite3_column_text(stmt, 2);
  5195. int salesrep_id = sqlite3_column_int(stmt, 3);
  5196. if (name == NULL) {
  5197. name = "";
  5198. }
  5199. if (code == NULL) {
  5200. code = "";
  5201. }
  5202. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5203. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5204. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5205. // 比较code 相等则check
  5206. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5207. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5208. }
  5209. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5210. }] mutableCopy];
  5211. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5212. return ret;
  5213. }
  5214. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5215. zipcode = [self translateSingleQuote:zipcode];
  5216. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5217. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5218. char *country = (char *)sqlite3_column_text(stmt, 0);
  5219. char *state = (char *)sqlite3_column_text(stmt, 1);
  5220. char *city = (char *)sqlite3_column_text(stmt, 2);
  5221. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5222. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5223. if (country == NULL) {
  5224. country = "";
  5225. }
  5226. if (state == NULL) {
  5227. state = "";
  5228. }
  5229. if (city == NULL) {
  5230. city = "";
  5231. }
  5232. if (country_code == NULL) {
  5233. country_code = "";
  5234. }
  5235. if (state_code == NULL) {
  5236. state_code = "";
  5237. }
  5238. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5239. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5240. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5241. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5242. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5243. }] mutableCopy];
  5244. return ret;
  5245. }
  5246. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5247. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5248. [item setValue:value forKey:valueKey];
  5249. [dic setValue:item forKey:itemKey];
  5250. }
  5251. + (NSString *)countryCodeByid:(NSString *)code_id {
  5252. NSString *ret = nil;
  5253. code_id = [self translateSingleQuote:code_id];
  5254. sqlite3 *db = [iSalesDB get_db];
  5255. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5256. sqlite3_stmt * statement;
  5257. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5258. while (sqlite3_step(statement) == SQLITE_ROW) {
  5259. char *code = (char *)sqlite3_column_text(statement, 0);
  5260. if (code == NULL) {
  5261. code = "";
  5262. }
  5263. ret = [NSString stringWithUTF8String:code];
  5264. }
  5265. sqlite3_finalize(statement);
  5266. }
  5267. [iSalesDB close_db:db];
  5268. return ret;
  5269. }
  5270. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5271. NSString *ret = nil;
  5272. code = [self translateSingleQuote:code];
  5273. sqlite3 *db = [iSalesDB get_db];
  5274. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5275. sqlite3_stmt * statement;
  5276. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5277. while (sqlite3_step(statement) == SQLITE_ROW) {
  5278. char *_id = (char *)sqlite3_column_text(statement, 0);
  5279. if (_id == NULL) {
  5280. _id = "";
  5281. }
  5282. ret = [NSString stringWithFormat:@"%s",_id];
  5283. }
  5284. sqlite3_finalize(statement);
  5285. }
  5286. [iSalesDB close_db:db];
  5287. return ret;
  5288. }
  5289. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5290. NSString *name = nil;
  5291. codeId = [self translateSingleQuote:codeId];
  5292. sqlite3 *db = [iSalesDB get_db];
  5293. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5294. sqlite3_stmt * statement;
  5295. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5296. while (sqlite3_step(statement) == SQLITE_ROW) {
  5297. char *value = (char *)sqlite3_column_text(statement, 0);
  5298. if (value == NULL) {
  5299. value = "";
  5300. }
  5301. name = [NSString stringWithUTF8String:value];
  5302. }
  5303. sqlite3_finalize(statement);
  5304. }
  5305. [iSalesDB close_db:db];
  5306. return name;
  5307. }
  5308. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5309. NSString *ret = nil;
  5310. sqlite3 *db = [iSalesDB get_db];
  5311. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5312. sqlite3_stmt * statement;
  5313. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5314. while (sqlite3_step(statement) == SQLITE_ROW) {
  5315. char *name = (char *)sqlite3_column_text(statement, 0);
  5316. if (name == NULL) {
  5317. name = "";
  5318. }
  5319. ret = [NSString stringWithUTF8String:name];
  5320. }
  5321. sqlite3_finalize(statement);
  5322. }
  5323. [iSalesDB close_db:db];
  5324. return ret;
  5325. }
  5326. + (NSString *)salesRepCodeById:(NSString *)_id {
  5327. NSString *ret = nil;
  5328. _id = [self translateSingleQuote:_id];
  5329. sqlite3 *db = [iSalesDB get_db];
  5330. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5331. sqlite3_stmt * statement;
  5332. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5333. while (sqlite3_step(statement) == SQLITE_ROW) {
  5334. char *rep = (char *)sqlite3_column_text(statement, 0);
  5335. if (rep == NULL) {
  5336. rep = "";
  5337. }
  5338. ret = [NSString stringWithUTF8String:rep];
  5339. }
  5340. sqlite3_finalize(statement);
  5341. }
  5342. [iSalesDB close_db:db];
  5343. return ret;
  5344. }
  5345. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5346. char *tx = (char *)sqlite3_column_text(stmt, col);
  5347. if (tx == NULL) {
  5348. tx = "";
  5349. }
  5350. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5351. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5352. if (!text) {
  5353. text = @"";
  5354. }
  5355. // 将字符全部为' '的字符串干掉
  5356. int spaceCount = 0;
  5357. for (int i = 0; i < text.length; i++) {
  5358. if ([text characterAtIndex:i] == ' ') {
  5359. spaceCount++;
  5360. }
  5361. }
  5362. if (spaceCount == text.length) {
  5363. text = @"";
  5364. }
  5365. return text;
  5366. }
  5367. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5368. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5369. NSData *data = [NSData dataWithContentsOfFile:path];
  5370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5371. return ret;
  5372. }
  5373. + (NSString *)textFileName:(NSString *)name {
  5374. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5375. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5376. if (!text) {
  5377. text = @"";
  5378. }
  5379. return text;
  5380. }
  5381. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5382. return [[dic objectForKey:key] mutableCopy];
  5383. }
  5384. + (id)translateSingleQuote:(NSString *)string {
  5385. if ([string isKindOfClass:[NSString class]])
  5386. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5387. return string;
  5388. }
  5389. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5390. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5391. }
  5392. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5393. NSString* ret= nil;
  5394. NSString *sqlQuery = nil;
  5395. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  5396. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  5397. sqlite3_stmt * statement;
  5398. // int count=0;
  5399. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5400. {
  5401. if (sqlite3_step(statement) == SQLITE_ROW)
  5402. {
  5403. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5404. if(imgurl==nil)
  5405. imgurl="";
  5406. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5407. ret=nsimgurl;
  5408. }
  5409. sqlite3_finalize(statement);
  5410. }
  5411. else
  5412. {
  5413. [ret setValue:@"8" forKey:@"result"];
  5414. }
  5415. // [iSalesDB close_db:db];
  5416. // DebugLog(@"data string: %@",ret );
  5417. return ret;
  5418. }
  5419. #pragma mark contact Advanced search
  5420. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5421. {
  5422. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5423. return [RAConvertor dict2data:contactAdvanceDic];
  5424. }
  5425. #pragma mark create new contact
  5426. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5427. assert(params[@"user"]!=nil);
  5428. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5429. NSData *data = [NSData dataWithContentsOfFile:path];
  5430. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5431. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5432. NSString *countryCode = nil;
  5433. NSString *countryCode_id = nil;
  5434. NSString *stateCode = nil;
  5435. NSString *city = nil;
  5436. NSString *zipCode = nil;
  5437. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5438. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5439. NSString *code_id = params[@"country"];
  5440. countryCode_id = code_id;
  5441. countryCode = [self countryCodeByid:code_id];
  5442. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5443. NSString *zip_code = params[@"zipcode"];
  5444. // 剔除全部为空格
  5445. int spaceCount = 0;
  5446. for (int i = 0; i < zip_code.length; i++) {
  5447. if ([zip_code characterAtIndex:i] == ' ') {
  5448. spaceCount++;
  5449. }
  5450. }
  5451. if (spaceCount == zip_code.length) {
  5452. zip_code = @"";
  5453. }
  5454. zipCode = zip_code;
  5455. if (zipCode.length > 0) {
  5456. countryCode_id = params[@"country"];
  5457. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5458. countryCode = [dic valueForKey:@"country_code"];
  5459. if (!countryCode) {
  5460. // countryCode = @"US";
  5461. NSString *code_id = params[@"country"];
  5462. countryCode = [self countryCodeByid:code_id];
  5463. }
  5464. stateCode = [dic valueForKey:@"state_code"];
  5465. if (!stateCode.length) {
  5466. stateCode = params[@"state"];
  5467. }
  5468. city = [dic valueForKey:@"city"];
  5469. if (!city.length) {
  5470. city = params[@"city"];
  5471. }
  5472. // zip code
  5473. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5474. [zipDic setValue:zipCode forKey:@"value"];
  5475. [section_0 setValue:zipDic forKey:@"item_11"];
  5476. } else {
  5477. NSString *code_id = params[@"country"];
  5478. countryCode = [self countryCodeByid:code_id];
  5479. stateCode = params[@"state"];
  5480. city = params[@"city"];
  5481. }
  5482. }
  5483. } else {
  5484. // default: US United States
  5485. countryCode = @"US";
  5486. countryCode_id = @"228";
  5487. }
  5488. // country
  5489. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5490. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5491. // state
  5492. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5493. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5494. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5495. // city
  5496. if (city) {
  5497. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5498. [cityDic setValue:city forKey:@"value"];
  5499. [section_0 setValue:cityDic forKey:@"item_13"];
  5500. }
  5501. // price type
  5502. NSDictionary *priceDic = [self offline_getPrice];
  5503. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5504. // contact type
  5505. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5506. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5507. // Sales Rep
  5508. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5509. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5510. [ret setValue:section_0 forKey:@"section_0"];
  5511. return [RAConvertor dict2data:ret];
  5512. }
  5513. #pragma mark save
  5514. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5515. NSString *addr = nil;
  5516. NSString *contact_name = nil;
  5517. NSString *companyName = [params objectForKey:@"company"];
  5518. if (companyName) {
  5519. companyName = [AESCrypt fastencrypt:companyName];
  5520. } else {
  5521. companyName = @"";
  5522. }
  5523. DebugLog(@"company");
  5524. companyName = [self translateSingleQuote:companyName];
  5525. NSString *addr1 = [params objectForKey:@"address"];
  5526. NSString *addr2 = [params objectForKey:@"address2"];
  5527. NSString *addr3 = [params objectForKey:@"address_3"];
  5528. NSString *addr4 = [params objectForKey:@"address_4"];
  5529. if (!addr2) {
  5530. addr2 = @"";
  5531. }
  5532. if (!addr3) {
  5533. addr3 = @"";
  5534. }
  5535. if (!addr4) {
  5536. addr4 = @"";
  5537. }
  5538. if (!addr1) {
  5539. addr1 = @"";
  5540. }
  5541. DebugLog(@"addr");
  5542. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5543. addr = [AESCrypt fastencrypt:addr];
  5544. addr = [self translateSingleQuote:addr];
  5545. if (addr1 && ![addr1 isEqualToString:@""]) {
  5546. addr1 = [AESCrypt fastencrypt:addr1];
  5547. }
  5548. addr1 = [self translateSingleQuote:addr1];
  5549. addr2 = [self translateSingleQuote:addr2];
  5550. addr3 = [self translateSingleQuote:addr3];
  5551. addr4 = [self translateSingleQuote:addr4];
  5552. NSString *country = [params objectForKey:@"country"];
  5553. if (country) {
  5554. country = [self countryNameByCountryCodeId:country];
  5555. } else {
  5556. country = @"";
  5557. }
  5558. DebugLog(@"country");
  5559. country = [self translateSingleQuote:country];
  5560. NSString *state = [params objectForKey:@"state"];
  5561. if (!state) {
  5562. state = @"";
  5563. }
  5564. DebugLog(@"state");
  5565. state = [self translateSingleQuote:state];
  5566. NSString *city = [params objectForKey:@"city"];
  5567. if (!city) {
  5568. city = @"";
  5569. }
  5570. city = [self translateSingleQuote:city];
  5571. NSString *zipcode = [params objectForKey:@"zipcode"];
  5572. if (!zipcode) {
  5573. zipcode = @"";
  5574. }
  5575. DebugLog(@"zip");
  5576. zipcode = [self translateSingleQuote:zipcode];
  5577. NSString *fistName = [params objectForKey:@"firstname"];
  5578. if (!fistName) {
  5579. fistName = @"";
  5580. }
  5581. NSString *lastName = [params objectForKey:@"lastname"];
  5582. if (!lastName) {
  5583. lastName = @"";
  5584. }
  5585. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5586. DebugLog(@"contact_name");
  5587. contact_name = [self translateSingleQuote:contact_name];
  5588. fistName = [self translateSingleQuote:fistName];
  5589. lastName = [self translateSingleQuote:lastName];
  5590. NSString *phone = [params objectForKey:@"phone"];
  5591. if (phone) {
  5592. phone = [AESCrypt fastencrypt:phone];
  5593. } else {
  5594. phone = @"";
  5595. }
  5596. DebugLog(@"PHONE");
  5597. phone = [self translateSingleQuote:phone];
  5598. NSString *fax = [params objectForKey:@"fax"];
  5599. if (!fax) {
  5600. fax = @"";
  5601. }
  5602. DebugLog(@"FAX");
  5603. fax = [self translateSingleQuote:fax];
  5604. NSString *email = [params objectForKey:@"email"];
  5605. if (!email) {
  5606. email = @"";
  5607. }
  5608. DebugLog(@"EMAIL:%@",email);
  5609. email = [self translateSingleQuote:email];
  5610. NSString *notes = [params objectForKey:@"contact_notes"];
  5611. if (!notes) {
  5612. notes = @"";
  5613. }
  5614. DebugLog(@"NOTE:%@",notes);
  5615. notes = [self translateSingleQuote:notes];
  5616. NSString *price = [params objectForKey:@"price_name"];
  5617. if (price) {
  5618. price = [self priceNameByPriceId:price];
  5619. } else {
  5620. price = @"";
  5621. }
  5622. DebugLog(@"PRICE");
  5623. price = [self translateSingleQuote:price];
  5624. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5625. if (salesRep) {
  5626. salesRep = [self salesRepCodeById:salesRep];
  5627. } else {
  5628. salesRep = @"";
  5629. }
  5630. salesRep = [self translateSingleQuote:salesRep];
  5631. NSString *img = [params objectForKey:@"business_card"];
  5632. NSArray *array = [img componentsSeparatedByString:@","];
  5633. NSString *img_0 = array[0];
  5634. if (!img_0) {
  5635. img_0 = @"";
  5636. }
  5637. img_0 = [self translateSingleQuote:img_0];
  5638. NSString *img_1 = array[1];
  5639. if (!img_1) {
  5640. img_1 = @"";
  5641. }
  5642. img_1 = [self translateSingleQuote:img_1];
  5643. NSString *img_2 = array[2];
  5644. if (!img_2) {
  5645. img_2 = @"";
  5646. }
  5647. img_2 = [self translateSingleQuote:img_2];
  5648. NSString *contact_id = [NSUUID UUID].UUIDString;
  5649. NSString *contact_type = [params objectForKey:@"type_name"];
  5650. if (!contact_type) {
  5651. contact_type = @"";
  5652. }
  5653. contact_type = [self translateSingleQuote:contact_type];
  5654. // 判断更新时是否为customer
  5655. if (update) {
  5656. contact_id = [params objectForKey:@"contact_id"];
  5657. if (!contact_id) {
  5658. contact_id = @"";
  5659. }
  5660. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5661. __block int customer = 0;
  5662. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5663. customer = sqlite3_column_int(stmt, 0);
  5664. }];
  5665. isCustomer = customer ? YES : NO;
  5666. }
  5667. NSMutableDictionary *sync_dic = [params mutableCopy];
  5668. if (isCustomer) {
  5669. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5670. } else {
  5671. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5672. }
  5673. NSString *sync_data = nil;
  5674. NSString *sql = nil;
  5675. if (update){
  5676. contact_id = [params objectForKey:@"contact_id"];
  5677. if (!contact_id) {
  5678. contact_id = @"";
  5679. }
  5680. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5681. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5682. sync_data = [RAConvertor dict2string:sync_dic];
  5683. sync_data = [self translateSingleQuote:sync_data];
  5684. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,contact_id];
  5685. } else {
  5686. contact_id = [self translateSingleQuote:contact_id];
  5687. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5688. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5689. sync_data = [RAConvertor dict2string:sync_dic];
  5690. sync_data = [self translateSingleQuote:sync_data];
  5691. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  5692. }
  5693. int result = [iSalesDB execSql:sql];
  5694. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5695. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5696. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5697. }
  5698. #pragma mark save new contact
  5699. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5700. {
  5701. return [self offline_saveContact:params update:NO isCustomer:YES];
  5702. }
  5703. #pragma mark edit contact
  5704. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5705. {
  5706. assert(params[@"user"]!=nil);
  5707. // {
  5708. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5709. // password = 123456;
  5710. // user = EvanK;
  5711. // }
  5712. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5713. NSData *data = [NSData dataWithContentsOfFile:path];
  5714. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5715. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5716. NSString *countryCode = nil;
  5717. NSString *countryCode_id = nil;
  5718. NSString *stateCode = nil;
  5719. /*------contact infor------*/
  5720. __block NSString *country = nil;
  5721. __block NSString *company_name = nil;
  5722. __block NSString *contact_id = params[@"contact_id"];
  5723. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5724. __block NSString *zipcode = nil;
  5725. __block NSString *state = nil; // state_code
  5726. __block NSString *city = nil; //
  5727. __block NSString *firt_name,*last_name;
  5728. __block NSString *phone,*fax,*email;
  5729. __block NSString *notes,*price_type,*sales_rep;
  5730. __block NSString *img_0,*img_1,*img_2;
  5731. __block NSString *contact_type;
  5732. contact_id = [self translateSingleQuote:contact_id];
  5733. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type from offline_contact where contact_id = '%@';",contact_id];
  5734. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5735. country = [self textAtColumn:0 statement:stmt]; // country name
  5736. company_name = [self textAtColumn:1 statement:stmt];
  5737. addr_1 = [self textAtColumn:2 statement:stmt];
  5738. addr_2 = [self textAtColumn:3 statement:stmt];
  5739. addr_3 = [self textAtColumn:4 statement:stmt];
  5740. addr_4 = [self textAtColumn:5 statement:stmt];
  5741. zipcode = [self textAtColumn:6 statement:stmt];
  5742. state = [self textAtColumn:7 statement:stmt]; // state code
  5743. city = [self textAtColumn:8 statement:stmt];
  5744. firt_name = [self textAtColumn:9 statement:stmt];
  5745. last_name = [self textAtColumn:10 statement:stmt];
  5746. phone = [self textAtColumn:11 statement:stmt];
  5747. fax = [self textAtColumn:12 statement:stmt];
  5748. email = [self textAtColumn:13 statement:stmt];
  5749. notes = [self textAtColumn:14 statement:stmt];
  5750. price_type = [self textAtColumn:15 statement:stmt]; // name
  5751. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5752. img_0 = [self textAtColumn:17 statement:stmt];
  5753. img_1 = [self textAtColumn:18 statement:stmt];
  5754. img_2 = [self textAtColumn:19 statement:stmt];
  5755. contact_type = [self textAtColumn:20 statement:stmt];
  5756. }];
  5757. // decrypt
  5758. if (company_name) {
  5759. company_name = [AESCrypt fastdecrypt:company_name];
  5760. }
  5761. if (addr_1) {
  5762. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5763. }
  5764. if (phone) {
  5765. phone = [AESCrypt fastdecrypt:phone];
  5766. }
  5767. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5768. countryCode = [iSalesDB jk_queryText:countrySql];
  5769. stateCode = state;
  5770. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5771. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5772. NSString *code_id = params[@"country"];
  5773. countryCode_id = code_id;
  5774. countryCode = [self countryCodeByid:code_id];
  5775. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5776. NSString *zip_code = params[@"zipcode"];
  5777. // 剔除全部为空格
  5778. int spaceCount = 0;
  5779. for (int i = 0; i < zip_code.length; i++) {
  5780. if ([zip_code characterAtIndex:i] == ' ') {
  5781. spaceCount++;
  5782. }
  5783. }
  5784. if (spaceCount == zip_code.length) {
  5785. zip_code = @"";
  5786. }
  5787. if (zipcode.length > 0) {
  5788. zipcode = zip_code;
  5789. countryCode_id = params[@"country"];
  5790. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5791. countryCode = [dic valueForKey:@"country_code"];
  5792. if (!countryCode) {
  5793. NSString *code_id = params[@"country"];
  5794. countryCode = [self countryCodeByid:code_id];
  5795. }
  5796. stateCode = [dic valueForKey:@"state_code"];
  5797. if (!stateCode.length) {
  5798. stateCode = params[@"state"];
  5799. }
  5800. city = [dic valueForKey:@"city"];
  5801. if (!city.length) {
  5802. city = params[@"city"];
  5803. }
  5804. // zip code
  5805. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5806. [zipDic setValue:zipcode forKey:@"value"];
  5807. [section_0 setValue:zipDic forKey:@"item_8"];
  5808. } else {
  5809. NSString *code_id = params[@"country"];
  5810. countryCode = [self countryCodeByid:code_id];
  5811. stateCode = params[@"state"];
  5812. city = params[@"city"];
  5813. }
  5814. }
  5815. }
  5816. // 0 Country
  5817. // 1 Company Name
  5818. // 2 Contact ID
  5819. // 3 Picture
  5820. // 4 Address 1
  5821. // 5 Address 2
  5822. // 6 Address 3
  5823. // 7 Address 4
  5824. // 8 Zip Code
  5825. // 9 State/Province
  5826. // 10 City
  5827. // 11 Contact First Name
  5828. // 12 Contact Last Name
  5829. // 13 Phone
  5830. // 14 Fax
  5831. // 15 Email
  5832. // 16 Contact Notes
  5833. // 17 Price Type
  5834. // 18 Contact Type
  5835. // 19 Sales Rep
  5836. // country
  5837. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5838. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5839. // company
  5840. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5841. // contact_id
  5842. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5843. // picture
  5844. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5845. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5846. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5847. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5848. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5849. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5850. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5851. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5852. // addr 1 2 3 4
  5853. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5854. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5855. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5856. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5857. // zip code
  5858. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5859. // state
  5860. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5861. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5862. // city
  5863. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5864. // first last
  5865. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5866. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5867. // phone fax email
  5868. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5869. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5870. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5871. // notes
  5872. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5873. // price
  5874. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5875. for (NSString *key in priceDic.allKeys) {
  5876. if ([key containsString:@"val_"]) {
  5877. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5878. if ([dic[@"value"] isEqualToString:price_type]) {
  5879. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5880. [priceDic setValue:dic forKey:key];
  5881. }
  5882. }
  5883. }
  5884. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5885. // Contact Rep
  5886. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5887. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5888. // Sales Rep
  5889. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5890. for (NSString *key in repDic.allKeys) {
  5891. if ([key containsString:@"val_"]) {
  5892. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5893. NSString *value = dic[@"value"];
  5894. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5895. if (code && [code isEqualToString:sales_rep]) {
  5896. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5897. [repDic setValue:dic forKey:key];
  5898. }
  5899. }
  5900. }
  5901. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5902. [ret setValue:section_0 forKey:@"section_0"];
  5903. return [RAConvertor dict2data:ret];
  5904. }
  5905. #pragma mark save contact
  5906. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5907. {
  5908. return [self offline_saveContact:params update:YES isCustomer:YES];
  5909. }
  5910. #pragma mark category
  5911. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5912. if (ck) {
  5913. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5914. for (NSString *key in res.allKeys) {
  5915. if (![key isEqualToString:@"count"]) {
  5916. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5917. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5918. if ([val[@"value"] isEqualToString:ck]) {
  5919. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5920. }
  5921. [res setValue:val forKey:key];
  5922. }
  5923. }
  5924. [dic setValue:res forKey:valueKey];
  5925. }
  5926. }
  5927. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5928. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5929. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5930. NSString* category = [params valueForKey:@"category"];
  5931. if (!category || [category isEqualToString:@""]) {
  5932. category = @"%";
  5933. }
  5934. category = [self translateSingleQuote:category];
  5935. int limit = [[params valueForKey:@"limit"] intValue];
  5936. int offset = [[params valueForKey:@"offset"] intValue];
  5937. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5938. NSString *limit_str = @"";
  5939. if (limited) {
  5940. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5941. }
  5942. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5943. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5944. sqlite3 *db = [iSalesDB get_db];
  5945. // [iSalesDB AddExFunction:db];
  5946. int count;
  5947. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5948. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",params[@"orderCode"], category,limit_str];
  5949. double price_min = 0;
  5950. double price_max = 0;
  5951. if ([params.allKeys containsObject:@"alert"]) {
  5952. // alert
  5953. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5954. NSString *alert = params[@"alert"];
  5955. if ([alert isEqualToString:@"Display All"]) {
  5956. alert = [NSString stringWithFormat:@""];
  5957. } else {
  5958. alert = [self translateSingleQuote:alert];
  5959. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5960. }
  5961. // available
  5962. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5963. NSString *available = params[@"available"];
  5964. NSString *available_condition;
  5965. if ([available isEqualToString:@"Display All"]) {
  5966. available_condition = @"";
  5967. } else if ([available isEqualToString:@"Available Now"]) {
  5968. available_condition = @"and availability > 0";
  5969. } else {
  5970. available_condition = @"and availability == 0";
  5971. }
  5972. // best seller
  5973. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5974. NSString *best_seller = @"";
  5975. NSString *order_best_seller = @"m.name asc";
  5976. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5977. best_seller = @"and best_seller > 0";
  5978. order_best_seller = @"m.best_seller desc,m.name asc";
  5979. }
  5980. // price
  5981. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5982. NSString *price = params[@"price"];
  5983. price_min = 0;
  5984. price_max = MAXFLOAT;
  5985. if (params[@"user"] && price != nil) {
  5986. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5987. NSMutableString *priceName = [NSMutableString string];
  5988. for (int i = 0; i < priceTypeArray.count; i++) {
  5989. NSString *pricetype = priceTypeArray[i];
  5990. pricetype = [self translateSingleQuote:pricetype];
  5991. if (i == 0) {
  5992. [priceName appendFormat:@"'%@'",pricetype];
  5993. } else {
  5994. [priceName appendFormat:@",'%@'",pricetype];
  5995. }
  5996. }
  5997. if ([price isEqualToString:@"Display All"]) {
  5998. price = [NSString stringWithFormat:@""];
  5999. } else if([price containsString:@"+"]){
  6000. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6001. price_min = [price doubleValue];
  6002. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6003. } else {
  6004. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6005. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6006. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6007. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6008. }
  6009. } else {
  6010. price = @"";
  6011. }
  6012. // sold_by_qty : Sold in quantities of %@
  6013. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6014. NSString *qty = params[@"sold_by_qty"];
  6015. if ([qty isEqualToString:@"Display All"]) {
  6016. qty = @"";
  6017. } else {
  6018. qty = [self translateSingleQuote:qty];
  6019. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6020. }
  6021. // model name;
  6022. NSString* modelname =params[@"modelName"];
  6023. if(modelname.length==0)
  6024. {
  6025. modelname=@"";
  6026. }
  6027. else
  6028. {
  6029. modelname = modelname.lowercaseString;
  6030. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6031. }
  6032. //modelDescription
  6033. NSString* modelDescription =params[@"modelDescription"];
  6034. if(modelDescription.length==0)
  6035. {
  6036. modelDescription=@"";
  6037. }
  6038. else
  6039. {
  6040. modelDescription = modelDescription.lowercaseString;
  6041. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6042. }
  6043. // cate
  6044. // category = [self translateSingleQuote:category];
  6045. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6046. // cate mutiple selection
  6047. NSString *category_id = params[@"category"];
  6048. NSMutableArray *cate_id_array = nil;
  6049. NSMutableString *cateWhere = [NSMutableString string];
  6050. if ([category_id isEqualToString:@""] || !category_id) {
  6051. [cateWhere appendString:@"1 = 1"];
  6052. } else {
  6053. if ([category_id containsString:@","]) {
  6054. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6055. } else {
  6056. cate_id_array = [@[category_id] mutableCopy];
  6057. }
  6058. [cateWhere appendString:@"("];
  6059. for (int i = 0; i < cate_id_array.count; i++) {
  6060. if (i == 0) {
  6061. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6062. } else {
  6063. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6064. }
  6065. }
  6066. [cateWhere appendString:@")"];
  6067. }
  6068. // where bestseller > 0 order by bestseller desc
  6069. // sql query: alert availability(int) best_seller(int) price qty
  6070. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color from product where %@ %@ %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price,params[@"orderCode"],order_best_seller,limit_str];
  6071. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6072. }
  6073. DebugLog(@"offline category where: %@",where);
  6074. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6075. if (!params[@"user"]) {
  6076. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6077. }
  6078. [ret setValue:filter forKey:@"filter"];
  6079. DebugLog(@"offline_category sql:%@",sqlQuery);
  6080. sqlite3_stmt * statement;
  6081. [ret setValue:@"2" forKey:@"result"];
  6082. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6083. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6084. // int count=0;
  6085. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6086. {
  6087. int i=0;
  6088. while (sqlite3_step(statement) == SQLITE_ROW)
  6089. {
  6090. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6091. char *name = (char*)sqlite3_column_text(statement, 0);
  6092. if(name==nil)
  6093. name="";
  6094. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6095. char *description = (char*)sqlite3_column_text(statement, 1);
  6096. if(description==nil)
  6097. description="";
  6098. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6099. int product_id = sqlite3_column_int(statement, 2);
  6100. int wid = sqlite3_column_int(statement, 3);
  6101. int closeout = sqlite3_column_int(statement, 4);
  6102. int cid = sqlite3_column_int(statement, 5);
  6103. int wisdelete = sqlite3_column_int(statement, 6);
  6104. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6105. int more_color = sqlite3_column_int(statement, 8);
  6106. // Defaul Category ID
  6107. __block NSString *categoryID = nil;
  6108. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6109. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6110. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6111. if(default_category==nil)
  6112. default_category="";
  6113. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6114. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6115. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6116. categoryID = nsdefault_category;
  6117. }];
  6118. if (!categoryID.length) {
  6119. NSString *cateIDs = params[@"category"];
  6120. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6121. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6122. for (NSString *cateID in requestCategoryArr) {
  6123. BOOL needBreak = NO;
  6124. for (NSString *itemCateIDBox in itemCategoryArr) {
  6125. if (itemCateIDBox.length > 4) {
  6126. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6127. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6128. if ([itemCategoryID isEqualToString:cateID]) {
  6129. needBreak = YES;
  6130. categoryID = itemCategoryID;
  6131. break;
  6132. }
  6133. }
  6134. }
  6135. if (needBreak) {
  6136. break;
  6137. }
  6138. }
  6139. }
  6140. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6141. if(wid !=0 && wisdelete != 1)
  6142. [item setValue:@"true" forKey:@"wish_exists"];
  6143. else
  6144. [item setValue:@"false" forKey:@"wish_exists"];
  6145. if(closeout==0)
  6146. [item setValue:@"false" forKey:@"is_closeout"];
  6147. else
  6148. [item setValue:@"true" forKey:@"is_closeout"];
  6149. if(cid==0)
  6150. [item setValue:@"false" forKey:@"cart_exists"];
  6151. else
  6152. [item setValue:@"true" forKey:@"cart_exists"];
  6153. if (more_color == 0) {
  6154. [item setObject:@(false) forKey:@"more_color"];
  6155. } else if (more_color == 1) {
  6156. [item setObject:@(true) forKey:@"more_color"];
  6157. }
  6158. [item addEntriesFromDictionary:imgjson];
  6159. // [item setValue:nsurl forKey:@"img"];
  6160. [item setValue:nsname forKey:@"name"];
  6161. [item setValue:nsdescription forKey:@"description"];
  6162. if (categoryID) {
  6163. [item setValue:categoryID forKey:@"item_category_id"];
  6164. }
  6165. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6166. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6167. i++;
  6168. }
  6169. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6170. [ret setObject:items forKey:@"items"];
  6171. sqlite3_finalize(statement);
  6172. } else {
  6173. DebugLog(@"nothing...");
  6174. }
  6175. DebugLog(@"count:%d",count);
  6176. [iSalesDB close_db:db];
  6177. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6178. return ret;
  6179. }
  6180. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6181. {
  6182. return [self categoryList:params limited:YES];
  6183. }
  6184. # pragma mark item search
  6185. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6186. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6187. // assert(params[@"order_code"]);
  6188. // params[@"user"]
  6189. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6190. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6191. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6192. // category
  6193. NSDictionary *category_menu = [self offline_category_menu];
  6194. [filter setValue:category_menu forKey:@"category"];
  6195. NSString* where= nil;
  6196. NSString* orderby= @"m.name";
  6197. if (!filterSearch) {
  6198. int covertype = [[params valueForKey:@"covertype"] intValue];
  6199. switch (covertype) {
  6200. case 0:
  6201. {
  6202. where=@"m.category like'%%#005#%%'";
  6203. break;
  6204. }
  6205. case 1:
  6206. {
  6207. where=@"m.alert like '%QS%'";
  6208. break;
  6209. }
  6210. case 2:
  6211. {
  6212. where=@"m.availability>0";
  6213. break;
  6214. }
  6215. case 3:
  6216. {
  6217. where=@"m.best_seller>0";
  6218. orderby=@"m.best_seller desc,m.name asc";
  6219. break;
  6220. }
  6221. default:
  6222. where=@"1=1";
  6223. break;
  6224. }
  6225. }
  6226. int limit = [[params valueForKey:@"limit"] intValue];
  6227. int offset = [[params valueForKey:@"offset"] intValue];
  6228. NSString *limit_str = @"";
  6229. if (limited) {
  6230. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6231. }
  6232. sqlite3 *db = [iSalesDB get_db];
  6233. // [iSalesDB AddExFunction:db];
  6234. int count;
  6235. NSString *sqlQuery = nil;
  6236. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6237. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",params[@"orderCode"], where,orderby,limit_str];
  6238. double price_min = 0;
  6239. double price_max = 0;
  6240. if (filterSearch) {
  6241. // alert
  6242. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6243. NSString *alert = params[@"alert"];
  6244. if ([alert isEqualToString:@"Display All"]) {
  6245. alert = [NSString stringWithFormat:@""];
  6246. } else {
  6247. alert = [self translateSingleQuote:alert];
  6248. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6249. }
  6250. // available
  6251. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6252. NSString *available = params[@"available"];
  6253. NSString *available_condition;
  6254. if ([available isEqualToString:@"Display All"]) {
  6255. available_condition = @"";
  6256. } else if ([available isEqualToString:@"Available Now"]) {
  6257. available_condition = @"and availability > 0";
  6258. } else {
  6259. available_condition = @"and availability == 0";
  6260. }
  6261. // best seller
  6262. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6263. NSString *best_seller = @"";
  6264. NSString *order_best_seller = @"m.name asc";
  6265. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6266. best_seller = @"and best_seller > 0";
  6267. order_best_seller = @"m.best_seller desc,m.name asc";
  6268. }
  6269. // price
  6270. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6271. NSString *price = params[@"price"];
  6272. price_min = 0;
  6273. price_max = MAXFLOAT;
  6274. if (params[@"user"]) {
  6275. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6276. NSMutableString *priceName = [NSMutableString string];
  6277. for (int i = 0; i < priceTypeArray.count; i++) {
  6278. NSString *pricetype = priceTypeArray[i];
  6279. pricetype = [self translateSingleQuote:pricetype];
  6280. if (i == 0) {
  6281. [priceName appendFormat:@"'%@'",pricetype];
  6282. } else {
  6283. [priceName appendFormat:@",'%@'",pricetype];
  6284. }
  6285. }
  6286. if ([price isEqualToString:@"Display All"]) {
  6287. price = [NSString stringWithFormat:@""];
  6288. } else if([price containsString:@"+"]){
  6289. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6290. price_min = [price doubleValue];
  6291. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6292. } else {
  6293. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6294. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6295. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6296. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6297. }
  6298. } else {
  6299. price = @"";
  6300. }
  6301. // sold_by_qty : Sold in quantities of %@
  6302. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6303. NSString *qty = params[@"sold_by_qty"];
  6304. if ([qty isEqualToString:@"Display All"]) {
  6305. qty = @"";
  6306. } else {
  6307. qty = [self translateSingleQuote:qty];
  6308. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6309. }
  6310. // model name;
  6311. NSString* modelname =params[@"modelName"];
  6312. if(modelname.length==0)
  6313. {
  6314. modelname=@"";
  6315. }
  6316. else
  6317. {
  6318. modelname = modelname.lowercaseString;
  6319. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6320. }
  6321. //modelDescription
  6322. NSString* modelDescription =params[@"modelDescription"];
  6323. if(modelDescription.length==0)
  6324. {
  6325. modelDescription=@"";
  6326. }
  6327. else
  6328. {
  6329. modelDescription = modelDescription.lowercaseString;
  6330. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6331. }
  6332. //lower(description) like'%%%@%%'
  6333. // category
  6334. NSString *category_id = params[@"ctgId"];
  6335. NSMutableArray *cate_id_array = nil;
  6336. NSMutableString *cateWhere = [NSMutableString string];
  6337. if ([category_id isEqualToString:@""] || !category_id) {
  6338. [cateWhere appendString:@"1 = 1"];
  6339. } else {
  6340. if ([category_id containsString:@","]) {
  6341. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6342. } else {
  6343. cate_id_array = [@[category_id] mutableCopy];
  6344. }
  6345. /*-----------*/
  6346. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6347. [cateWhere appendString:@"("];
  6348. for (int i = 0; i < cate_id_array.count; i++) {
  6349. for (NSString *key0 in cateDic.allKeys) {
  6350. if ([key0 containsString:@"category_"]) {
  6351. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6352. for (NSString *key1 in category0.allKeys) {
  6353. if ([key1 containsString:@"category_"]) {
  6354. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6355. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6356. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6357. cate_id_array[i] = [category1 objectForKey:@"id"];
  6358. if (i == 0) {
  6359. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6360. } else {
  6361. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6362. }
  6363. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6364. [category0 setValue:category1 forKey:key1];
  6365. [cateDic setValue:category0 forKey:key0];
  6366. }
  6367. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6368. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6369. cate_id_array[i] = [category0 objectForKey:@"id"];
  6370. if (i == 0) {
  6371. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6372. } else {
  6373. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6374. }
  6375. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6376. [cateDic setValue:category0 forKey:key0];
  6377. }
  6378. }
  6379. }
  6380. }
  6381. }
  6382. }
  6383. [cateWhere appendString:@")"];
  6384. [filter setValue:cateDic forKey:@"category"];
  6385. }
  6386. // where bestseller > 0 order by bestseller desc
  6387. // sql query: alert availability(int) best_seller(int) price qty
  6388. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color from product where %@ %@ %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price,params[@"orderCode"],order_best_seller,limit_str];
  6389. // count
  6390. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6391. }
  6392. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6393. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6394. if (!params[@"user"]) {
  6395. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6396. }
  6397. [ret setValue:filter forKey:@"filter"];
  6398. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6399. sqlite3_stmt * statement;
  6400. [ret setValue:@"2" forKey:@"result"];
  6401. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6402. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6403. // int count=0;
  6404. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6405. {
  6406. int i=0;
  6407. while (sqlite3_step(statement) == SQLITE_ROW)
  6408. {
  6409. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6410. char *name = (char*)sqlite3_column_text(statement, 0);
  6411. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6412. char *description = (char*)sqlite3_column_text(statement, 1);
  6413. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6414. int product_id = sqlite3_column_int(statement, 2);
  6415. int wid = sqlite3_column_int(statement, 3);
  6416. int closeout = sqlite3_column_int(statement, 4);
  6417. int cid = sqlite3_column_int(statement, 5);
  6418. int wisdelete = sqlite3_column_int(statement, 6);
  6419. int more_color = sqlite3_column_int(statement, 7);
  6420. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6421. if(wid !=0 && wisdelete != 1)
  6422. [item setValue:@"true" forKey:@"wish_exists"];
  6423. else
  6424. [item setValue:@"false" forKey:@"wish_exists"];
  6425. if(closeout==0)
  6426. [item setValue:@"false" forKey:@"is_closeout"];
  6427. else
  6428. [item setValue:@"true" forKey:@"is_closeout"];
  6429. if(cid==0)
  6430. [item setValue:@"false" forKey:@"cart_exists"];
  6431. else
  6432. [item setValue:@"true" forKey:@"cart_exists"];
  6433. if (more_color == 0) {
  6434. [item setObject:@(false) forKey:@"more_color"];
  6435. } else if (more_color == 1) {
  6436. [item setObject:@(true) forKey:@"more_color"];
  6437. }
  6438. [item addEntriesFromDictionary:imgjson];
  6439. // [item setValue:nsurl forKey:@"img"];
  6440. [item setValue:nsname forKey:@"fash_name"];
  6441. [item setValue:nsdescription forKey:@"description"];
  6442. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6443. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6444. i++;
  6445. }
  6446. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6447. [ret setObject:items forKey:@"items"];
  6448. sqlite3_finalize(statement);
  6449. }
  6450. [iSalesDB close_db:db];
  6451. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6452. return ret;
  6453. }
  6454. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6455. {
  6456. return [self itemsearch:params limited:YES];
  6457. }
  6458. #pragma mark order detail
  6459. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6460. if (str1.length == 0) {
  6461. str1 = @"&nbsp";
  6462. }
  6463. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6464. return str;
  6465. }
  6466. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6467. {
  6468. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6469. [fromformatter setDateFormat:from];
  6470. NSDate *date = [fromformatter dateFromString:datetime];
  6471. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6472. [toformatter setDateFormat:to];
  6473. NSString * ret = [toformatter stringFromDate:date];
  6474. return ret;
  6475. }
  6476. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6477. // 把毫秒去掉
  6478. if ([dateTime containsString:@"."]) {
  6479. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6480. }
  6481. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6482. formatter.dateFormat = formate;
  6483. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6484. NSDate *date = [formatter dateFromString:dateTime];
  6485. formatter.dateFormat = newFormate;
  6486. NSString *result = [formatter stringFromDate:date];
  6487. return result ? result : @"";
  6488. }
  6489. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6490. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6491. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6492. }
  6493. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6494. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6495. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6496. }
  6497. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6498. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6499. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6500. }
  6501. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6502. {
  6503. assert(params[@"mode"]!=nil);
  6504. assert(params[@"user"]!=nil);
  6505. DebugLog(@"offline oderdetail params: %@",params);
  6506. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6507. int orderId = [params[@"orderId"] intValue];
  6508. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6509. // decrypt card number and card security code
  6510. // 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 ];
  6511. 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];
  6512. sqlite3 *db = [iSalesDB get_db];
  6513. sqlite3_stmt * statement;
  6514. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6515. NSString *nssoid = @"";
  6516. NSString* orderinfo = @"";
  6517. NSString *moreInfo = @"";
  6518. double handlingFee = 0;
  6519. double payments_and_credist = 0;
  6520. double totalPrice = 0;
  6521. double shippingFee = 0;
  6522. double lift_gate = 0;
  6523. NSString *customer_contact = @"";
  6524. NSString *customer_email = @"";
  6525. NSString *customer_fax = @"";
  6526. NSString *customer_phone = @"";
  6527. NSString *customer_city = @"";
  6528. NSString *customer_state = @"";
  6529. NSString *customer_zipcode = @"";
  6530. NSString *customer_country = @"";
  6531. BOOL must_call = NO;
  6532. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6533. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6534. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6535. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6536. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6537. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6538. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6539. {
  6540. if (sqlite3_step(statement) == SQLITE_ROW)
  6541. {
  6542. // int order_id = sqlite3_column_int(statement, 0);
  6543. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6544. // ret[@"sign_url"] = sign_url;
  6545. section_1 = @{
  6546. @"data":sign_url,
  6547. @"title":@"Signature",
  6548. @"type":@"sign_url"
  6549. }.mutableCopy;
  6550. [ret setObject:section_1 forKey:@"section_1"];
  6551. customer_contact = [self textAtColumn:52 statement:statement];
  6552. customer_email = [self textAtColumn:53 statement:statement];
  6553. customer_phone = [self textAtColumn:54 statement:statement];
  6554. customer_fax = [self textAtColumn:55 statement:statement];
  6555. customer_city = [self textAtColumn:60 statement:statement];
  6556. customer_state = [self textAtColumn:61 statement:statement];
  6557. customer_zipcode = [self textAtColumn:62 statement:statement];
  6558. customer_country = [self textAtColumn:63 statement:statement];
  6559. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6560. int offline_edit=sqlite3_column_int(statement, 56);
  6561. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6562. char *soid = (char*)sqlite3_column_text(statement, 1);
  6563. if(soid==nil)
  6564. soid= "";
  6565. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6566. // so#
  6567. ret[@"so#"] = nssoid;
  6568. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6569. if(poNumber==nil)
  6570. poNumber= "";
  6571. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6572. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6573. if(create_time==nil)
  6574. create_time= "";
  6575. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6576. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6577. int status = sqlite3_column_int(statement, 4);
  6578. int erpStatus = sqlite3_column_int(statement, 49);
  6579. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6580. // status
  6581. if (status > 1 && status != 3) {
  6582. status = erpStatus;
  6583. } else if (status == 3) {
  6584. status = 15;
  6585. }
  6586. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6587. ret[@"order_status"] = nsstatus;
  6588. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6589. if(company_name==nil)
  6590. company_name= "";
  6591. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6592. // company name
  6593. ret[@"company_name"] = nscompany_name;
  6594. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6595. if(customer_contact==nil)
  6596. customer_contact= "";
  6597. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6598. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6599. if(addr_1==nil)
  6600. addr_1="";
  6601. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6602. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6603. if(addr_2==nil)
  6604. addr_2="";
  6605. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6606. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6607. if(addr_3==nil)
  6608. addr_3="";
  6609. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6610. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6611. if(addr_4==nil)
  6612. addr_4="";
  6613. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6614. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6615. [arr_addr addObject:nsaddr_1];
  6616. [arr_addr addObject:nsaddr_2];
  6617. [arr_addr addObject:nsaddr_3];
  6618. [arr_addr addObject:nsaddr_4];
  6619. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6620. [arr_addr addObject:customer_state];
  6621. [arr_addr addObject:customer_zipcode];
  6622. [arr_addr addObject:customer_country];
  6623. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6624. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6625. char *logist = (char*)sqlite3_column_text(statement, 11);
  6626. if(logist==nil)
  6627. logist= "";
  6628. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6629. if (/*status == -11 || */status == 10 || status == 11) {
  6630. nslogist = [self textAtColumn:59 statement:statement];
  6631. };
  6632. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6633. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6634. shipping = @"Shipping To Be Quoted";
  6635. } else {
  6636. shippingFee = sqlite3_column_double(statement, 12);
  6637. }
  6638. // Shipping
  6639. // ret[@"Shipping"] = shipping;
  6640. NSDictionary *shipping_item = @{
  6641. @"title":@"Shipping",
  6642. @"value":shipping
  6643. };
  6644. [price_data setObject:shipping_item forKey:@"item_1"];
  6645. int have_lift_gate = sqlite3_column_int(statement, 17);
  6646. lift_gate = sqlite3_column_double(statement, 13);
  6647. // Liftgate Fee(No loading dock)
  6648. if (!have_lift_gate) {
  6649. lift_gate = 0;
  6650. }
  6651. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6652. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6653. if (sqlite3_column_int(statement, 57)) {
  6654. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6655. liftgate_value = @"Shipping To Be Quoted";
  6656. }
  6657. NSDictionary *liftgate_item = @{
  6658. @"title":@"Liftgate Fee(No loading dock)",
  6659. @"value":liftgate_value
  6660. };
  6661. [price_data setObject:liftgate_item forKey:@"item_2"];
  6662. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6663. // [ret removeObjectForKey:@"Shipping"];
  6664. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6665. // }
  6666. //
  6667. // if (have_lift_gate) {
  6668. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6669. // }
  6670. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6671. if(general_notes==nil)
  6672. general_notes= "";
  6673. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6674. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6675. if(internal_notes==nil)
  6676. internal_notes= "";
  6677. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6678. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6679. if(payment_type==nil)
  6680. payment_type= "";
  6681. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6682. // order info
  6683. orderinfo = [self textFileName:@"order_info.html"];
  6684. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6685. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6686. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6687. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6688. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6689. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6690. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6691. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6692. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6693. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6694. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6695. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6696. NSString *payment = nil;
  6697. // id -> show
  6698. __block NSString *show_pay_type = nspayment_type;
  6699. [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) {
  6700. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6701. if (show_typ_ch != NULL) {
  6702. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6703. }
  6704. }];
  6705. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6706. {
  6707. payment = [self textFileName:@"creditcardpayment.html"];
  6708. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6709. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6710. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6711. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6712. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6713. NSString *card_type = [self textAtColumn:42 statement:statement];
  6714. if (card_type.length > 0) { // 显示星号
  6715. card_type = @"****";
  6716. }
  6717. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6718. if (card_number.length > 0 && card_number.length > 4) {
  6719. for (int i = 0; i < card_number.length - 4; i++) {
  6720. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6721. }
  6722. } else {
  6723. card_number = @"";
  6724. }
  6725. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6726. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6727. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6728. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6729. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6730. card_expiration = @"****";
  6731. }
  6732. NSString *card_city = [self textAtColumn:46 statement:statement];
  6733. NSString *card_state = [self textAtColumn:47 statement:statement];
  6734. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6735. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6736. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6737. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6738. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6739. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6740. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6741. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6742. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6743. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6744. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6745. }
  6746. else
  6747. {
  6748. payment=[self textFileName:@"normalpayment.html"];
  6749. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6750. }
  6751. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6752. ret[@"result"]= [NSNumber numberWithInt:2];
  6753. // more info
  6754. moreInfo = [self textFileName:@"more_info.html"];
  6755. /*****ship to******/
  6756. // ShipToCompany_or_&nbsp
  6757. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6758. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6759. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6760. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6761. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6762. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6763. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6764. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6765. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6766. /*****ship from******/
  6767. // ShipFromCompany_or_&nbsp
  6768. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6769. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6770. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6771. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6772. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6773. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6774. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6775. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6776. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6777. /*****freight to******/
  6778. // FreightBillToCompany_or_&nbsp
  6779. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6780. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6781. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6782. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6783. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6784. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6785. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6786. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6787. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6788. /*****merchandise to******/
  6789. // MerchandiseBillToCompany_or_&nbsp
  6790. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6791. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6792. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6793. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6794. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6795. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6796. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6797. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6798. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6799. /*****return to******/
  6800. // ReturnToCompany_or_&nbsp
  6801. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6802. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6803. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6804. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6805. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6806. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6807. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6808. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6809. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6810. //
  6811. // DebugLog(@"more info : %@",moreInfo);
  6812. // handling fee
  6813. handlingFee = sqlite3_column_double(statement, 33);
  6814. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6815. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6816. if (sqlite3_column_int(statement, 58)) {
  6817. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6818. handling_fee_value = @"Shipping To Be Quoted";
  6819. }
  6820. NSDictionary *handling_fee_item = @{
  6821. @"title":@"Handling Fee",
  6822. @"value":handling_fee_value
  6823. };
  6824. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6825. //
  6826. // customer info
  6827. customerID = [self textAtColumn:36 statement:statement];
  6828. // mode
  6829. ret[@"mode"] = params[@"mode"];
  6830. // model_count
  6831. ret[@"model_count"] = @(0);
  6832. }
  6833. sqlite3_finalize(statement);
  6834. }
  6835. [iSalesDB close_db:db];
  6836. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6837. // "customer_email" = "Shui Hu";
  6838. // "customer_fax" = "";
  6839. // "customer_first_name" = F;
  6840. // "customer_last_name" = L;
  6841. // "customer_name" = ",da He Xiang Dong Liu A";
  6842. // "customer_phone" = "Hey Xuan Feng";
  6843. contactInfo[@"customer_phone"] = customer_phone;
  6844. contactInfo[@"customer_fax"] = customer_fax;
  6845. contactInfo[@"customer_email"] = customer_email;
  6846. NSString *first_name = @"";
  6847. NSString *last_name = @"";
  6848. if ([customer_contact isEqualToString:@""]) {
  6849. } else if ([customer_contact containsString:@" "]) {
  6850. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6851. first_name = [customer_contact substringToIndex:first_space_index];
  6852. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6853. }
  6854. contactInfo[@"customer_first_name"] = first_name;
  6855. contactInfo[@"customer_last_name"] = last_name;
  6856. ret[@"customerInfo"] = contactInfo;
  6857. // models
  6858. if (nssoid) {
  6859. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6860. __block double TotalCuft = 0;
  6861. __block double TotalWeight = 0;
  6862. __block int TotalCarton = 0;
  6863. __block double allItemPrice = 0;
  6864. 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];
  6865. sqlite3 *db1 = [iSalesDB get_db];
  6866. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6867. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6868. int product_id = sqlite3_column_int(stmt, 0);
  6869. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6870. int item_id = sqlite3_column_int(stmt, 7);
  6871. NSString* Price=nil;
  6872. if(str_price==nil)
  6873. {
  6874. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6875. if(price==nil)
  6876. Price=@"No Price.";
  6877. else
  6878. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6879. }
  6880. else
  6881. {
  6882. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6883. }
  6884. double discount = sqlite3_column_double(stmt, 2);
  6885. int item_count = sqlite3_column_int(stmt, 3);
  6886. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6887. NSString *nsline_note=nil;
  6888. if(line_note!=nil)
  6889. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6890. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6891. // NSString *nsname = nil;
  6892. // if(name!=nil)
  6893. // nsname= [[NSString alloc]initWithUTF8String:name];
  6894. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6895. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6896. // NSString *nsdescription=nil;
  6897. // if(description!=nil)
  6898. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6899. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6900. // int stockUom = sqlite3_column_int(stmt, 8);
  6901. // int _id = sqlite3_column_int(stmt, 9);
  6902. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6903. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6904. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6905. int carton=[bsubtotaljson[@"carton"] intValue];
  6906. TotalCuft += cuft;
  6907. TotalWeight += weight;
  6908. TotalCarton += carton;
  6909. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6910. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6911. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6912. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6913. itemjson[@"note"]=nsline_note;
  6914. itemjson[@"origin_price"] = Price;
  6915. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6916. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6917. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6918. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6919. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6920. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6921. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6922. if(itemjson[@"combine"] != nil)
  6923. {
  6924. // int citem=0;
  6925. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6926. for(int bc=0;bc<bcount;bc++)
  6927. {
  6928. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6929. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6930. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6931. subTotal += uprice * modulus * item_count;
  6932. }
  6933. }
  6934. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6935. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6936. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6937. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6938. itemjson[@"type"] = @"order_item";
  6939. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6940. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6941. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6942. allItemPrice += subTotal;
  6943. }];
  6944. section_3[@"data"] = model_data;
  6945. section_3[@"type"] = @"sub_order";
  6946. section_3[@"title"] = @"Models";
  6947. section_3[@"switch"] = @(false);
  6948. ret[@"section_3"] = section_3;
  6949. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6950. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6951. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6952. // payments/Credits
  6953. // payments_and_credist = sqlite3_column_double(statement, 34);
  6954. payments_and_credist = allItemPrice;
  6955. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6956. NSDictionary *sub_total_item = @{
  6957. @"title":@"Sub-Total",
  6958. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6959. };
  6960. [price_data setObject:sub_total_item forKey:@"item_0"];
  6961. // // total
  6962. // totalPrice = sqlite3_column_double(statement, 35);
  6963. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6964. } else {
  6965. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6966. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6967. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6968. // payments/Credits
  6969. payments_and_credist = 0;
  6970. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6971. NSDictionary *sub_total_item = @{
  6972. @"title":@"Sub-Total",
  6973. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6974. };
  6975. [price_data setObject:sub_total_item forKey:@"item_0"];
  6976. }
  6977. // total
  6978. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6979. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6980. NSDictionary *total_item = @{
  6981. @"title":@"Total",
  6982. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6983. };
  6984. [price_data setObject:total_item forKey:@"item_4"];
  6985. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6986. section_4 = @{
  6987. @"data":price_data,
  6988. @"title":@"Price Info",
  6989. @"type":@"price_info"
  6990. }.mutableCopy;
  6991. ret[@"section_4"] = section_4;
  6992. // ret[@"order_info"]= orderinfo;
  6993. section_0 = @{
  6994. @"data":orderinfo,
  6995. @"title":@"Order Info",
  6996. @"type":@"order_info"
  6997. }.mutableCopy;
  6998. ret[@"section_0"] = section_0;
  6999. // ret[@"more_order_info"] = moreInfo;
  7000. section_2 = @{
  7001. @"data":moreInfo,
  7002. @"title":@"More Info",
  7003. @"type":@"more_order_info"
  7004. }.mutableCopy;
  7005. ret[@"section_2"] = section_2;
  7006. ret[@"count"] = @(5);
  7007. return [RAConvertor dict2data:ret];
  7008. }
  7009. #pragma mark order list
  7010. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7011. NSString *nsstatus = @"";
  7012. switch (status) {
  7013. case 0:
  7014. {
  7015. nsstatus=@"Temp Order";
  7016. break;
  7017. }
  7018. case 1:
  7019. {
  7020. nsstatus=@"Saved Order";
  7021. break;
  7022. }
  7023. case 2: {
  7024. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7025. break;
  7026. }
  7027. case 3:
  7028. case 15:
  7029. {
  7030. nsstatus=@"Cancelled";
  7031. break;
  7032. }
  7033. case 10:
  7034. {
  7035. nsstatus=@"Quote Submitted";
  7036. break;
  7037. }
  7038. case 11:
  7039. {
  7040. nsstatus=@"Sales Order Submitted";
  7041. break;
  7042. }
  7043. case 12:
  7044. {
  7045. nsstatus=@"Processing";
  7046. break;
  7047. }
  7048. case 13:
  7049. {
  7050. nsstatus=@"Shipped";
  7051. break;
  7052. }
  7053. case 14:
  7054. {
  7055. nsstatus=@"Closed";
  7056. break;
  7057. }
  7058. case -11:
  7059. {
  7060. nsstatus = @"Ready For Submit";
  7061. break;
  7062. }
  7063. default:
  7064. break;
  7065. }
  7066. return nsstatus;
  7067. }
  7068. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7069. {
  7070. double total = 0;
  7071. __block double payments_and_credist = 0;
  7072. __block double allItemPrice = 0;
  7073. // sqlite3 *db1 = [iSalesDB get_db];
  7074. if (so_id) {
  7075. // 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];
  7076. 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];
  7077. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7078. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7079. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7080. int product_id = sqlite3_column_int(stmt, 0);
  7081. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7082. int discount = sqlite3_column_int(stmt, 2);
  7083. int item_count = sqlite3_column_int(stmt, 3);
  7084. // int item_id = sqlite3_column_int(stmt, 7);
  7085. int item_id = sqlite3_column_int(stmt, 4);
  7086. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7087. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7088. NSString* Price=nil;
  7089. if(str_price==nil)
  7090. {
  7091. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7092. if(price==nil)
  7093. Price=@"No Price.";
  7094. else
  7095. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7096. }
  7097. else
  7098. {
  7099. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7100. }
  7101. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7102. itemjson[@"The unit price"]=Price;
  7103. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7104. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7105. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7106. if(itemjson[@"combine"] != nil)
  7107. {
  7108. // int citem=0;
  7109. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7110. for(int bc=0;bc<bcount;bc++)
  7111. {
  7112. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7113. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7114. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7115. subTotal += uprice * modulus * item_count;
  7116. }
  7117. }
  7118. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7119. allItemPrice += subTotal;
  7120. }];
  7121. payments_and_credist = allItemPrice;
  7122. } else {
  7123. // payments/Credits
  7124. payments_and_credist = 0;
  7125. }
  7126. // lift_gate handlingFee shippingFee
  7127. __block double lift_gate = 0;
  7128. __block double handlingFee = 0;
  7129. __block double shippingFee = 0;
  7130. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7131. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7132. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7133. if (have_lift_gate) {
  7134. lift_gate = sqlite3_column_double(stmt, 1);
  7135. }
  7136. handlingFee = sqlite3_column_double(stmt, 2);
  7137. shippingFee = sqlite3_column_double(stmt, 3);
  7138. }];
  7139. // total
  7140. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7141. if (close) {
  7142. [iSalesDB close_db:db1];
  7143. }
  7144. return total;
  7145. }
  7146. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7147. {
  7148. assert(params[@"user"]!=nil);
  7149. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7150. int limit = [[params valueForKey:@"limit"] intValue];
  7151. int offset = [[params valueForKey:@"offset"] intValue];
  7152. NSString* keyword = [params valueForKey:@"keyWord"];
  7153. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7154. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7155. NSString* where=@"1 = 1";
  7156. if(keyword.length>0)
  7157. 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]];
  7158. if (orderStatus.length > 0) {
  7159. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7160. if (order_status_array.count == 1) {
  7161. int status_value = [[order_status_array firstObject] intValue];
  7162. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7163. if (status_value == 15 || status_value == 3) {
  7164. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7165. } else {
  7166. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7167. }
  7168. } else {
  7169. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7170. }
  7171. } else if (order_status_array.count > 1) {
  7172. for (int i = 0; i < order_status_array.count;i++) {
  7173. NSString *status = order_status_array[i];
  7174. NSString *condition = @" or";
  7175. if (i == 0) {
  7176. condition = @" and (";
  7177. }
  7178. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7179. int status_value = [status intValue];
  7180. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7181. if (status_value == 15 || status_value == 3) {
  7182. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7183. } else {
  7184. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7185. }
  7186. } else {
  7187. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7188. }
  7189. }
  7190. where = [where stringByAppendingString:@" )"];
  7191. }
  7192. }
  7193. 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];
  7194. // DebugLog(@"order list sql: %@",sqlQuery);
  7195. sqlite3 *db = [iSalesDB get_db];
  7196. sqlite3_stmt * statement;
  7197. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7198. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7199. {
  7200. int count=0;
  7201. while (sqlite3_step(statement) == SQLITE_ROW)
  7202. {
  7203. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7204. int order_id = sqlite3_column_double(statement, 0);
  7205. char *soid = (char*)sqlite3_column_text(statement, 1);
  7206. if(soid==nil)
  7207. soid= "";
  7208. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7209. int status = sqlite3_column_double(statement, 2);
  7210. int erpStatus = sqlite3_column_double(statement, 9);
  7211. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7212. if(sales_rep==nil)
  7213. sales_rep= "";
  7214. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7215. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7216. if(create_by==nil)
  7217. create_by= "";
  7218. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7219. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7220. if(company_name==nil)
  7221. company_name= "";
  7222. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7223. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7224. if(create_time==nil)
  7225. create_time= "";
  7226. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7227. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7228. // double total_price = sqlite3_column_double(statement, 7);
  7229. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7230. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7231. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7232. int offline_edit = sqlite3_column_int(statement, 10);
  7233. // ": "JH",
  7234. // "": "$8307.00",
  7235. // "": "MOB1608050001",
  7236. // "": "ArpithaT",
  7237. // "": "1st Stage Property Transformations",
  7238. // "": "JANICE SUTTON",
  7239. // "": 2255,
  7240. // "": "08/02/2016 09:49:18",
  7241. // "": 1,
  7242. // "": "Saved Order"
  7243. // "": "1470384050483",
  7244. // "model_count": "6 / 28"
  7245. item[@"sales_rep"]= nssales_rep;
  7246. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7247. item[@"so#"]= nssoid;
  7248. item[@"create_by"]= nscreate_by;
  7249. item[@"customer_name"]= nscompany_name;
  7250. item[@"customer_contact"] = customer_contact;
  7251. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7252. item[@"purchase_time"]= nscreate_time;
  7253. int statusCode = status;
  7254. if (statusCode == 2) {
  7255. statusCode = erpStatus;
  7256. } else if (statusCode == 3) {
  7257. statusCode = 15;
  7258. }
  7259. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7260. item[@"order_status"]= nsstatus;
  7261. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7262. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7263. // item[@"model_count"]
  7264. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7265. count++;
  7266. }
  7267. ret[@"count"]= [NSNumber numberWithInt:count];
  7268. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7269. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7270. ret[@"result"]= [NSNumber numberWithInt:2];
  7271. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7272. ret[@"time_zone"] = @"PST";
  7273. sqlite3_finalize(statement);
  7274. }
  7275. 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];
  7276. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7277. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7278. [iSalesDB close_db:db];
  7279. return [RAConvertor dict2data:ret];
  7280. }
  7281. #pragma mark update gnotes
  7282. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7283. {
  7284. DebugLog(@"params: %@",params);
  7285. // comments = Meyoyoyoyoyoyoy;
  7286. // orderCode = MOB1608110001;
  7287. // password = 123456;
  7288. // user = EvanK;
  7289. 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]];
  7290. int ret = [iSalesDB execSql:sql];
  7291. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7292. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7293. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7294. // [dic setValue:@"160409" forKey:@"min_ver"];
  7295. return [RAConvertor dict2data:dic];
  7296. }
  7297. #pragma mark move to wishlist
  7298. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7299. {
  7300. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7301. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7302. _id = [NSString stringWithFormat:@"(%@)",_id];
  7303. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7304. sqlite3 *db = [iSalesDB get_db];
  7305. __block NSString *product_id = @"";
  7306. __block NSString *item_count_str = @"";
  7307. // __block NSString *item_id = nil;
  7308. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7309. // product_id = [self textAtColumn:0 statement:stmt];
  7310. int item_count = sqlite3_column_int(stmt, 1);
  7311. // item_id = [self textAtColumn:2 statement:stmt];
  7312. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7313. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7314. if (p_id.length) {
  7315. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7316. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7317. }
  7318. }];
  7319. [iSalesDB close_db:db];
  7320. // 去除第一个,
  7321. if (product_id.length > 1) {
  7322. product_id = [product_id substringFromIndex:1];
  7323. }
  7324. if (item_count_str.length > 1) {
  7325. item_count_str = [item_count_str substringFromIndex:1];
  7326. }
  7327. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7328. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7329. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7330. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7331. sqlite3 *db1 = [iSalesDB get_db];
  7332. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7333. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7334. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7335. // 删除
  7336. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7337. int ret = [iSalesDB execSql:deleteSql db:db1];
  7338. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7339. [iSalesDB close_db:db1];
  7340. return [RAConvertor dict2data:dic];
  7341. }
  7342. #pragma mark cart delete
  7343. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7344. {
  7345. // cartItemId = 548;
  7346. // orderCode = MOB1608110001;
  7347. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7348. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7349. _id = [NSString stringWithFormat:@"(%@)",_id];
  7350. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7351. int ret = [iSalesDB execSql:sql];
  7352. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7353. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7354. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7355. // [dic setValue:@"160409" forKey:@"min_ver"];
  7356. return [RAConvertor dict2data:dic];
  7357. }
  7358. #pragma mark set price
  7359. +(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7360. {
  7361. DebugLog(@"cart set price params: %@",params);
  7362. // "cartitem_id" = 1;
  7363. // discount = "0.000000";
  7364. // "item_note" = "";
  7365. // price = "269.000000";
  7366. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7367. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7368. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7369. // NSString *price = [self valueInParams:params key:@"price"];
  7370. // bool badd_price_changed=false;
  7371. // sqlite3* db=[iSalesDB get_db];
  7372. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7373. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7374. // NSRange range;
  7375. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7376. //
  7377. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7378. // badd_price_changed=true;
  7379. // [iSalesDB close_db:db];
  7380. //
  7381. // if(badd_price_changed)
  7382. // {
  7383. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7384. // }
  7385. //
  7386. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7387. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7388. int ret = [iSalesDB execSql:sql];
  7389. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7390. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7391. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7392. // [dic setValue:@"160409" forKey:@"min_ver"];
  7393. // return [RAConvertor dict2data:dic];
  7394. return dic;
  7395. }
  7396. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7397. {
  7398. DebugLog(@"cart set price params: %@",params);
  7399. // "cartitem_id" = 1;
  7400. // discount = "0.000000";
  7401. // "item_note" = "";
  7402. // price = "269.000000";
  7403. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7404. NSString *notes = [self valueInParams:params key:@"item_note"];
  7405. NSString *discount = [self valueInParams:params key:@"discount"];
  7406. NSString *price = [self valueInParams:params key:@"price"];
  7407. // bool badd_price_changed=false;
  7408. // sqlite3* db=[iSalesDB get_db];
  7409. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7410. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7411. // NSRange range;
  7412. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7413. //
  7414. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7415. // badd_price_changed=true;
  7416. // [iSalesDB close_db:db];
  7417. //
  7418. // if(badd_price_changed)
  7419. // {
  7420. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7421. // }
  7422. //
  7423. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7424. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7425. int ret = [iSalesDB execSql:sql];
  7426. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7427. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7428. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7429. // [dic setValue:@"160409" forKey:@"min_ver"];
  7430. return [RAConvertor dict2data:dic];
  7431. }
  7432. #pragma mark set line notes
  7433. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7434. {
  7435. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7436. NSString *notes = [self valueInParams:params key:@"notes"];
  7437. notes = [self translateSingleQuote:notes];
  7438. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7439. int ret = [iSalesDB execSql:sql];
  7440. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7441. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7442. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7443. // [dic setValue:@"160409" forKey:@"min_ver"];
  7444. return [RAConvertor dict2data:dic];
  7445. }
  7446. #pragma mark set qty
  7447. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7448. {
  7449. assert(params[@"can_create_backorder"]!=nil);
  7450. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7451. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7452. int item_count = [params[@"inputInt"] intValue];
  7453. // 购买检查数量大于库存
  7454. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7455. if (!params[@"can_create_backorder"]) {
  7456. 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];
  7457. __block BOOL out_of_stock = NO;
  7458. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7459. int availability = sqlite3_column_int(stmt, 0);
  7460. if (availability < item_count) {
  7461. out_of_stock = YES;
  7462. }
  7463. }];
  7464. if (out_of_stock) { // 缺货
  7465. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7466. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7467. return [RAConvertor dict2data:dic];
  7468. }
  7469. }
  7470. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7471. int ret = [iSalesDB execSql:sql];
  7472. __block int item_id = 0;
  7473. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7474. item_id = sqlite3_column_int(stmt, 0);
  7475. }];
  7476. sqlite3 *db = [iSalesDB get_db];
  7477. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7478. [iSalesDB close_db:db];
  7479. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7480. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7481. // [dic setValue:@"160409" forKey:@"min_ver"];
  7482. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7483. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7484. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7485. return [RAConvertor dict2data:dic];
  7486. }
  7487. #pragma mark place order
  7488. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7489. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7490. if (aname) {
  7491. [dic setValue:aname forKey:@"aname"];
  7492. }
  7493. if (control) {
  7494. [dic setValue:control forKey:@"control"];
  7495. }
  7496. if (keyboard) {
  7497. [dic setValue:keyboard forKey:@"keyboard"];
  7498. }
  7499. if (name) {
  7500. [dic setValue:name forKey:@"name"];
  7501. }
  7502. if (value) {
  7503. [dic setValue:value forKey:@"value"];
  7504. }
  7505. return dic;
  7506. }
  7507. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7508. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7509. orderCode = [self translateSingleQuote:orderCode];
  7510. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7511. 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];
  7512. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7513. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7514. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7515. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7516. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7517. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7518. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7519. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7520. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7521. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7522. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7523. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7524. NSString *email = [self textAtColumn:11 statement:stmt];
  7525. NSString *phone = [self textAtColumn:12 statement:stmt];
  7526. NSString *fax = [self textAtColumn:13 statement:stmt];
  7527. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7528. NSString *city = [self textAtColumn:15 statement:stmt];
  7529. NSString *state = [self textAtColumn:16 statement:stmt];
  7530. NSString *country = [self textAtColumn:17 statement:stmt];
  7531. NSString *name = [self textAtColumn:18 statement:stmt];
  7532. // contact id
  7533. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7534. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7535. [contact_id_dic setValue:@"text" forKey:@"control"];
  7536. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7537. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7538. [contact_id_dic setValue:@"true" forKey:@"required"];
  7539. [contact_id_dic setValue:contact_id forKey:@"value"];
  7540. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7541. // business card
  7542. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7543. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7544. [business_card_dic setValue:@"img" forKey:@"control"];
  7545. [business_card_dic setValue:@"1" forKey:@"disable"];
  7546. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7547. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7548. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7549. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7550. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7551. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7552. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7553. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7554. // fax
  7555. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7556. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7557. // zipcode
  7558. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7559. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7560. // city
  7561. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7562. [customer_dic setValue:city_dic forKey:@"item_12"];
  7563. // state
  7564. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7565. [customer_dic setValue:state_dic forKey:@"item_13"];
  7566. // country
  7567. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7568. [customer_dic setValue:country_dic forKey:@"item_14"];
  7569. // company name
  7570. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7571. [customer_dic setValue:company_dic forKey:@"item_2"];
  7572. // addr_1
  7573. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7574. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7575. // addr_2
  7576. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7577. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7578. // addr_3
  7579. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7580. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7581. // addr_4
  7582. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7583. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7584. // Contact
  7585. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7586. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7587. // email
  7588. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7589. [customer_dic setValue:email_dic forKey:@"item_8"];
  7590. // phone
  7591. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7592. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7593. // title
  7594. [customer_dic setValue:@"Customer" forKey:@"title"];
  7595. // count
  7596. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7597. }];
  7598. // setting
  7599. NSDictionary *setting = params[@"setting"];
  7600. NSNumber *hide = setting[@"CustomerHide"];
  7601. [customer_dic setValue:hide forKey:@"hide"];
  7602. return customer_dic;
  7603. }
  7604. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7605. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7606. // setting
  7607. NSDictionary *setting = params[@"setting"];
  7608. NSNumber *hide = setting[@"ShipToHide"];
  7609. [dic setValue:hide forKey:@"hide"];
  7610. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7611. orderCode = [self translateSingleQuote:orderCode];
  7612. 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];
  7613. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7614. NSString *cid = [self textAtColumn:0 statement:stmt];
  7615. NSString *name = [self textAtColumn:1 statement:stmt];
  7616. NSString *ext = [self textAtColumn:2 statement:stmt];
  7617. NSString *contact = [self textAtColumn:3 statement:stmt];
  7618. NSString *email = [self textAtColumn:4 statement:stmt];
  7619. NSString *fax = [self textAtColumn:5 statement:stmt];
  7620. NSString *phone = [self textAtColumn:6 statement:stmt];
  7621. // count
  7622. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7623. // title
  7624. [dic setValue:@"Ship To" forKey:@"title"];
  7625. // choose
  7626. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7627. [choose_dic setValue:@"choose" forKey:@"aname"];
  7628. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7629. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7630. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7631. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7632. @"receive_contact" : @"customer_contact",
  7633. @"receive_email" : @"customer_email",
  7634. @"receive_ext" : @"customer_contact_ext",
  7635. @"receive_fax" : @"customer_fax",
  7636. @"receive_name" : @"customer_name",
  7637. @"receive_phone" : @"customer_phone"},
  7638. @"refresh" : [NSNumber numberWithInteger:1],
  7639. @"type" : @"pull"};
  7640. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7641. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7642. @"name" : @"Add new address",
  7643. @"refresh" : [NSNumber numberWithInteger:1],
  7644. @"value" : @"new_addr"
  7645. };
  7646. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7647. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7648. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7649. @"receive_contact" : @"customer_contact",
  7650. @"receive_email" : @"customer_email",
  7651. @"receive_ext" : @"customer_contact_ext",
  7652. @"receive_fax" : @"customer_fax",
  7653. @"receive_name" : @"customer_name",
  7654. @"receive_phone" : @"customer_phone"},
  7655. @"name" : @"select_ship_to",
  7656. @"refresh" : [NSNumber numberWithInteger:1],
  7657. @"value" : @"Sales_Order_Ship_To"};
  7658. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7659. [dic setValue:choose_dic forKey:@"item_0"];
  7660. // contact id
  7661. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_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:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7667. [dic setValue:company_name_dic forKey:@"item_2"];
  7668. // address
  7669. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7670. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7671. if ([required integerValue]) {
  7672. [ext_dic setValue:@"true" forKey:@"required"];
  7673. }
  7674. [dic setValue:ext_dic forKey:@"item_3"];
  7675. // contact
  7676. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7677. [dic setValue:contact_dic forKey:@"item_4"];
  7678. // phone
  7679. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7680. [dic setValue:phone_dic forKey:@"item_5"];
  7681. // fax
  7682. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7683. [dic setValue:fax_dic forKey:@"item_6"];
  7684. // email
  7685. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7686. [dic setValue:email_dic forKey:@"item_7"];
  7687. }];
  7688. return dic;
  7689. }
  7690. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7691. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7692. // setting
  7693. NSDictionary *setting = params[@"setting"];
  7694. NSNumber *hide = setting[@"ShipFromHide"];
  7695. [dic setValue:hide forKey:@"hide"];
  7696. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7697. orderCode = [self translateSingleQuote:orderCode];
  7698. 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];
  7699. 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';";
  7700. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7701. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7702. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7703. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7704. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7705. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7706. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7707. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7708. if (!cid.length) {
  7709. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7710. cid = [self textAtColumn:0 statement:statment];
  7711. name = [self textAtColumn:1 statement:statment];
  7712. ext = [self textAtColumn:2 statement:statment];
  7713. contact = [self textAtColumn:3 statement:statment];
  7714. email = [self textAtColumn:4 statement:statment];
  7715. fax = [self textAtColumn:5 statement:statment];
  7716. phone = [self textAtColumn:6 statement:statment];
  7717. }];
  7718. }
  7719. // count
  7720. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7721. // title
  7722. [dic setValue:@"Ship From" forKey:@"title"];
  7723. // hide
  7724. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7725. // choose
  7726. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7727. [choose_dic setValue:@"choose" forKey:@"aname"];
  7728. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7729. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7730. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7731. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7732. @"sender_contact" : @"customer_contact",
  7733. @"sender_email" : @"customer_email",
  7734. @"sender_ext" : @"customer_contact_ext",
  7735. @"sender_fax" : @"customer_fax",
  7736. @"sender_name" : @"customer_name",
  7737. @"sender_phone" : @"customer_phone"},
  7738. @"name" : @"select_cid",
  7739. @"refresh" : [NSNumber numberWithInteger:0],
  7740. @"value" : @"Sales_Order_Ship_From"};
  7741. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7742. [dic setValue:choose_dic forKey:@"item_0"];
  7743. // contact id
  7744. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7745. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7746. [contact_id_dic setValue:@"true" forKey:@"required"];
  7747. [dic setValue:contact_id_dic forKey:@"item_1"];
  7748. // company name
  7749. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7750. [dic setValue:company_name_dic forKey:@"item_2"];
  7751. // address
  7752. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7753. [dic setValue:ext_dic forKey:@"item_3"];
  7754. // contact
  7755. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7756. [dic setValue:contact_dic forKey:@"item_4"];
  7757. // phone
  7758. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7759. [dic setValue:phone_dic forKey:@"item_5"];
  7760. // fax
  7761. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7762. [dic setValue:fax_dic forKey:@"item_6"];
  7763. // email
  7764. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7765. [dic setValue:email_dic forKey:@"item_7"];
  7766. }];
  7767. return dic;
  7768. }
  7769. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7770. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7771. // setting
  7772. NSDictionary *setting = params[@"setting"];
  7773. NSNumber *hide = setting[@"FreightBillToHide"];
  7774. [dic setValue:hide forKey:@"hide"];
  7775. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7776. orderCode = [self translateSingleQuote:orderCode];
  7777. 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];
  7778. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7779. NSString *cid = [self textAtColumn:0 statement:stmt];
  7780. NSString *name = [self textAtColumn:1 statement:stmt];
  7781. NSString *ext = [self textAtColumn:2 statement:stmt];
  7782. NSString *contact = [self textAtColumn:3 statement:stmt];
  7783. NSString *email = [self textAtColumn:4 statement:stmt];
  7784. NSString *fax = [self textAtColumn:5 statement:stmt];
  7785. NSString *phone = [self textAtColumn:6 statement:stmt];
  7786. // count
  7787. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7788. // title
  7789. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7790. // hide
  7791. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7792. // choose
  7793. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7794. [choose_dic setValue:@"choose" forKey:@"aname"];
  7795. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7796. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7797. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7798. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7799. @"shipping_billto_contact" : @"receive_contact",
  7800. @"shipping_billto_email" : @"receive_email",
  7801. @"shipping_billto_ext" : @"receive_ext",
  7802. @"shipping_billto_fax" : @"receive_fax",
  7803. @"shipping_billto_name" : @"receive_name",
  7804. @"shipping_billto_phone" : @"receive_phone"},
  7805. @"type" : @"pull"};
  7806. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7807. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7808. @"type" : @"pull",
  7809. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7810. @"shipping_billto_contact" : @"customer_contact",
  7811. @"shipping_billto_email" : @"customer_email",
  7812. @"shipping_billto_ext" : @"customer_contact_ext",
  7813. @"shipping_billto_fax" : @"customer_fax",
  7814. @"shipping_billto_name" : @"customer_name",
  7815. @"shipping_billto_phone" : @"customer_phone"}
  7816. };
  7817. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7818. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7819. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7820. @"shipping_billto_contact" : @"sender_contact",
  7821. @"shipping_billto_email" : @"sender_email",
  7822. @"shipping_billto_ext" : @"sender_ext",
  7823. @"shipping_billto_fax" : @"sender_fax",
  7824. @"shipping_billto_name" : @"sender_name",
  7825. @"shipping_billto_phone" : @"sender_phone"},
  7826. @"type" : @"pull"
  7827. };
  7828. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7829. NSDictionary *select_freight_bill_to_dic = @{
  7830. @"aname" : @"Select freight bill to",
  7831. @"name" : @"select_cid",
  7832. @"refresh" : [NSNumber numberWithInteger:0],
  7833. @"value" : @"Sales_Order_Freight_Bill_To",
  7834. @"key_map" : @{
  7835. @"shipping_billto_cid" : @"customer_cid",
  7836. @"shipping_billto_contact" : @"customer_contact",
  7837. @"shipping_billto_email" : @"customer_email",
  7838. @"shipping_billto_ext" : @"customer_contact_ext",
  7839. @"shipping_billto_fax" : @"customer_fax",
  7840. @"shipping_billto_name" : @"customer_name",
  7841. @"shipping_billto_phone" : @"customer_phone"
  7842. }
  7843. };
  7844. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7845. [dic setValue:choose_dic forKey:@"item_0"];
  7846. // contact id
  7847. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7848. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7849. [contact_id_dic setValue:@"true" forKey:@"required"];
  7850. [dic setValue:contact_id_dic forKey:@"item_1"];
  7851. // company name
  7852. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7853. [dic setValue:company_name_dic forKey:@"item_2"];
  7854. // address
  7855. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7856. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7857. if ([ext_required integerValue]) {
  7858. [ext_dic setValue:@"true" forKey:@"required"];
  7859. }
  7860. [dic setValue:ext_dic forKey:@"item_3"];
  7861. // contact
  7862. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7863. [dic setValue:contact_dic forKey:@"item_4"];
  7864. // phone
  7865. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7866. [dic setValue:phone_dic forKey:@"item_5"];
  7867. // fax
  7868. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7869. [dic setValue:fax_dic forKey:@"item_6"];
  7870. // email
  7871. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7872. [dic setValue:email_dic forKey:@"item_7"];
  7873. }];
  7874. return dic;
  7875. }
  7876. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7877. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7878. // setting
  7879. NSDictionary *setting = params[@"setting"];
  7880. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7881. [dic setValue:hide forKey:@"hide"];
  7882. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7883. orderCode = [self translateSingleQuote:orderCode];
  7884. 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];
  7885. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7886. NSString *cid = [self textAtColumn:0 statement:stmt];
  7887. NSString *name = [self textAtColumn:1 statement:stmt];
  7888. NSString *ext = [self textAtColumn:2 statement:stmt];
  7889. NSString *contact = [self textAtColumn:3 statement:stmt];
  7890. NSString *email = [self textAtColumn:4 statement:stmt];
  7891. NSString *fax = [self textAtColumn:5 statement:stmt];
  7892. NSString *phone = [self textAtColumn:6 statement:stmt];
  7893. // count
  7894. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7895. // title
  7896. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7897. // hide
  7898. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7899. // choose
  7900. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7901. [choose_dic setValue:@"choose" forKey:@"aname"];
  7902. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7903. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7904. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7905. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7906. @"billing_contact" : @"receive_contact",
  7907. @"billing_email" : @"receive_email",
  7908. @"billing_ext" : @"receive_ext",
  7909. @"billing_fax" : @"receive_fax",
  7910. @"billing_name" : @"receive_name",
  7911. @"billing_phone" : @"receive_phone"},
  7912. @"type" : @"pull"};
  7913. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7914. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7915. @"type" : @"pull",
  7916. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7917. @"billing_contact" : @"customer_contact",
  7918. @"billing_email" : @"customer_email",
  7919. @"billing_ext" : @"customer_contact_ext",
  7920. @"billing_fax" : @"customer_fax",
  7921. @"billing_name" : @"customer_name",
  7922. @"billing_phone" : @"customer_phone"}
  7923. };
  7924. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7925. NSDictionary *select_bill_to_dic = @{
  7926. @"aname" : @"Select bill to",
  7927. @"name" : @"select_cid",
  7928. @"refresh" : [NSNumber numberWithInteger:0],
  7929. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7930. @"key_map" : @{
  7931. @"billing_cid" : @"customer_cid",
  7932. @"billing_contact" : @"customer_contact",
  7933. @"billing_email" : @"customer_email",
  7934. @"billing_ext" : @"customer_contact_ext",
  7935. @"billing_fax" : @"customer_fax",
  7936. @"billing_name" : @"customer_name",
  7937. @"billing_phone" : @"customer_phone"
  7938. }
  7939. };
  7940. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7941. [dic setValue:choose_dic forKey:@"item_0"];
  7942. // contact id
  7943. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7944. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7945. [contact_id_dic setValue:@"true" forKey:@"required"];
  7946. [dic setValue:contact_id_dic forKey:@"item_1"];
  7947. // company name
  7948. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7949. [dic setValue:company_name_dic forKey:@"item_2"];
  7950. // address
  7951. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7952. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7953. if ([ext_required integerValue]) {
  7954. [ext_dic setValue:@"true" forKey:@"required"];
  7955. }
  7956. [dic setValue:ext_dic forKey:@"item_3"];
  7957. // contact
  7958. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7959. [dic setValue:contact_dic forKey:@"item_4"];
  7960. // phone
  7961. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7962. [dic setValue:phone_dic forKey:@"item_5"];
  7963. // fax
  7964. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7965. [dic setValue:fax_dic forKey:@"item_6"];
  7966. // email
  7967. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7968. [dic setValue:email_dic forKey:@"item_7"];
  7969. }];
  7970. return dic;
  7971. }
  7972. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7973. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7974. // setting
  7975. NSDictionary *setting = params[@"setting"];
  7976. NSNumber *hide = setting[@"ReturnToHide"];
  7977. [dic setValue:hide forKey:@"hide"];
  7978. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7979. orderCode = [self translateSingleQuote:orderCode];
  7980. 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];
  7981. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7982. NSString *cid = [self textAtColumn:0 statement:stmt];
  7983. NSString *name = [self textAtColumn:1 statement:stmt];
  7984. NSString *ext = [self textAtColumn:2 statement:stmt];
  7985. NSString *contact = [self textAtColumn:3 statement:stmt];
  7986. NSString *email = [self textAtColumn:4 statement:stmt];
  7987. NSString *fax = [self textAtColumn:5 statement:stmt];
  7988. NSString *phone = [self textAtColumn:6 statement:stmt];
  7989. // count
  7990. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7991. // title
  7992. [dic setValue:@"Return To" forKey:@"title"];
  7993. // hide
  7994. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7995. // choose
  7996. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7997. [choose_dic setValue:@"choose" forKey:@"aname"];
  7998. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7999. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8000. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8001. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8002. @"returnto_contact" : @"sender_contact",
  8003. @"returnto_email" : @"sender_email",
  8004. @"returnto_ext" : @"sender_ext",
  8005. @"returnto_fax" : @"sender_fax",
  8006. @"returnto_name" : @"sender_name",
  8007. @"returnto_phone" : @"sender_phone"},
  8008. @"type" : @"pull"};
  8009. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8010. NSDictionary *select_return_to_dic = @{
  8011. @"aname" : @"Select return to",
  8012. @"name" : @"select_cid",
  8013. @"refresh" : [NSNumber numberWithInteger:0],
  8014. @"value" : @"Contact_Return_To",
  8015. @"key_map" : @{
  8016. @"returnto_cid" : @"customer_cid",
  8017. @"returnto_contact" : @"customer_contact",
  8018. @"returnto_email" : @"customer_email",
  8019. @"returnto_ext" : @"customer_contact_ext",
  8020. @"returnto_fax" : @"customer_fax",
  8021. @"returnto_name" : @"customer_name",
  8022. @"returnto_phone" : @"customer_phone"
  8023. }
  8024. };
  8025. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8026. [dic setValue:choose_dic forKey:@"item_0"];
  8027. // contact id
  8028. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8029. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8030. [contact_id_dic setValue:@"true" forKey:@"required"];
  8031. [dic setValue:contact_id_dic forKey:@"item_1"];
  8032. // company name
  8033. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8034. [dic setValue:company_name_dic forKey:@"item_2"];
  8035. // address
  8036. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8037. [dic setValue:ext_dic forKey:@"item_3"];
  8038. // contact
  8039. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8040. [dic setValue:contact_dic forKey:@"item_4"];
  8041. // phone
  8042. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8043. [dic setValue:phone_dic forKey:@"item_5"];
  8044. // fax
  8045. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8046. [dic setValue:fax_dic forKey:@"item_6"];
  8047. // email
  8048. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8049. [dic setValue:email_dic forKey:@"item_7"];
  8050. }];
  8051. return dic;
  8052. }
  8053. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8054. {
  8055. assert(params[@"user"]!=nil);
  8056. assert(params[@"contact_id"]!=nil);
  8057. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8058. orderCode = [self translateSingleQuote:orderCode];
  8059. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8060. __block double TotalCuft = 0;
  8061. __block double TotalWeight = 0;
  8062. __block int TotalCarton = 0;
  8063. __block double payments = 0;
  8064. // setting
  8065. NSDictionary *setting = params[@"setting"];
  8066. NSNumber *hide = setting[@"ModelInformationHide"];
  8067. [dic setValue:hide forKey:@"hide"];
  8068. 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];
  8069. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8070. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8071. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8072. // item id
  8073. int item_id = sqlite3_column_int(stmt, 0);
  8074. // count
  8075. int item_count = sqlite3_column_int(stmt, 1);
  8076. // stockUom
  8077. int stockUom = sqlite3_column_int(stmt, 2);
  8078. // unit price
  8079. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8080. NSString* Price=nil;
  8081. if([str_price isEqualToString:@""])
  8082. {
  8083. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8084. if(price==nil)
  8085. Price=@"No Price.";
  8086. else
  8087. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8088. }
  8089. else
  8090. {
  8091. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8092. }
  8093. // discount
  8094. double discount = sqlite3_column_double(stmt, 4);
  8095. // name
  8096. NSString *name = [self textAtColumn:5 statement:stmt];
  8097. // description
  8098. NSString *description = [self textAtColumn:6 statement:stmt];
  8099. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8100. // line note
  8101. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8102. int avaulability = sqlite3_column_int(stmt, 8);
  8103. // img
  8104. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8105. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8106. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8107. if(combine != nil)
  8108. {
  8109. // int citem=0;
  8110. int bcount=[[combine valueForKey:@"count"] intValue];
  8111. for(int bc=0;bc<bcount;bc++)
  8112. {
  8113. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8114. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8115. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8116. subTotal += uprice * modulus * item_count;
  8117. }
  8118. }
  8119. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8120. [model_dic setValue:@"model" forKey:@"control"];
  8121. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8122. [model_dic setValue:description forKey:@"description"];
  8123. [model_dic setValue:line_note forKey:@"note"];
  8124. [model_dic setValue:img forKey:@"img_url"];
  8125. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8126. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8127. [model_dic setValue:Price forKey:@"unit_price"];
  8128. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8129. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8130. if (combine) {
  8131. [model_dic setValue:combine forKey:@"combine"];
  8132. }
  8133. // well,what under the row is the info for total
  8134. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8135. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8136. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8137. int carton=[bsubtotaljson[@"carton"] intValue];
  8138. TotalCuft += cuft;
  8139. TotalWeight += weight;
  8140. TotalCarton += carton;
  8141. payments += subTotal;
  8142. //---------------------------
  8143. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8144. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8145. }];
  8146. [dic setValue:@"Model Information" forKey:@"title"];
  8147. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8148. return dic;
  8149. }
  8150. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8151. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8152. [dic setValue:@"Remarks Content" forKey:@"title"];
  8153. // setting
  8154. NSDictionary *setting = params[@"setting"];
  8155. NSNumber *hide = setting[@"RemarksContentHide"];
  8156. [dic setValue:hide forKey:@"hide"];
  8157. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8158. orderCode = [self translateSingleQuote:orderCode];
  8159. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8160. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8161. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8162. int mustCall = sqlite3_column_int(stmt, 1);
  8163. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8164. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8165. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8166. NSDictionary *po_dic = @{
  8167. @"aname" : @"PO#",
  8168. @"control" : @"edit",
  8169. @"keyboard" : @"text",
  8170. @"name" : @"poNumber",
  8171. @"value" : poNumber
  8172. };
  8173. #ifdef BUILD_CONTRAST
  8174. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8175. NSDictionary *schedule_dict = @{
  8176. @"aname" : @"Schdule Date",
  8177. @"control" : @"datepicker",
  8178. @"type": @"date",
  8179. @"required": @"true",
  8180. @"name" : @"schedule_date_str",
  8181. @"value" : nsdate
  8182. };
  8183. #endif
  8184. NSDictionary *must_call_dic = @{
  8185. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8186. @"control" : @"switch",
  8187. @"name" : @"must_call",
  8188. @"value" : mustCall ? @"true" : @"false"
  8189. };
  8190. NSDictionary *general_notes_dic = @{
  8191. @"aname" : @"General notes",
  8192. @"control" : @"text_view",
  8193. @"keyboard" : @"text",
  8194. @"name" : @"comments",
  8195. @"value" : generalNotes
  8196. };
  8197. // NSDictionary *internal_notes_dic = @{
  8198. // @"aname" : @"Internal notes",
  8199. // @"control" : @"text_view",
  8200. // @"keyboard" : @"text",
  8201. // @"name" : @"internal_notes",
  8202. // @"value" : internalNotes
  8203. // };
  8204. #ifdef BUILD_CONTRAST
  8205. [dic setValue:po_dic forKey:@"item_0"];
  8206. [dic setValue:schedule_dict forKey:@"item_1"];
  8207. [dic setValue:must_call_dic forKey:@"item_2"];
  8208. [dic setValue:general_notes_dic forKey:@"item_3"];
  8209. #else
  8210. [dic setValue:po_dic forKey:@"item_0"];
  8211. [dic setValue:must_call_dic forKey:@"item_1"];
  8212. [dic setValue:general_notes_dic forKey:@"item_2"];
  8213. #endif
  8214. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8215. }];
  8216. #ifdef BUILD_CONTRAST
  8217. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8218. #else
  8219. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8220. #endif
  8221. return dic;
  8222. }
  8223. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8224. // params
  8225. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8226. orderCode = [self translateSingleQuote:orderCode];
  8227. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8228. // setting
  8229. NSDictionary *setting = params[@"setting"];
  8230. NSNumber *hide = setting[@"OrderTotalHide"];
  8231. [dic setValue:hide forKey:@"hide"];
  8232. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8233. __block double lift_gate_value = 0;
  8234. __block double handling_fee = 0;
  8235. __block double shipping = 0;
  8236. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8237. int lift_gate = sqlite3_column_int(stmt, 0);
  8238. lift_gate_value = sqlite3_column_double(stmt, 1);
  8239. shipping = sqlite3_column_double(stmt, 2);
  8240. handling_fee = sqlite3_column_double(stmt, 3);
  8241. if (!lift_gate) {
  8242. lift_gate_value = 0;
  8243. }
  8244. }];
  8245. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8246. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8247. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8248. double payments = [[total valueForKey:@"payments"] doubleValue];
  8249. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8250. double totalPrice = payments;
  8251. [dic setValue:@"Order Total" forKey:@"title"];
  8252. NSDictionary *payments_dic = @{
  8253. @"align" : @"right",
  8254. @"aname" : @"Payments/Credits",
  8255. @"control" : @"text",
  8256. @"name" : @"paymentsAndCredits",
  8257. @"type" : @"price",
  8258. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8259. };
  8260. [dic setValue:payments_dic forKey:@"item_0"];
  8261. // version 1.71 remove
  8262. // NSDictionary *handling_fee_dic = @{
  8263. // @"align" : @"right",
  8264. // @"aname" : @"Handling Fee",
  8265. // @"control" : @"text",
  8266. // @"name" : @"handling_fee_value",
  8267. // @"required" : @"true",
  8268. // @"type" : @"price",
  8269. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8270. // };
  8271. NSDictionary *shipping_dic = @{
  8272. @"align" : @"right",
  8273. @"aname" : @"Shipping*",
  8274. @"control" : @"text",
  8275. @"name" : @"shipping",
  8276. @"required" : @"true",
  8277. @"type" : @"price",
  8278. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8279. };
  8280. NSDictionary *lift_gate_dic = @{
  8281. @"align" : @"right",
  8282. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8283. @"control" : @"text",
  8284. @"name" : @"lift_gate_value",
  8285. @"required" : @"true",
  8286. @"type" : @"price",
  8287. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8288. };
  8289. int item_count = 1;
  8290. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8291. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8292. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8293. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8294. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8295. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8296. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8297. } else {
  8298. }
  8299. }
  8300. // version 1.71 remove
  8301. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8302. NSDictionary *total_price_dic = @{
  8303. @"align" : @"right",
  8304. @"aname" : @"Total",
  8305. @"control" : @"text",
  8306. @"name" : @"totalPrice",
  8307. @"type" : @"price",
  8308. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8309. };
  8310. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8311. NSDictionary *total_cuft_dic = @{
  8312. @"align" : @"right",
  8313. @"aname" : @"Total Cuft",
  8314. @"control" : @"text",
  8315. @"name" : @"",
  8316. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8317. };
  8318. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8319. NSDictionary *total_weight_dic = @{
  8320. @"align" : @"right",
  8321. @"aname" : @"Total Weight",
  8322. @"control" : @"text",
  8323. @"name" : @"",
  8324. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8325. };
  8326. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8327. NSDictionary *total_carton_dic = @{
  8328. @"align" : @"right",
  8329. @"aname" : @"Total Carton",
  8330. @"control" : @"text",
  8331. @"name" : @"",
  8332. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8333. };
  8334. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8335. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8336. return dic;
  8337. }
  8338. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8339. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8340. orderCode = [self translateSingleQuote:orderCode];
  8341. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8342. // setting
  8343. NSDictionary *setting = params[@"setting"];
  8344. NSNumber *hide = setting[@"SignatureHide"];
  8345. [dic setValue:hide forKey:@"hide"];
  8346. [dic setValue:@"Signature" forKey:@"title"];
  8347. __block NSString *pic_path = @"";
  8348. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8349. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8350. pic_path = [self textAtColumn:0 statement:stmt];
  8351. }];
  8352. NSDictionary *pic_dic = @{
  8353. @"aname" : @"Signature",
  8354. @"avalue" :pic_path,
  8355. @"control" : @"signature",
  8356. @"name" : @"sign_picpath",
  8357. @"value" : pic_path
  8358. };
  8359. [dic setValue:pic_dic forKey:@"item_0"];
  8360. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8361. return dic;
  8362. }
  8363. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8364. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8365. // setting
  8366. NSDictionary *setting = params[@"setting"];
  8367. NSNumber *hide = setting[@"ShippingMethodHide"];
  8368. [dic setValue:hide forKey:@"hide"];
  8369. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8370. orderCode = [self translateSingleQuote:orderCode];
  8371. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8372. __block NSString *logist = @"";
  8373. __block NSString *lift_gate = @"";
  8374. __block int lift_gate_integer = 0;
  8375. __block NSString *logistic_note = @"";
  8376. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8377. logist = [self textAtColumn:0 statement:stmt];
  8378. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8379. logistic_note = [self textAtColumn:2 statement:stmt];
  8380. }];
  8381. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8382. [dic setValue:@"Shipping Method" forKey:@"title"];
  8383. // val_0
  8384. int PERSONAL_PICK_UP_check = 0;
  8385. int USE_MY_CARRIER_check = 0;
  8386. NSString *logistic_note_text = @"";
  8387. int will_call_check = 0;
  8388. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8389. will_call_check = 1;
  8390. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8391. PERSONAL_PICK_UP_check = 1;
  8392. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8393. USE_MY_CARRIER_check = 1;
  8394. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8395. logistic_note = [logistic_note_array firstObject];
  8396. if (logistic_note_array.count > 1) {
  8397. logistic_note_text = [logistic_note_array lastObject];
  8398. }
  8399. }
  8400. }
  8401. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8402. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8403. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8404. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8405. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8406. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8407. NSDictionary *val0_subItem_item0 = @{
  8408. @"aname" : @"Option",
  8409. @"cadedate" : @{
  8410. @"count" : [NSNumber numberWithInteger:2],
  8411. @"val_0" : @{
  8412. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8413. @"refresh" : [NSNumber numberWithInteger:0],
  8414. @"value" : @"PERSONAL PICK UP",
  8415. @"value_id" : @"PERSONAL PICK UP"
  8416. },
  8417. @"val_1" : @{
  8418. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8419. @"refresh" : [NSNumber numberWithInteger:0],
  8420. @"sub_item" : @{
  8421. @"count" : [NSNumber numberWithInteger:1],
  8422. @"item_0" : @{
  8423. @"aname" : @"BOL",
  8424. @"control" : @"edit",
  8425. @"keyboard" : @"text",
  8426. @"name" : @"logist_note_text",
  8427. @"refresh" : [NSNumber numberWithInteger:0],
  8428. @"required" : @"false",
  8429. @"value" : logistic_note_text
  8430. }
  8431. },
  8432. @"value" : @"USE MY CARRIER",
  8433. @"value_id" : @"USE MY CARRIER"
  8434. }
  8435. },
  8436. @"control" : @"enum",
  8437. @"name" : @"logistic_note",
  8438. @"required" : @"true",
  8439. @"single_select" : @"true"
  8440. };
  8441. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8442. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8443. // val_1
  8444. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8445. if([lift_gate isEqualToString:@""]) {
  8446. if (lift_gate_integer == 1) {
  8447. lift_gate = @"true";
  8448. } else {
  8449. lift_gate = @"false";
  8450. }
  8451. }
  8452. int common_carrier_check = 0;
  8453. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8454. common_carrier_check = 1;
  8455. [params setValue:lift_gate forKey:@"lift_gate"];
  8456. }
  8457. NSDictionary *val_1 = @{
  8458. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8459. @"sub_item" : @{
  8460. @"count" : [NSNumber numberWithInteger:1],
  8461. @"item_0" : @{
  8462. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8463. @"control" : @"switch",
  8464. @"name" : @"lift_gate",
  8465. @"refresh" : [NSNumber numberWithInteger:1],
  8466. @"required" : @"true",
  8467. @"value" : lift_gate
  8468. }
  8469. },
  8470. @"value" : @"COMMON CARRIER",
  8471. @"value_id" : @"COMMON CARRIER"
  8472. };
  8473. // cadedate
  8474. NSDictionary *cadedate = @{
  8475. @"count" : [NSNumber numberWithInteger:2],
  8476. @"val_0" : val_0,
  8477. @"val_1" : val_1
  8478. };
  8479. // item_0
  8480. NSMutableDictionary *item_0 = @{
  8481. @"aname" : @"Shipping",
  8482. @"cadedate" : cadedate,
  8483. @"control" : @"enum",
  8484. @"name" : @"logist",
  8485. @"refresh" : [NSNumber numberWithInteger:1],
  8486. @"single_select" : @"true",
  8487. }.mutableCopy;
  8488. NSNumber *required = setting[@"ShippingMethodRequire"];
  8489. if ([required integerValue]) {
  8490. [item_0 setValue:@"true" forKey:@"required"];
  8491. }
  8492. [dic setValue:item_0 forKey:@"item_0"];
  8493. if ([logist isEqualToString:@"WILL CALL"]) {
  8494. [dic removeObjectForKey:@"lift_gate"];
  8495. }
  8496. return dic;
  8497. }
  8498. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8499. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8500. orderCode = [self translateSingleQuote:orderCode];
  8501. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8502. __block int submit_as_integer = 0;
  8503. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8504. submit_as_integer = sqlite3_column_int(stmt, 0);
  8505. }];
  8506. int check11 = 0;
  8507. int check10 = 0;
  8508. if (submit_as_integer == 11) {
  8509. check11 = 1;
  8510. }
  8511. if (submit_as_integer == 10) {
  8512. check10 = 1;
  8513. }
  8514. // section_0
  8515. NSMutableDictionary *dic = @{
  8516. @"count" : @(1),
  8517. @"item_0" : @{
  8518. @"aname" : @"Submit Order As",
  8519. @"cadedate" : @{
  8520. @"count" : @(2),
  8521. @"val_0" : @{
  8522. @"check" : [NSNumber numberWithInteger:check11],
  8523. @"value" : @"Sales Order",
  8524. @"value_id" : @(11)
  8525. },
  8526. @"val_1" : @{
  8527. @"check" : [NSNumber numberWithInteger:check10],
  8528. @"value" : @"Quote",
  8529. @"value_id" : @(10)
  8530. }
  8531. },
  8532. @"control" : @"enum",
  8533. @"name" : @"erpOrderStatus",
  8534. @"required" : @"true",
  8535. @"single_select" : @"true"
  8536. },
  8537. @"title" : @"Order Type"
  8538. }.mutableCopy;
  8539. // setting
  8540. NSDictionary *setting = params[@"setting"];
  8541. NSNumber *hide = setting[@"OrderTypeHide"];
  8542. [dic setValue:hide forKey:@"hide"];
  8543. return dic;
  8544. }
  8545. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8546. countryCode = [self translateSingleQuote:countryCode];
  8547. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8548. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8549. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8550. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8551. if (name == NULL) {
  8552. name = "";
  8553. }
  8554. if (code == NULL) {
  8555. code = "";
  8556. }
  8557. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8558. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8559. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8560. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8561. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8562. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8563. }
  8564. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8565. [container setValue:stateDic forKey:key];
  8566. }] mutableCopy];
  8567. [ret removeObjectForKey:@"result"];
  8568. // failure 可以不用了,一样的
  8569. if (ret.allKeys.count == 0) {
  8570. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8571. [stateDic setValue:@"Other" forKey:@"value"];
  8572. [stateDic setValue:@"" forKey:@"value_id"];
  8573. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8574. if (state_code && [@"" isEqualToString:state_code]) {
  8575. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8576. }
  8577. NSString *key = [NSString stringWithFormat:@"val_0"];
  8578. [ret setValue:stateDic forKey:key];
  8579. }
  8580. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8581. return ret;
  8582. }
  8583. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8584. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8585. orderCode = [self translateSingleQuote:orderCode];
  8586. 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];
  8587. __block NSString *payType = @"";
  8588. __block NSString *firstName = @"";
  8589. __block NSString *lastName = @"";
  8590. __block NSString *addr1 = @"";
  8591. __block NSString *addr2 = @"";
  8592. __block NSString *zipcode = @"";
  8593. __block NSString *cardType = @"";
  8594. __block NSString *cardNumber = @"";
  8595. __block NSString *securityCode = @"";
  8596. __block NSString *month = @"";
  8597. __block NSString *year = @"";
  8598. __block NSString *city = @"";
  8599. __block NSString *state = @"";
  8600. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8601. payType = [self textAtColumn:0 statement:stmt];
  8602. firstName = [self textAtColumn:1 statement:stmt];
  8603. lastName = [self textAtColumn:2 statement:stmt];
  8604. addr1 = [self textAtColumn:3 statement:stmt];
  8605. addr2 = [self textAtColumn:4 statement:stmt];
  8606. zipcode = [self textAtColumn:5 statement:stmt];
  8607. cardType = [self textAtColumn:6 statement:stmt];
  8608. cardNumber = [self textAtColumn:7 statement:stmt];
  8609. securityCode = [self textAtColumn:8 statement:stmt];
  8610. month = [self textAtColumn:9 statement:stmt];
  8611. year = [self textAtColumn:10 statement:stmt];
  8612. city = [self textAtColumn:11 statement:stmt];
  8613. state = [self textAtColumn:12 statement:stmt];
  8614. }];
  8615. NSString *required = @"true";
  8616. // setting
  8617. NSDictionary *setting = params[@"setting"];
  8618. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8619. if ([requiredNumber integerValue]) {
  8620. required = @"true";
  8621. } else {
  8622. required = @"false";
  8623. }
  8624. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8625. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8626. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8627. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8628. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8629. NSString *type = [self textAtColumn:1 statement:stmt];
  8630. NSMutableDictionary *val = @{
  8631. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8632. @"value" : type,
  8633. @"value_id" : type_id
  8634. }.mutableCopy;
  8635. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  8636. NSDictionary *sub_item =nil;
  8637. #ifdef BUILD_CONTRAST
  8638. sub_item = @{
  8639. @"count" : @(3),
  8640. @"item_0" : @{
  8641. @"aname" : @"Number",
  8642. @"control" : @"edit",
  8643. @"keyboard" : @"int",
  8644. @"length" : @"16",
  8645. @"name" : @"credit_card_number",
  8646. @"required" : @"true",
  8647. @"value" : cardNumber
  8648. },
  8649. @"item_1" : @{
  8650. @"aname" : @"Expiration Date",
  8651. @"control" : @"monthpicker",
  8652. @"name" : @"credit_card_expiration",
  8653. @"required" : @"true",
  8654. @"type" : @"date",
  8655. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8656. },
  8657. @"item_2" : @{
  8658. @"aname" : @"Security Code",
  8659. @"control" : @"edit",
  8660. @"keyboard" : @"int",
  8661. @"length" : @"3",
  8662. @"name" : @"credit_card_security_code",
  8663. @"required" : @"true",
  8664. @"value" : securityCode
  8665. },
  8666. };
  8667. #else
  8668. sub_item= @{
  8669. @"count" : @(3),
  8670. @"item_0" : @{
  8671. @"aname" : @"choose",
  8672. @"control" : @"multi_action",
  8673. @"count" : @(1),
  8674. @"item_0" : @{
  8675. @"aname" : @"Same as customer",
  8676. @"key_map" : @{
  8677. @"credit_card_address1" : @"customer_address1",
  8678. @"credit_card_address2" : @"customer_address2",
  8679. @"credit_card_city" : @"customer_city",
  8680. @"credit_card_first_name" : @"customer_first_name",
  8681. @"credit_card_last_name" : @"customer_last_name",
  8682. @"credit_card_state" : @"customer_state",
  8683. @"credit_card_zipcode" : @"customer_zipcode"
  8684. },
  8685. @"type" : @"pull"
  8686. }
  8687. },
  8688. @"item_1" : @{
  8689. @"aname" : @"",
  8690. @"color" : @"red",
  8691. @"control" : @"text",
  8692. @"name" : @"",
  8693. @"value" : @"USA Credit cards only"
  8694. },
  8695. @"item_2" : @{
  8696. @"aname" : @"Fill",
  8697. @"cadedate" : @{
  8698. @"count" : @(2),
  8699. @"val_0" : @{
  8700. @"check" : @(1),
  8701. @"sub_item" : @{
  8702. @"count" : @(11),
  8703. @"item_0" : @{
  8704. @"aname" : @"Type",
  8705. @"cadedate" : @{
  8706. @"count" : @(2),
  8707. @"val_0" : @{
  8708. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8709. @"value" : @"VISA",
  8710. @"value_id" : @"VISA"/*@(0)*/
  8711. },
  8712. @"val_1" : @{
  8713. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8714. @"value" : @"MASTER CARD",
  8715. @"value_id" : @"MASTER CARD"/*@(1)*/
  8716. }
  8717. },
  8718. @"control" : @"enum",
  8719. @"name" : @"credit_card_type",
  8720. @"required" : @"true",
  8721. @"single_select" : @"true"
  8722. },
  8723. @"item_1" : @{
  8724. @"aname" : @"Number",
  8725. @"control" : @"edit",
  8726. @"keyboard" : @"int",
  8727. @"length" : @"16",
  8728. @"name" : @"credit_card_number",
  8729. @"required" : @"true",
  8730. @"value" : cardNumber
  8731. },
  8732. @"item_10" : @{
  8733. @"aname" : @"State",
  8734. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8735. @"control" : @"enum",
  8736. @"enum" : @"true",
  8737. @"name" : @"credit_card_state",
  8738. @"required" : @"true",
  8739. @"single_select" : @"true"
  8740. },
  8741. @"item_2" : @{
  8742. @"aname" : @"Expiration Date",
  8743. @"control" : @"monthpicker",
  8744. @"name" : @"credit_card_expiration",
  8745. @"required" : @"true",
  8746. @"type" : @"date",
  8747. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8748. },
  8749. @"item_3" : @{
  8750. @"aname" : @"Security Code",
  8751. @"control" : @"edit",
  8752. @"keyboard" : @"int",
  8753. @"length" : @"3",
  8754. @"name" : @"credit_card_security_code",
  8755. @"required" : @"true",
  8756. @"value" : securityCode
  8757. },
  8758. @"item_4" : @{
  8759. @"aname" : @"First Name",
  8760. @"control" : @"edit",
  8761. @"keyboard" : @"text",
  8762. @"name" : @"credit_card_first_name",
  8763. @"required" : @"true",
  8764. @"value" : firstName
  8765. },
  8766. @"item_5" : @{
  8767. @"aname" : @"Last Name",
  8768. @"control" : @"edit",
  8769. @"keyboard" : @"text",
  8770. @"name" : @"credit_card_last_name",
  8771. @"required" : @"true",
  8772. @"value" : lastName
  8773. },
  8774. @"item_6" : @{
  8775. @"aname" : @"Address 1",
  8776. @"control" : @"edit",
  8777. @"keyboard" : @"text",
  8778. @"name" : @"credit_card_address1",
  8779. @"required" : @"true",
  8780. @"value" : addr1
  8781. },
  8782. @"item_7" : @{
  8783. @"aname" : @"Address 2",
  8784. @"control" : @"edit",
  8785. @"keyboard" : @"text",
  8786. @"name" : @"credit_card_address2",
  8787. @"value" : addr2
  8788. },
  8789. @"item_8" : @{
  8790. @"aname" : @"zip code",
  8791. @"control" : @"edit",
  8792. @"keyboard" : @"text",
  8793. @"name" : @"credit_card_zipcode",
  8794. @"required" : @"true",
  8795. @"value" : zipcode
  8796. },
  8797. @"item_9" : @{
  8798. @"aname" : @"City",
  8799. @"control" : @"edit",
  8800. @"keyboard" : @"text",
  8801. @"name" : @"credit_card_city",
  8802. @"required" : @"true",
  8803. @"value" : city
  8804. }
  8805. },
  8806. @"value" : @"Fill Now",
  8807. @"value_id" : @""
  8808. },
  8809. @"val_1" : @{
  8810. @"check" : @(0),
  8811. @"value" : @"Fill Later",
  8812. @"value_id" : @""
  8813. }
  8814. },
  8815. @"control" : @"enum",
  8816. @"name" : @"",
  8817. @"single_select" : @"true"
  8818. }
  8819. };
  8820. #endif;
  8821. [val setObject:sub_item forKey:@"sub_item"];
  8822. }
  8823. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8824. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8825. }];
  8826. // "section_2"
  8827. NSMutableDictionary *dic = @{
  8828. @"count" : @(1),
  8829. @"item_0" : @{
  8830. @"aname" : @"Payment",
  8831. @"required" : required,
  8832. @"cadedate" : cadedate,
  8833. // @{
  8834. // @"count" : @(6),
  8835. // @"val_3" : @{
  8836. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8837. // @"value" : @"Check",
  8838. // @"value_id" : @"Check"
  8839. // },
  8840. // @"val_2" : @{
  8841. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8842. // @"value" : @"Cash",
  8843. // @"value_id" : @"Cash"
  8844. // },
  8845. // @"val_1" : @{
  8846. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8847. // @"value" : @"NET 60",
  8848. // @"value_id" : @"NET 30"
  8849. // },
  8850. // @"val_4" : @{
  8851. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8852. // @"value" : @"Wire Transfer",
  8853. // @"value_id" : @"Wire Transfer"
  8854. // },
  8855. // @"val_0" : @{
  8856. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8857. // @"sub_item" : @{
  8858. // @"count" : @(3),
  8859. // @"item_0" : @{
  8860. // @"aname" : @"choose",
  8861. // @"control" : @"multi_action",
  8862. // @"count" : @(1),
  8863. // @"item_0" : @{
  8864. // @"aname" : @"Same as customer",
  8865. // @"key_map" : @{
  8866. // @"credit_card_address1" : @"customer_address1",
  8867. // @"credit_card_address2" : @"customer_address2",
  8868. // @"credit_card_city" : @"customer_city",
  8869. // @"credit_card_first_name" : @"customer_first_name",
  8870. // @"credit_card_last_name" : @"customer_last_name",
  8871. // @"credit_card_state" : @"customer_state",
  8872. // @"credit_card_zipcode" : @"customer_zipcode"
  8873. // },
  8874. // @"type" : @"pull"
  8875. // }
  8876. // },
  8877. // @"item_1" : @{
  8878. // @"aname" : @"",
  8879. // @"color" : @"red",
  8880. // @"control" : @"text",
  8881. // @"name" : @"",
  8882. // @"value" : @"USA Credit cards only"
  8883. // },
  8884. // @"item_2" : @{
  8885. // @"aname" : @"Fill",
  8886. // @"cadedate" : @{
  8887. // @"count" : @(2),
  8888. // @"val_0" : @{
  8889. // @"check" : @(1),
  8890. // @"sub_item" : @{
  8891. // @"count" : @(11),
  8892. // @"item_0" : @{
  8893. // @"aname" : @"Type",
  8894. // @"cadedate" : @{
  8895. // @"count" : @(2),
  8896. // @"val_0" : @{
  8897. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8898. // @"value" : @"VISA",
  8899. // @"value_id" : @(0)
  8900. // },
  8901. // @"val_1" : @{
  8902. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8903. // @"value" : @"MASTER CARD",
  8904. // @"value_id" : @(1)
  8905. // }
  8906. // },
  8907. // @"control" : @"enum",
  8908. // @"name" : @"credit_card_type",
  8909. // @"required" : @"true",
  8910. // @"single_select" : @"true"
  8911. // },
  8912. // @"item_1" : @{
  8913. // @"aname" : @"Number",
  8914. // @"control" : @"edit",
  8915. // @"keyboard" : @"int",
  8916. // @"length" : @"16",
  8917. // @"name" : @"credit_card_number",
  8918. // @"required" : @"true",
  8919. // @"value" : cardNumber
  8920. // },
  8921. // @"item_10" : @{
  8922. // @"aname" : @"State",
  8923. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8924. // @"control" : @"enum",
  8925. // @"enum" : @"true",
  8926. // @"name" : @"credit_card_state",
  8927. // @"required" : @"true",
  8928. // @"single_select" : @"true"
  8929. // },
  8930. // @"item_2" : @{
  8931. // @"aname" : @"Expiration Date",
  8932. // @"control" : @"monthpicker",
  8933. // @"name" : @"credit_card_expiration",
  8934. // @"required" : @"true",
  8935. // @"type" : @"date",
  8936. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8937. // },
  8938. // @"item_3" : @{
  8939. // @"aname" : @"Security Code",
  8940. // @"control" : @"edit",
  8941. // @"keyboard" : @"int",
  8942. // @"length" : @"3",
  8943. // @"name" : @"credit_card_security_code",
  8944. // @"required" : @"true",
  8945. // @"value" : securityCode
  8946. // },
  8947. // @"item_4" : @{
  8948. // @"aname" : @"First Name",
  8949. // @"control" : @"edit",
  8950. // @"keyboard" : @"text",
  8951. // @"name" : @"credit_card_first_name",
  8952. // @"required" : @"true",
  8953. // @"value" : firstName
  8954. // },
  8955. // @"item_5" : @{
  8956. // @"aname" : @"Last Name",
  8957. // @"control" : @"edit",
  8958. // @"keyboard" : @"text",
  8959. // @"name" : @"credit_card_last_name",
  8960. // @"required" : @"true",
  8961. // @"value" : lastName
  8962. // },
  8963. // @"item_6" : @{
  8964. // @"aname" : @"Address 1",
  8965. // @"control" : @"edit",
  8966. // @"keyboard" : @"text",
  8967. // @"name" : @"credit_card_address1",
  8968. // @"required" : @"true",
  8969. // @"value" : addr1
  8970. // },
  8971. // @"item_7" : @{
  8972. // @"aname" : @"Address 2",
  8973. // @"control" : @"edit",
  8974. // @"keyboard" : @"text",
  8975. // @"name" : @"credit_card_address2",
  8976. // @"value" : addr2
  8977. // },
  8978. // @"item_8" : @{
  8979. // @"aname" : @"zip code",
  8980. // @"control" : @"edit",
  8981. // @"keyboard" : @"text",
  8982. // @"name" : @"credit_card_zipcode",
  8983. // @"required" : @"true",
  8984. // @"value" : zipcode
  8985. // },
  8986. // @"item_9" : @{
  8987. // @"aname" : @"City",
  8988. // @"control" : @"edit",
  8989. // @"keyboard" : @"text",
  8990. // @"name" : @"credit_card_city",
  8991. // @"required" : @"true",
  8992. // @"value" : city
  8993. // }
  8994. // },
  8995. // @"value" : @"Fill Now",
  8996. // @"value_id" : @""
  8997. // },
  8998. // @"val_1" : @{
  8999. // @"check" : @(0),
  9000. // @"value" : @"Fill Later",
  9001. // @"value_id" : @""
  9002. // }
  9003. // },
  9004. // @"control" : @"enum",
  9005. // @"name" : @"",
  9006. // @"single_select" : @"true"
  9007. // }
  9008. // },
  9009. // @"value" : @"Visa/Master",
  9010. // @"value_id" : @"Credit Card"
  9011. // },
  9012. // @"val_5" : @{
  9013. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9014. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9015. // @"value_id" : @"FOLLOW EXISTING"
  9016. // }
  9017. // },
  9018. @"control" : @"enum",
  9019. @"name" : @"paymentType",
  9020. @"single_select" : @"true"
  9021. },
  9022. @"title" : @"Payment Information"
  9023. }.mutableCopy;
  9024. NSNumber *hide = setting[@"PaymentInformationHide"];
  9025. [dic setValue:hide forKey:@"hide"];
  9026. return dic;
  9027. }
  9028. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9029. // params
  9030. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9031. orderCode = [self translateSingleQuote:orderCode];
  9032. // 缺货检查
  9033. 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];
  9034. __block BOOL outOfStock = NO;
  9035. sqlite3 *database = db;
  9036. if (!db) {
  9037. database = [iSalesDB get_db];
  9038. }
  9039. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9040. int availability = sqlite3_column_int(stmt, 0);
  9041. int item_qty = sqlite3_column_int(stmt, 1);
  9042. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9043. }];
  9044. if (!db) {
  9045. [iSalesDB close_db:database];
  9046. }
  9047. return outOfStock;
  9048. }
  9049. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9050. {
  9051. assert(params[@"user"]!=nil);
  9052. assert(params[@"contact_id"]!=nil);
  9053. assert(params[@"can_create_backorder"]!=nil);
  9054. sqlite3 *db = [iSalesDB get_db];
  9055. // params
  9056. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9057. orderCode = [self translateSingleQuote:orderCode];
  9058. // 缺货检查
  9059. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9060. if (![params[@"can_create_backorder"] boolValue]) {
  9061. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9062. if (out_of_stock) {
  9063. [iSalesDB close_db:db];
  9064. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9065. [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"];
  9066. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9067. return [RAConvertor dict2data:resultDic];
  9068. }
  9069. }
  9070. // UISetting
  9071. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9072. NSString *cachefolder = [paths objectAtIndex:0];
  9073. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9074. NSData* json =nil;
  9075. json=[NSData dataWithContentsOfFile:img_cache];
  9076. NSError *error=nil;
  9077. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9078. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9079. [params setObject:setting forKey:@"setting"];
  9080. int section_count = 0;
  9081. // 0 Order Type 1 Shipping Method 2 Payment Information
  9082. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9083. // 0 Order Type
  9084. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9085. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9086. [ret setValue:order_type_dic forKey:key0];
  9087. // 1 Shipping Method
  9088. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9089. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9090. [ret setValue:shipping_method_dic forKey:key1];
  9091. // 2 Payment Information
  9092. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9093. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9094. [ret setValue:payment_info_dic forKey:key2];
  9095. // 3 Customer
  9096. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9097. NSDictionary *customer_dic = [self customerDic:params db:db];
  9098. [ret setValue:customer_dic forKey:key3];
  9099. // 4 Ship To
  9100. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9101. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9102. [ret setValue:ship_to_dic forKey:key4];
  9103. // 5 Ship From
  9104. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9105. if (![shipFromDisable integerValue]) {
  9106. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9107. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9108. [ret setValue:ship_from_dic forKey:key5];
  9109. }
  9110. // 6 Freight Bill To
  9111. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9112. if (![freightBillToDisable integerValue]) {
  9113. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9114. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9115. [ret setValue:freight_bill_to forKey:key6];
  9116. }
  9117. // 7 Merchandise Bill To
  9118. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9119. if (![merchandiseBillToDisable integerValue]) {
  9120. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9121. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9122. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9123. }
  9124. // 8 Return To
  9125. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9126. if (![returnToDisable integerValue]) {
  9127. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9128. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9129. [ret setValue:return_to_dic forKey:key8];
  9130. }
  9131. // 9 Model Information
  9132. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9133. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9134. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9135. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9136. [ret setValue:model_info_dic forKey:key9];
  9137. // 10 Remarks Content
  9138. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9139. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9140. [ret setValue:remarks_content_dic forKey:key10];
  9141. // 11 Order Total
  9142. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9143. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9144. [ret setValue:order_total_dic forKey:key11];
  9145. // 12 Signature
  9146. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9147. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9148. [ret setValue:sign_dic forKey:key12];
  9149. [ret setValue:@(section_count) forKey:@"section_count"];
  9150. [iSalesDB close_db:db];
  9151. return [RAConvertor dict2data:ret];
  9152. // return nil;
  9153. }
  9154. #pragma mark addr editor
  9155. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9156. NSMutableDictionary *new_item = [item mutableCopy];
  9157. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9158. return new_item;
  9159. }
  9160. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9161. {
  9162. // "is_subaction" = true;
  9163. // orderCode = MOB1608240002;
  9164. // password = 123456;
  9165. // "subaction_tag" = 1;
  9166. // user = EvanK;
  9167. // {
  9168. // "is_subaction" = true;
  9169. // orderCode = MOB1608240002;
  9170. // password = 123456;
  9171. // "refresh_trigger" = zipcode;
  9172. // "subaction_tag" = 1;
  9173. // user = EvanK;
  9174. // }
  9175. NSString *country_code = nil;
  9176. NSString *zipCode = nil;
  9177. NSString *stateCode = nil;
  9178. NSString *city = nil;
  9179. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9180. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9181. NSString *code_id = params[@"country"];
  9182. country_code = [self countryCodeByid:code_id];
  9183. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9184. NSString *zip_code = params[@"zipcode"];
  9185. // 剔除全部为空格
  9186. int spaceCount = 0;
  9187. for (int i = 0; i < zip_code.length; i++) {
  9188. if ([zip_code characterAtIndex:i] == ' ') {
  9189. spaceCount++;
  9190. }
  9191. }
  9192. if (spaceCount == zip_code.length) {
  9193. zip_code = @"";
  9194. }
  9195. zipCode = zip_code;
  9196. if (zipCode.length > 0) {
  9197. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9198. country_code = [dic valueForKey:@"country_code"];
  9199. if (!country_code) {
  9200. // country_code = @"US";
  9201. NSString *code_id = params[@"country"];
  9202. country_code = [self countryCodeByid:code_id];
  9203. }
  9204. stateCode = [dic valueForKey:@"state_code"];
  9205. if(!stateCode.length) {
  9206. stateCode = params[@"state"];
  9207. }
  9208. city = [dic valueForKey:@"city"];
  9209. if (!city.length) {
  9210. city = params[@"city"];
  9211. }
  9212. // zip code
  9213. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9214. // [zipDic setValue:zipCode forKey:@"value"];
  9215. // [section_0 setValue:zipDic forKey:@"item_11"];
  9216. } else {
  9217. NSString *code_id = params[@"country"];
  9218. country_code = [self countryCodeByid:code_id];
  9219. stateCode = params[@"state"];
  9220. city = params[@"city"];
  9221. }
  9222. }
  9223. }
  9224. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9225. // [ret removeObjectForKey:@"up_params"];
  9226. [ret setObject:@"New Address" forKey:@"title"];
  9227. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9228. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9229. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9230. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9231. // [country_dic removeObjectForKey:@"refresh"];
  9232. // [country_dic removeObjectForKey:@"restore"];
  9233. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9234. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9235. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9236. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9237. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9238. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9239. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9240. // [zip_code_dic removeObjectForKey:@"refresh"];
  9241. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9242. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9243. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9244. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9245. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9246. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9247. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9248. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9249. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9250. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9251. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9252. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9253. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9254. // country
  9255. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9256. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9257. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9258. // state
  9259. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9260. // NSDictionary *tmpDic = @{
  9261. // @"value" : @"Other",
  9262. // @"value_id" : @"",
  9263. // @"check" : [NSNumber numberWithInteger:0]
  9264. // };
  9265. //
  9266. // for (int i = 0; i < allState.allKeys.count; i++) {
  9267. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9268. //
  9269. // NSDictionary *tmp = allState[key];
  9270. // [allState setValue:tmpDic forKey:key];
  9271. // tmpDic = tmp;
  9272. // }
  9273. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9274. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9275. [ret setValue:new_section_0 forKey:@"section_0"];
  9276. return [RAConvertor dict2data:ret];
  9277. }
  9278. #pragma mark save addr
  9279. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9280. {
  9281. // NSString *companyName = [self valueInParams:params key:@"company"];
  9282. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9283. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9284. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9285. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9286. // NSString *countryId = [self valueInParams:params key:@"country"];
  9287. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9288. // NSString *city = [self valueInParams:params key:@"city"];
  9289. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9290. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9291. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9292. // NSString *phone = [self valueInParams:params key:@"phone"];
  9293. // NSString *fax = [self valueInParams:params key:@"fax"];
  9294. // NSString *email = [self valueInParams:params key:@"email"];
  9295. return [self offline_saveContact:params update:NO isCustomer:NO];
  9296. }
  9297. #pragma mark cancel order
  9298. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9299. {
  9300. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9301. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9302. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9303. if (order_id.length) {
  9304. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9305. }
  9306. int ret = [iSalesDB execSql:sql];
  9307. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9308. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9309. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9310. // [dic setValue:@"160409" forKey:@"min_ver"];
  9311. return [RAConvertor dict2data:dic];
  9312. }
  9313. #pragma mark sign order
  9314. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9315. {
  9316. //参考 offline_saveBusinesscard
  9317. DebugLog(@"sign order params: %@",params);
  9318. // orderCode = MOB1608240002;
  9319. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9320. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9321. orderCode = [self translateSingleQuote:orderCode];
  9322. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9323. picPath = [self translateSingleQuote:picPath];
  9324. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9325. int ret = [iSalesDB execSql:sql];
  9326. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9327. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9328. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9329. // [dic setValue:@"160409" forKey:@"min_ver"];
  9330. return [RAConvertor dict2data:dic];
  9331. }
  9332. #pragma mark save order
  9333. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9334. NSString *ret = [self valueInParams:params key:key];
  9335. if (translate) {
  9336. ret = [self translateSingleQuote:ret];
  9337. }
  9338. return ret;
  9339. }
  9340. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9341. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9342. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9343. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9344. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9345. if (schedule_date.length==0 ) {
  9346. schedule_date = @"";
  9347. } else {
  9348. schedule_date = [self rchangeDateFormate:schedule_date];
  9349. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9350. }
  9351. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9352. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9353. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9354. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9355. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9356. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9357. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9358. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9359. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9360. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9361. if (![total_price isEqualToString:@""]) {
  9362. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9363. } else {
  9364. total_price = @"";
  9365. }
  9366. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9367. if ([paymentsAndCredits isEqualToString:@""]) {
  9368. paymentsAndCredits = @"";
  9369. } else {
  9370. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9371. }
  9372. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9373. if ([handling_fee_value isEqualToString:@""]) {
  9374. handling_fee_value = @"";
  9375. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9376. handling_fee_value = @"";
  9377. } else {
  9378. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9379. }
  9380. NSString *handling_fee_placeholder = handling_fee_value;
  9381. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9382. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9383. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9384. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9385. NSString *lift_gate_placeholder = @"";
  9386. if ([lift_gate_value isEqualToString:@""]) {
  9387. lift_gate_placeholder = @"";
  9388. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9389. lift_gate_placeholder = @"";
  9390. } else {
  9391. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9392. }
  9393. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9394. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9395. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9396. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9397. 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];
  9398. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9399. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9400. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9401. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9402. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9403. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9404. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9405. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9406. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9407. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9408. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9409. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9410. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9411. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9412. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9413. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9414. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9415. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9416. if (!number_nil) {
  9417. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9418. }
  9419. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9420. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9421. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9422. if (!security_code_nil) {
  9423. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9424. }
  9425. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9426. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9427. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9428. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9429. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9430. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9431. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9432. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9433. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9434. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9435. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9436. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9437. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9438. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9439. NSString *contact_id = customer_cid;
  9440. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9441. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9442. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9443. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9444. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9445. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9446. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9447. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9448. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9449. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9450. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9451. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9452. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9453. //
  9454. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9455. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9456. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9457. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9458. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9459. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9460. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9461. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9462. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9463. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9464. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9465. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9466. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9467. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9468. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9469. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9470. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9471. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9472. 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];
  9473. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9474. if (receive_cid.length) {
  9475. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9476. }
  9477. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9478. if (receive_name.length) {
  9479. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9480. }
  9481. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9482. if (receive_ext.length) {
  9483. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9484. }
  9485. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9486. if (receive_contact.length) {
  9487. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9488. }
  9489. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9490. if (receive_phone.length) {
  9491. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9492. }
  9493. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9494. if (receive_email.length) {
  9495. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9496. }
  9497. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9498. if (receive_fax.length) {
  9499. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9500. }
  9501. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9502. if (sender_cid.length) {
  9503. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9504. }
  9505. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9506. if (sender_name.length) {
  9507. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9508. }
  9509. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9510. if (sender_ext.length) {
  9511. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9512. }
  9513. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9514. if(sender_contact.length) {
  9515. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9516. }
  9517. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9518. if (sender_phone.length) {
  9519. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9520. }
  9521. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9522. if (sender_fax.length) {
  9523. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9524. }
  9525. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9526. if (sender_email.length) {
  9527. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9528. }
  9529. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9530. if (shipping_billto_cid.length) {
  9531. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9532. }
  9533. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9534. if (shipping_billto_name.length) {
  9535. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9536. }
  9537. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9538. if (shipping_billto_ext.length) {
  9539. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9540. }
  9541. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9542. if (shipping_billto_contact.length) {
  9543. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9544. }
  9545. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9546. if (shipping_billto_phone.length) {
  9547. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9548. }
  9549. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9550. if (shipping_billto_fax.length) {
  9551. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9552. }
  9553. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9554. if (shipping_billto_email.length) {
  9555. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9556. }
  9557. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9558. if (billing_cid.length) {
  9559. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9560. }
  9561. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9562. if (billing_name.length) {
  9563. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9564. }
  9565. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9566. if (billing_ext.length) {
  9567. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9568. }
  9569. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9570. if (billing_contact.length) {
  9571. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9572. }
  9573. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9574. if (billing_phone.length) {
  9575. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9576. }
  9577. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9578. if (billing_fax.length) {
  9579. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9580. }
  9581. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9582. if (billing_email.length) {
  9583. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9584. }
  9585. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9586. if (returnto_cid.length) {
  9587. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9588. }
  9589. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9590. if (returnto_name.length) {
  9591. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9592. }
  9593. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9594. if (returnto_ext.length) {
  9595. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9596. }
  9597. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9598. if (returnto_contact.length) {
  9599. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9600. }
  9601. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9602. if (returnto_phone.length) {
  9603. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9604. }
  9605. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9606. if (returnto_fax.length) {
  9607. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9608. }
  9609. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9610. if (returnto_email.length) {
  9611. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9612. }
  9613. NSString *order_status = @"status = 1,";
  9614. if (submit) {
  9615. order_status = @"status = -11,";
  9616. }
  9617. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9618. NSString *sync_sql = @"";
  9619. if (submit) {
  9620. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9621. [param setValue:sales_rep forKey:@"sales_rep"];
  9622. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9623. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9624. }
  9625. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",schedule_date, 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];
  9626. DebugLog(@"save order contactSql: %@",contactSql);
  9627. DebugLog(@"save order orderSql: %@",orderSql);
  9628. // int contact_ret = [iSalesDB execSql:contactSql];
  9629. int order_ret = [iSalesDB execSql:orderSql];
  9630. int ret = order_ret;
  9631. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9632. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9633. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9634. // [dic setValue:@"160409" forKey:@"min_ver"];
  9635. [dic setObject:so_id forKey:@"so#"];
  9636. return [RAConvertor dict2data:dic];
  9637. }
  9638. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9639. {
  9640. // id foo = nil;
  9641. // NSMutableArray *a = @[].mutableCopy;
  9642. // [a addObject:foo];
  9643. return [self saveorder:param submit:NO];
  9644. }
  9645. #pragma mark add to cart by name
  9646. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  9647. {
  9648. NSString *orderCode = [params objectForKey:@"orderCode"];
  9649. NSDictionary *newParams = @{
  9650. @"product_id" : params[@"product_id_string"],
  9651. @"orderCode" : orderCode,
  9652. @"can_create_backorder":params[@"can_create_backorder"]
  9653. };
  9654. return [self offline_add2cart:[newParams mutableCopy]];
  9655. }
  9656. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  9657. {
  9658. NSString *orderCode = [params objectForKey:@"orderCode"];
  9659. NSString *upccode = [params objectForKey:@"upc_code"];
  9660. // product_name = [self translateSingleQuote:product_name];
  9661. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9662. // bool search_upc = [params[@"search_upc"] boolValue];
  9663. sqlite3 *db = [iSalesDB get_db];
  9664. __block NSMutableString *product_id_string = [NSMutableString string];
  9665. // NSString *name = [product_name_array objectAtIndex:i];
  9666. NSString *sql =nil;
  9667. 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];
  9668. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9669. int product_id = sqlite3_column_int(stmt, 0);
  9670. [product_id_string appendFormat:@"%d",product_id];
  9671. }];
  9672. if (!orderCode) {
  9673. orderCode = @"";
  9674. }
  9675. NSDictionary *newParams = @{
  9676. @"product_id" : product_id_string,
  9677. @"orderCode" : orderCode,
  9678. @"can_create_backorder":params[@"can_create_backorder"]
  9679. };
  9680. [iSalesDB close_db:db];
  9681. return [self offline_add2cart:[newParams mutableCopy]];
  9682. }
  9683. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9684. {
  9685. NSString *orderCode = [params objectForKey:@"orderCode"];
  9686. NSString *product_name = [params objectForKey:@"product_name"];
  9687. product_name = [self translateSingleQuote:product_name];
  9688. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9689. bool search_upc = [params[@"search_upc"] boolValue];
  9690. sqlite3 *db = [iSalesDB get_db];
  9691. __block NSMutableString *product_id_string = [NSMutableString string];
  9692. for (int i = 0; i < product_name_array.count; i++) {
  9693. NSString *name = [product_name_array objectAtIndex:i];
  9694. NSString *sql =nil;
  9695. if(search_upc)
  9696. 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];
  9697. else
  9698. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9699. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9700. int product_id = sqlite3_column_int(stmt, 0);
  9701. if (i == product_name_array.count - 1) {
  9702. [product_id_string appendFormat:@"%d",product_id];
  9703. } else {
  9704. [product_id_string appendFormat:@"%d,",product_id];
  9705. }
  9706. }];
  9707. }
  9708. if (!orderCode) {
  9709. orderCode = @"";
  9710. }
  9711. NSDictionary *newParams = @{
  9712. @"product_id" : product_id_string,
  9713. @"orderCode" : orderCode,
  9714. @"can_create_backorder":params[@"can_create_backorder"]
  9715. };
  9716. [iSalesDB close_db:db];
  9717. return [self offline_add2cart:[newParams mutableCopy]];
  9718. }
  9719. #pragma mark reset order
  9720. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9721. {
  9722. // UIApplication * app = [UIApplication sharedApplication];
  9723. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9724. [iSalesDB disable_trigger];
  9725. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9726. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9727. [iSalesDB enable_trigger];
  9728. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9729. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9730. return [RAConvertor dict2data:dic];
  9731. }
  9732. #pragma mark submit order
  9733. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9734. {
  9735. return [self saveorder:params submit:YES];
  9736. }
  9737. #pragma mark copy order
  9738. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9739. {
  9740. NSMutableDictionary *ret = @{}.mutableCopy;
  9741. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9742. sqlite3 *db = [iSalesDB get_db];
  9743. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9744. // 首先查看联系人是否active
  9745. 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];
  9746. __block int customer_is_active = 1;
  9747. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9748. customer_is_active = sqlite3_column_int(stmt, 0);
  9749. }];
  9750. if (!customer_is_active) {
  9751. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9752. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9753. [iSalesDB close_db:db];
  9754. return [RAConvertor dict2data:ret];
  9755. }
  9756. // new order
  9757. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9758. NSString *new_order_code = [self get_offline_soid:db];
  9759. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9760. user = [self translateSingleQuote:user];
  9761. 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
  9762. __block int result = 1;
  9763. result = [iSalesDB execSql:insert_order_sql db:db];
  9764. if (!result) {
  9765. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9766. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9767. [iSalesDB close_db:db];
  9768. return [RAConvertor dict2data:ret];
  9769. }
  9770. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9771. // __block NSString *product_id = @"";
  9772. __weak typeof(self) weakSelf = self;
  9773. 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];
  9774. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9775. int is_active = sqlite3_column_int(stmt, 1);
  9776. if (is_active) {
  9777. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9778. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9779. 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];
  9780. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9781. }
  9782. }];
  9783. // product_id = [product_id substringFromIndex:1];
  9784. //
  9785. // [self offline_add2cart:@{}.mutableCopy];
  9786. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9787. [iSalesDB close_db:db];
  9788. if (result) {
  9789. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9790. } else {
  9791. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9792. }
  9793. [ret setObject:new_order_code forKey:@"so_id"];
  9794. return [RAConvertor dict2data:ret];
  9795. }
  9796. #pragma mark move wish list to cart
  9797. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9798. assert(params[@"can_create_backorder"]!=nil);
  9799. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9800. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9801. NSString *collectId = params[@"collectId"];
  9802. 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];
  9803. __block NSString *product_id = @"";
  9804. __block NSString *qty = @"";
  9805. __block int number_of_outOfStock = 0;
  9806. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9807. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9808. int productId = sqlite3_column_int(stmt, 0);
  9809. int item_qty = sqlite3_column_int(stmt, 1);
  9810. int availability = sqlite3_column_int(stmt, 2);
  9811. int _id = sqlite3_column_int(stmt, 3);
  9812. if (![params[@"can_create_backorder"] boolValue]) {
  9813. // 库存小于购买量为缺货
  9814. if (availability >= item_qty) {
  9815. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9816. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9817. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9818. } else {
  9819. number_of_outOfStock++;
  9820. }
  9821. } else {
  9822. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9823. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9824. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9825. }
  9826. }];
  9827. NSMutableDictionary *retDic = nil;
  9828. if (![params[@"can_create_backorder"] boolValue]) {
  9829. if (delete_collectId.count == 0) {
  9830. retDic = [NSMutableDictionary dictionary];
  9831. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9832. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9833. return [RAConvertor dict2data:retDic];
  9834. }
  9835. }
  9836. if (product_id.length > 1) {
  9837. product_id = [product_id substringFromIndex:1];
  9838. }
  9839. if (qty.length > 1) {
  9840. qty = [qty substringFromIndex:1];
  9841. }
  9842. NSString *orderCode = params[@"orderCode"];
  9843. if (!orderCode) {
  9844. orderCode = @"";
  9845. }
  9846. NSDictionary *newParams = @{
  9847. @"product_id" : product_id,
  9848. @"orderCode" : orderCode,
  9849. @"qty" : qty,
  9850. @"can_create_backorder":params[@"can_create_backorder"]
  9851. };
  9852. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9853. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9854. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9855. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9856. NSInteger ret = [wish_return[@"result"] intValue];
  9857. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9858. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9859. }
  9860. if (![params[@"can_create_backorder"] boolValue]) {
  9861. if (number_of_outOfStock > 0) {
  9862. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9863. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9864. }
  9865. }
  9866. return [RAConvertor dict2data:retDic];
  9867. }
  9868. #pragma mark - portfolio
  9869. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9870. // assert(params[@"contact_id"]!=nil);
  9871. assert(params[@"user"]!=nil);
  9872. assert(params[@"can_see_price"]!=nil);
  9873. int sort = [[params valueForKey:@"sort"] intValue];
  9874. int offset = [[params valueForKey:@"offset"] intValue];
  9875. int limit = [[params valueForKey:@"limit"] intValue];
  9876. NSString *orderBy = @"";
  9877. switch (sort) {
  9878. case 0:{
  9879. orderBy = @"p.modify_time desc";
  9880. }
  9881. break;
  9882. case 1:{
  9883. orderBy = @"modify_time asc";
  9884. }
  9885. break;
  9886. case 2:{
  9887. orderBy = @"p.name asc";
  9888. }
  9889. break;
  9890. case 3:{
  9891. orderBy = @"p.name desc";
  9892. }
  9893. break;
  9894. case 4:{
  9895. orderBy = @"p.description asc";
  9896. }
  9897. break;
  9898. case 5: {
  9899. orderBy = @"m.default_category asc";
  9900. }
  9901. default:
  9902. break;
  9903. }
  9904. // 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];
  9905. 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];
  9906. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9907. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9908. sqlite3 *db = [iSalesDB get_db];
  9909. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9910. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9911. int product_id = sqlite3_column_int(stmt, 0);
  9912. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9913. NSString *name = [self textAtColumn:1 statement:stmt];
  9914. NSString *description = [self textAtColumn:2 statement:stmt];
  9915. double price = sqlite3_column_double(stmt, 3);
  9916. double discount = sqlite3_column_double(stmt,4);
  9917. int qty = sqlite3_column_int(stmt, 5);
  9918. int percentage = sqlite3_column_int(stmt, 6);
  9919. int item_id = sqlite3_column_int(stmt, 7);
  9920. // int fashion_id = sqlite3_column_int(stmt, 8);
  9921. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9922. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9923. double percent = sqlite3_column_double(stmt, 11);
  9924. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9925. if ([price_null isEqualToString:@"null"]) {
  9926. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9927. }
  9928. NSMutableDictionary *item = @{
  9929. @"linenotes": line_note,
  9930. @"check": @(1),
  9931. @"product_id": product_id_string,
  9932. @"available_qty": @(qty),
  9933. @"available_percent" : @(percent),
  9934. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9935. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9936. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9937. @"img": img_path,
  9938. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9939. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9940. }.mutableCopy;
  9941. if (percentage) {
  9942. [item removeObjectForKey:@"available_qty"];
  9943. } else {
  9944. [item removeObjectForKey:@"available_percent"];
  9945. }
  9946. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9947. if ([qty_null isEqualToString:@"null"]) {
  9948. [item removeObjectForKey:@"available_qty"];
  9949. }
  9950. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9951. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9952. }];
  9953. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9954. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9955. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9956. }
  9957. [iSalesDB close_db:db];
  9958. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9959. [dic setValue:@"" forKey:@"email_content"];
  9960. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9961. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9962. return [RAConvertor dict2data:dic];
  9963. }
  9964. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9965. __block int qty = 0;
  9966. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9967. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9968. qty = sqlite3_column_int(stmt, 0);
  9969. }];
  9970. return qty;
  9971. }
  9972. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9973. {
  9974. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9975. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9976. // NSMutableDictionary * values = params[@"replaceValue"];
  9977. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9978. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9979. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9980. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9981. NSString *pdf_path = @"";
  9982. if (direct) {
  9983. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9984. } else {
  9985. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9986. }
  9987. NSString *create_user = [self valueInParams:params key:@"user"];
  9988. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9989. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9990. // model info
  9991. // 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];
  9992. // V1.90 more color
  9993. 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];
  9994. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9995. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9996. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9997. sqlite3 *db = [iSalesDB get_db];
  9998. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9999. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10000. int product_id = sqlite3_column_int(stmt, 0);
  10001. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10002. double price = sqlite3_column_double(stmt, 1);
  10003. double discount = sqlite3_column_double(stmt,2);
  10004. int qty = sqlite3_column_int(stmt, 3);
  10005. int percentage = sqlite3_column_int(stmt, 4);
  10006. int item_id = sqlite3_column_int(stmt, 5);
  10007. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10008. double percent = sqlite3_column_double(stmt, 7);
  10009. int more_color = sqlite3_column_int(stmt, 8);
  10010. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10011. /* if ([price_null isEqualToString:@"null"]) {
  10012. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10013. }
  10014. */
  10015. [product_ids_string appendFormat:@"%@,",product_id_string];
  10016. // Regular Price
  10017. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10018. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10019. // QTY
  10020. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10021. if ([qty_null isEqualToString:@"null"]) {
  10022. // 查available
  10023. qty = [self model_QTY:product_id_string db:db];
  10024. }
  10025. if (percentage) {
  10026. qty = qty * percent / 100;
  10027. }
  10028. // Special Price
  10029. if ([price_null isEqualToString:@"null"]) {
  10030. // price = regular price
  10031. price = [regular_price_str doubleValue];
  10032. }
  10033. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10034. if (![discount_null isEqualToString:@"null"]) {
  10035. price = price * (1 - discount / 100.0);
  10036. }
  10037. NSMutableDictionary *item = @{
  10038. @"line_note": line_note,
  10039. @"product_id": product_id_string,
  10040. @"available_qty": @(qty),
  10041. @"more_color":@(more_color),
  10042. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10043. @"regular_price" : regular_price_str,
  10044. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10045. }.mutableCopy;
  10046. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10047. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10048. }];
  10049. [iSalesDB close_db:db];
  10050. if (product_ids_string.length > 0) {
  10051. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10052. }
  10053. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10054. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10055. return [RAConvertor dict2data:resultDictionary];
  10056. }
  10057. NSString *model_info = [RAConvertor dict2string:dic];
  10058. // 创建PDF
  10059. // 在preview情况下保存,则不需要新建了
  10060. if (direct) {
  10061. NSMutableDictionary *tear_sheet_params = params;
  10062. // if (tear_sheet_id) {
  10063. // tear_sheet_params = values;
  10064. // }
  10065. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10066. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10067. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10068. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10069. resultDictionary = pdfInfo.mutableCopy;
  10070. } else { // 创建PDF失败
  10071. return pdfData;
  10072. }
  10073. } else {
  10074. // pdf_path 就是本地路径
  10075. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10076. }
  10077. // 将文件移动到PDF Cache文件夹
  10078. NSString *newPath = [pdf_path lastPathComponent];
  10079. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10080. NSString *cachefolder = [paths objectAtIndex:0];
  10081. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10082. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10083. NSFileManager *fileManager = [NSFileManager defaultManager];
  10084. NSError *error = nil;
  10085. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10086. if (error) { // 移动文件失败
  10087. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10088. return [RAConvertor dict2data:resultDictionary];
  10089. }
  10090. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10091. pdf_path = [newPath lastPathComponent];
  10092. // 保存信息
  10093. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10094. NSString *off_params = [RAConvertor dict2string:params];
  10095. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10096. pdf_path = [self translateSingleQuote:pdf_path];
  10097. create_user = [self translateSingleQuote:create_user];
  10098. tear_note = [self translateSingleQuote:tear_note];
  10099. tear_name = [self translateSingleQuote:tear_name];
  10100. model_info = [self translateSingleQuote:model_info];
  10101. configureParams = [self translateSingleQuote:configureParams];
  10102. off_params = [self translateSingleQuote:off_params];
  10103. 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];
  10104. if (tear_sheet_id) {
  10105. int _id = [tear_sheet_id intValue];
  10106. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10107. }
  10108. int result = [iSalesDB execSql:save_pdf_sql];
  10109. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10110. //
  10111. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10112. if (remove_Item) {
  10113. // portfolioId
  10114. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10115. }
  10116. return [RAConvertor dict2data:resultDictionary];
  10117. }
  10118. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10119. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10120. }
  10121. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10122. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10123. }
  10124. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10125. int offset = [[params valueForKey:@"offset"] intValue];
  10126. int limit = [[params valueForKey:@"limit"] intValue];
  10127. NSString *keyword = [params valueForKey:@"keyWord"];
  10128. NSString *where = @"where is_delete is null or is_delete = 0";
  10129. if (keyword.length) {
  10130. keyword = [self translateSingleQuote:keyword];
  10131. 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];
  10132. }
  10133. 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
  10134. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10135. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10136. NSString *cachefolder = [paths objectAtIndex:0];
  10137. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10138. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10139. NSString *name = [self textAtColumn:0 statement:stmt];
  10140. NSString *note = [self textAtColumn:1 statement:stmt];
  10141. NSString *time = [self textAtColumn:2 statement:stmt];
  10142. NSString *user = [self textAtColumn:3 statement:stmt];
  10143. NSString *path = [self textAtColumn:4 statement:stmt];
  10144. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10145. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10146. path = [pdfFolder stringByAppendingPathComponent:path];
  10147. BOOL bdir=NO;
  10148. NSFileManager* fileManager = [NSFileManager defaultManager];
  10149. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10150. {
  10151. //pdf文件不存在
  10152. path=nil;
  10153. }
  10154. time = [self changeDateTimeFormate:time];
  10155. time = [time stringByAppendingString:@" PST"];
  10156. int sheet_id = sqlite3_column_int(stmt, 5);
  10157. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10158. item[@"tearsheetsId"]=@(sheet_id);
  10159. item[@"pdf_path"]=path;
  10160. item[@"create_time"]=time;
  10161. item[@"create_user"]=user;
  10162. item[@"tear_note"]=note;
  10163. item[@"tear_name"]=name;
  10164. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10165. item[@"model_info"]=model_info;
  10166. item[@"off_params"]=off_params;
  10167. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10168. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10169. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10170. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10171. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10172. }];
  10173. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10174. return [RAConvertor dict2data:dic];
  10175. }
  10176. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10177. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10178. // NSString *user = [params objectForKey:@"user"];
  10179. // 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];
  10180. 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];
  10181. int result = [iSalesDB execSql:sql];
  10182. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10183. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10184. if (result == RESULT_TRUE) {
  10185. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10186. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10187. sqlite3 *db = [iSalesDB get_db];
  10188. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10189. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10190. [iSalesDB close_db:db];
  10191. }
  10192. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10193. return [RAConvertor dict2data:dic];
  10194. }
  10195. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10196. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10197. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10198. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10199. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10200. NSDictionary *company_item = @{
  10201. @"control": @"text",
  10202. @"keyboard": @"text",
  10203. @"name": @"company_name",
  10204. @"value": COMPANY_FULL_NAME,
  10205. @"aname": @"Company Name"
  10206. };
  10207. [section_0 setObject:company_item forKey:@"item_0"];
  10208. [dic setObject:section_0 forKey:@"section_0"];
  10209. // Regurlar Price
  10210. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10211. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10212. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10213. __block long val_count = 0;
  10214. NSString *sql = @"select name,type,order_by from price order by order_by";
  10215. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10216. NSString *name = [self textAtColumn:0 statement:stmt];
  10217. int type = sqlite3_column_int(stmt, 1);
  10218. int order_by = sqlite3_column_int(stmt, 2);
  10219. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10220. NSDictionary *price_dic = @{
  10221. @"value" : name,
  10222. @"value_id" : [NSNumber numberWithInteger:type],
  10223. @"check" : order_by == 0 ? @(1) : @(0)
  10224. };
  10225. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10226. val_count = ++(*count);
  10227. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10228. val_count = 0;
  10229. }];
  10230. [cadedate setObject:@{@"value" : @"None",
  10231. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10232. val_count++;
  10233. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10234. [price setObject:cadedate forKey:@"cadedate"];
  10235. [section1 setObject:price forKey:@"item_2"];
  10236. [dic setObject:section1 forKey:@"section_1"];
  10237. return [RAConvertor dict2data:dic];
  10238. }
  10239. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10240. NSString *product_id = [params objectForKey:@"fashionId"];
  10241. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10242. __block int result = RESULT_TRUE;
  10243. __block int qty = 0;
  10244. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10245. qty = sqlite3_column_int(stmt, 0);
  10246. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10247. result = RESULT_FALSE;
  10248. }];
  10249. NSMutableDictionary *dic = @{
  10250. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10251. @"mode" : @"Regular Mode",
  10252. @"quantity_available" : @(qty),
  10253. @"result" : @(result),
  10254. }.mutableCopy;
  10255. return [RAConvertor dict2data:dic];
  10256. }
  10257. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10258. NSString *item_ids = [params objectForKey:@"item_id"];
  10259. NSString *line_notes = [params objectForKey:@"notes"];
  10260. NSString *price_str = [params objectForKey:@"price"];
  10261. NSString *discount_str = [params objectForKey:@"discount"];
  10262. NSString *percent = [params objectForKey:@"available_percent"];
  10263. NSString *qty = [params objectForKey:@"available_qty"];
  10264. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10265. int dot = 0;
  10266. if (line_notes) {
  10267. line_notes = [self translateSingleQuote:line_notes];
  10268. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10269. sql = [sql stringByAppendingString:line_notes];
  10270. dot = 1;
  10271. } else {
  10272. line_notes = @"";
  10273. }
  10274. if (price_str) {
  10275. if (dot) {
  10276. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10277. } else {
  10278. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10279. dot = 1;
  10280. }
  10281. sql = [sql stringByAppendingString:price_str];
  10282. } else {
  10283. price_str = @"";
  10284. }
  10285. if (discount_str) {
  10286. if (dot) {
  10287. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10288. } else {
  10289. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10290. dot = 1;
  10291. }
  10292. sql = [sql stringByAppendingString:discount_str];
  10293. } else {
  10294. discount_str = @"";
  10295. }
  10296. if (percent) {
  10297. if (dot) {
  10298. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10299. } else {
  10300. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10301. dot = 1;
  10302. }
  10303. sql = [sql stringByAppendingString:percent];
  10304. } else {
  10305. percent = @"";
  10306. }
  10307. if (qty) {
  10308. if (dot) {
  10309. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10310. } else {
  10311. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10312. dot = 1;
  10313. }
  10314. sql = [sql stringByAppendingString:qty];
  10315. } else {
  10316. qty = @"";
  10317. }
  10318. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10319. sql = [sql stringByAppendingString:where];
  10320. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10321. int result = [iSalesDB execSql:sql];
  10322. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10323. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10324. return [RAConvertor dict2data:dic];
  10325. }
  10326. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10327. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10328. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10329. sqlite3 *db = [iSalesDB get_db];
  10330. 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];
  10331. int result = [iSalesDB execSql:sql db:db];
  10332. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10333. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10334. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10335. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10336. [iSalesDB close_db:db];
  10337. return [RAConvertor dict2data:dic];
  10338. }
  10339. + (void)offline_removePDFWithName:(NSString *)name {
  10340. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10341. NSString *cachefolder = [paths objectAtIndex:0];
  10342. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10343. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10344. NSFileManager *fileManager = [NSFileManager defaultManager];
  10345. [fileManager removeItemAtPath:path error:nil];
  10346. }
  10347. + (void)offline_clear_PDFCache {
  10348. NSFileManager *fileManager = [NSFileManager defaultManager];
  10349. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10350. NSString *cachefolder = [paths objectAtIndex:0];
  10351. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10352. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10353. for (NSString *path in pdf_files) {
  10354. [self offline_removePDFWithName:[path lastPathComponent]];
  10355. }
  10356. }
  10357. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10358. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10359. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10360. NSString *user = [params objectForKey:@"user"];
  10361. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10362. if (![create_user isEqualToString:user]) {
  10363. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10364. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10365. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10366. return [RAConvertor dict2data:dic];
  10367. }
  10368. 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]];
  10369. __block int is_local = 0;
  10370. __block NSString *path = @"";
  10371. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10372. is_local = sqlite3_column_int(stmt, 0);
  10373. path = [self textAtColumn:1 statement:stmt];
  10374. }];
  10375. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10376. if (is_local == 1) {
  10377. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10378. // 删除文件
  10379. [self offline_removePDFWithName:path];
  10380. }
  10381. int result = [iSalesDB execSql:sql];
  10382. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10383. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10384. return [RAConvertor dict2data:dic];
  10385. }
  10386. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10387. {
  10388. // assert(add_params[@"contact_id"]!=nil);
  10389. assert(add_params[@"user"]!=nil);
  10390. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10391. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10392. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10393. NSString* where=@"1=1";
  10394. if (ver!=nil) {
  10395. where=@"is_dirty=1";
  10396. }
  10397. 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];
  10398. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10399. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10400. sqlite3 *db = [iSalesDB get_db];
  10401. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10402. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10403. NSInteger _id = sqlite3_column_int(stmt, 0);
  10404. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10405. NSString *name = [self textAtColumn:2 statement:stmt];
  10406. NSString *desc = [self textAtColumn:3 statement:stmt];
  10407. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10408. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10409. NSInteger qty = sqlite3_column_int(stmt, 6);
  10410. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10411. double percent = sqlite3_column_double(stmt, 8);
  10412. double price = sqlite3_column_double(stmt, 9);
  10413. double discount = sqlite3_column_double(stmt, 10);
  10414. NSString *img = [self textAtColumn:11 statement:stmt];
  10415. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10416. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10417. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10418. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10419. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10420. if ([price_null isEqualToString:@"null"]) {
  10421. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10422. }
  10423. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10424. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10425. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10426. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10427. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10428. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10429. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10430. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10431. if ([qty_null isEqualToString:@"null"]) {
  10432. // [item setValue:@"null" forKey:@"available_qty"];
  10433. } else {
  10434. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10435. }
  10436. if ([is_percent_null isEqualToString:@"null"]) {
  10437. // [item setValue:@"null" forKey:@"percentage"];
  10438. } else {
  10439. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10440. }
  10441. if ([percent_null isEqualToString:@"null"]) {
  10442. // [item setValue:@"null" forKey:@"percent"];
  10443. } else {
  10444. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10445. }
  10446. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10447. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10448. [item setValue:name forKey:@"name"];
  10449. [item setValue:desc forKey:@"description"];
  10450. [item setValue:img forKey:@"img"];
  10451. [item setValue:line_note forKey:@"line_note"];
  10452. [item setValue:create_time forKey:@"createtime"];
  10453. [item setValue:modify_time forKey:@"modifytime"];
  10454. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10455. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10456. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10457. }];
  10458. [iSalesDB close_db:db];
  10459. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10460. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10461. }
  10462. return ret;
  10463. }
  10464. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10465. {
  10466. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10467. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10468. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10469. NSString* where=@"1=1";
  10470. if (ver!=nil) {
  10471. where=@"is_dirty=1";
  10472. }
  10473. 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];
  10474. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10475. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10476. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10477. NSInteger _id = sqlite3_column_int(stmt, 0);
  10478. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10479. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10480. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10481. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10482. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10483. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10484. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10485. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10486. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10487. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10488. NSString *uuid = [NSUUID UUID].UUIDString;
  10489. int is_delete = sqlite3_column_int(stmt, 11);
  10490. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10491. int is_local = sqlite3_column_int(stmt, 12);
  10492. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10493. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10494. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10495. }
  10496. [item setObject:pdf_path forKey:@"pdf_path"];
  10497. [item setObject:create_user forKey:@"create_user"];
  10498. [item setObject:tear_note forKey:@"tear_note"];
  10499. [item setObject:tear_name forKey:@"tear_name"];
  10500. [item setObject:model_info forKey:@"model_info"];
  10501. [item setObject:createtime forKey:@"createtime"];
  10502. [item setObject:modifytime forKey:@"modifytime"];
  10503. [item setObject:urlParams forKey:@"urlParams"];
  10504. [item setObject:off_params forKey:@"off_params"];
  10505. [item setObject:uuid forKey:@"pdf_token"];
  10506. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10507. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10508. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10509. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10510. } else {
  10511. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10512. }
  10513. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10514. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10515. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10516. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10517. }];
  10518. return ret;
  10519. }
  10520. #pragma mark 2020
  10521. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10522. {
  10523. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10524. sqlite3 *db = [iSalesDB get_db];
  10525. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10526. if (contactType) {
  10527. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10528. } else {
  10529. contactType = @"1 = 1";
  10530. }
  10531. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10532. DebugLog(@"offline contact list keyword: %@",keyword);
  10533. // advanced search
  10534. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10535. if (contact_name) {
  10536. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10537. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10538. } else {
  10539. contact_name = @"";
  10540. }
  10541. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10542. if (customer_phone) {
  10543. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10544. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10545. } else {
  10546. customer_phone = @"";
  10547. }
  10548. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10549. if (customer_fax) {
  10550. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10551. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10552. } else {
  10553. customer_fax = @"";
  10554. }
  10555. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10556. if (customer_zipcode) {
  10557. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10558. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10559. } else {
  10560. customer_zipcode = @"";
  10561. }
  10562. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10563. if (customer_sales_rep) {
  10564. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10565. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10566. } else {
  10567. customer_sales_rep = @"";
  10568. }
  10569. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10570. if (customer_state) {
  10571. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10572. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10573. } else {
  10574. customer_state = @"";
  10575. }
  10576. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10577. if (customer_name) {
  10578. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10579. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10580. } else {
  10581. customer_name = @"";
  10582. }
  10583. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10584. if (customer_country) {
  10585. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10586. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10587. } else {
  10588. customer_country = @"";
  10589. }
  10590. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10591. if (customer_cid) {
  10592. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10593. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10594. } else {
  10595. customer_cid = @"";
  10596. }
  10597. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10598. if (customer_city) {
  10599. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10600. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  10601. } else {
  10602. customer_city = @"";
  10603. }
  10604. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  10605. if (customer_address) {
  10606. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10607. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  10608. } else {
  10609. customer_address = @"";
  10610. }
  10611. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  10612. if (customer_email) {
  10613. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10614. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  10615. } else {
  10616. customer_email = @"";
  10617. }
  10618. NSString *price_name = [params valueForKey:@"price_name"];
  10619. if (price_name) {
  10620. if ([price_name containsString:@","]) {
  10621. // 首先从 price表中查处name
  10622. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  10623. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  10624. for (int i = 1;i < pArray.count;i++) {
  10625. NSString *p = pArray[i];
  10626. [mutablePStr appendFormat:@" or type = %@ ",p];
  10627. }
  10628. [mutablePStr appendString:@";"];
  10629. __block NSMutableArray *price_name_array = [NSMutableArray array];
  10630. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10631. char *name = (char *)sqlite3_column_text(stmt, 0);
  10632. if (!name)
  10633. name = "";
  10634. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  10635. }];
  10636. // 再根据name 拼sql
  10637. NSMutableString *mutable_price_name = [NSMutableString string];
  10638. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  10639. for (int i = 1; i < price_name_array.count; i++) {
  10640. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  10641. }
  10642. [mutable_price_name appendString:@")"];
  10643. price_name = mutable_price_name;
  10644. } else {
  10645. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  10646. if ([price_name isEqualToString:@""]) {
  10647. price_name = @"";
  10648. } else {
  10649. __block NSString *price;
  10650. price_name = [self translateSingleQuote:price_name];
  10651. [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) {
  10652. char *p = (char *)sqlite3_column_text(stmt, 0);
  10653. if (p == NULL) {
  10654. p = "";
  10655. }
  10656. price = [NSString stringWithUTF8String:p];
  10657. }];
  10658. if ([price isEqualToString:@""]) {
  10659. price_name = @"";
  10660. } else {
  10661. price = [self translateSingleQuote:price];
  10662. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  10663. }
  10664. }
  10665. }
  10666. } else {
  10667. price_name = @"";
  10668. }
  10669. int limit = [[params valueForKey:@"limit"] intValue];
  10670. int offset = [[params valueForKey:@"offset"] intValue];
  10671. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10672. 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];
  10673. 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];
  10674. // int result= [iSalesDB AddExFunction:db];
  10675. int count =0;
  10676. NSString *sqlQuery = nil;
  10677. if(keyword.length==0)
  10678. {
  10679. // 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];
  10680. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  10681. sqlQuery = sql;
  10682. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  10683. }
  10684. else
  10685. {
  10686. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  10687. keyword = keyword.lowercaseString;
  10688. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10689. 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];
  10690. 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]];
  10691. }
  10692. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10693. sqlite3_stmt * statement;
  10694. [ret setValue:@"2" forKey:@"result"];
  10695. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  10696. // 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";
  10697. int i = 0;
  10698. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10699. {
  10700. while (sqlite3_step(statement) == SQLITE_ROW)
  10701. {
  10702. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10703. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10704. int editable = sqlite3_column_int(statement, 0);
  10705. char *company_name = (char*)sqlite3_column_text(statement, 1);
  10706. NSString *nscompany_name =nil;
  10707. if(company_name==nil)
  10708. nscompany_name=@"";
  10709. else
  10710. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  10711. char *country = (char*)sqlite3_column_text(statement, 2);
  10712. if(country==nil)
  10713. country="";
  10714. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10715. // char *addr = (char*)sqlite3_column_text(statement, 3);
  10716. // if(addr==nil)
  10717. // addr="";
  10718. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10719. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10720. if(zipcode==nil)
  10721. zipcode="";
  10722. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10723. char *state = (char*)sqlite3_column_text(statement, 5);
  10724. if(state==nil)
  10725. state="";
  10726. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10727. char *city = (char*)sqlite3_column_text(statement, 6);
  10728. if(city==nil)
  10729. city="";
  10730. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10731. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10732. // NSString *nscontact_name = nil;
  10733. // if(contact_name==nil)
  10734. // nscontact_name=@"";
  10735. // else
  10736. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10737. char *phone = (char*)sqlite3_column_text(statement, 8);
  10738. NSString *nsphone = nil;
  10739. if(phone==nil)
  10740. nsphone=@"";
  10741. else
  10742. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10743. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10744. if(contact_id==nil)
  10745. contact_id="";
  10746. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10747. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10748. if(addr_1==nil)
  10749. addr_1="";
  10750. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10751. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10752. if(addr_2==nil)
  10753. addr_2="";
  10754. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10755. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10756. if(addr_3==nil)
  10757. addr_3="";
  10758. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10759. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10760. if(addr_4==nil)
  10761. addr_4="";
  10762. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10763. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10764. if(first_name==nil)
  10765. first_name="";
  10766. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10767. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10768. if(last_name==nil)
  10769. last_name="";
  10770. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10771. char *fax = (char*)sqlite3_column_text(statement, 16);
  10772. NSString *nsfax = nil;
  10773. if(fax==nil)
  10774. nsfax=@"";
  10775. else
  10776. {
  10777. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10778. if(nsfax.length>0)
  10779. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10780. }
  10781. char *email = (char*)sqlite3_column_text(statement, 17);
  10782. NSString *nsemail = nil;
  10783. if(email==nil)
  10784. nsemail=@"";
  10785. else
  10786. {
  10787. nsemail= [[NSString alloc]initWithUTF8String:email];
  10788. if(nsemail.length>0)
  10789. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10790. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10791. }
  10792. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10793. [arr_name addObject:nsfirst_name];
  10794. [arr_name addObject:nslast_name];
  10795. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10796. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10797. {
  10798. // decrypt
  10799. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10800. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10801. nsphone=[AESCrypt fastdecrypt:nsphone];
  10802. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10803. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10804. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10805. }
  10806. [arr_addr addObject:nscompany_name];
  10807. [arr_addr addObject:nscontact_name];
  10808. [arr_addr addObject:@"<br>"];
  10809. [arr_addr addObject:nsaddr_1];
  10810. [arr_addr addObject:nsaddr_2];
  10811. [arr_addr addObject:nsaddr_3];
  10812. [arr_addr addObject:nsaddr_4];
  10813. //[arr_addr addObject:nsaddr];
  10814. [arr_addr addObject:nszipcode];
  10815. [arr_addr addObject:nscity];
  10816. [arr_addr addObject:nsstate];
  10817. [arr_addr addObject:nscountry];
  10818. [arr_addr addObject:@"<br>"];
  10819. [arr_addr addObject:nsphone];
  10820. [arr_addr addObject:nsfax];
  10821. [arr_addr addObject:nsemail];
  10822. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10823. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10824. [item setValue:name forKey:@"name"];
  10825. [item setValue:nscontact_id forKey:@"contact_id"];
  10826. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10827. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10828. i++;
  10829. }
  10830. sqlite3_finalize(statement);
  10831. }
  10832. [iSalesDB close_db:db];
  10833. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10834. dispatch_async(dispatch_get_main_queue(), ^{
  10835. UIApplication * app = [UIApplication sharedApplication];
  10836. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10837. [ret setValue:appDelegate.mode forKey:@"mode"];
  10838. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10839. result(ret);
  10840. });
  10841. return ;
  10842. });
  10843. }
  10844. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10845. //{
  10846. //
  10847. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10848. //
  10849. // NSString* contactId = [params valueForKey:@"contactId"];
  10850. //
  10851. //
  10852. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10853. //
  10854. //
  10855. //
  10856. //
  10857. //
  10858. //
  10859. // sqlite3 *db = [iSalesDB get_db];
  10860. //
  10861. //
  10862. //
  10863. //
  10864. //
  10865. // NSString *sqlQuery = nil;
  10866. //
  10867. //
  10868. // {
  10869. // 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];
  10870. //
  10871. // }
  10872. //
  10873. //
  10874. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10875. // sqlite3_stmt * statement;
  10876. //
  10877. //
  10878. // [ret setValue:@"2" forKey:@"result"];
  10879. //
  10880. //
  10881. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10882. // {
  10883. //
  10884. // //int i = 0;
  10885. // if (sqlite3_step(statement) == SQLITE_ROW)
  10886. // {
  10887. //
  10888. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10889. //
  10890. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10891. //
  10892. //
  10893. // // int editable = sqlite3_column_int(statement, 0);
  10894. //
  10895. //
  10896. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10897. // NSString *nscompany_name =nil;
  10898. // if(company_name==nil)
  10899. // nscompany_name=@"";
  10900. // else
  10901. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10902. //
  10903. //
  10904. // char *country = (char*)sqlite3_column_text(statement, 2);
  10905. // if(country==nil)
  10906. // country="";
  10907. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10908. //
  10909. //
  10910. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10911. // // if(addr==nil)
  10912. // // addr="";
  10913. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10914. //
  10915. //
  10916. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10917. // if(zipcode==nil)
  10918. // zipcode="";
  10919. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10920. //
  10921. //
  10922. // char *state = (char*)sqlite3_column_text(statement, 5);
  10923. // if(state==nil)
  10924. // state="";
  10925. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10926. //
  10927. // char *city = (char*)sqlite3_column_text(statement, 6);
  10928. // if(city==nil)
  10929. // city="";
  10930. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10931. //
  10932. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10933. // // NSString *nscontact_name = nil;
  10934. // // if(contact_name==nil)
  10935. // // nscontact_name=@"";
  10936. // // else
  10937. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10938. //
  10939. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10940. // NSString *nsphone = nil;
  10941. // if(phone==nil)
  10942. // nsphone=@"";
  10943. // else
  10944. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10945. //
  10946. //
  10947. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10948. // // if(contact_id==nil)
  10949. // // contact_id="";
  10950. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10951. //
  10952. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10953. // if(addr_1==nil)
  10954. // addr_1="";
  10955. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10956. //
  10957. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10958. // if(addr_2==nil)
  10959. // addr_2="";
  10960. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10961. //
  10962. //
  10963. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10964. // if(addr_3==nil)
  10965. // addr_3="";
  10966. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10967. //
  10968. //
  10969. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10970. // if(addr_4==nil)
  10971. // addr_4="";
  10972. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10973. //
  10974. //
  10975. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10976. // if(first_name==nil)
  10977. // first_name="";
  10978. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10979. //
  10980. //
  10981. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10982. // if(last_name==nil)
  10983. // last_name="";
  10984. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10985. //
  10986. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10987. // NSString *nsfax = nil;
  10988. // if(fax==nil)
  10989. // nsfax=@"";
  10990. // else
  10991. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10992. //
  10993. // char *email = (char*)sqlite3_column_text(statement, 17);
  10994. // NSString *nsemail = nil;
  10995. // if(email==nil)
  10996. // nsemail=@"";
  10997. // else
  10998. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10999. //
  11000. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11001. // NSString *nsimg_0 = nil;
  11002. // if(img_0==nil)
  11003. // nsimg_0=@"";
  11004. // else
  11005. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11006. //
  11007. // [self copy_bcardImg:nsimg_0];
  11008. //
  11009. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11010. // NSString *nsimg_1 = nil;
  11011. // if(img_1==nil)
  11012. // nsimg_1=@"";
  11013. // else
  11014. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11015. // [self copy_bcardImg:nsimg_1];
  11016. //
  11017. //
  11018. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11019. // NSString *nsimg_2 = nil;
  11020. // if(img_2==nil)
  11021. // nsimg_2=@"";
  11022. // else
  11023. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11024. // [self copy_bcardImg:nsimg_2];
  11025. //
  11026. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11027. // NSString *nsprice_type = nil;
  11028. // if(price_type==nil)
  11029. // nsprice_type=@"";
  11030. // else
  11031. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11032. //
  11033. //
  11034. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11035. // NSString *nsnotes = nil;
  11036. // if(notes==nil)
  11037. // nsnotes=@"";
  11038. // else
  11039. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11040. //
  11041. //
  11042. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11043. // NSString *nssalesrep = nil;
  11044. // if(salesrep==nil)
  11045. // nssalesrep=@"";
  11046. // else
  11047. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11048. //
  11049. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11050. //
  11051. //
  11052. // {
  11053. // // decrypt
  11054. //
  11055. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11056. //
  11057. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11058. //
  11059. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11060. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11061. //
  11062. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11063. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11064. //
  11065. // }
  11066. //
  11067. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11068. //
  11069. // [arr_name addObject:nsfirst_name];
  11070. // [arr_name addObject:nslast_name];
  11071. //
  11072. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11073. //
  11074. //
  11075. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11076. //
  11077. // [arr_ext addObject:nsaddr_1];
  11078. // [arr_ext addObject:nsaddr_2];
  11079. // [arr_ext addObject:nsaddr_3];
  11080. // [arr_ext addObject:nsaddr_4];
  11081. // [arr_ext addObject:@"\r\n"];
  11082. //
  11083. // [arr_ext addObject:nscity];
  11084. // [arr_ext addObject:nsstate];
  11085. // [arr_ext addObject:nszipcode];
  11086. // [arr_ext addObject:nscountry];
  11087. //
  11088. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11089. //
  11090. //
  11091. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11092. //
  11093. //
  11094. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11095. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11096. // [item setValue:nscountry forKey:@"customer_country"];
  11097. // [item setValue:nsphone forKey:@"customer_phone"];
  11098. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11099. // [item setValue:nscompany_name forKey:@"customer_name"];
  11100. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11101. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11102. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11103. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11104. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11105. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11106. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11107. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11108. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11109. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11110. // [item setValue:nscity forKey:@"customer_city"];
  11111. // [item setValue:nsstate forKey:@"customer_state"];
  11112. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11113. // [item setValue:contactId forKey:@"customer_cid"];
  11114. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11115. // [item setValue:nsfax forKey:@"customer_fax"];
  11116. // [item setValue:nsemail forKey:@"customer_email"];
  11117. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11118. //
  11119. //
  11120. // [ret setObject:item forKey:@"customerInfo"];
  11121. // // i++;
  11122. //
  11123. //
  11124. //
  11125. // }
  11126. //
  11127. //
  11128. //
  11129. //
  11130. // sqlite3_finalize(statement);
  11131. // }
  11132. //
  11133. //
  11134. //
  11135. // [iSalesDB close_db:db];
  11136. //
  11137. //
  11138. //
  11139. //
  11140. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11141. // dispatch_async(dispatch_get_main_queue(), ^{
  11142. // UIApplication * app = [UIApplication sharedApplication];
  11143. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11144. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11145. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11146. // result(ret);
  11147. // });
  11148. //
  11149. // return ;
  11150. // });
  11151. //}
  11152. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11153. {
  11154. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11155. NSString* order_code= appDelegate.order_code;
  11156. NSString* user = appDelegate.user;
  11157. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11158. NSString* category = [params valueForKey:@"category"];
  11159. if (!category || [category isEqualToString:@""]) {
  11160. category = @"%";
  11161. }
  11162. category = [self translateSingleQuote:category];
  11163. int limit = [[params valueForKey:@"limit"] intValue];
  11164. int offset = [[params valueForKey:@"offset"] intValue];
  11165. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11166. NSString *limit_str = @"";
  11167. if (limited) {
  11168. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11169. }
  11170. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11171. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11172. sqlite3 *db = [iSalesDB get_db];
  11173. // [iSalesDB AddExFunction:db];
  11174. int count;
  11175. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11176. 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];
  11177. double price_min = 0;
  11178. double price_max = 0;
  11179. if ([params.allKeys containsObject:@"alert"]) {
  11180. // alert
  11181. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11182. NSString *alert = params[@"alert"];
  11183. if ([alert isEqualToString:@"Display All"]) {
  11184. alert = [NSString stringWithFormat:@""];
  11185. } else {
  11186. alert = [self translateSingleQuote:alert];
  11187. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11188. }
  11189. // available
  11190. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11191. NSString *available = params[@"available"];
  11192. NSString *available_condition;
  11193. if ([available isEqualToString:@"Display All"]) {
  11194. available_condition = @"";
  11195. } else if ([available isEqualToString:@"Available Now"]) {
  11196. available_condition = @"and availability > 0";
  11197. } else {
  11198. available_condition = @"and availability == 0";
  11199. }
  11200. // best seller
  11201. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11202. NSString *best_seller = @"";
  11203. NSString *order_best_seller = @"m.name asc";
  11204. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11205. best_seller = @"and best_seller > 0";
  11206. order_best_seller = @"m.best_seller desc,m.name asc";
  11207. }
  11208. // price
  11209. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11210. NSString *price = params[@"price"];
  11211. price_min = 0;
  11212. price_max = MAXFLOAT;
  11213. if (user && price != nil) {
  11214. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11215. NSMutableString *priceName = [NSMutableString string];
  11216. for (int i = 0; i < priceTypeArray.count; i++) {
  11217. NSString *pricetype = priceTypeArray[i];
  11218. pricetype = [self translateSingleQuote:pricetype];
  11219. if (i == 0) {
  11220. [priceName appendFormat:@"'%@'",pricetype];
  11221. } else {
  11222. [priceName appendFormat:@",'%@'",pricetype];
  11223. }
  11224. }
  11225. if ([price isEqualToString:@"Display All"]) {
  11226. price = [NSString stringWithFormat:@""];
  11227. } else if([price containsString:@"+"]){
  11228. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11229. price_min = [price doubleValue];
  11230. 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];
  11231. } else {
  11232. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11233. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11234. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11235. 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];
  11236. }
  11237. } else {
  11238. price = @"";
  11239. }
  11240. // sold_by_qty : Sold in quantities of %@
  11241. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11242. NSString *qty = params[@"sold_by_qty"];
  11243. if ([qty isEqualToString:@"Display All"]) {
  11244. qty = @"";
  11245. } else {
  11246. qty = [self translateSingleQuote:qty];
  11247. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11248. }
  11249. // cate
  11250. // category = [self translateSingleQuote:category];
  11251. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11252. // cate mutiple selection
  11253. NSString *category_id = params[@"category"];
  11254. NSMutableArray *cate_id_array = nil;
  11255. NSMutableString *cateWhere = [NSMutableString string];
  11256. if ([category_id isEqualToString:@""] || !category_id) {
  11257. [cateWhere appendString:@"1 = 1"];
  11258. } else {
  11259. if ([category_id containsString:@","]) {
  11260. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11261. } else {
  11262. cate_id_array = [@[category_id] mutableCopy];
  11263. }
  11264. [cateWhere appendString:@"("];
  11265. for (int i = 0; i < cate_id_array.count; i++) {
  11266. if (i == 0) {
  11267. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11268. } else {
  11269. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11270. }
  11271. }
  11272. [cateWhere appendString:@")"];
  11273. }
  11274. // where bestseller > 0 order by bestseller desc
  11275. // sql query: alert availability(int) best_seller(int) price qty
  11276. 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];
  11277. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11278. }
  11279. DebugLog(@"offline category where: %@",where);
  11280. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11281. if (!user) {
  11282. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11283. }
  11284. [ret setValue:filter forKey:@"filter"];
  11285. DebugLog(@"offline_category sql:%@",sqlQuery);
  11286. sqlite3_stmt * statement;
  11287. [ret setValue:@"2" forKey:@"result"];
  11288. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11289. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11290. // int count=0;
  11291. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11292. {
  11293. int i=0;
  11294. while (sqlite3_step(statement) == SQLITE_ROW)
  11295. {
  11296. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11297. char *name = (char*)sqlite3_column_text(statement, 0);
  11298. if(name==nil)
  11299. name="";
  11300. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11301. char *description = (char*)sqlite3_column_text(statement, 1);
  11302. if(description==nil)
  11303. description="";
  11304. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11305. int product_id = sqlite3_column_int(statement, 2);
  11306. int wid = sqlite3_column_int(statement, 3);
  11307. int closeout = sqlite3_column_int(statement, 4);
  11308. int cid = sqlite3_column_int(statement, 5);
  11309. int wisdelete = sqlite3_column_int(statement, 6);
  11310. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11311. int more_color = sqlite3_column_int(statement, 8);
  11312. // Defaul Category ID
  11313. __block NSString *categoryID = nil;
  11314. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11315. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11316. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11317. if(default_category==nil)
  11318. default_category="";
  11319. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11320. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11321. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11322. categoryID = nsdefault_category;
  11323. }];
  11324. if (!categoryID.length) {
  11325. NSString *cateIDs = params[@"category"];
  11326. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11327. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11328. for (NSString *cateID in requestCategoryArr) {
  11329. BOOL needBreak = NO;
  11330. for (NSString *itemCateIDBox in itemCategoryArr) {
  11331. if (itemCateIDBox.length > 4) {
  11332. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11333. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11334. if ([itemCategoryID isEqualToString:cateID]) {
  11335. needBreak = YES;
  11336. categoryID = itemCategoryID;
  11337. break;
  11338. }
  11339. }
  11340. }
  11341. if (needBreak) {
  11342. break;
  11343. }
  11344. }
  11345. }
  11346. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11347. if(wid !=0 && wisdelete != 1)
  11348. [item setValue:@"true" forKey:@"wish_exists"];
  11349. else
  11350. [item setValue:@"false" forKey:@"wish_exists"];
  11351. if(closeout==0)
  11352. [item setValue:@"false" forKey:@"is_closeout"];
  11353. else
  11354. [item setValue:@"true" forKey:@"is_closeout"];
  11355. if(cid==0)
  11356. [item setValue:@"false" forKey:@"cart_exists"];
  11357. else
  11358. [item setValue:@"true" forKey:@"cart_exists"];
  11359. if (more_color == 0) {
  11360. [item setObject:@(false) forKey:@"more_color"];
  11361. } else if (more_color == 1) {
  11362. [item setObject:@(true) forKey:@"more_color"];
  11363. }
  11364. [item addEntriesFromDictionary:imgjson];
  11365. // [item setValue:nsurl forKey:@"img"];
  11366. [item setValue:nsname forKey:@"name"];
  11367. [item setValue:nsdescription forKey:@"description"];
  11368. if (categoryID) {
  11369. [item setValue:categoryID forKey:@"item_category_id"];
  11370. }
  11371. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11372. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11373. i++;
  11374. }
  11375. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11376. [ret setObject:items forKey:@"items"];
  11377. sqlite3_finalize(statement);
  11378. } else {
  11379. DebugLog(@"nothing...");
  11380. }
  11381. DebugLog(@"count:%d",count);
  11382. [iSalesDB close_db:db];
  11383. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11384. dispatch_async(dispatch_get_main_queue(), ^{
  11385. // UIApplication * app = [UIApplication sharedApplication];
  11386. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11387. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11388. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11389. result(ret);
  11390. });
  11391. });
  11392. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11393. }
  11394. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11395. {
  11396. [self categoryList:params limited:YES completionHandler:result];
  11397. }
  11398. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11399. {
  11400. UIApplication * app = [UIApplication sharedApplication];
  11401. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11402. NSString* order_code = appDelegate.order_code;
  11403. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11404. NSString* keyword = [params valueForKey:@"keyword"];
  11405. keyword=keyword.lowercaseString;
  11406. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11407. int limit = [[params valueForKey:@"limit"] intValue];
  11408. int offset = [[params valueForKey:@"offset"] intValue];
  11409. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11410. NSString *limit_str = @"";
  11411. if (limited) {
  11412. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11413. }
  11414. sqlite3 *db = [iSalesDB get_db];
  11415. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11416. NSString *sqlQuery = nil;
  11417. if(exactMatch )
  11418. 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 ;
  11419. else
  11420. 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
  11421. DebugLog(@"offline_search sql:%@",sqlQuery);
  11422. sqlite3_stmt * statement;
  11423. [ret setValue:@"2" forKey:@"result"];
  11424. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11425. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11426. // int count=0;
  11427. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11428. {
  11429. int i=0;
  11430. while (sqlite3_step(statement) == SQLITE_ROW)
  11431. {
  11432. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11433. // char *name = (char*)sqlite3_column_text(statement, 1);
  11434. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11435. char *name = (char*)sqlite3_column_text(statement, 0);
  11436. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11437. char *description = (char*)sqlite3_column_text(statement, 1);
  11438. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11439. int product_id = sqlite3_column_int(statement, 2);
  11440. // char *url = (char*)sqlite3_column_text(statement, 3);
  11441. // if(url==nil)
  11442. // url="";
  11443. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11444. int wid = sqlite3_column_int(statement, 3);
  11445. int closeout = sqlite3_column_int(statement, 4);
  11446. int cid = sqlite3_column_int(statement, 5);
  11447. int wisdelete = sqlite3_column_int(statement, 6);
  11448. int more_color = sqlite3_column_int(statement, 7);
  11449. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11450. if(wid !=0 && wisdelete != 1)
  11451. [item setValue:@"true" forKey:@"wish_exists"];
  11452. else
  11453. [item setValue:@"false" forKey:@"wish_exists"];
  11454. if(closeout==0)
  11455. [item setValue:@"false" forKey:@"is_closeout"];
  11456. else
  11457. [item setValue:@"true" forKey:@"is_closeout"];
  11458. if(cid==0)
  11459. [item setValue:@"false" forKey:@"cart_exists"];
  11460. else
  11461. [item setValue:@"true" forKey:@"cart_exists"];
  11462. if (more_color == 0) {
  11463. [item setObject:@(false) forKey:@"more_color"];
  11464. } else if (more_color == 1) {
  11465. [item setObject:@(true) forKey:@"more_color"];
  11466. }
  11467. [item addEntriesFromDictionary:imgjson];
  11468. // [item setValue:nsurl forKey:@"img"];
  11469. [item setValue:nsname forKey:@"fash_name"];
  11470. [item setValue:nsdescription forKey:@"description"];
  11471. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11472. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11473. i++;
  11474. }
  11475. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11476. [ret setObject:items forKey:@"items"];
  11477. sqlite3_finalize(statement);
  11478. }
  11479. DebugLog(@"count:%d",count);
  11480. [iSalesDB close_db:db];
  11481. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11482. dispatch_async(dispatch_get_main_queue(), ^{
  11483. result(ret);
  11484. });
  11485. });
  11486. }
  11487. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11488. {
  11489. [self search:params limited:YES completionHandler:result];
  11490. }
  11491. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11492. //{
  11493. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11494. // NSString* order_code = appDelegate.order_code;
  11495. // NSString* user = appDelegate.user;
  11496. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11497. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11498. //
  11499. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11500. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11501. //
  11502. // // category
  11503. // NSDictionary *category_menu = [self offline_category_menu];
  11504. // [filter setValue:category_menu forKey:@"category"];
  11505. //
  11506. // NSString* where= nil;
  11507. // NSString* orderby= @"m.name";
  11508. // if (!filterSearch) {
  11509. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11510. //
  11511. // switch (covertype) {
  11512. // case 0:
  11513. // {
  11514. // where=@"m.category like'%%#005#%%'";
  11515. // break;
  11516. // }
  11517. // case 1:
  11518. // {
  11519. // where=@"m.alert like '%QS%'";
  11520. // break;
  11521. // }
  11522. // case 2:
  11523. // {
  11524. // where=@"m.availability>0";
  11525. // break;
  11526. // }
  11527. // case 3:
  11528. // {
  11529. // where=@"m.best_seller>0";
  11530. // orderby=@"m.best_seller desc,m.name asc";
  11531. // break;
  11532. // }
  11533. // default:
  11534. // where=@"1=1";
  11535. // break;
  11536. // }
  11537. //
  11538. // }
  11539. //
  11540. //
  11541. // int limit = [[params valueForKey:@"limit"] intValue];
  11542. // int offset = [[params valueForKey:@"offset"] intValue];
  11543. //
  11544. // NSString *limit_str = @"";
  11545. // if (limited) {
  11546. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11547. // }
  11548. //
  11549. //
  11550. // sqlite3 *db = [iSalesDB get_db];
  11551. // // [iSalesDB AddExFunction:db];
  11552. //
  11553. // int count;
  11554. //
  11555. // NSString *sqlQuery = nil;
  11556. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11557. // 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];
  11558. //
  11559. //
  11560. // double price_min = 0;
  11561. // double price_max = 0;
  11562. // if (filterSearch) {
  11563. // // alert
  11564. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11565. // NSString *alert = params[@"alert"];
  11566. // if ([alert isEqualToString:@"Display All"]) {
  11567. // alert = [NSString stringWithFormat:@""];
  11568. // } else {
  11569. // alert = [self translateSingleQuote:alert];
  11570. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11571. // }
  11572. //
  11573. // // available
  11574. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11575. // NSString *available = params[@"available"];
  11576. // NSString *available_condition;
  11577. // if ([available isEqualToString:@"Display All"]) {
  11578. // available_condition = @"";
  11579. // } else if ([available isEqualToString:@"Available Now"]) {
  11580. // available_condition = @"and availability > 0";
  11581. // } else {
  11582. // available_condition = @"and availability == 0";
  11583. // }
  11584. //
  11585. // // best seller
  11586. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11587. // NSString *best_seller = @"";
  11588. // NSString *order_best_seller = @"m.name asc";
  11589. //
  11590. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11591. // best_seller = @"and best_seller > 0";
  11592. // order_best_seller = @"m.best_seller desc,m.name asc";
  11593. // }
  11594. //
  11595. // // price
  11596. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11597. // NSString *price = params[@"price"];
  11598. // price_min = 0;
  11599. // price_max = MAXFLOAT;
  11600. // if (user) {
  11601. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11602. // NSMutableString *priceName = [NSMutableString string];
  11603. // for (int i = 0; i < priceTypeArray.count; i++) {
  11604. // NSString *pricetype = priceTypeArray[i];
  11605. // pricetype = [self translateSingleQuote:pricetype];
  11606. // if (i == 0) {
  11607. // [priceName appendFormat:@"'%@'",pricetype];
  11608. // } else {
  11609. // [priceName appendFormat:@",'%@'",pricetype];
  11610. // }
  11611. // }
  11612. //
  11613. // if ([price isEqualToString:@"Display All"]) {
  11614. // price = [NSString stringWithFormat:@""];
  11615. // } else if([price containsString:@"+"]){
  11616. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11617. // price_min = [price doubleValue];
  11618. // 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];
  11619. // } else {
  11620. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11621. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  11622. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  11623. // 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];
  11624. // }
  11625. // } else {
  11626. // price = @"";
  11627. // }
  11628. //
  11629. // // sold_by_qty : Sold in quantities of %@
  11630. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11631. // NSString *qty = params[@"sold_by_qty"];
  11632. // if ([qty isEqualToString:@"Display All"]) {
  11633. // qty = @"";
  11634. // } else {
  11635. // qty = [self translateSingleQuote:qty];
  11636. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11637. // }
  11638. //
  11639. // // category
  11640. // NSString *category_id = params[@"ctgId"];
  11641. // NSMutableArray *cate_id_array = nil;
  11642. // NSMutableString *cateWhere = [NSMutableString string];
  11643. //
  11644. // if ([category_id isEqualToString:@""] || !category_id) {
  11645. // [cateWhere appendString:@"1 = 1"];
  11646. // } else {
  11647. //
  11648. // if ([category_id containsString:@","]) {
  11649. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11650. // } else {
  11651. // cate_id_array = [@[category_id] mutableCopy];
  11652. // }
  11653. // /*-----------*/
  11654. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  11655. //
  11656. // [cateWhere appendString:@"("];
  11657. // for (int i = 0; i < cate_id_array.count; i++) {
  11658. //
  11659. // for (NSString *key0 in cateDic.allKeys) {
  11660. //
  11661. // if ([key0 containsString:@"category_"]) {
  11662. //
  11663. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  11664. //
  11665. // for (NSString *key1 in category0.allKeys) {
  11666. //
  11667. // if ([key1 containsString:@"category_"]) {
  11668. //
  11669. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  11670. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  11671. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11672. //
  11673. // cate_id_array[i] = [category1 objectForKey:@"id"];
  11674. // if (i == 0) {
  11675. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11676. // } else {
  11677. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11678. // }
  11679. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11680. // [category0 setValue:category1 forKey:key1];
  11681. // [cateDic setValue:category0 forKey:key0];
  11682. //
  11683. // }
  11684. //
  11685. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  11686. //
  11687. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11688. //
  11689. // cate_id_array[i] = [category0 objectForKey:@"id"];
  11690. // if (i == 0) {
  11691. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11692. // } else {
  11693. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11694. // }
  11695. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11696. // [cateDic setValue:category0 forKey:key0];
  11697. //
  11698. // }
  11699. //
  11700. // }
  11701. //
  11702. // }
  11703. //
  11704. // }
  11705. //
  11706. // }
  11707. //
  11708. // }
  11709. // [cateWhere appendString:@")"];
  11710. //
  11711. // [filter setValue:cateDic forKey:@"category"];
  11712. // }
  11713. //
  11714. // // where bestseller > 0 order by bestseller desc
  11715. // // sql query: alert availability(int) best_seller(int) price qty
  11716. //
  11717. // 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];
  11718. //
  11719. //
  11720. // // count
  11721. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11722. //
  11723. // }
  11724. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11725. //
  11726. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11727. //
  11728. //
  11729. //
  11730. //
  11731. // if (!user) {
  11732. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11733. // }
  11734. //
  11735. // [ret setValue:filter forKey:@"filter"];
  11736. //
  11737. //
  11738. //
  11739. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11740. //
  11741. // sqlite3_stmt * statement;
  11742. // [ret setValue:@"2" forKey:@"result"];
  11743. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11744. //
  11745. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11746. // // int count=0;
  11747. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11748. // {
  11749. //
  11750. // int i=0;
  11751. // while (sqlite3_step(statement) == SQLITE_ROW)
  11752. // {
  11753. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11754. //
  11755. //
  11756. // char *name = (char*)sqlite3_column_text(statement, 0);
  11757. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11758. //
  11759. // char *description = (char*)sqlite3_column_text(statement, 1);
  11760. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11761. //
  11762. //
  11763. //
  11764. // int product_id = sqlite3_column_int(statement, 2);
  11765. //
  11766. //
  11767. // int wid = sqlite3_column_int(statement, 3);
  11768. // int closeout = sqlite3_column_int(statement, 4);
  11769. // int cid = sqlite3_column_int(statement, 5);
  11770. // int wisdelete = sqlite3_column_int(statement, 6);
  11771. // int more_color = sqlite3_column_int(statement, 7);
  11772. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11773. //
  11774. // if(wid !=0 && wisdelete != 1)
  11775. // [item setValue:@"true" forKey:@"wish_exists"];
  11776. // else
  11777. // [item setValue:@"false" forKey:@"wish_exists"];
  11778. //
  11779. // if(closeout==0)
  11780. // [item setValue:@"false" forKey:@"is_closeout"];
  11781. // else
  11782. // [item setValue:@"true" forKey:@"is_closeout"];
  11783. //
  11784. // if(cid==0)
  11785. // [item setValue:@"false" forKey:@"cart_exists"];
  11786. // else
  11787. // [item setValue:@"true" forKey:@"cart_exists"];
  11788. //
  11789. // if (more_color == 0) {
  11790. // [item setObject:@(false) forKey:@"more_color"];
  11791. // } else if (more_color == 1) {
  11792. // [item setObject:@(true) forKey:@"more_color"];
  11793. // }
  11794. //
  11795. // [item addEntriesFromDictionary:imgjson];
  11796. //
  11797. //
  11798. // // [item setValue:nsurl forKey:@"img"];
  11799. // [item setValue:nsname forKey:@"fash_name"];
  11800. // [item setValue:nsdescription forKey:@"description"];
  11801. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11802. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11803. // i++;
  11804. // }
  11805. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11806. // [ret setObject:items forKey:@"items"];
  11807. // sqlite3_finalize(statement);
  11808. // }
  11809. //
  11810. // [iSalesDB close_db:db];
  11811. //
  11812. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11813. //
  11814. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11815. // dispatch_async(dispatch_get_main_queue(), ^{
  11816. //
  11817. // result(ret);
  11818. // });
  11819. //
  11820. // });
  11821. //
  11822. //
  11823. //
  11824. //}
  11825. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11826. //{
  11827. // [self itemsearch:params limited:YES completionHandler:result];
  11828. //}
  11829. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11830. {
  11831. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11832. NSString* offline_command=params[@"offline_Command"];
  11833. NSMutableDictionary* ret=nil;
  11834. if([offline_command isEqualToString:@"model_NIYMAL"])
  11835. {
  11836. NSString* category = params[@"category"];
  11837. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11838. }
  11839. dispatch_async(dispatch_get_main_queue(), ^{
  11840. result(ret);
  11841. });
  11842. });
  11843. }
  11844. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11845. {
  11846. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11847. int sort = [[params objectForKey:@"sort"] intValue];
  11848. NSString *sort_str = @"";
  11849. switch (sort) {
  11850. case 0:{
  11851. sort_str = @"order by w.modify_time desc";
  11852. }
  11853. break;
  11854. case 1:{
  11855. sort_str = @"order by w.modify_time asc";
  11856. }
  11857. break;
  11858. case 2:{
  11859. sort_str = @"order by m.name asc";
  11860. }
  11861. break;
  11862. case 3:{
  11863. sort_str = @"order by m.name desc";
  11864. }
  11865. break;
  11866. case 4:{
  11867. sort_str = @"order by m.description asc";
  11868. }
  11869. break;
  11870. default:
  11871. break;
  11872. }
  11873. // NSString* user = appDelegate.user;
  11874. sqlite3 *db = [iSalesDB get_db];
  11875. // order by w.create_time
  11876. 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];
  11877. // 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];
  11878. sqlite3_stmt * statement;
  11879. NSDate *date1 = [NSDate date];
  11880. // NSDate *date2 = nil;
  11881. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11882. int count=0;
  11883. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11884. {
  11885. while (sqlite3_step(statement) == SQLITE_ROW)
  11886. {
  11887. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11888. int product_id = sqlite3_column_double(statement, 0);
  11889. char *description = (char*)sqlite3_column_text(statement, 1);
  11890. if(description==nil)
  11891. description= "";
  11892. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11893. int item_id = sqlite3_column_double(statement, 2);
  11894. NSDate *date_image = [NSDate date];
  11895. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11896. printf("image : ");
  11897. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11898. // char *url = (char*)sqlite3_column_text(statement, 3);
  11899. // if(url==nil)
  11900. // url="";
  11901. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11902. int qty = sqlite3_column_int(statement, 3);
  11903. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11904. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11905. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11906. item[@"description"]= nsdescription;
  11907. item[@"img"]= nsurl;
  11908. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11909. count++;
  11910. }
  11911. printf("total time:");
  11912. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11913. ret[@"count"]= [NSNumber numberWithInt:count];
  11914. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11915. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11916. ret[@"result"]= [NSNumber numberWithInt:2];
  11917. sqlite3_finalize(statement);
  11918. }
  11919. [iSalesDB close_db:db];
  11920. dispatch_async(dispatch_get_main_queue(), ^{
  11921. UIApplication * app = [UIApplication sharedApplication];
  11922. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11923. appDelegate.wish_count =count;
  11924. [appDelegate update_count_mark];
  11925. result(ret);
  11926. });
  11927. });
  11928. }
  11929. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11930. {
  11931. // UIApplication * app = [UIApplication sharedApplication];
  11932. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11933. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11934. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11935. sqlite3 *db = [iSalesDB get_db];
  11936. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11937. NSString* product_id=params[@"product_id"];
  11938. NSString *item_count_str = params[@"item_count"];
  11939. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11940. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11941. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11942. // NSString *sql = @"";
  11943. for(int i=0;i<arr.count;i++)
  11944. {
  11945. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11946. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11947. __block int cart_count = 0;
  11948. if (!item_count_str) {
  11949. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11950. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11951. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11952. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11953. cart_count = [[model_set_components lastObject] intValue];
  11954. }];
  11955. }
  11956. if(count==0)
  11957. {
  11958. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11959. sqlite3_stmt *stmt;
  11960. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11961. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11962. if (item_count_arr) {
  11963. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11964. } else {
  11965. sqlite3_bind_int(stmt,2,cart_count);
  11966. }
  11967. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11968. [iSalesDB execSql:@"ROLLBACK" db:db];
  11969. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11970. [iSalesDB close_db:db];
  11971. DebugLog(@"add to wishlist error");
  11972. dispatch_async(dispatch_get_main_queue(), ^{
  11973. result(ret);
  11974. });
  11975. }
  11976. } else {
  11977. int qty = 0;
  11978. if (item_count_arr) {
  11979. qty = [item_count_arr[i] intValue];
  11980. } else {
  11981. qty = cart_count;
  11982. }
  11983. 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]];
  11984. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11985. [iSalesDB execSql:@"ROLLBACK" db:db];
  11986. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11987. [iSalesDB close_db:db];
  11988. DebugLog(@"add to wishlist error");
  11989. dispatch_async(dispatch_get_main_queue(), ^{
  11990. result(ret);
  11991. });
  11992. }
  11993. }
  11994. }
  11995. // [iSalesDB execSql:sql db:db];
  11996. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11997. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11998. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11999. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12000. [iSalesDB close_db:db];
  12001. ret[@"result"]= [NSNumber numberWithInt:2];
  12002. dispatch_async(dispatch_get_main_queue(), ^{
  12003. UIApplication * app = [UIApplication sharedApplication];
  12004. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12005. appDelegate.wish_count =count;
  12006. [appDelegate update_count_mark];
  12007. result(ret);
  12008. });
  12009. });
  12010. }
  12011. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12012. {
  12013. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12014. bool bLogin = appDelegate.bLogin;
  12015. __block NSString* contact_id = appDelegate.contact_id;
  12016. __block NSString* user = appDelegate.user;
  12017. __block NSString* order_code = appDelegate.order_code;
  12018. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12019. NSString* model_name = [params valueForKey:@"product_name"];
  12020. NSString* product_id = [params valueForKey:@"product_id"];
  12021. NSString* upc_code = [params valueForKey:@"upc_code"];
  12022. NSString* category = [params valueForKey:@"category"];
  12023. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12024. if(category==nil) {
  12025. category = default_category_id;
  12026. } else {
  12027. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12028. // 参数重有多个category id
  12029. if (arr_0.count > 1) {
  12030. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12031. // 取交集
  12032. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12033. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12034. [set_0 intersectSet:set_1];
  12035. if (set_0.count == 1) {
  12036. category = (NSString *)[set_0 anyObject];
  12037. } else {
  12038. if ([set_0 containsObject:default_category_id]) {
  12039. category = default_category_id;
  12040. } else {
  12041. category = (NSString *)[set_0 anyObject];
  12042. }
  12043. }
  12044. }
  12045. }
  12046. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12047. sqlite3 *db = [iSalesDB get_db];
  12048. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12049. NSString *sqlQuery = nil;
  12050. if(product_id==nil)
  12051. if(model_name==nil)
  12052. {
  12053. 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='%@';
  12054. }
  12055. else
  12056. {
  12057. 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='%@';
  12058. }
  12059. else
  12060. 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=%@;
  12061. sqlite3_stmt * statement;
  12062. [ret setValue:@"2" forKey:@"result"];
  12063. [ret setValue:@"3" forKey:@"detail_section_count"];
  12064. // int count=0;
  12065. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12066. {
  12067. if (sqlite3_step(statement) == SQLITE_ROW)
  12068. {
  12069. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12070. char *name = (char*)sqlite3_column_text(statement, 0);
  12071. if(name==nil)
  12072. name="";
  12073. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12074. char *description = (char*)sqlite3_column_text(statement, 1);
  12075. if(description==nil)
  12076. description="";
  12077. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12078. int product_id = sqlite3_column_int(statement, 2);
  12079. char *color = (char*)sqlite3_column_text(statement, 3);
  12080. if(color==nil)
  12081. color="";
  12082. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12083. //
  12084. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12085. // if(legcolor==nil)
  12086. // legcolor="";
  12087. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12088. //
  12089. //
  12090. int availability = sqlite3_column_int(statement, 5);
  12091. //
  12092. int incoming_stock = sqlite3_column_int(statement, 6);
  12093. char *demension = (char*)sqlite3_column_text(statement, 7);
  12094. if(demension==nil)
  12095. demension="";
  12096. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12097. // ,,,,,,,,,
  12098. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12099. if(seat_height==nil)
  12100. seat_height="";
  12101. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12102. char *material = (char*)sqlite3_column_text(statement, 9);
  12103. if(material==nil)
  12104. material="";
  12105. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12106. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12107. if(box_dim==nil)
  12108. box_dim="";
  12109. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12110. char *volume = (char*)sqlite3_column_text(statement, 11);
  12111. if(volume==nil)
  12112. volume="";
  12113. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12114. double weight = sqlite3_column_double(statement, 12);
  12115. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12116. if(model_set==nil)
  12117. model_set="";
  12118. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12119. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12120. if(load_ability==nil)
  12121. load_ability="";
  12122. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12123. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12124. // if(default_category==nil)
  12125. // default_category="";
  12126. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12127. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12128. if(fabric_content==nil)
  12129. fabric_content="";
  12130. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12131. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12132. if(assembling==nil)
  12133. assembling="";
  12134. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12135. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12136. if(made_in==nil)
  12137. made_in="";
  12138. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12139. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12140. if(special_remarks==nil)
  12141. special_remarks="";
  12142. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12143. int stockUcom = sqlite3_column_double(statement, 20);
  12144. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12145. if(product_group==nil)
  12146. product_group="";
  12147. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12148. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12149. // if(fashion_selector==nil)
  12150. // fashion_selector="";
  12151. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12152. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12153. if(selector_field==nil)
  12154. selector_field="";
  12155. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12156. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12157. if(property_field==nil)
  12158. property_field="";
  12159. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12160. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12161. if(packaging==nil)
  12162. packaging="";
  12163. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12164. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12165. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12166. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12167. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12168. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12169. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12170. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12171. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12172. NSString* Availability=nil;
  12173. if(availability>0)
  12174. Availability=[NSString stringWithFormat:@"%d",availability];
  12175. else
  12176. Availability = @"Out of Stock";
  12177. [img_section setValue:Availability forKey:@"Availability"];
  12178. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12179. char *eta = (char*)sqlite3_column_text(statement, 25);
  12180. if(eta==nil)
  12181. eta="";
  12182. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12183. if ([nseta isEqualToString:@"null"]) {
  12184. nseta = @"";
  12185. }
  12186. if (availability <= 0) {
  12187. [img_section setValue:nseta forKey:@"ETA"];
  12188. }
  12189. int item_id = sqlite3_column_int(statement, 26);
  12190. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12191. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12192. NSString* Price=nil;
  12193. if(bLogin==false)
  12194. Price=@"Must Sign in.";
  12195. else
  12196. {
  12197. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12198. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12199. if(price==nil)
  12200. Price=@"No Price.";
  12201. else
  12202. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12203. }
  12204. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12205. [img_section setValue:Price forKey:@"price"];
  12206. [img_section setValue:nsname forKey:@"model_name"];
  12207. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12208. if (order_code) { // 离线order code即so#
  12209. 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];
  12210. __block int cartQTY = 0;
  12211. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12212. cartQTY = sqlite3_column_int(stmt, 0);
  12213. }];
  12214. if (cartQTY > 0) {
  12215. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12216. }
  12217. }
  12218. [ret setObject:img_section forKey:@"img_section"];
  12219. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12220. int detail0_item_count=0;
  12221. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12222. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12223. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12224. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12225. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12226. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12227. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12228. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12229. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12230. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12231. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12232. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12233. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12234. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12235. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12236. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12237. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12238. {
  12239. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12240. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12241. }
  12242. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12243. [detail0_section setValue:@"kv" forKey:@"type"];
  12244. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12245. [ret setObject:detail0_section forKey:@"detail_0"];
  12246. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12247. // [detail1_section setValue:@"detail" forKey:@"target"];
  12248. // [detail1_section setValue:@"popup" forKey:@"action"];
  12249. // [detail1_section setValue:@"content" forKey:@"type"];
  12250. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12251. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12252. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12253. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12254. [ret setObject:detail1_section forKey:@"detail_1"];
  12255. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12256. [detail2_section setValue:@"detail" forKey:@"target"];
  12257. [detail2_section setValue:@"popup" forKey:@"action"];
  12258. [detail2_section setValue:@"content" forKey:@"type"];
  12259. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12260. [detail2_section setValue:@"true" forKey:@"single_row"];
  12261. [detail2_section setValue:@"local" forKey:@"data"];
  12262. [ret setObject:detail2_section forKey:@"detail_2"];
  12263. }
  12264. sqlite3_finalize(statement);
  12265. }
  12266. else
  12267. {
  12268. [ret setValue:@"8" forKey:@"result"];
  12269. }
  12270. // DebugLog(@"count:%d",count);
  12271. [iSalesDB close_db:db];
  12272. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12273. dispatch_async(dispatch_get_main_queue(), ^{
  12274. result(ret);
  12275. });
  12276. });
  12277. }
  12278. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12279. //{
  12280. //
  12281. // UIApplication * app = [UIApplication sharedApplication];
  12282. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12283. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12284. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12285. // sqlite3 *db = [iSalesDB get_db];
  12286. // NSString* collectId=params[@"collectId"];
  12287. //
  12288. //
  12289. //
  12290. //
  12291. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12292. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12293. // [iSalesDB execSql:sqlQuery db:db];
  12294. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12295. // [iSalesDB close_db:db];
  12296. //
  12297. //
  12298. // ret[@"result"]= [NSNumber numberWithInt:2];
  12299. // dispatch_async(dispatch_get_main_queue(), ^{
  12300. //
  12301. // appDelegate.wish_count =count;
  12302. //
  12303. // [appDelegate update_count_mark];
  12304. // result(ret);
  12305. // });
  12306. //
  12307. // });
  12308. //}
  12309. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12310. {
  12311. UIApplication * app = [UIApplication sharedApplication];
  12312. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12313. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12314. [iSalesDB disable_trigger];
  12315. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12316. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12317. [iSalesDB enable_trigger];
  12318. dispatch_async(dispatch_get_main_queue(), ^{
  12319. //
  12320. // NSString* user = [params valueForKey:@"user"];
  12321. //
  12322. // NSString* password = [params valueForKey:@"password"];
  12323. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12324. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12325. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12326. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12327. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12328. [appDelegate update_count_mark];
  12329. appDelegate.can_show_price =false;
  12330. appDelegate.can_see_price =false;
  12331. appDelegate.can_create_portfolio =false;
  12332. appDelegate.can_create_order =false;
  12333. appDelegate.can_cancel_order =false;
  12334. appDelegate.can_set_cart_price =false;
  12335. appDelegate.can_delete_order =false;
  12336. appDelegate.can_submit_order =false;
  12337. appDelegate.can_set_tearsheet_price =false;
  12338. appDelegate.can_update_contact_info = false;
  12339. appDelegate.save_order_logout = false;
  12340. appDelegate.submit_order_logout = false;
  12341. appDelegate.alert_sold_in_quantities = false;
  12342. appDelegate.ipad_perm =nil ;
  12343. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12344. appDelegate.OrderFilter= nil;
  12345. [appDelegate SetSo:nil];
  12346. [appDelegate set_main_button_panel];
  12347. result(ret);
  12348. });
  12349. });
  12350. }
  12351. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12352. {
  12353. // iSalesDB.
  12354. // UIApplication * app = [UIApplication sharedApplication];
  12355. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12356. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12357. [iSalesDB disable_trigger];
  12358. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12359. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12360. [iSalesDB enable_trigger];
  12361. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12362. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12363. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12364. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12365. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12366. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12367. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12368. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12369. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12370. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12371. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12372. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12373. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12374. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12375. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12376. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12377. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12378. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12379. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12380. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12381. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12382. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12383. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12384. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12385. [arr_name addObject:customer_first_name];
  12386. [arr_name addObject:customer_last_name];
  12387. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12388. // default ship from
  12389. 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';";
  12390. __block NSString *cid = @"";
  12391. __block NSString *name = @"";
  12392. __block NSString *ext = @"";
  12393. __block NSString *contact = @"";
  12394. __block NSString *email = @"";
  12395. __block NSString *fax = @"";
  12396. __block NSString *phone = @"";
  12397. sqlite3 *db = [iSalesDB get_db];
  12398. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12399. cid = [self textAtColumn:0 statement:statment];
  12400. name = [self textAtColumn:1 statement:statment];
  12401. ext = [self textAtColumn:2 statement:statment];
  12402. contact = [self textAtColumn:3 statement:statment];
  12403. email = [self textAtColumn:4 statement:statment];
  12404. fax = [self textAtColumn:5 statement:statment];
  12405. phone = [self textAtColumn:6 statement:statment];
  12406. }];
  12407. NSString* so_id = [self get_offline_soid:db];
  12408. if(so_id==nil)
  12409. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12410. 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];
  12411. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12412. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12413. //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'
  12414. //soId
  12415. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12416. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12417. sqlite3_stmt * statement;
  12418. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12419. {
  12420. if (sqlite3_step(statement) == SQLITE_ROW)
  12421. {
  12422. // char *name = (char*)sqlite3_column_text(statement, 1);
  12423. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12424. //ret = sqlite3_column_int(statement, 0);
  12425. char *soId = (char*)sqlite3_column_text(statement, 0);
  12426. if(soId==nil)
  12427. soId="";
  12428. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12429. [ret setValue:nssoId forKey:@"soId"];
  12430. [ret setValue:nssoId forKey:@"orderCode"];
  12431. }
  12432. sqlite3_finalize(statement);
  12433. }
  12434. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12435. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12436. [iSalesDB close_db:db];
  12437. dispatch_async(dispatch_get_main_queue(), ^{
  12438. result(ret);
  12439. });
  12440. });
  12441. }
  12442. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12443. {
  12444. UIApplication * app = [UIApplication sharedApplication];
  12445. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12446. bool can_create_backorder= appDelegate.can_create_backorder;
  12447. NSString* user = appDelegate.user;
  12448. NSString* contact_id = appDelegate.contact_id;
  12449. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12450. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12451. sqlite3 *db = [iSalesDB get_db];
  12452. NSString* orderCode=params[@"orderCode"];
  12453. int sort = [[params objectForKey:@"sort"] intValue];
  12454. NSString *sort_str = @"";
  12455. switch (sort) {
  12456. case 0:{
  12457. sort_str = @"order by c.modify_time desc";
  12458. }
  12459. break;
  12460. case 1:{
  12461. sort_str = @"order by c.modify_time asc";
  12462. }
  12463. break;
  12464. case 2:{
  12465. sort_str = @"order by m.name asc";
  12466. }
  12467. break;
  12468. case 3:{
  12469. sort_str = @"order by m.name desc";
  12470. }
  12471. break;
  12472. case 4:{
  12473. sort_str = @"order by m.description asc";
  12474. }
  12475. break;
  12476. default:
  12477. break;
  12478. }
  12479. 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 ];
  12480. // 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 ];
  12481. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12482. DebugLog(@"offline_login sql:%@",sqlQuery);
  12483. sqlite3_stmt * statement;
  12484. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12485. NSDate *date1 = [NSDate date];
  12486. int count=0;
  12487. int cart_count=0;
  12488. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12489. if ( dbresult== SQLITE_OK)
  12490. {
  12491. while (sqlite3_step(statement) == SQLITE_ROW)
  12492. {
  12493. // NSDate *row_date = [NSDate date];
  12494. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12495. int product_id = sqlite3_column_int(statement, 0);
  12496. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12497. int item_id = sqlite3_column_int(statement, 7);
  12498. NSString* Price=nil;
  12499. if(str_price==nil)
  12500. {
  12501. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12502. // NSDate *price_date = [NSDate date];
  12503. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12504. // DebugLog(@"price time interval");
  12505. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12506. if(price==nil)
  12507. Price=@"No Price.";
  12508. else
  12509. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12510. }
  12511. else
  12512. {
  12513. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12514. }
  12515. double discount = sqlite3_column_double(statement, 2);
  12516. int item_count = sqlite3_column_int(statement, 3);
  12517. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12518. NSString *nsline_note=nil;
  12519. if(line_note!=nil)
  12520. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12521. char *name = (char*)sqlite3_column_text(statement, 5);
  12522. NSString *nsname=nil;
  12523. if(name!=nil)
  12524. nsname= [[NSString alloc]initWithUTF8String:name];
  12525. char *description = (char*)sqlite3_column_text(statement, 6);
  12526. NSString *nsdescription=nil;
  12527. if(description!=nil)
  12528. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12529. int stockUom = sqlite3_column_int(statement, 8);
  12530. int _id = sqlite3_column_int(statement, 9);
  12531. int availability = sqlite3_column_int(statement, 10);
  12532. // NSDate *subtotal_date = [NSDate date];
  12533. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12534. // DebugLog(@"subtotal_date time interval");
  12535. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12536. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12537. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12538. int carton=[bsubtotaljson[@"carton"] intValue];
  12539. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12540. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12541. // NSDate *img_date = [NSDate date];
  12542. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12543. // DebugLog(@"img_date time interval");
  12544. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12545. itemjson[@"model"]=nsname;
  12546. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12547. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12548. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12549. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12550. itemjson[@"check"]=@"true";
  12551. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12552. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12553. itemjson[@"unit_price"]=Price;
  12554. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12555. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12556. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12557. itemjson[@"note"]=nsline_note;
  12558. if (!can_create_backorder) {
  12559. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12560. }
  12561. // NSDate *date2 = [NSDate date];
  12562. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12563. // DebugLog(@"model_bundle time interval");
  12564. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12565. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12566. count++;
  12567. // DebugLog(@"row time interval");
  12568. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12569. }
  12570. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12571. sqlite3_finalize(statement);
  12572. }
  12573. DebugLog(@"request cart total time interval");
  12574. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12575. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12576. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12577. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12578. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12579. ret[@"count"]=[NSNumber numberWithInt:count ];
  12580. ret[@"mode"]=@"Regular Mode";
  12581. [iSalesDB close_db:db];
  12582. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12583. DebugLog(@"general notes :%@",general_note);
  12584. ret[@"general_note"]= general_note;
  12585. dispatch_async(dispatch_get_main_queue(), ^{
  12586. result(ret);
  12587. });
  12588. });
  12589. }
  12590. @end