OLDataProvider.m 711 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859
  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. #import "RADataProvider.h"
  26. @interface OLDataProvider ()
  27. @end
  28. @implementation OLDataProvider
  29. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  30. {
  31. 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];
  32. NSString* ret=@"No Price.";
  33. sqlite3_stmt * statement;
  34. // int count=0;
  35. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  36. {
  37. if (sqlite3_step(statement) == SQLITE_ROW)
  38. {
  39. char *price = (char*)sqlite3_column_text(statement, 0);
  40. if(price==nil)
  41. price="";
  42. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  43. double dp= [nsprice doubleValue];
  44. ret=[NSString stringWithFormat:@"%.2f",dp];
  45. }
  46. sqlite3_finalize(statement);
  47. }
  48. return ret;
  49. // // [iSalesDB close_db:db];
  50. //
  51. // return nil;
  52. //// if(dprice==DBL_MAX)
  53. //// ret= nil;
  54. //// else
  55. //// ret= [NSNumber numberWithDouble:dprice];
  56. //// return ret;
  57. }
  58. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  59. {
  60. NSString* ret= nil;
  61. NSString *sqlQuery = nil;
  62. // 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
  63. 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];
  64. sqlite3_stmt * statement;
  65. // int count=0;
  66. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  67. {
  68. if (sqlite3_step(statement) == SQLITE_ROW)
  69. {
  70. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  71. if(imgurl==nil)
  72. imgurl="";
  73. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  74. ret=nsimgurl;
  75. }
  76. sqlite3_finalize(statement);
  77. }
  78. else
  79. {
  80. ret=@"";
  81. }
  82. // [iSalesDB close_db:db];
  83. // DebugLog(@"data string: %@",ret );
  84. return ret;
  85. }
  86. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  87. {
  88. // assert(params[@"contact_id"]!=nil);
  89. assert(params[@"user"]!=nil);
  90. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  91. //生成portfolio pdf需要的数据
  92. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  93. data[@"npd_url"]=COMPANY_WEB;
  94. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  95. [formatter setDateFormat:@"MMMM yyyy"];
  96. NSString* date = [formatter stringFromDate:[NSDate date]];
  97. data[@"create_month"]=date;
  98. data[@"company_name"]=COMPANY_FULL_NAME;
  99. data[@"catalog_name"]=params[@"catalog_name"];
  100. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  101. data[@"TOTAL_PAGE"]=0;
  102. data[@"CURRENT_PAGE"]=0;
  103. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  104. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  105. bool show_stockout=[params[@"show_stockout"] boolValue];
  106. NSString *sql = nil;
  107. // v1.90 more color
  108. NSString *group_by = @"";
  109. if(values==nil)
  110. {
  111. if (show_group_by) {
  112. group_by = @"group by b.product_group ";
  113. }
  114. 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];
  115. }
  116. else
  117. {
  118. if (show_group_by) {
  119. group_by = @"group by product_group ";
  120. }
  121. 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];
  122. }
  123. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  124. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  125. sqlite3 *db = [iSalesDB get_db];
  126. // NSDictionary *resultDic =
  127. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  128. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  129. int product_id = sqlite3_column_int(stmt, 0);
  130. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  131. NSString *name = [self textAtColumn:1 statement:stmt];
  132. NSString *description = [self textAtColumn:2 statement:stmt];
  133. double price = sqlite3_column_double(stmt, 3);
  134. NSString *s_price = [self textAtColumn:3 statement:stmt];
  135. double discount = sqlite3_column_double(stmt,4);
  136. int qty = sqlite3_column_int(stmt, 5);
  137. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  138. int is_percentage = sqlite3_column_int(stmt, 6);
  139. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  140. int item_id = sqlite3_column_int(stmt, 7);
  141. // int fashion_id = sqlite3_column_int(stmt, 8);
  142. NSString *line_note = [self textAtColumn:9 statement:stmt];
  143. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  144. double percent = sqlite3_column_double(stmt, 10);
  145. // int portfolio_id = sqlite3_column_int(stmt, 11);
  146. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  147. int availability = sqlite3_column_int(stmt, 12);
  148. NSString *color = [self textAtColumn:13 statement:stmt];
  149. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  150. NSString *demension = [self textAtColumn:15 statement:stmt];
  151. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  152. NSString *material = [self textAtColumn:17 statement:stmt];
  153. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  154. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  155. double volume = sqlite3_column_double(stmt, 19);
  156. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  157. double weight = sqlite3_column_double(stmt, 20);
  158. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  159. int model_set = sqlite3_column_int(stmt, 21);
  160. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  161. double load_ability = sqlite3_column_double(stmt, 22);
  162. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  163. NSString *assembling = [self textAtColumn:24 statement:stmt];
  164. NSString *made_in = [self textAtColumn:25 statement:stmt];
  165. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  166. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  167. float bprice=0;
  168. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  169. for(int b=0;b< [bundle[@"count"] intValue];b++)
  170. {
  171. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  172. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  173. }
  174. if(gprice!=nil)
  175. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  176. if(s_percent.length==0||is_percentage==0)
  177. {
  178. percent=100.0;
  179. if([s_qty isEqualToString:@"null"])
  180. qty=availability;
  181. }
  182. else
  183. {
  184. qty=availability;
  185. }
  186. qty=qty*percent/100+0.5;
  187. for(int i=0;i<[values[@"count"] intValue];i++)
  188. {
  189. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  190. if([valueitem[@"product_id"] intValue]==product_id)
  191. {
  192. line_note=valueitem[@"line_note"];
  193. qty=[valueitem[@"available_qty"] intValue];
  194. if([[valueitem allKeys] containsObject:@"available_qty"])
  195. s_qty=[NSString stringWithFormat:@"%d",qty];
  196. else
  197. s_qty=@"null";
  198. if(valueitem[@"regular_price"]!=nil)
  199. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  200. s_price=valueitem[@"special_price"];
  201. price=[valueitem[@"special_price"] floatValue];
  202. discount=0;
  203. // v1.90
  204. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  205. }
  206. }
  207. if(is_percentage==0&&qty==0&&!show_stockout)
  208. {
  209. return;
  210. }
  211. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  212. {
  213. return;
  214. }
  215. NSString* set_price=@"";
  216. if([params[@"entered_price"] boolValue])
  217. {
  218. if (s_price==nil ) {
  219. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  220. }
  221. else
  222. {
  223. if(price*(1-discount/100.0)!=gprice.floatValue)
  224. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  225. }
  226. }
  227. NSString* get_price=@"";
  228. {
  229. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  230. // DebugLog(@"price time interval");
  231. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  232. if(gprice==nil)
  233. get_price=@"Price:No Price.";
  234. else
  235. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  236. }
  237. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  238. // if ([qty_null isEqualToString:@"null"]) {
  239. // qty=availability;
  240. // }
  241. //
  242. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  243. // if ([qty_percent_null isEqualToString:@"null"]) {
  244. // percentage=1;
  245. // }
  246. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  247. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  248. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  249. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  250. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  251. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  252. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  253. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  254. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  255. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  256. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  257. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  258. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  259. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  260. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  261. if([params[@"availability"] boolValue]==false)
  262. str_availability=@"";
  263. else
  264. {
  265. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  266. str_availability=@"<b>Availability:</b> In Production";
  267. }
  268. if([params[@"color"] boolValue]==false || color.length==0 )
  269. str_color=@"";
  270. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  271. str_model_set=@"";
  272. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  273. str_legcolor=@"";
  274. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  275. str_demension=@"";
  276. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  277. str_seat_height=@"";
  278. if([params[@"material"] boolValue]==false || material.length==0 )
  279. str_meterial=@"";
  280. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  281. str_box_dim=@"";
  282. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  283. str_volume=@"";
  284. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  285. str_weight=@"";
  286. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  287. str_load_ability=@"";
  288. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  289. str_fabric_content=@"";
  290. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  291. str_assembling=@"";
  292. if([params[@"made"] boolValue]==false || made_in.length==0 )
  293. str_made_in=@"";
  294. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  295. str_line_note=@"";
  296. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  297. [arr_detail addObject:str_availability];
  298. [arr_detail addObject:str_color];
  299. [arr_detail addObject:str_model_set];
  300. [arr_detail addObject:str_legcolor];
  301. [arr_detail addObject:str_demension];
  302. [arr_detail addObject:str_seat_height];
  303. [arr_detail addObject:str_meterial];
  304. [arr_detail addObject:str_box_dim];
  305. [arr_detail addObject:str_volume];
  306. [arr_detail addObject:str_weight];
  307. [arr_detail addObject:str_load_ability];
  308. [arr_detail addObject:str_fabric_content];
  309. [arr_detail addObject:str_assembling];
  310. [arr_detail addObject:str_made_in];
  311. [arr_detail addObject:str_line_note];
  312. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  313. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  314. //model image;
  315. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  316. // qr image
  317. NSString* qrpath=nil;
  318. if([params[@"show_barcode"] boolValue])
  319. {
  320. NSString* temp = NSTemporaryDirectory();
  321. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  322. qrpath=[temp stringByAppendingPathComponent:filename];
  323. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  324. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  325. }
  326. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  327. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  328. item[@"img"]=img_path;
  329. item[@"img_qr"]=qrpath;
  330. item[@"special_price"]=set_price;
  331. item[@"price"]=get_price;
  332. item[@"name"]=name;
  333. item[@"description"]=description;
  334. item[@"detail"]=detail;
  335. item[@"img"]=img_path;
  336. if (show_group_by && more_color_int != 0) {
  337. item[@"more_color"] = more_color_path;
  338. }
  339. // @{
  340. // //@"linenotes": line_note,
  341. //// @"product_id": product_id_string,
  342. //// @"available_qty": @(qty),
  343. //// @"available_percent" : @(percent),
  344. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  345. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  346. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"": ,
  352. // @"": ,
  353. // @"":
  354. // }.mutableCopy;
  355. // if (percentage) {
  356. // [item removeObjectForKey:@"available_qty"];
  357. // } else {
  358. // [item removeObjectForKey:@"available_percent"];
  359. // }
  360. //
  361. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  362. // if ([qty_null isEqualToString:@"null"]) {
  363. // [item removeObjectForKey:@"available_qty"];
  364. // }
  365. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  366. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  367. }];
  368. [iSalesDB close_db:db];
  369. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  370. // [dic setValue:@"" forKey:@"email_content"];
  371. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  372. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  373. //
  374. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  375. // cell[@"count"]=[NSNumber numberWithInt:10];
  376. grid[@"cell0"]=dic;
  377. data[@"grid0"]=grid;
  378. return data;
  379. }
  380. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  381. {
  382. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  383. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  384. NSString *cache_folder=[paths objectAtIndex:0];
  385. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  386. BOOL bdir=NO;
  387. NSFileManager* fileManager = [NSFileManager defaultManager];
  388. if(PDF_DEBUG)
  389. {
  390. NSData *data = [NSData dataWithContentsOfFile:default_path];
  391. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  392. return ret;
  393. }
  394. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  395. {
  396. NSError * error=nil;
  397. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  398. {
  399. return nil;
  400. }
  401. }
  402. NSData *data = [NSData dataWithContentsOfFile:template_path];
  403. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  404. return ret;
  405. }
  406. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  407. {
  408. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  409. 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 ];
  410. DebugLog(@"offline_login sql:%@",sqlQuery);
  411. sqlite3_stmt * statement;
  412. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  413. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  414. {
  415. if (sqlite3_step(statement) == SQLITE_ROW)
  416. {
  417. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  418. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  419. // int can_show_price = sqlite3_column_int(statement, 0);
  420. // int can_see_price = sqlite3_column_int(statement, 1);
  421. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  422. // if(contact_id==nil)
  423. // contact_id="";
  424. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  425. // int user_type = sqlite3_column_int(statement, 3);
  426. // int can_cancel_order = sqlite3_column_int(statement, 4);
  427. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  428. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  429. // int can_delete_order = sqlite3_column_int(statement, 7);
  430. // int can_submit_order = sqlite3_column_int(statement, 8);
  431. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  432. // int can_create_order = sqlite3_column_int(statement, 10);
  433. // char *mode = (char*)sqlite3_column_text(statement, 11);
  434. // if(mode==nil)
  435. // mode="";
  436. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  437. // char *username = (char*)sqlite3_column_text(statement, 12);
  438. // if(username==nil)
  439. // username="";
  440. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  441. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  442. char *first_name = (char*)sqlite3_column_text(statement, 14);
  443. if(first_name==nil)
  444. first_name="";
  445. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  446. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  447. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  448. // [header setValue:nscontact_id forKey:@"contact_id"];
  449. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  450. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  451. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  452. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  453. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  454. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  455. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  456. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  457. //
  458. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  459. //
  460. // [header setValue:nsusername forKey:@"username"];
  461. //
  462. // NSError* error=nil;
  463. //
  464. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  465. // [header setValue:statusFilter forKey:@"statusFilter"];
  466. //
  467. //
  468. //
  469. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  470. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  471. //
  472. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  473. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  474. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  475. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  476. //
  477. // [ret setObject:header forKey:@"header"];
  478. [ret setValue:nsfirst_name forKey:@"first_name"];
  479. }
  480. sqlite3_finalize(statement);
  481. }
  482. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  483. return ret;
  484. }
  485. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  486. {
  487. assert(user!=nil);
  488. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  489. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  490. data[@"npd_url"]=COMPANY_WEB;
  491. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  492. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  493. NSString* date = [formatter stringFromDate:[NSDate date]];
  494. data[@"print_date"]=date;
  495. data[@"company_name"]=COMPANY_FULL_NAME;
  496. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  497. data[@"TOTAL_PAGE"]=0;
  498. data[@"CURRENT_PAGE"]=0;
  499. // [formatter setDateFormat:@"MM/dd/yyyy"];
  500. // date = [formatter stringFromDate:[NSDate date]];
  501. // data[@"creat_date"]=date;
  502. NSString* temp = NSTemporaryDirectory();
  503. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  504. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  505. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  506. // bool bwrite=
  507. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  508. data[@"img_barcode"]=qrpath;
  509. data[@"so_id"]=order_code;
  510. data[@"company"]=COMPANY_FULL_NAME;
  511. // 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];
  512. 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];
  513. sqlite3 *db = [iSalesDB get_db];
  514. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  515. sqlite3_stmt * statement;
  516. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  517. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  518. row0[@"count"]=[NSNumber numberWithInt:1];
  519. datasource[@"row0"]=row0;
  520. NSString* print_status=@"";
  521. NSString* order_type=@"Offline Quote";
  522. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  523. {
  524. if (sqlite3_step(statement) == SQLITE_ROW)
  525. {
  526. int erpOrderStatus = sqlite3_column_int(statement, 0);
  527. int status = sqlite3_column_int(statement, 26);
  528. if(status==2)
  529. {
  530. switch (erpOrderStatus) {
  531. case 10:
  532. // order_type=@"Quoted Order";
  533. print_status=@"Quote";
  534. break;
  535. case 11:
  536. print_status=@"Booked";
  537. break;
  538. case 12:
  539. print_status=@"Picked";
  540. break;
  541. case 13:
  542. print_status=@"Shipped";
  543. break;
  544. case 14:
  545. print_status=@"Closed";
  546. break;
  547. case 15:
  548. print_status=@"Canceled";
  549. break;
  550. case 16:
  551. print_status=@"All";
  552. break;
  553. case 18:
  554. print_status=@"Processing";
  555. break;
  556. default:
  557. break;
  558. }
  559. }
  560. else{
  561. switch (status) {
  562. case 0:
  563. {
  564. print_status=@"Quote";
  565. break;
  566. }
  567. case 1:
  568. {
  569. print_status=@"Saved";
  570. break;
  571. }
  572. case 3:
  573. {
  574. //canceled
  575. print_status=@"Canceled";
  576. break;
  577. }
  578. default:
  579. break;
  580. }
  581. }
  582. NSString* sender_name = [self textAtColumn:1 statement:statement];
  583. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  584. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  585. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  586. NSString* sender_email = [self textAtColumn:5 statement:statement];
  587. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  588. NSString* billing_name = [self textAtColumn:7 statement:statement];
  589. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  590. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  591. if(billing_phone.length>0)
  592. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  593. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  594. if(billing_fax.length>0)
  595. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  596. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  597. [billto_arr0 addObject:billing_phone];
  598. [billto_arr0 addObject:billing_fax];
  599. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  600. NSString* billing_email = [self textAtColumn:11 statement:statement];
  601. if(billing_email.length>0)
  602. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  603. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  604. if(billing_contact.length>0)
  605. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  606. NSString* receive_name = [self textAtColumn:12 statement:statement];
  607. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  608. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  609. if(receive_phone.length>0)
  610. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  611. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  612. if(receive_fax.length>0)
  613. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  614. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  615. [receive_arr0 addObject:receive_phone];
  616. [receive_arr0 addObject:receive_fax];
  617. NSString* receive_email = [self textAtColumn:16 statement:statement];
  618. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  619. if(receive_email.length>0)
  620. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  621. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  622. if(receive_contact.length>0)
  623. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  624. NSString* poNumber = [self textAtColumn:17 statement:statement];
  625. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  626. NSString* carrier = [self textAtColumn:19 statement:statement];
  627. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  628. NSString* terms = [self textAtColumn:21 statement:statement];
  629. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  630. NSString* port_destination = [self textAtColumn:23 statement:statement];
  631. NSString* etd = [self textAtColumn:24 statement:statement];
  632. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  633. if(sign_picpath.length==0)
  634. sign_picpath=nil;
  635. NSString* create_date = [self textAtColumn:27 statement:statement];
  636. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  637. NSString* general_notes=[self textAtColumn:33 statement:statement];
  638. NSString *customerID = [self textAtColumn:34 statement:statement];
  639. if (customerID.length == 0) {
  640. customerID = nil;
  641. }
  642. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  643. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  644. if(general_notes.length>0)
  645. {
  646. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  647. row2[@"item_0"]=@{@"g_notes":general_notes };
  648. row2[@"count"]=[NSNumber numberWithInt:1];
  649. datasource[@"row2"]=row2;
  650. }
  651. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  652. [billto_arr addObject:billing_ext];
  653. [billto_arr addObject:billing_pf];
  654. [billto_arr addObject:billing_contact];
  655. [billto_arr addObject:billing_email];
  656. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  657. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  658. [receive_arr addObject:receive_ext];
  659. [receive_arr addObject:receive_pf];
  660. [receive_arr addObject:receive_contact];
  661. [receive_arr addObject:receive_email];
  662. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  663. float shipping = sqlite3_column_double(statement, 28);
  664. float lift_gate_value = sqlite3_column_double(statement, 29);
  665. float handling_fee_value = sqlite3_column_double(statement, 30);
  666. data[@"company"]=sender_name;
  667. data[@"shipfrom_addr"]=sender_ext;
  668. data[@"shipfrom_phone"]=sender_phone;
  669. data[@"shipfrom_fax"]=sender_fax;
  670. data[@"shipfrom_email"]=sender_email;
  671. data[@"warehouse_name"]=warehouse_name;
  672. data[@"bill_to_name"]=billing_name;
  673. data[@"bill_to_ext"]=billto;
  674. data[@"ship_to_name"]=receive_name;
  675. data[@"ship_to_ext"]=receive;
  676. data[@"po_no"]=poNumber;
  677. data[@"rep"]=sales_rep;
  678. data[@"e_schdule"]=etd;
  679. data[@"sales_terms"]=sales_terms;
  680. data[@"port_destination"]=port_destination;
  681. data[@"terms"]=terms;
  682. data[@"carrier"]=carrier;
  683. data[@"vendor_no"]=vendor_no;
  684. data[@"customer_sign"]=sign_picpath;
  685. data[@"print_status"]=print_status;
  686. data[@"create_date"]=create_date;
  687. 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];;
  688. sqlite3_stmt * statement_cart;
  689. // NSDate *date1 = [NSDate date];
  690. int count=0;
  691. // int cart_count=0;
  692. float credits=0;
  693. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  694. if ( dbresult== SQLITE_OK)
  695. {
  696. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  697. int row1count=0;
  698. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  699. {
  700. // NSDate *row_date = [NSDate date];
  701. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  702. // int product_id = sqlite3_column_int(statement_cart, 0);
  703. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  704. int item_id = sqlite3_column_int(statement_cart, 7);
  705. double discount = sqlite3_column_double(statement_cart, 2);
  706. NSString* Price=nil;
  707. if(str_price==nil)
  708. {
  709. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  710. // NSDate *price_date = [NSDate date];
  711. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  712. // DebugLog(@"price time interval");
  713. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  714. if(price==nil)
  715. Price=@"No Price.";
  716. else
  717. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  718. }
  719. else
  720. {
  721. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  722. }
  723. float dunitprice=0;
  724. int item_count = sqlite3_column_int(statement_cart, 3);
  725. NSString* Amount=@"";
  726. if(![Price isEqualToString:@"No Price."])
  727. {
  728. dunitprice=[Price floatValue];
  729. dunitprice=dunitprice* (1-discount/100.0);
  730. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  731. credits+=dunitprice*item_count;
  732. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  733. }
  734. else
  735. {
  736. Price=@"";
  737. }
  738. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  739. // NSString *nsline_note=nil;
  740. // if(line_note!=nil)
  741. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  742. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  743. NSString *nsname=nil;
  744. if(name!=nil)
  745. nsname= [[NSString alloc]initWithUTF8String:name];
  746. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  747. NSString *nsdescription=nil;
  748. if(description!=nil)
  749. nsdescription= [[NSString alloc]initWithUTF8String:description];
  750. // int stockUom = sqlite3_column_int(statement_cart, 8);
  751. // int _id = sqlite3_column_int(statement_cart, 9);
  752. // NSDate *subtotal_date = [NSDate date];
  753. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  754. // DebugLog(@"subtotal_date time interval");
  755. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  756. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  757. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  758. // int carton=[bsubtotaljson[@"carton"] intValue];
  759. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  760. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  761. // NSDate *img_date = [NSDate date];
  762. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  763. // DebugLog(@"img_date time interval");
  764. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  765. itemjson[@"model"]=nsname;
  766. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  767. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  768. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  769. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  770. // itemjson[@"check"]=@"true";
  771. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  772. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  773. itemjson[@"unit_price"]=Price;
  774. itemjson[@"amount"]=Amount;
  775. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  776. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  777. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  778. // itemjson[@"note"]=nsline_note;
  779. // NSDate *date2 = [NSDate date];
  780. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  781. // DebugLog(@"model_bundle time interval");
  782. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  783. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  784. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  785. row1count++;
  786. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  787. for(int c=0;c<[combine[@"count"] intValue];c++)
  788. {
  789. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  790. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  791. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  792. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  793. credits+=combine_amount;
  794. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  795. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  796. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  797. row1count++;
  798. }
  799. count++;
  800. // DebugLog(@"row time interval");
  801. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  802. }
  803. row1[@"count"]=[NSNumber numberWithInt:row1count];
  804. datasource[@"row1"]=row1;
  805. data[@"table_model"]=datasource;
  806. sqlite3_finalize(statement_cart);
  807. }
  808. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  809. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  810. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  811. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  812. // if(credits>0)
  813. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  814. // else
  815. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  816. if(shipping>0)
  817. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  818. else
  819. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  820. if(lift_gate_value>0)
  821. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  822. else
  823. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  824. if(handling_fee_value>0)
  825. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  826. else
  827. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  828. float total = credits+shipping+lift_gate_value+handling_fee_value;
  829. if(total>0)
  830. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  831. else
  832. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  833. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  834. fee[@"row0"]=fee_row0;
  835. data[@"table_fee"]=fee;
  836. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  837. // ret[@"sign_url"] = sign_url;
  838. //
  839. // customer_contact = [self textAtColumn:52 statement:statement];
  840. // customer_email = [self textAtColumn:53 statement:statement];
  841. // customer_phone = [self textAtColumn:54 statement:statement];
  842. // customer_fax = [self textAtColumn:55 statement:statement];
  843. //
  844. //
  845. // int offline_edit=sqlite3_column_int(statement, 56);
  846. //
  847. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  848. //
  849. // char *soid = (char*)sqlite3_column_text(statement, 1);
  850. // if(soid==nil)
  851. // soid= "";
  852. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  853. // // so#
  854. // ret[@"so#"] = nssoid;
  855. //
  856. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  857. // if(poNumber==nil)
  858. // poNumber= "";
  859. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  860. //
  861. //
  862. //
  863. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  864. // if(create_time==nil)
  865. // create_time= "";
  866. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  867. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  868. //
  869. // int status = sqlite3_column_int(statement, 4);
  870. // int erpStatus = sqlite3_column_int(statement, 49);
  871. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  872. //
  873. // // status
  874. // if (status > 1 && status != 3) {
  875. // status = erpStatus;
  876. // } else if (status == 3) {
  877. // status = 15;
  878. // }
  879. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  880. // ret[@"order_status"] = nsstatus;
  881. //
  882. //
  883. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  884. // if(company_name==nil)
  885. // company_name= "";
  886. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  887. // // company name
  888. // ret[@"company_name"] = nscompany_name;
  889. //
  890. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  891. // if(customer_contact==nil)
  892. // customer_contact= "";
  893. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  894. //
  895. //
  896. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  897. // if(addr_1==nil)
  898. // addr_1="";
  899. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  900. //
  901. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  902. // if(addr_2==nil)
  903. // addr_2="";
  904. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  905. //
  906. //
  907. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  908. // if(addr_3==nil)
  909. // addr_3="";
  910. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  911. //
  912. //
  913. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  914. // if(addr_4==nil)
  915. // addr_4="";
  916. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  917. //
  918. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  919. // [arr_addr addObject:nsaddr_1];
  920. // [arr_addr addObject:nsaddr_2];
  921. // [arr_addr addObject:nsaddr_3];
  922. // [arr_addr addObject:nsaddr_4];
  923. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  924. //
  925. //
  926. // char *logist = (char*)sqlite3_column_text(statement, 11);
  927. // if(logist==nil)
  928. // logist= "";
  929. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  930. // if (status == -11 || status == 10 || status == 11) {
  931. // nslogist = [self textAtColumn:59 statement:statement];
  932. // };
  933. //
  934. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  935. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  936. // shipping = @"Shipping To Be Quoted";
  937. // } else {
  938. // shippingFee = sqlite3_column_double(statement, 12);
  939. // }
  940. //
  941. // // Shipping
  942. // ret[@"Shipping"] = shipping;
  943. //
  944. // int have_lift_gate = sqlite3_column_int(statement, 17);
  945. // lift_gate = sqlite3_column_double(statement, 13);
  946. // // Liftgate Fee(No loading dock)
  947. // if (!have_lift_gate) {
  948. // lift_gate = 0;
  949. // }
  950. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  951. // if (sqlite3_column_int(statement, 57)) {
  952. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  953. // }
  954. //
  955. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  956. // if(general_notes==nil)
  957. // general_notes= "";
  958. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  959. //
  960. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  961. // if(internal_notes==nil)
  962. // internal_notes= "";
  963. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  964. //
  965. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  966. // if(payment_type==nil)
  967. // payment_type= "";
  968. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  969. //
  970. //
  971. //
  972. // // order info
  973. // orderinfo = [self textFileName:@"order_info.html"];
  974. //
  975. //
  976. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  978. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  979. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  980. //
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  987. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  988. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  989. //
  990. // NSString *payment = nil;
  991. // if([nspayment_type isEqualToString:@"Credit Card"])
  992. // {
  993. // payment = [self textFileName:@"creditcardpayment.html"];
  994. //
  995. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  996. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  997. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  998. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  999. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  1000. // NSString *card_type = [self textAtColumn:42 statement:statement];
  1001. // if (card_type.length > 0) { // 显示星号
  1002. // card_type = @"****";
  1003. // }
  1004. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1005. // if (card_number.length > 0 && card_number.length > 4) {
  1006. // for (int i = 0; i < card_number.length - 4; i++) {
  1007. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1008. // }
  1009. // } else {
  1010. // card_number = @"";
  1011. // }
  1012. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1013. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1014. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1015. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1016. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1017. // card_expiration = @"****";
  1018. // }
  1019. //
  1020. //
  1021. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1022. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1023. //
  1024. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1025. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1026. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1027. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1028. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1029. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1030. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1031. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1032. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1033. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1034. //
  1035. //
  1036. // }
  1037. // else
  1038. // {
  1039. // payment=[self textFileName:@"normalpayment.html"];
  1040. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1041. // }
  1042. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1043. //
  1044. // ret[@"result"]= [NSNumber numberWithInt:2];
  1045. //
  1046. // // more info
  1047. // moreInfo = [self textFileName:@"more_info.html"];
  1048. //
  1049. //
  1050. // // ShipToCompany_or_&nbsp
  1051. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1053. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1055. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1056. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1057. //
  1058. //
  1059. // // ShipFromCompany_or_&nbsp
  1060. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1062. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1064. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1065. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1066. //
  1067. //
  1068. // // FreightBillToCompany_or_&nbsp
  1069. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1071. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1073. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1074. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1075. //
  1076. //
  1077. // // MerchandiseBillToCompany_or_&nbsp
  1078. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1079. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1080. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1081. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1082. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1083. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1084. //
  1085. //
  1086. // // ReturnToCompany_or_&nbsp
  1087. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1088. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1089. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1090. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1091. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1092. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1093. // //
  1094. //
  1095. // // DebugLog(@"more info : %@",moreInfo);
  1096. //
  1097. // // handling fee
  1098. // handlingFee = sqlite3_column_double(statement, 33);
  1099. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1100. // if (sqlite3_column_int(statement, 58)) {
  1101. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1102. // }
  1103. // //
  1104. //
  1105. // // customer info
  1106. // customerID = [self textAtColumn:36 statement:statement];
  1107. //
  1108. // // mode
  1109. // ret[@"mode"] = appDelegate.mode;
  1110. //
  1111. // // model_count
  1112. // ret[@"model_count"] = @(0);
  1113. }
  1114. sqlite3_finalize(statement);
  1115. }
  1116. [iSalesDB close_db:db];
  1117. data[@"order_type"]=order_type;
  1118. /*
  1119. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1120. // "customer_email" = "Shui Hu";
  1121. // "customer_fax" = "";
  1122. // "customer_first_name" = F;
  1123. // "customer_last_name" = L;
  1124. // "customer_name" = ",da He Xiang Dong Liu A";
  1125. // "customer_phone" = "Hey Xuan Feng";
  1126. contactInfo[@"customer_phone"] = customer_phone;
  1127. contactInfo[@"customer_fax"] = customer_fax;
  1128. contactInfo[@"customer_email"] = customer_email;
  1129. NSString *first_name = @"";
  1130. NSString *last_name = @"";
  1131. if ([customer_contact isEqualToString:@""]) {
  1132. } else if ([customer_contact containsString:@" "]) {
  1133. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1134. first_name = [customer_contact substringToIndex:first_space_index];
  1135. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1136. }
  1137. contactInfo[@"customer_first_name"] = first_name;
  1138. contactInfo[@"customer_last_name"] = last_name;
  1139. ret[@"customerInfo"] = contactInfo;
  1140. // models
  1141. if (nssoid) {
  1142. __block double TotalCuft = 0;
  1143. __block double TotalWeight = 0;
  1144. __block int TotalCarton = 0;
  1145. __block double allItemPrice = 0;
  1146. 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];
  1147. sqlite3 *db1 = [iSalesDB get_db];
  1148. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1149. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1150. int product_id = sqlite3_column_int(stmt, 0);
  1151. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1152. int item_id = sqlite3_column_int(stmt, 7);
  1153. NSString* Price=nil;
  1154. if(str_price==nil)
  1155. {
  1156. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1157. if(price==nil)
  1158. Price=@"No Price.";
  1159. else
  1160. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1161. }
  1162. else
  1163. {
  1164. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1165. }
  1166. double discount = sqlite3_column_double(stmt, 2);
  1167. int item_count = sqlite3_column_int(stmt, 3);
  1168. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1169. NSString *nsline_note=nil;
  1170. if(line_note!=nil)
  1171. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1172. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1173. // NSString *nsname = nil;
  1174. // if(name!=nil)
  1175. // nsname= [[NSString alloc]initWithUTF8String:name];
  1176. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1177. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1178. // NSString *nsdescription=nil;
  1179. // if(description!=nil)
  1180. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1181. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1182. // int stockUom = sqlite3_column_int(stmt, 8);
  1183. // int _id = sqlite3_column_int(stmt, 9);
  1184. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1185. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1186. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1187. int carton=[bsubtotaljson[@"carton"] intValue];
  1188. TotalCuft += cuft;
  1189. TotalWeight += weight;
  1190. TotalCarton += carton;
  1191. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1192. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1193. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1194. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1195. itemjson[@"note"]=nsline_note;
  1196. itemjson[@"origin_price"] = Price;
  1197. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1198. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1199. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1200. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1201. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1202. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1203. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1204. if(itemjson[@"combine"] != nil)
  1205. {
  1206. // int citem=0;
  1207. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1208. for(int bc=0;bc<bcount;bc++)
  1209. {
  1210. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1211. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1212. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1213. subTotal += uprice * modulus * item_count;
  1214. }
  1215. }
  1216. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1217. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1218. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1219. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1220. allItemPrice += subTotal;
  1221. }];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1223. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1224. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1225. // payments/Credits
  1226. // payments_and_credist = sqlite3_column_double(statement, 34);
  1227. payments_and_credist = allItemPrice;
  1228. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1229. // // total
  1230. // totalPrice = sqlite3_column_double(statement, 35);
  1231. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1232. } else {
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1234. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1235. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1236. // payments/Credits
  1237. payments_and_credist = 0;
  1238. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1239. }
  1240. // total
  1241. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1242. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1243. ret[@"order_info"]= orderinfo;
  1244. ret[@"more_order_info"] = moreInfo;
  1245. return [RAConvertor dict2data:ret];
  1246. */
  1247. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1248. return data;
  1249. }
  1250. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1251. {
  1252. assert(params[@"user"]!=nil);
  1253. NSString* template_file=nil;
  1254. switch([params[@"thumb"] intValue])
  1255. {
  1256. case 0:
  1257. template_file= @"so.json";
  1258. break;
  1259. case 1:
  1260. template_file= @"so_thumb.json";
  1261. break;
  1262. default:
  1263. template_file= @"so.json";
  1264. }
  1265. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1266. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1267. if(data[@"table_model"][@"row2"]==nil)
  1268. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1269. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1270. DebugLog(@"%@",file);
  1271. // return nil;
  1272. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1273. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1274. if (file) {
  1275. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1276. [dic setObject:file forKey:@"pdf_path"];
  1277. dic[@"isLocalFile"]=@"true";
  1278. return [RAConvertor dict2data:dic];
  1279. }
  1280. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1281. dic[@"isLocalFile"]=@"true";
  1282. return [RAConvertor dict2data:dic];
  1283. }
  1284. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1285. {
  1286. // assert(params[@"contact_id"]!=nil);
  1287. // assert(params[@"user"]!=nil);
  1288. if(params[@"user"]==nil)
  1289. {
  1290. AppDelegate *appDelegate = nil;
  1291. //some UI methods ej
  1292. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1293. if(appDelegate.user!=nil)
  1294. [params setValue:appDelegate.user forKey:@"user"];
  1295. }
  1296. NSString* template_file=nil;
  1297. switch([params[@"pdf_style"] intValue])
  1298. {
  1299. case 0:
  1300. template_file= @"portfolio_2x3.json";
  1301. break;
  1302. case 1:
  1303. template_file= @"portfolio_3x2.json";
  1304. break;
  1305. default:
  1306. template_file= @"portfolio_3x2.json";
  1307. }
  1308. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1309. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1310. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1311. DebugLog(@"%@",file);
  1312. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1313. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1314. if (file) {
  1315. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1316. [dic setObject:file forKey:@"pdf_path"];
  1317. dic[@"isLocalFile"]=@"true";
  1318. return [RAConvertor dict2data:dic];
  1319. }
  1320. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1321. dic[@"isLocalFile"]=@"true";
  1322. return [RAConvertor dict2data:dic];
  1323. }
  1324. +(NSString*) get_offline_soid:(sqlite3*)db
  1325. {
  1326. NSString* soid=nil;
  1327. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1328. [formatter setDateFormat:@"yyMMdd"];
  1329. NSString* date = [formatter stringFromDate:[NSDate date]];
  1330. for(int i=1;i<999;i++)
  1331. {
  1332. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1333. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1334. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1335. if(count==0)
  1336. return soid;
  1337. }
  1338. return nil;
  1339. }
  1340. +(NSArray*) enumOfflineOrder
  1341. {
  1342. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1343. sqlite3 *db = [iSalesDB get_db];
  1344. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1345. sqlite3_stmt * statement;
  1346. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1347. if ( dbresult== SQLITE_OK)
  1348. {
  1349. while (sqlite3_step(statement) == SQLITE_ROW)
  1350. {
  1351. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1352. NSString* nsso_id=nil;
  1353. if(so_id!=nil)
  1354. {
  1355. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1356. }
  1357. [ret addObject:nsso_id];
  1358. }
  1359. sqlite3_finalize(statement);
  1360. }
  1361. [iSalesDB close_db:db];
  1362. return ret;
  1363. }
  1364. //+(void) uploadFile:(NSString*) file
  1365. //{
  1366. // NSData* data = [NSData dataWithContentsOfFile: file];
  1367. // UIApplication * app = [UIApplication sharedApplication];
  1368. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1369. //
  1370. //
  1371. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1372. //
  1373. //
  1374. //
  1375. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1376. //
  1377. // if(appDelegate.user!=nil)
  1378. // [params setValue:appDelegate.user forKey:@"user"];
  1379. // // if(appDelegate.contact_id!=nil)
  1380. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1381. // if(appDelegate.password!=nil)
  1382. // [params setValue:appDelegate.password forKey:@"password"];
  1383. //
  1384. //
  1385. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1386. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1387. // } error:nil];
  1388. //
  1389. //
  1390. //
  1391. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1392. //
  1393. // NSProgress *progress = nil;
  1394. //
  1395. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1396. //
  1397. //
  1398. //
  1399. // if (error) {
  1400. //
  1401. // NSString* err_msg = [error localizedDescription];
  1402. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1403. //
  1404. //
  1405. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1406. //
  1407. // DebugLog(@"data string: %@",str);
  1408. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1409. //
  1410. // } else {
  1411. // DebugLog(@"response ");
  1412. //
  1413. //
  1414. //
  1415. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1416. //
  1417. // // 再将NSData转为字符串
  1418. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1419. //
  1420. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1421. //
  1422. // DebugLog(@"data string: %@",jsonStr);
  1423. //
  1424. // NSDictionary* json = responseObject;
  1425. //
  1426. //
  1427. // if([[json valueForKey:@"result"] intValue]==2)
  1428. // {
  1429. //// NSString* img_url_down = json[@"img_url_aname"];
  1430. //// NSString* img_url_up = json[@"img_url"];
  1431. //
  1432. //
  1433. // }
  1434. // else
  1435. // {
  1436. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1437. // }
  1438. //
  1439. //
  1440. // }
  1441. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1442. // }];
  1443. //
  1444. //
  1445. //
  1446. //
  1447. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1448. // //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. //
  1454. //
  1455. // [uploadTask resume];
  1456. //
  1457. //}
  1458. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1459. //{
  1460. //
  1461. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1462. // NSString *documents = [paths objectAtIndex:0];
  1463. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1464. //
  1465. //
  1466. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1467. //
  1468. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1469. //
  1470. //
  1471. //
  1472. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1473. //
  1474. //
  1475. //
  1476. //
  1477. //
  1478. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1479. //
  1480. //
  1481. //
  1482. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1483. //
  1484. //
  1485. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1486. //
  1487. //
  1488. //
  1489. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1490. //
  1491. //
  1492. //
  1493. //
  1494. // NSMutableDictionary* orderlist = nil;
  1495. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1496. //
  1497. //
  1498. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1499. //
  1500. //
  1501. // orderlist = [NSMutableDictionary new];
  1502. //
  1503. //// [orderlist addObject:appDelegate.order_code];
  1504. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1505. //
  1506. // }
  1507. // else
  1508. // {
  1509. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1510. //
  1511. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1512. //
  1513. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1514. // }
  1515. //
  1516. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1517. // orderitem[@"create_by"] = appDelegate.user;
  1518. // orderitem[@"price"] = upparams[@"total_price"];
  1519. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1520. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1521. // orderitem[@"order_status"] = @"Saved Order";
  1522. // orderitem[@"order_code"] = appDelegate.order_code;
  1523. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1524. //
  1525. //
  1526. // int count = [orderlist[@"count"] intValue];
  1527. //
  1528. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1529. //
  1530. // newlist[@"item_0"]=orderitem;
  1531. // int newcount = 1;
  1532. // for(int i=0;i<count;i++)
  1533. // {
  1534. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1535. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1536. // continue;
  1537. // else
  1538. // {
  1539. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1540. // newcount++;
  1541. // }
  1542. // }
  1543. // newlist[@"count"] = @(newcount);
  1544. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1545. //
  1546. // return;
  1547. //}
  1548. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1549. {
  1550. if(submitlist==nil)
  1551. return;
  1552. NSString *userdir = [OLDataProvider getUserPath];
  1553. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1554. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1555. }
  1556. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1557. {
  1558. if(orderlist==nil)
  1559. return;
  1560. NSString *userdir = [OLDataProvider getUserPath];
  1561. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1562. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1563. }
  1564. +(NSMutableDictionary*) getScanSubmitList
  1565. {
  1566. NSString *userdir = [OLDataProvider getUserPath];
  1567. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1568. NSMutableDictionary* submitlist = nil;
  1569. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1570. submitlist = [NSMutableDictionary new];
  1571. }
  1572. else
  1573. {
  1574. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1575. }
  1576. return submitlist;
  1577. }
  1578. +(NSMutableDictionary*) getScanOrderList{
  1579. NSString *userdir = [OLDataProvider getUserPath];
  1580. NSMutableDictionary* orderlist = nil;
  1581. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1582. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1583. orderlist = [NSMutableDictionary new];
  1584. // [orderlist addObject:appDelegate.order_code];
  1585. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1586. }
  1587. else
  1588. {
  1589. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1590. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1591. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1592. }
  1593. return orderlist;
  1594. }
  1595. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1596. {
  1597. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1598. // NSString *documents = [paths objectAtIndex:0];
  1599. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1600. // NSString *userdir = [OLDataProvider getUserPath];
  1601. NSMutableDictionary* orderlist = [self getScanOrderList];
  1602. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1603. int count = [orderlist[@"count"] intValue];
  1604. if(count==0)
  1605. {
  1606. // order list 为空,不用维护
  1607. #ifdef RA_NOTIFICATION
  1608. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1609. #else
  1610. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1611. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1612. #endif
  1613. return;
  1614. }
  1615. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1616. //
  1617. // // order list 为空,不用维护
  1618. //
  1619. //
  1620. // // [orderlist addObject:appDelegate.order_code];
  1621. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1622. //
  1623. // }
  1624. // else
  1625. // {
  1626. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1627. //
  1628. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1629. //
  1630. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1631. // }
  1632. //==================== order list 删除
  1633. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1634. NSMutableDictionary* submititem =nil;
  1635. int newcount = 0;
  1636. for(int i=0;i<count;i++)
  1637. {
  1638. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1639. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1640. {
  1641. submititem = item;
  1642. continue;
  1643. }
  1644. else
  1645. {
  1646. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1647. newcount++;
  1648. }
  1649. }
  1650. if(submititem==nil)
  1651. {
  1652. //order list 里没有和当前打开的order code一致的order
  1653. //这种情况往往是因为重复点提交按钮,order已关闭。
  1654. return;
  1655. }
  1656. newlist[@"count"] = @(newcount);
  1657. [self saveScanOrderList:newlist];
  1658. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1659. //////////////////---=============================== submitlist 添加
  1660. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1661. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1662. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1663. //
  1664. // submitlist = [NSMutableDictionary new];
  1665. //
  1666. //
  1667. // }
  1668. // else
  1669. // {
  1670. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1671. //
  1672. //
  1673. // }
  1674. int submitcount =[submitlist[@"count"] intValue];
  1675. submititem[@"order_status"] = soid;//@"Submitted Order";
  1676. submititem[@"customer_name"] = company;//@"Submitted Order";
  1677. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1678. submitlist[@"count"] = @(submitcount+1);
  1679. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1680. [self saveScanSubmitList:submitlist];
  1681. #ifdef RA_NOTIFICATION
  1682. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1683. #else
  1684. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1685. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1686. #endif
  1687. }
  1688. +(void) save2submitScanOrder1
  1689. {
  1690. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1691. // NSString *documents = [paths objectAtIndex:0];
  1692. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1693. // NSString *userdir = [OLDataProvider getUserPath];
  1694. NSMutableDictionary* orderlist = [self getScanOrderList];
  1695. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1696. int count = [orderlist[@"count"] intValue];
  1697. if(count==0)
  1698. {
  1699. // order list 为空,不用维护
  1700. #ifdef RA_NOTIFICATION
  1701. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1702. #else
  1703. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1704. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1705. #endif
  1706. return;
  1707. return;
  1708. }
  1709. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1710. int newcount = 0;
  1711. for(int i=0;i<count;i++)
  1712. {
  1713. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1714. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1715. continue;
  1716. else
  1717. {
  1718. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1719. newcount++;
  1720. }
  1721. }
  1722. newlist[@"count"] = @(newcount);
  1723. [self saveScanOrderList:newlist];
  1724. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1725. #ifdef RA_NOTIFICATION
  1726. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1727. #else
  1728. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1729. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1730. #endif
  1731. }
  1732. //+(void) createScanTempFolder
  1733. //{
  1734. // return;
  1735. //// NSString* scanFilePath = [self getScanTempPath];
  1736. //// BOOL bdir=YES;
  1737. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1738. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1739. //// {
  1740. ////
  1741. //// NSError *error = nil;
  1742. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1743. ////
  1744. //// if(!bsuccess)
  1745. //// DebugLog(@"Create cache folder failed");
  1746. ////
  1747. //// // if(bsuccess)
  1748. //// // {
  1749. //// // sqlite3 *db = [self get_db];
  1750. //// //
  1751. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1752. //// // [iSalesDB close_db:db];
  1753. //// // }
  1754. //// }
  1755. //}
  1756. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1757. {
  1758. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1759. NSString *documents = [paths objectAtIndex:0];
  1760. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1761. NSString* servername = addressDic[@"name"];
  1762. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1763. NSString* user = appDelegate.user;
  1764. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1765. orderpath = [orderpath stringByAppendingPathComponent:user];
  1766. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1767. BOOL bdir=YES;
  1768. NSFileManager* fileManager = [NSFileManager defaultManager];
  1769. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1770. {
  1771. NSError *error = nil;
  1772. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1773. if(!bsuccess)
  1774. DebugLog(@"Create cache folder failed");
  1775. // if(bsuccess)
  1776. // {
  1777. // sqlite3 *db = [self get_db];
  1778. //
  1779. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1780. // [iSalesDB close_db:db];
  1781. // }
  1782. }
  1783. return orderpath;
  1784. }
  1785. +(NSString*) getScanOrderPath
  1786. {
  1787. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1788. NSString *documents = [paths objectAtIndex:0];
  1789. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1790. NSString* servername = addressDic[@"name"];
  1791. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1792. NSString* user = appDelegate.user;
  1793. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1794. orderpath = [orderpath stringByAppendingPathComponent:user];
  1795. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1796. BOOL bdir=YES;
  1797. NSFileManager* fileManager = [NSFileManager defaultManager];
  1798. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1799. {
  1800. NSError *error = nil;
  1801. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1802. if(!bsuccess)
  1803. DebugLog(@"Create cache folder failed");
  1804. // if(bsuccess)
  1805. // {
  1806. // sqlite3 *db = [self get_db];
  1807. //
  1808. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1809. // [iSalesDB close_db:db];
  1810. // }
  1811. }
  1812. return orderpath;
  1813. }
  1814. //+(NSString*) getScanTempPath
  1815. //{
  1816. // return nil;
  1817. ////
  1818. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1819. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1820. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1821. //}
  1822. //+(void) moveScanTemp2Order
  1823. //{
  1824. // return;
  1825. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1826. ////
  1827. //// NSError *error = nil;
  1828. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1829. ////
  1830. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1831. //// // for(int i=0;i<arr_files.count;i++)
  1832. //// // {
  1833. //// // NSString* file=arr_files[i];
  1834. //// //
  1835. //// //
  1836. //// // }
  1837. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1838. ////
  1839. //
  1840. //}
  1841. +(NSString*) getUserPath
  1842. {
  1843. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1844. NSString *documents = [paths objectAtIndex:0];
  1845. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1846. NSString* servername = addressDic[@"name"];
  1847. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1848. NSString* user = appDelegate.user;
  1849. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1850. userpath = [userpath stringByAppendingPathComponent:user];
  1851. return userpath;
  1852. }
  1853. +(bool) isSavedScanOrder:(NSString*) order_code
  1854. {
  1855. if(order_code == nil)
  1856. return false;
  1857. NSMutableDictionary * orderlist=[self getScanOrderList];
  1858. int count = [orderlist[@"count"] intValue];
  1859. for(int i=0;i<count;i++)
  1860. {
  1861. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1862. if([item[@"order_code"] isEqualToString: order_code ])
  1863. {
  1864. return true;
  1865. }
  1866. }
  1867. return false;
  1868. }
  1869. +(NSString*) getScanPath
  1870. {
  1871. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1872. if(appDelegate.order_code==nil)
  1873. {
  1874. appDelegate.order_code=[[NSUUID new] UUIDString];
  1875. }
  1876. return [self getScanOrderPath];
  1877. // else
  1878. // {
  1879. // [self createScanTempFolder];
  1880. // return [self getScanTempPath];
  1881. // }
  1882. }
  1883. +(void) scanCloseOrder
  1884. {
  1885. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1886. // if(appDelegate.user.length<=0)
  1887. // {
  1888. // [RAUtils message_alert:@"Please login first." title:@"Warring" controller:self];
  1889. // return;
  1890. // }
  1891. // if(appDelegate.order_code.length>0)
  1892. {
  1893. // 当前订单先关闭
  1894. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1895. appDelegate.order_code = nil;
  1896. // RASingleton.sharedInstance.scan_temp_code = nil;
  1897. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1898. { appDelegate.customerInfo = nil;
  1899. appDelegate.contact_id = nil;
  1900. }
  1901. RASingleton.sharedInstance.scan_cart = nil;
  1902. RASingleton.sharedInstance.price_type = 1;
  1903. // [appDelegate updateScanButton:false];
  1904. [appDelegate update_count_mark];
  1905. [appDelegate closeOrder];
  1906. }
  1907. }
  1908. +(NSString*) createScanTempCode
  1909. {
  1910. return [[NSUUID new] UUIDString];
  1911. }
  1912. +(NSString*) scanTemplatePath:(NSString*)file
  1913. {
  1914. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1915. NSString *documents = [paths objectAtIndex:0];
  1916. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1917. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1918. return templatefile;
  1919. }
  1920. +(void) realguestLogin
  1921. {
  1922. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1923. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1924. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1925. }
  1926. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1927. {
  1928. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1929. NSString *documents = [paths objectAtIndex:0];
  1930. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1931. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1932. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1933. return [dict mutableCopy];
  1934. }
  1935. +(void) deleteSubmittedOrder:(NSString*) order_code
  1936. {
  1937. NSString *userdir = [OLDataProvider getUserPath];
  1938. if(order_code.length==0)
  1939. return;
  1940. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1941. // NSString *documents = [paths objectAtIndex:0];
  1942. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1943. // if([appDelegate.order_code isEqualToString:order_code])
  1944. // {
  1945. // //要删除的订单是当前打开的订单;
  1946. // appDelegate.order_code = nil;
  1947. // appDelegate.customerInfo = nil;
  1948. // RASingleton.sharedInstance.scan_cart = nil;
  1949. // appDelegate.contact_id = nil;
  1950. // // [appDelegate updateScanButton:false];
  1951. //
  1952. // [appDelegate update_count_mark];
  1953. // }
  1954. //删除订单目录
  1955. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1956. //维护订单列表
  1957. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1958. int count = [submitlist[@"count"] intValue];
  1959. if(count==0)
  1960. {
  1961. // order list 为空,不用维护
  1962. #ifdef RA_NOTIFICATION
  1963. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1964. #else
  1965. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1966. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1967. #endif
  1968. return;
  1969. }
  1970. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1971. //
  1972. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1973. //
  1974. //
  1975. //
  1976. // // [orderlist addObject:appDelegate.order_code];
  1977. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1978. //
  1979. // }
  1980. // else
  1981. // {
  1982. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1983. //
  1984. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1985. //
  1986. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1987. // }
  1988. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1989. int newcount = 0;
  1990. for(int i=0;i<count;i++)
  1991. {
  1992. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1993. if([item[@"order_code"] isEqualToString: order_code ])
  1994. continue;
  1995. else
  1996. {
  1997. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1998. newcount++;
  1999. }
  2000. }
  2001. newlist[@"count"] = @(newcount);
  2002. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2003. [self saveScanSubmitList:newlist];
  2004. #ifdef RA_NOTIFICATION
  2005. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2006. #else
  2007. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2008. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2009. #endif
  2010. }
  2011. +(void) updateScanOrder:(NSString*) order_code
  2012. {
  2013. NSString *userdir = [OLDataProvider getUserPath];
  2014. if(order_code.length==0)
  2015. return;
  2016. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2017. // NSString *documents = [paths objectAtIndex:0];
  2018. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2019. NSMutableDictionary* orderlist = [self getScanOrderList];
  2020. int count = [orderlist[@"count"] intValue];
  2021. if(count==0)
  2022. {
  2023. // order list 为空,不用维护
  2024. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2025. return;
  2026. }
  2027. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2028. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2029. //
  2030. //
  2031. //
  2032. // // [orderlist addObject:appDelegate.order_code];
  2033. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2034. //
  2035. // }
  2036. // else
  2037. // {
  2038. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2039. //
  2040. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2041. //
  2042. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2043. // }
  2044. //
  2045. //
  2046. // int count = [orderlist[@"count"] intValue];
  2047. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  2048. float p =0;
  2049. float s =0;
  2050. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  2051. {
  2052. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  2053. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  2054. int mcount = [cartitem[@"count"] intValue];
  2055. double msurcharge =0;
  2056. if(RASingleton.sharedInstance.price_type==1)
  2057. {
  2058. if([cartitem[@"special_price"] boolValue])
  2059. {
  2060. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price2"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2061. }
  2062. else
  2063. {
  2064. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price1"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2065. }
  2066. }
  2067. s+=msurcharge;
  2068. }
  2069. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  2070. int order_qty=0;
  2071. for(int c=0;c<[section[@"count"] intValue];c++)
  2072. {
  2073. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  2074. order_qty+=[item[@"count"] intValue];
  2075. }
  2076. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  2077. // int newcount = 0;
  2078. for(int i=0;i<count;i++)
  2079. {
  2080. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2081. if([item[@"order_code"] isEqualToString: order_code ])
  2082. {
  2083. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  2084. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  2085. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  2086. }
  2087. }
  2088. // newlist[@"count"] = @(newcount);
  2089. [self saveScanOrderList:orderlist];
  2090. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2091. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2092. }
  2093. +(void) deleteScanOrder:(NSString*) order_code
  2094. {
  2095. NSString *userdir = [OLDataProvider getUserPath];
  2096. if(order_code.length==0)
  2097. return;
  2098. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2099. // NSString *documents = [paths objectAtIndex:0];
  2100. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2101. if([appDelegate.order_code isEqualToString:order_code])
  2102. {
  2103. //要删除的订单是当前打开的订单;
  2104. // appDelegate.order_code = nil;
  2105. // appDelegate.customerInfo = nil;
  2106. // RASingleton.sharedInstance.scan_cart = nil;
  2107. // appDelegate.contact_id = nil;
  2108. [self scanCloseOrder];
  2109. // [appDelegate updateScanButton:false];
  2110. [appDelegate update_count_mark];
  2111. }
  2112. //删除订单目录
  2113. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2114. //维护订单列表
  2115. NSMutableDictionary* orderlist = [self getScanOrderList];
  2116. int count = [orderlist[@"count"] intValue];
  2117. if(count==0)
  2118. {
  2119. // order list 为空,不用维护
  2120. #ifdef RA_NOTIFICATION
  2121. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2122. #else
  2123. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2124. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2125. #endif
  2126. return;
  2127. }
  2128. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2129. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2130. //
  2131. //
  2132. //
  2133. // // [orderlist addObject:appDelegate.order_code];
  2134. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2135. //
  2136. // }
  2137. // else
  2138. // {
  2139. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2140. //
  2141. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2142. //
  2143. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2144. // }
  2145. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2146. int newcount = 0;
  2147. for(int i=0;i<count;i++)
  2148. {
  2149. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2150. if([item[@"order_code"] isEqualToString: order_code ])
  2151. continue;
  2152. else
  2153. {
  2154. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2155. newcount++;
  2156. }
  2157. }
  2158. newlist[@"count"] = @(newcount);
  2159. [self saveScanOrderList:newlist];
  2160. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2161. #ifdef RA_NOTIFICATION
  2162. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2163. #else
  2164. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2165. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2166. #endif
  2167. }
  2168. +(void) scanRefreshCart
  2169. {
  2170. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2171. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2172. NSMutableDictionary * section = cart[@"section_0"];
  2173. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2174. int count = [section[@"count"] intValue];
  2175. int newcount = 0;
  2176. for(int i=0;i<count;i++)
  2177. {
  2178. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2179. NSString* model = item[@"model"];
  2180. NSDictionary* modeljson=appDelegate.scan_model[model];
  2181. if(modeljson==nil)
  2182. continue;
  2183. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2184. cartitem[@"note"]=item[@"note"];
  2185. cartitem[@"discount"]=item[@"discount"];
  2186. // [item removeObjectForKey:@"discount"];
  2187. // [item removeObjectForKey:@"note"];
  2188. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2189. newcount++;
  2190. }
  2191. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2192. }
  2193. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2194. {
  2195. // _modelJson = modelJson;
  2196. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2197. // self.labelPrice0.text = appDelegate.price0_name;
  2198. // self.labelPrice1.text = appDelegate.price1_name;
  2199. // self.labelPrice2.text = appDelegate.price2_name;
  2200. // self.labelPrice3.text = appDelegate.price3_name;
  2201. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2202. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2203. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2204. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2205. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2206. NSLog( [RAConvertor dict2string:_modelJson]);
  2207. NSString * port=_modelJson[@"port"];
  2208. if(port.length==0)
  2209. port = @"N/A";
  2210. NSString * origin=_modelJson[@"origin"];
  2211. if(origin.length==0)
  2212. origin = @"N/A";
  2213. NSString * dimension=_modelJson[@"dimension"];
  2214. if(dimension.length==0)
  2215. dimension = @"N/A";
  2216. // _labelModel.text=_modelJson[@"model"];
  2217. // _labelDescription.text=_modelJson[@"description"];
  2218. // _labelDimension.text=dimension;
  2219. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2220. // _labelOrigin.text=origin;
  2221. // _labelPort.text=port;
  2222. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2223. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2224. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2225. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2226. if(![price0 isEqualToString: @"N/A"])
  2227. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2228. if(![price1 isEqualToString: @"N/A"])
  2229. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2230. if(![price2 isEqualToString: @"N/A"])
  2231. {
  2232. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2233. // if(RASingleton.sharedInstance.price_type==1)
  2234. _modelJson [@"special_price"] = @true;
  2235. }
  2236. if(![price3 isEqualToString: @"N/A"])
  2237. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2238. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2239. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2240. {
  2241. // if(RASingleton.sharedInstance.price_type==1)
  2242. _modelJson [@"net_price"] = @true;
  2243. }
  2244. //
  2245. // if(![price1 isEqualToString:@"N/A"])
  2246. // {
  2247. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2248. // _modelJson[@"price2"]= price2;
  2249. // }
  2250. // else
  2251. // {
  2252. // price2=@"N/A";
  2253. // }
  2254. //
  2255. // if([price0 isEqualToString:@"N/A"])
  2256. // _labelPriceCTNR.text= @"";//price0;
  2257. // else
  2258. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2259. //
  2260. // if([price3 isEqualToString:@"N/A"])
  2261. // _labelPriceNCA.text= price3;
  2262. // else
  2263. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2264. // _labelPrice25p.text=price2;
  2265. //
  2266. // if([_modelJson [@"special_price"] boolValue])
  2267. // {
  2268. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2269. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2270. // else
  2271. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2272. // }
  2273. // else
  2274. // {
  2275. // _labelPriceSpecial.text=@"";//@"N/A";
  2276. // }
  2277. //
  2278. // if([_modelJson [@"net_price"] boolValue])
  2279. // {
  2280. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2281. //// {
  2282. //// _labelPriceNet.text=_modelJson[@"price1"];
  2283. ////// _labelPriceNCA.text=@"N/A";
  2284. //// }
  2285. //// else
  2286. //// {
  2287. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2288. ////// _labelPriceNCA.text=@"N/A";
  2289. //// }
  2290. //
  2291. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2292. // {
  2293. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2294. //// _labelPriceNCA.text=@"N/A";
  2295. // }
  2296. // else
  2297. // {
  2298. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2299. //// _labelPriceNCA.text=@"N/A";
  2300. // }
  2301. //
  2302. // }
  2303. // else
  2304. // {
  2305. // _labelPriceNet.text=@"";//@"N/A";
  2306. // }
  2307. //
  2308. // {
  2309. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2310. // {
  2311. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2312. //// _labelPriceNCA.text=@"N/A";
  2313. // }
  2314. // else
  2315. // {
  2316. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2317. //// _labelPriceNet.text=@"N/A";
  2318. // }
  2319. // }
  2320. NSString * available=_modelJson[@"available"];
  2321. if(available.length==0)
  2322. available = @"N/A";
  2323. // _labelAvailable.text=available;
  2324. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2325. if(unit_cuft.length==0)
  2326. unit_cuft = @"N/A";
  2327. else
  2328. {
  2329. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2330. }
  2331. NSString * cell_price;
  2332. if(RASingleton.sharedInstance.price_type==1)
  2333. {
  2334. // if(_modelJson [@"net_price"])
  2335. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2336. // else if(_modelJson [@"special_price"])
  2337. // {
  2338. // unit_price= _modelJson[@"price2"];
  2339. // }
  2340. // else
  2341. // {
  2342. // unit_price= _modelJson[@"price1"];
  2343. // }
  2344. }
  2345. else
  2346. {
  2347. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2348. }
  2349. NSString * mpack=_modelJson[@"stockUom"];
  2350. if([mpack isEqualToString:@"N/A"])
  2351. mpack=@"1";
  2352. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2353. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2354. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2355. NSMutableDictionary* jitem = nil;
  2356. jitem = [_modelJson mutableCopy];
  2357. int stockUom =[jitem[@"stockUom"] intValue];
  2358. if(stockUom==0)
  2359. stockUom=1;
  2360. bool newitem = true;
  2361. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2362. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2363. // for(int i=0;i<count;i++)
  2364. // {
  2365. //
  2366. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2367. //
  2368. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2369. // {
  2370. //
  2371. //// int oldcount = [litem[@"stockUom"] intValue];
  2372. // newitem = false;
  2373. //
  2374. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2375. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2376. //
  2377. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2378. // break;
  2379. //
  2380. //
  2381. // }
  2382. //
  2383. //
  2384. //
  2385. // }
  2386. // if(true)
  2387. // {
  2388. jitem[@"count"]=@(stockUom);
  2389. jitem[@"check"]=@(true);
  2390. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2391. NSString * unit_price;
  2392. if(RASingleton.sharedInstance.price_type==0)
  2393. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2394. else if(_modelJson [@"special_price"])
  2395. {
  2396. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2397. }
  2398. else if(_modelJson [@"net_price"])
  2399. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2400. else
  2401. {
  2402. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2403. }
  2404. if([unit_price isEqualToString:@"N/A"])
  2405. unit_price = @"0";
  2406. else
  2407. {
  2408. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2409. }
  2410. jitem[@"unit_price"] = unit_price;
  2411. jitem[@"erp_unit_price"] = unit_price;
  2412. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2413. // section[@"count"]= @(count+1);
  2414. // count++;
  2415. // }
  2416. return jitem;
  2417. }
  2418. +(void) saveScanCart:(NSMutableDictionary*) cart
  2419. {
  2420. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2421. if(appDelegate.order_code.length<=0)
  2422. return;
  2423. if(cart==nil)
  2424. return;
  2425. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2426. // NSString *documents = [paths objectAtIndex:0];
  2427. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2428. if(RASingleton.sharedInstance.price_type==0)
  2429. {
  2430. NSMutableDictionary * section = cart[@"section_0"];
  2431. int count = [section[@"count"] intValue];
  2432. for(int i=0;i<count;i++)
  2433. {
  2434. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2435. [item removeObjectForKey:@"discount"];
  2436. [item removeObjectForKey:@"note"];
  2437. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2438. }
  2439. cart[@"section_0"]=section;
  2440. }
  2441. NSString *orderdir = [self getScanPath];
  2442. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2443. [RAUtils dicttofile:cartpath dict:cart];
  2444. }
  2445. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2446. {
  2447. assert(add_params[@"user"]!=nil);
  2448. // assert(add_params[@"contact_id"]!=nil);
  2449. // assert(add_params[@"password"]!=nil);
  2450. NSString* serial= [[NSUUID UUID] UUIDString];
  2451. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2452. NSString *cachefolder = [paths objectAtIndex:0];
  2453. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2454. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2455. NSFileManager* fileManager = [NSFileManager defaultManager];
  2456. BOOL bdir=YES;
  2457. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2458. {
  2459. NSError *error = nil;
  2460. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2461. if(!bsuccess)
  2462. DebugLog(@"Create UPLOAD folder failed");
  2463. }
  2464. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2465. ret[@"contact"]=[self prepareContact:serial];
  2466. ret[@"wishlist"]=[self prepareWishlist:serial];
  2467. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2468. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2469. ret[@"view_portfolio"] = [self preparePDF:serial];
  2470. NSString* str= [RAConvertor dict2string:ret];
  2471. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2472. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2473. NSError *error=nil;
  2474. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2475. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2476. ZipArchive* zip = [[ZipArchive alloc] init];
  2477. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2478. for(int i=0;i<arr_files.count;i++)
  2479. {
  2480. NSString* file=arr_files[i];
  2481. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2482. }
  2483. if( ![zip CloseZipFile2] )
  2484. {
  2485. zippath = @"";
  2486. }
  2487. ret[@"file"]=zippath;
  2488. return ret;
  2489. }
  2490. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2491. {
  2492. if(filename.length==0)
  2493. return false;
  2494. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2495. bool ret=false;
  2496. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2497. NSString *cachefolder = [paths objectAtIndex:0];
  2498. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2499. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2500. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2501. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2502. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2503. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2504. NSFileManager* fileManager = [NSFileManager defaultManager];
  2505. BOOL bdir=NO;
  2506. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2507. {
  2508. NSError *error = nil;
  2509. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2510. {
  2511. ret=false;
  2512. }
  2513. else
  2514. {
  2515. ret=true;
  2516. }
  2517. }
  2518. return ret;
  2519. }
  2520. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2521. {
  2522. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2523. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2524. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2525. NSString* where=@" 1=1";
  2526. if (ver!=nil) {
  2527. where=@"is_dirty=1";
  2528. }
  2529. sqlite3 *db = [iSalesDB get_db];
  2530. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2531. sqlite3_stmt * statement;
  2532. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2533. int count=0;
  2534. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2535. if ( dbresult== SQLITE_OK)
  2536. {
  2537. while (sqlite3_step(statement) == SQLITE_ROW)
  2538. {
  2539. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2540. int _id = sqlite3_column_int(statement, 0);
  2541. int product_id = sqlite3_column_int(statement, 1);
  2542. int item_id = sqlite3_column_int(statement, 2);
  2543. int qty = sqlite3_column_int(statement, 3);
  2544. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2545. NSString* nscreate_time=nil;
  2546. if(create_time!=nil)
  2547. {
  2548. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2549. }
  2550. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2551. NSString* nsmodify_time=nil;
  2552. if(modify_time!=nil)
  2553. {
  2554. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2555. }
  2556. int is_delete = sqlite3_column_int(statement, 6);
  2557. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2558. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2559. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2560. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2561. itemjson[@"create_time"]=nscreate_time;
  2562. itemjson[@"modify_time"]=nsmodify_time;
  2563. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2564. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2565. count++;
  2566. }
  2567. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2568. sqlite3_finalize(statement);
  2569. }
  2570. ret[@"count"]=[NSNumber numberWithInt:count ];
  2571. [iSalesDB close_db:db];
  2572. return ret;
  2573. }
  2574. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2575. {
  2576. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2577. // UIApplication * app = [UIApplication sharedApplication];
  2578. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2579. assert(add_params[@"user"]!=nil);
  2580. // assert(add_params[@"password"]!=nil);
  2581. 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 ];
  2582. // 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 ];
  2583. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2584. DebugLog(@"offline_login sql:%@",sqlQuery);
  2585. sqlite3_stmt * statement;
  2586. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2587. int count=0;
  2588. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2589. if ( dbresult== SQLITE_OK)
  2590. {
  2591. while (sqlite3_step(statement) == SQLITE_ROW)
  2592. {
  2593. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2594. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2595. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2596. int product_id = sqlite3_column_int(statement, 0);
  2597. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2598. int item_id = sqlite3_column_int(statement, 7);
  2599. NSString* Price=nil;
  2600. if(str_price==nil)
  2601. {
  2602. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2603. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2604. if(price==nil)
  2605. Price=@"No Price.";
  2606. else
  2607. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2608. }
  2609. else
  2610. {
  2611. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2612. }
  2613. double discount = sqlite3_column_double(statement, 2);
  2614. int item_count = sqlite3_column_int(statement, 3);
  2615. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2616. NSString *nsline_note=nil;
  2617. if(line_note!=nil)
  2618. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2619. char *name = (char*)sqlite3_column_text(statement, 5);
  2620. NSString *nsname=nil;
  2621. if(name!=nil)
  2622. nsname= [[NSString alloc]initWithUTF8String:name];
  2623. char *description = (char*)sqlite3_column_text(statement, 6);
  2624. NSString *nsdescription=nil;
  2625. if(description!=nil)
  2626. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2627. // int stockUom = sqlite3_column_int(statement, 8);
  2628. // int _id = sqlite3_column_int(statement, 9);
  2629. //
  2630. //
  2631. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2632. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2633. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2634. // int carton=[bsubtotaljson[@"carton"] intValue];
  2635. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2636. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2637. itemjson[@"model"]=nsname;
  2638. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2639. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2640. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2641. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2642. // itemjson[@"check"]=@"true";
  2643. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2644. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2645. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2646. itemjson[@"unit_price"]=Price;
  2647. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2648. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2649. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2650. itemjson[@"note"]=nsline_note;
  2651. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2652. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2653. count++;
  2654. }
  2655. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2656. sqlite3_finalize(statement);
  2657. }
  2658. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2659. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2660. return ret;
  2661. }
  2662. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2663. {
  2664. assert(add_params[@"user"]!=nil);
  2665. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2666. sqlite3 *db = [iSalesDB get_db];
  2667. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2668. // for(int i=0;i<arr_soid.count;i++)
  2669. sqlite3_stmt * statement;
  2670. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2671. //int count=0;
  2672. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2673. if ( dbresult== SQLITE_OK)
  2674. {
  2675. int count=0;
  2676. while (sqlite3_step(statement) == SQLITE_ROW)
  2677. {
  2678. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2679. int _id = sqlite3_column_int(statement, 0);
  2680. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2681. NSString* nssync_data=nil;
  2682. if(sync_data!=nil)
  2683. {
  2684. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2685. }
  2686. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2687. NSString* nsimg_1=nil;
  2688. if(img_1!=nil)
  2689. {
  2690. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2691. [self copy_upImg:serial file:nsimg_1];
  2692. }
  2693. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2694. NSString* nsso_no=nil;
  2695. if(so_no!=nil)
  2696. {
  2697. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2698. }
  2699. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2700. itemjson[@"order_type"]=@"submit order";
  2701. else
  2702. itemjson[@"order_type"]=@"archive order";
  2703. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2704. itemjson[@"json_data"]= nssync_data;
  2705. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2706. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2707. count++;
  2708. }
  2709. ret[@"count"]=[NSNumber numberWithInt:count ];
  2710. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2711. }
  2712. sqlite3_finalize(statement);
  2713. [iSalesDB close_db:db];
  2714. return ret;
  2715. }
  2716. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2717. {
  2718. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2719. sqlite3 *db = [iSalesDB get_db];
  2720. // UIApplication * app = [UIApplication sharedApplication];
  2721. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2722. 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";
  2723. sqlite3_stmt * statement;
  2724. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2725. int count=0;
  2726. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2727. if ( dbresult== SQLITE_OK)
  2728. {
  2729. while (sqlite3_step(statement) == SQLITE_ROW)
  2730. {
  2731. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2732. int _id = sqlite3_column_int(statement, 0);
  2733. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2734. NSString* nsimg_0=nil;
  2735. if(img_0!=nil)
  2736. {
  2737. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2738. [self copy_upImg:serial file:nsimg_0];
  2739. }
  2740. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2741. NSString* nsimg_1=nil;
  2742. if(img_1!=nil)
  2743. {
  2744. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2745. [self copy_upImg:serial file:nsimg_1];
  2746. }
  2747. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2748. NSString* nsimg_2=nil;
  2749. if(img_2!=nil)
  2750. {
  2751. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2752. [self copy_upImg:serial file:nsimg_2];
  2753. }
  2754. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2755. NSString* nssync_data=nil;
  2756. if(sync_data!=nil)
  2757. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2758. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2759. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2760. itemjson[@"json_data"]= nssync_data;
  2761. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2762. count++;
  2763. }
  2764. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2765. sqlite3_finalize(statement);
  2766. }
  2767. ret[@"count"]=[NSNumber numberWithInt:count ];
  2768. [iSalesDB close_db:db];
  2769. return ret;
  2770. }
  2771. +(bool) check_offlinedata
  2772. {
  2773. // UIApplication * app = [UIApplication sharedApplication];
  2774. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2775. sqlite3 *db = [iSalesDB get_db];
  2776. NSString * where=@"1=1";
  2777. // if(appDelegate.user!=nil)
  2778. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2779. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2780. [iSalesDB close_db:db];
  2781. if(count==0)
  2782. {
  2783. return false;
  2784. }
  2785. return true;
  2786. //
  2787. //[iSalesDB close_db:db];
  2788. }
  2789. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2790. {
  2791. // UIApplication * app = [UIApplication sharedApplication];
  2792. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2793. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2794. sqlite3 *db = [iSalesDB get_db];
  2795. NSString* collectId=params[@"collectId"];
  2796. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2797. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2798. [iSalesDB execSql:sqlQuery db:db];
  2799. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2800. [iSalesDB close_db:db];
  2801. // appDelegate.wish_count =count;
  2802. //
  2803. // [appDelegate update_count_mark];
  2804. ret[@"result"]= [NSNumber numberWithInt:2];
  2805. ret[@"wish_count"]=@(count);
  2806. return ret;
  2807. }
  2808. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2809. //{
  2810. //
  2811. // UIApplication * app = [UIApplication sharedApplication];
  2812. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2813. //
  2814. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2815. // sqlite3 *db = [iSalesDB get_db];
  2816. // NSString* product_id=params[@"product_id"];
  2817. //
  2818. //
  2819. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2820. //
  2821. //
  2822. // for(int i=0;i<arr.count;i++)
  2823. // {
  2824. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2825. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2826. // if(count==0)
  2827. // {
  2828. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2829. // [iSalesDB execSql:sqlQuery db:db];
  2830. // }
  2831. // }
  2832. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2833. // [iSalesDB close_db:db];
  2834. //
  2835. // appDelegate.wish_count =count;
  2836. //
  2837. // [appDelegate update_count_mark];
  2838. // ret[@"result"]= [NSNumber numberWithInt:2];
  2839. // return ret;
  2840. // //
  2841. // //return ret;
  2842. //}
  2843. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2844. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2845. DebugLog(@"time interval: %lf",interval);
  2846. }
  2847. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2848. //{
  2849. // UIApplication * app = [UIApplication sharedApplication];
  2850. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2851. //
  2852. // int sort = [[params objectForKey:@"sort"] intValue];
  2853. // NSString *sort_str = @"";
  2854. // switch (sort) {
  2855. // case 0:{
  2856. // sort_str = @"order by w.modify_time desc";
  2857. // }
  2858. // break;
  2859. // case 1:{
  2860. // sort_str = @"order by w.modify_time asc";
  2861. // }
  2862. // break;
  2863. // case 2:{
  2864. // sort_str = @"order by m.name asc";
  2865. // }
  2866. // break;
  2867. // case 3:{
  2868. // sort_str = @"order by m.name desc";
  2869. // }
  2870. // break;
  2871. // case 4:{
  2872. // sort_str = @"order by m.description asc";
  2873. // }
  2874. // break;
  2875. //
  2876. // default:
  2877. // break;
  2878. //
  2879. // }
  2880. //
  2881. //
  2882. //// NSString* user = appDelegate.user;
  2883. //
  2884. // sqlite3 *db = [iSalesDB get_db];
  2885. //
  2886. // // order by w.create_time
  2887. // 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];
  2888. //
  2889. //// 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];
  2890. //
  2891. //
  2892. // sqlite3_stmt * statement;
  2893. //
  2894. // NSDate *date1 = [NSDate date];
  2895. //// NSDate *date2 = nil;
  2896. //
  2897. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2898. // int count=0;
  2899. //
  2900. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2901. // {
  2902. //
  2903. //
  2904. // while (sqlite3_step(statement) == SQLITE_ROW)
  2905. // {
  2906. //
  2907. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2908. //
  2909. // int product_id = sqlite3_column_double(statement, 0);
  2910. //
  2911. //
  2912. //
  2913. //
  2914. //
  2915. // char *description = (char*)sqlite3_column_text(statement, 1);
  2916. // if(description==nil)
  2917. // description= "";
  2918. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2919. //
  2920. // int item_id = sqlite3_column_double(statement, 2);
  2921. //
  2922. // NSDate *date_image = [NSDate date];
  2923. //
  2924. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2925. //
  2926. // printf("image : ");
  2927. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2928. //
  2929. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2930. // // if(url==nil)
  2931. // // url="";
  2932. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2933. //
  2934. // int qty = sqlite3_column_int(statement, 3);
  2935. //
  2936. //
  2937. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2938. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2939. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2940. // item[@"description"]= nsdescription;
  2941. // item[@"img"]= nsurl;
  2942. //
  2943. //
  2944. //
  2945. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2946. // count++;
  2947. //
  2948. // }
  2949. // printf("total time:");
  2950. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2951. //
  2952. // ret[@"count"]= [NSNumber numberWithInt:count];
  2953. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2954. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2955. // ret[@"result"]= [NSNumber numberWithInt:2];
  2956. //
  2957. //
  2958. // appDelegate.wish_count =count;
  2959. //
  2960. // [appDelegate update_count_mark];
  2961. // sqlite3_finalize(statement);
  2962. //
  2963. //
  2964. //
  2965. //
  2966. // }
  2967. //
  2968. // [iSalesDB close_db:db];
  2969. //
  2970. // return ret;
  2971. //}
  2972. +(NSDictionary*) offline_notimpl
  2973. {
  2974. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2975. ret[@"result"]=@"8";
  2976. ret[@"err_msg"]=@"offline mode does not support this function.";
  2977. return ret;
  2978. }
  2979. +(NSDictionary*) offline_home
  2980. {
  2981. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2982. NSString *cachefolder = [paths objectAtIndex:0];
  2983. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2984. NSData* json =nil;
  2985. json=[NSData dataWithContentsOfFile:img_cache];
  2986. if(json==nil)
  2987. return nil;
  2988. NSError *error=nil;
  2989. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2990. return menu;
  2991. }
  2992. +(NSDictionary*) offline_category_menu
  2993. {
  2994. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2995. NSString *cachefolder = [paths objectAtIndex:0];
  2996. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2997. NSData* json =nil;
  2998. json=[NSData dataWithContentsOfFile:img_cache];
  2999. if(json==nil)
  3000. return nil;
  3001. NSError *error=nil;
  3002. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3003. return menu;
  3004. }
  3005. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  3006. {
  3007. NSString* offline_command=params[@"offline_Command"];
  3008. NSDictionary* ret=nil;
  3009. if([offline_command isEqualToString:@"model_NIYMAL"])
  3010. {
  3011. NSString* category = params[@"category"];
  3012. ret = [self refresh_model_NIYMAL:category];
  3013. }
  3014. return ret;
  3015. }
  3016. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  3017. {
  3018. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3019. [ret setValue:@"2" forKey:@"result"];
  3020. [ret setValue:@"Regular Mode" forKey:@"mode"];
  3021. sqlite3* db= [iSalesDB get_db];
  3022. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3023. [iSalesDB close_db:db];
  3024. [ret setObject:detail1_section forKey:@"detail_1"];
  3025. return ret;
  3026. }
  3027. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  3028. {
  3029. //assert(user!=nil);
  3030. // UIApplication * app = [UIApplication sharedApplication];
  3031. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3032. // NSArray* arr1 = [self get_user_all_price_type];
  3033. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3034. // NSSet *set1 = [NSSet setWithArray:arr1];
  3035. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3036. // if(appDelegate.contact_id==nil)
  3037. // set2=[set1 mutableCopy];
  3038. // else
  3039. // [set2 intersectsSet:set1];
  3040. // NSArray *retarr = [set2 allObjects];
  3041. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3042. // sqlite3 *db = [iSalesDB get_db];
  3043. NSString* sqlQuery = nil;
  3044. if(contact_id==nil)
  3045. {
  3046. if(!blogin)
  3047. return nil;
  3048. 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];
  3049. }
  3050. else
  3051. 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];
  3052. sqlite3_stmt * statement;
  3053. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3054. int count=0;
  3055. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3056. {
  3057. while (sqlite3_step(statement) == SQLITE_ROW)
  3058. {
  3059. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  3060. char *name = (char*)sqlite3_column_text(statement, 0);
  3061. if(name==nil)
  3062. name="";
  3063. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3064. // double price = sqlite3_column_double(statement, 1);
  3065. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  3066. // if(isnull==nil)
  3067. // item[nsname]= @"No Price";
  3068. // else
  3069. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  3070. char *price = (char*)sqlite3_column_text(statement, 1);
  3071. if(price!=nil)
  3072. {
  3073. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3074. nsprice=[AESCrypt fastdecrypt:nsprice];
  3075. if(nsprice.length>0)
  3076. {
  3077. double dp= [nsprice doubleValue];
  3078. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  3079. }
  3080. }
  3081. else
  3082. {
  3083. item[nsname]= @"No Price";
  3084. }
  3085. // int type= sqlite3_column_int(statement, 2);
  3086. //item[@"type"]=@"price";
  3087. // item[nsname]= nsprice;
  3088. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3089. count++;
  3090. }
  3091. ret[@"count"]= [NSNumber numberWithInt:count];
  3092. sqlite3_finalize(statement);
  3093. }
  3094. // [iSalesDB close_db:db];
  3095. return ret;
  3096. }
  3097. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  3098. //{
  3099. // NSArray* arr1 = [self get_user_all_price_type:db];
  3100. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  3101. //
  3102. // // NSSet *set1 = [NSSet setWithArray:arr1];
  3103. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3104. // // [set2 intersectsSet:set1];
  3105. // //
  3106. // //
  3107. // // NSArray *retarr = [set2 allObjects];
  3108. //
  3109. // NSString* whereprice=nil;
  3110. // if(contact_id==nil)
  3111. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3112. // else
  3113. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3114. //
  3115. //
  3116. // // sqlite3 *db = [iSalesDB get_db];
  3117. //
  3118. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3119. // sqlite3_stmt * statement;
  3120. //
  3121. //
  3122. // NSNumber* ret = nil;
  3123. // double dprice=DBL_MAX;
  3124. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3125. // {
  3126. //
  3127. //
  3128. // while (sqlite3_step(statement) == SQLITE_ROW)
  3129. // {
  3130. //
  3131. // // double val = sqlite3_column_double(statement, 0);
  3132. // char *price = (char*)sqlite3_column_text(statement, 0);
  3133. // if(price!=nil)
  3134. // {
  3135. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3136. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3137. // if(nsprice.length>0)
  3138. // {
  3139. // double dp= [nsprice doubleValue];
  3140. // if(dp<dprice)
  3141. // dprice=dp;
  3142. // }
  3143. // }
  3144. // }
  3145. //
  3146. //
  3147. //
  3148. //
  3149. // sqlite3_finalize(statement);
  3150. //
  3151. //
  3152. //
  3153. //
  3154. // }
  3155. //
  3156. // // [iSalesDB close_db:db];
  3157. //
  3158. // if(dprice==DBL_MAX)
  3159. // ret= nil;
  3160. // else
  3161. // ret= [NSNumber numberWithDouble:dprice];
  3162. // return ret;
  3163. //}
  3164. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3165. {
  3166. assert(user!=nil);
  3167. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3168. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3169. NSString* whereprice=nil;
  3170. if(contact_id==nil)
  3171. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3172. else
  3173. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3174. // sqlite3 *db = [iSalesDB get_db];
  3175. NSString *productIdCondition = @"1 = 1";
  3176. if (product_id) {
  3177. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3178. }
  3179. NSString *itemIdCondition = @"";
  3180. if (item_id) {
  3181. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3182. }
  3183. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3184. sqlite3_stmt * statement;
  3185. NSNumber* ret = nil;
  3186. double dprice=DBL_MAX;
  3187. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3188. {
  3189. while (sqlite3_step(statement) == SQLITE_ROW)
  3190. {
  3191. // double val = sqlite3_column_double(statement, 0);
  3192. char *price = (char*)sqlite3_column_text(statement, 0);
  3193. if(price!=nil)
  3194. {
  3195. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3196. nsprice=[AESCrypt fastdecrypt:nsprice];
  3197. if(nsprice.length>0)
  3198. {
  3199. double dp= [nsprice doubleValue];
  3200. if(dp<dprice)
  3201. dprice=dp;
  3202. }
  3203. }
  3204. }
  3205. sqlite3_finalize(statement);
  3206. }
  3207. // [iSalesDB close_db:db];
  3208. if(dprice==DBL_MAX)
  3209. ret= nil;
  3210. else
  3211. ret= [NSNumber numberWithDouble:dprice];
  3212. return ret;
  3213. }
  3214. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3215. {
  3216. assert(user!=nil);
  3217. NSArray* ret=nil;
  3218. // sqlite3 *db = [iSalesDB get_db];
  3219. // no customer assigned , use login user contact_id
  3220. // UIApplication * app = [UIApplication sharedApplication];
  3221. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3222. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3223. sqlite3_stmt * statement;
  3224. // int count=0;
  3225. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3226. {
  3227. if (sqlite3_step(statement) == SQLITE_ROW)
  3228. {
  3229. char *val = (char*)sqlite3_column_text(statement, 0);
  3230. if(val==nil)
  3231. val="";
  3232. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3233. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3234. }
  3235. sqlite3_finalize(statement);
  3236. }
  3237. // [iSalesDB close_db:db];
  3238. return ret;
  3239. }
  3240. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3241. {
  3242. // sqlite3 *db = [iSalesDB get_db];
  3243. if(contact_id==nil)
  3244. {
  3245. // no customer assigned , use login user contact_id
  3246. // UIApplication * app = [UIApplication sharedApplication];
  3247. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3248. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3249. sqlite3_stmt * statement;
  3250. // int count=0;
  3251. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3252. {
  3253. if (sqlite3_step(statement) == SQLITE_ROW)
  3254. {
  3255. char *val = (char*)sqlite3_column_text(statement, 0);
  3256. if(val==nil)
  3257. val="";
  3258. contact_id = [[NSString alloc]initWithUTF8String:val];
  3259. }
  3260. sqlite3_finalize(statement);
  3261. }
  3262. if(contact_id.length<=0)
  3263. {
  3264. // [iSalesDB close_db:db];
  3265. return nil;
  3266. }
  3267. }
  3268. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3269. sqlite3_stmt * statement;
  3270. NSArray* ret=nil;
  3271. // int count=0;
  3272. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3273. {
  3274. if (sqlite3_step(statement) == SQLITE_ROW)
  3275. {
  3276. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3277. if(price_type==nil)
  3278. price_type="";
  3279. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3280. if(nsprice_type.length>0)
  3281. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3282. else
  3283. ret=nil;
  3284. }
  3285. sqlite3_finalize(statement);
  3286. }
  3287. // [iSalesDB close_db:db];
  3288. return ret;
  3289. }
  3290. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3291. {
  3292. NSString* ret= nil;
  3293. // sqlite3 *db = [iSalesDB get_db];
  3294. NSString *sqlQuery = nil;
  3295. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3296. // 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;
  3297. // 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
  3298. if(product_id==nil && model_name)
  3299. 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;
  3300. else if (product_id)
  3301. 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
  3302. sqlite3_stmt * statement;
  3303. // int count=0;
  3304. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3305. {
  3306. if (sqlite3_step(statement) == SQLITE_ROW)
  3307. {
  3308. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3309. if(imgurl==nil)
  3310. imgurl="";
  3311. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3312. ret=nsimgurl;
  3313. }
  3314. sqlite3_finalize(statement);
  3315. }
  3316. else
  3317. {
  3318. [ret setValue:@"8" forKey:@"result"];
  3319. }
  3320. // [iSalesDB close_db:db];
  3321. DebugLog(@"data string: %@",ret );
  3322. return ret;
  3323. }
  3324. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3325. {
  3326. NSString* ret= nil;
  3327. sqlite3 *db = [iSalesDB get_db];
  3328. NSString *sqlQuery = nil;
  3329. if(product_id==nil)
  3330. if(upc_code==nil)
  3331. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3332. else
  3333. 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='%@';
  3334. else
  3335. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3336. sqlite3_stmt * statement;
  3337. // int count=0;
  3338. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3339. {
  3340. if (sqlite3_step(statement) == SQLITE_ROW)
  3341. {
  3342. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3343. if(default_category==nil)
  3344. default_category="";
  3345. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3346. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3347. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3348. ret=nsdefault_category;
  3349. }
  3350. sqlite3_finalize(statement);
  3351. }
  3352. else
  3353. {
  3354. [ret setValue:@"8" forKey:@"result"];
  3355. }
  3356. [iSalesDB close_db:db];
  3357. DebugLog(@"data string: %@",ret );
  3358. return ret;
  3359. }
  3360. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3361. NSString *sql = nil;
  3362. if (product_id != nil) {
  3363. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3364. } else {
  3365. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3366. }
  3367. NSString *result_set = [iSalesDB jk_queryText:sql];
  3368. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3369. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3370. return result_set;
  3371. }
  3372. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3373. //{
  3374. //
  3375. //
  3376. // assert(params[@"user"]!=nil);
  3377. //
  3378. //
  3379. //
  3380. // NSString* model_name = [params valueForKey:@"product_name"];
  3381. //
  3382. // NSString* product_id = [params valueForKey:@"product_id"];
  3383. //
  3384. // NSString* category = [params valueForKey:@"category"];
  3385. //
  3386. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3387. // if(category==nil) {
  3388. // category = default_category_id;
  3389. // } else {
  3390. //
  3391. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3392. // // 参数重有多个category id
  3393. // if (arr_0.count > 1) {
  3394. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3395. //
  3396. // // 取交集
  3397. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3398. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3399. // [set_0 intersectSet:set_1];
  3400. //
  3401. // if (set_0.count == 1) {
  3402. // category = (NSString *)[set_0 anyObject];
  3403. // } else {
  3404. // if ([set_0 containsObject:default_category_id]) {
  3405. // category = default_category_id;
  3406. // } else {
  3407. // category = (NSString *)[set_0 anyObject];
  3408. // }
  3409. // }
  3410. // }
  3411. // }
  3412. //
  3413. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3414. //
  3415. //
  3416. //
  3417. //
  3418. //
  3419. //
  3420. // sqlite3 *db = [iSalesDB get_db];
  3421. //
  3422. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3423. //
  3424. //
  3425. // NSString *sqlQuery = nil;
  3426. //
  3427. // if(product_id==nil)
  3428. // 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='%@';
  3429. // else
  3430. //
  3431. // 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=%@;
  3432. //
  3433. //
  3434. // sqlite3_stmt * statement;
  3435. // [ret setValue:@"2" forKey:@"result"];
  3436. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3437. //
  3438. // // int count=0;
  3439. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3440. // {
  3441. //
  3442. //
  3443. // if (sqlite3_step(statement) == SQLITE_ROW)
  3444. // {
  3445. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3446. //
  3447. //
  3448. // char *name = (char*)sqlite3_column_text(statement, 0);
  3449. // if(name==nil)
  3450. // name="";
  3451. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3452. //
  3453. // char *description = (char*)sqlite3_column_text(statement, 1);
  3454. // if(description==nil)
  3455. // description="";
  3456. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3457. //
  3458. //
  3459. // int product_id = sqlite3_column_int(statement, 2);
  3460. //
  3461. //
  3462. // char *color = (char*)sqlite3_column_text(statement, 3);
  3463. // if(color==nil)
  3464. // color="";
  3465. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3466. // //
  3467. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3468. // // if(legcolor==nil)
  3469. // // legcolor="";
  3470. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3471. // //
  3472. // //
  3473. // int availability = sqlite3_column_int(statement, 5);
  3474. // //
  3475. // int incoming_stock = sqlite3_column_int(statement, 6);
  3476. //
  3477. //
  3478. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3479. // if(demension==nil)
  3480. // demension="";
  3481. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3482. //
  3483. //
  3484. //
  3485. // // ,,,,,,,,,
  3486. //
  3487. //
  3488. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3489. // if(seat_height==nil)
  3490. // seat_height="";
  3491. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3492. //
  3493. //
  3494. // char *material = (char*)sqlite3_column_text(statement, 9);
  3495. // if(material==nil)
  3496. // material="";
  3497. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3498. //
  3499. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3500. // if(box_dim==nil)
  3501. // box_dim="";
  3502. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3503. //
  3504. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3505. // if(volume==nil)
  3506. // volume="";
  3507. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3508. //
  3509. // double weight = sqlite3_column_double(statement, 12);
  3510. //
  3511. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3512. // if(model_set==nil)
  3513. // model_set="";
  3514. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3515. //
  3516. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3517. // if(load_ability==nil)
  3518. // load_ability="";
  3519. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3520. //
  3521. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3522. //// if(default_category==nil)
  3523. //// default_category="";
  3524. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3525. //
  3526. //
  3527. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3528. // if(fabric_content==nil)
  3529. // fabric_content="";
  3530. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3531. //
  3532. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3533. // if(assembling==nil)
  3534. // assembling="";
  3535. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3536. //
  3537. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3538. // if(made_in==nil)
  3539. // made_in="";
  3540. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3541. //
  3542. //
  3543. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3544. // if(special_remarks==nil)
  3545. // special_remarks="";
  3546. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3547. //
  3548. //
  3549. // int stockUcom = sqlite3_column_double(statement, 20);
  3550. //
  3551. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3552. // if(product_group==nil)
  3553. // product_group="";
  3554. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3555. //
  3556. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3557. // // if(fashion_selector==nil)
  3558. // // fashion_selector="";
  3559. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3560. //
  3561. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3562. // if(selector_field==nil)
  3563. // selector_field="";
  3564. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3565. //
  3566. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3567. // if(property_field==nil)
  3568. // property_field="";
  3569. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3570. //
  3571. //
  3572. //
  3573. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3574. // if(packaging==nil)
  3575. // packaging="";
  3576. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3577. //
  3578. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3579. //
  3580. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3581. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3582. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3583. //
  3584. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3585. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3586. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3587. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3588. //
  3589. //
  3590. //
  3591. //
  3592. //
  3593. // NSString* Availability=nil;
  3594. // if(availability>0)
  3595. // Availability=[NSString stringWithFormat:@"%d",availability];
  3596. // else
  3597. // Availability = @"Out of Stock";
  3598. //
  3599. // [img_section setValue:Availability forKey:@"Availability"];
  3600. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3601. //
  3602. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3603. //
  3604. //
  3605. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3606. // if(eta==nil)
  3607. // eta="";
  3608. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3609. // if ([nseta isEqualToString:@"null"]) {
  3610. // nseta = @"";
  3611. // }
  3612. // if (availability <= 0) {
  3613. // [img_section setValue:nseta forKey:@"ETA"];
  3614. // }
  3615. //
  3616. //
  3617. // int item_id = sqlite3_column_int(statement, 26);
  3618. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3619. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3620. //
  3621. // NSString* Price=nil;
  3622. // if(appDelegate.bLogin==false)
  3623. // Price=@"Must Sign in.";
  3624. // else
  3625. // {
  3626. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3627. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3628. // if(price==nil)
  3629. // Price=@"No Price.";
  3630. // else
  3631. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3632. // }
  3633. //
  3634. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3635. // [img_section setValue:Price forKey:@"price"];
  3636. // [img_section setValue:nsname forKey:@"model_name"];
  3637. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3638. //
  3639. // if (appDelegate.order_code) { // 离线order code即so#
  3640. //
  3641. // 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];
  3642. // __block int cartQTY = 0;
  3643. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3644. //
  3645. // cartQTY = sqlite3_column_int(stmt, 0);
  3646. //
  3647. // }];
  3648. //
  3649. // if (cartQTY > 0) {
  3650. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3651. // }
  3652. // }
  3653. //
  3654. // [ret setObject:img_section forKey:@"img_section"];
  3655. //
  3656. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3657. // int detail0_item_count=0;
  3658. //
  3659. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3660. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3661. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3662. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3663. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3664. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3665. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3666. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3667. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3668. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3669. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3670. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3671. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3672. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3673. //
  3674. //
  3675. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3676. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3677. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3678. // {
  3679. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3680. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3681. // }
  3682. //
  3683. //
  3684. //
  3685. //
  3686. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3687. // [detail0_section setValue:@"kv" forKey:@"type"];
  3688. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3689. //
  3690. // [ret setObject:detail0_section forKey:@"detail_0"];
  3691. //
  3692. //
  3693. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3694. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3695. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3696. // // [detail1_section setValue:@"content" forKey:@"type"];
  3697. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3698. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3699. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3700. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3701. // [ret setObject:detail1_section forKey:@"detail_1"];
  3702. //
  3703. //
  3704. //
  3705. //
  3706. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3707. // [detail2_section setValue:@"detail" forKey:@"target"];
  3708. // [detail2_section setValue:@"popup" forKey:@"action"];
  3709. // [detail2_section setValue:@"content" forKey:@"type"];
  3710. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3711. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3712. // [detail2_section setValue:@"local" forKey:@"data"];
  3713. // [ret setObject:detail2_section forKey:@"detail_2"];
  3714. // }
  3715. //
  3716. //
  3717. //
  3718. //
  3719. // sqlite3_finalize(statement);
  3720. // }
  3721. // else
  3722. // {
  3723. // [ret setValue:@"8" forKey:@"result"];
  3724. // }
  3725. //// DebugLog(@"count:%d",count);
  3726. //
  3727. //
  3728. // [iSalesDB close_db:db];
  3729. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3730. //
  3731. // return ret;
  3732. //}
  3733. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3734. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3735. NSString* keyword = [params valueForKey:@"keyword"];
  3736. keyword=keyword.lowercaseString;
  3737. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3738. int limit = [[params valueForKey:@"limit"] intValue];
  3739. int offset = [[params valueForKey:@"offset"] intValue];
  3740. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3741. NSString *limit_str = @"";
  3742. if (limited) {
  3743. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3744. }
  3745. sqlite3 *db = [iSalesDB get_db];
  3746. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3747. // UIApplication * app = [UIApplication sharedApplication];
  3748. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3749. NSString *sqlQuery = nil;
  3750. if(exactMatch )
  3751. 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 ;
  3752. else
  3753. 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
  3754. DebugLog(@"offline_search sql:%@",sqlQuery);
  3755. sqlite3_stmt * statement;
  3756. [ret setValue:@"2" forKey:@"result"];
  3757. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3758. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3759. // int count=0;
  3760. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3761. {
  3762. int i=0;
  3763. while (sqlite3_step(statement) == SQLITE_ROW)
  3764. {
  3765. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3766. // char *name = (char*)sqlite3_column_text(statement, 1);
  3767. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3768. char *name = (char*)sqlite3_column_text(statement, 0);
  3769. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3770. char *description = (char*)sqlite3_column_text(statement, 1);
  3771. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3772. int product_id = sqlite3_column_int(statement, 2);
  3773. // char *url = (char*)sqlite3_column_text(statement, 3);
  3774. // if(url==nil)
  3775. // url="";
  3776. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3777. int wid = sqlite3_column_int(statement, 3);
  3778. int closeout = sqlite3_column_int(statement, 4);
  3779. int cid = sqlite3_column_int(statement, 5);
  3780. int wisdelete = sqlite3_column_int(statement, 6);
  3781. int more_color = sqlite3_column_int(statement, 7);
  3782. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3783. if(wid !=0 && wisdelete != 1)
  3784. [item setValue:@"true" forKey:@"wish_exists"];
  3785. else
  3786. [item setValue:@"false" forKey:@"wish_exists"];
  3787. if(closeout==0)
  3788. [item setValue:@"false" forKey:@"is_closeout"];
  3789. else
  3790. [item setValue:@"true" forKey:@"is_closeout"];
  3791. if(cid==0)
  3792. [item setValue:@"false" forKey:@"cart_exists"];
  3793. else
  3794. [item setValue:@"true" forKey:@"cart_exists"];
  3795. if (more_color == 0) {
  3796. [item setObject:@(false) forKey:@"more_color"];
  3797. } else if (more_color == 1) {
  3798. [item setObject:@(true) forKey:@"more_color"];
  3799. }
  3800. [item addEntriesFromDictionary:imgjson];
  3801. // [item setValue:nsurl forKey:@"img"];
  3802. [item setValue:nsname forKey:@"fash_name"];
  3803. [item setValue:nsdescription forKey:@"description"];
  3804. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3805. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3806. i++;
  3807. }
  3808. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3809. [ret setObject:items forKey:@"items"];
  3810. sqlite3_finalize(statement);
  3811. }
  3812. DebugLog(@"count:%d",count);
  3813. [iSalesDB close_db:db];
  3814. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3815. return ret;
  3816. }
  3817. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3818. {
  3819. return [self search:params limited:YES];
  3820. }
  3821. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3822. //{
  3823. // UIApplication * app = [UIApplication sharedApplication];
  3824. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3825. //
  3826. //// [iSalesDB disable_trigger]
  3827. // [iSalesDB disable_trigger];
  3828. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3829. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3830. //
  3831. // [iSalesDB enable_trigger];
  3832. //// [iSalesDB enable_trigger]
  3833. // //
  3834. // // NSString* user = [params valueForKey:@"user"];
  3835. // //
  3836. // // NSString* password = [params valueForKey:@"password"];
  3837. //
  3838. //
  3839. //
  3840. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3841. //
  3842. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3843. //
  3844. //
  3845. //
  3846. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3847. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3848. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3849. //
  3850. // [appDelegate update_count_mark];
  3851. //
  3852. //
  3853. // appDelegate.can_show_price =false;
  3854. // appDelegate.can_see_price =false;
  3855. // appDelegate.can_create_portfolio =false;
  3856. // appDelegate.can_create_order =false;
  3857. //
  3858. //
  3859. // appDelegate.can_cancel_order =false;
  3860. // appDelegate.can_set_cart_price =false;
  3861. // appDelegate.can_delete_order =false;
  3862. // appDelegate.can_submit_order =false;
  3863. // appDelegate.can_set_tearsheet_price =false;
  3864. // appDelegate.can_update_contact_info = false;
  3865. //
  3866. // appDelegate.save_order_logout = false;
  3867. // appDelegate.submit_order_logout = false;
  3868. // appDelegate.alert_sold_in_quantities = false;
  3869. //
  3870. // appDelegate.ipad_perm =nil ;
  3871. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3872. // appDelegate.OrderFilter= nil;
  3873. // [appDelegate SetSo:nil];
  3874. // [appDelegate set_main_button_panel];
  3875. //
  3876. //
  3877. // // sqlite3 *db = [iSalesDB get_db];
  3878. // //
  3879. // //
  3880. // //
  3881. // //
  3882. // //
  3883. // // 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"]];
  3884. // //
  3885. // //
  3886. // //
  3887. // //
  3888. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3889. // // sqlite3_stmt * statement;
  3890. // //
  3891. // //
  3892. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3893. // //
  3894. // //
  3895. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3896. // // {
  3897. // //
  3898. // //
  3899. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3900. // // {
  3901. // //
  3902. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3903. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3904. // //
  3905. // //
  3906. // //
  3907. // // int can_show_price = sqlite3_column_int(statement, 0);
  3908. // // int can_see_price = sqlite3_column_int(statement, 1);
  3909. // //
  3910. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3911. // // if(contact_id==nil)
  3912. // // contact_id="";
  3913. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3914. // //
  3915. // // int user_type = sqlite3_column_int(statement, 3);
  3916. // //
  3917. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3918. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3919. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3920. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3921. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3922. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3923. // // int can_create_order = sqlite3_column_int(statement, 10);
  3924. // //
  3925. // //
  3926. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3927. // // if(mode==nil)
  3928. // // mode="";
  3929. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3930. // //
  3931. // //
  3932. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3933. // // if(username==nil)
  3934. // // username="";
  3935. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3936. // //
  3937. // //
  3938. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3939. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3940. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3941. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3942. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3943. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3944. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3945. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3946. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3947. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3948. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3949. // //
  3950. // // [header setValue:nsusername forKey:@"username"];
  3951. // //
  3952. // //
  3953. // // [ret setObject:header forKey:@"header"];
  3954. // // [ret setValue:nsmode forKey:@"mode"];
  3955. // //
  3956. // //
  3957. // // }
  3958. // //
  3959. // //
  3960. // //
  3961. // // sqlite3_finalize(statement);
  3962. // // }
  3963. // //
  3964. // //
  3965. // //
  3966. // // [iSalesDB close_db:db];
  3967. // //
  3968. // //
  3969. // //
  3970. // //
  3971. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3972. //
  3973. // return ret;
  3974. //}
  3975. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3976. //{
  3977. //
  3978. // UIApplication * app = [UIApplication sharedApplication];
  3979. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3980. //
  3981. // [iSalesDB disable_trigger];
  3982. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3983. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3984. // [iSalesDB enable_trigger];
  3985. //
  3986. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3987. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3988. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3989. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3990. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3991. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3992. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3993. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3994. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3995. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3996. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3997. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3998. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3999. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  4000. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  4001. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  4002. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4003. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  4004. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  4005. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  4006. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  4007. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  4008. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  4009. //
  4010. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4011. //
  4012. // [arr_name addObject:customer_first_name];
  4013. // [arr_name addObject:customer_last_name];
  4014. //
  4015. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4016. //
  4017. // // default ship from
  4018. // 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';";
  4019. //
  4020. // __block NSString *cid = @"";
  4021. // __block NSString *name = @"";
  4022. // __block NSString *ext = @"";
  4023. // __block NSString *contact = @"";
  4024. // __block NSString *email = @"";
  4025. // __block NSString *fax = @"";
  4026. // __block NSString *phone = @"";
  4027. //
  4028. // sqlite3 *db = [iSalesDB get_db];
  4029. //
  4030. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  4031. //
  4032. // cid = [self textAtColumn:0 statement:statment];
  4033. // name = [self textAtColumn:1 statement:statment];
  4034. // ext = [self textAtColumn:2 statement:statment];
  4035. // contact = [self textAtColumn:3 statement:statment];
  4036. // email = [self textAtColumn:4 statement:statment];
  4037. // fax = [self textAtColumn:5 statement:statment];
  4038. // phone = [self textAtColumn:6 statement:statment];
  4039. //
  4040. // }];
  4041. //
  4042. // NSString* so_id = [self get_offline_soid:db];
  4043. // if(so_id==nil)
  4044. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  4045. //
  4046. // 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];
  4047. //
  4048. //
  4049. //
  4050. // int result =[iSalesDB execSql:sql_neworder db:db];
  4051. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  4052. //
  4053. //
  4054. //
  4055. // //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'
  4056. // //soId
  4057. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  4058. //
  4059. //
  4060. //
  4061. //
  4062. //
  4063. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  4064. // sqlite3_stmt * statement;
  4065. //
  4066. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4067. // {
  4068. // if (sqlite3_step(statement) == SQLITE_ROW)
  4069. // {
  4070. // // char *name = (char*)sqlite3_column_text(statement, 1);
  4071. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4072. //
  4073. // //ret = sqlite3_column_int(statement, 0);
  4074. //
  4075. // char *soId = (char*)sqlite3_column_text(statement, 0);
  4076. // if(soId==nil)
  4077. // soId="";
  4078. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  4079. // [ret setValue:nssoId forKey:@"soId"];
  4080. // [ret setValue:nssoId forKey:@"orderCode"];
  4081. //
  4082. // }
  4083. // sqlite3_finalize(statement);
  4084. // }
  4085. //
  4086. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  4087. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  4088. //
  4089. //
  4090. // [iSalesDB close_db:db];
  4091. //
  4092. // return [RAConvertor dict2data:ret];
  4093. //
  4094. //}
  4095. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  4096. {
  4097. //assert(params[@"order_code"]);
  4098. // assert(params[@"order_code"]);
  4099. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4100. NSString* orderCode = [params valueForKey:@"orderCode"];
  4101. NSString* app_order_code= params[@"appDelegate.order_code"];
  4102. // UIApplication * app = [UIApplication sharedApplication];
  4103. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4104. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  4105. {
  4106. [iSalesDB disable_trigger];
  4107. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4108. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4109. [iSalesDB enable_trigger];
  4110. }
  4111. sqlite3 *db = [iSalesDB get_db];
  4112. int cart_count=[self query_ordercartcount:orderCode db:db];
  4113. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4114. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4115. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4116. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4117. [iSalesDB close_db:db];
  4118. return [RAConvertor dict2data:ret];
  4119. }
  4120. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4121. {
  4122. assert(params[@"can_create_backorder"]!=nil);
  4123. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4124. sqlite3 *db = [iSalesDB get_db];
  4125. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4126. int count =0;
  4127. if(params[@"count"]!=nil)
  4128. {
  4129. count = [params[@"count"] intValue];
  4130. }
  4131. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4132. NSString* product_id=params[@"product_id"];
  4133. NSString* orderCode=params[@"orderCode"];
  4134. NSString *qty = params[@"qty"];
  4135. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4136. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4137. __block int number_of_outOfStock = 0;
  4138. for(int i=0;i<arr_id.count;i++)
  4139. {
  4140. NSInteger item_qty= count;
  4141. if (qty) {
  4142. item_qty = [qty_arr[i] integerValue];
  4143. }
  4144. if(item_qty==0)
  4145. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4146. // 检查新加Model数量是否大于库存
  4147. if (![params[@"can_create_backorder"] boolValue]) {
  4148. __block BOOL needContinue = NO;
  4149. [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) {
  4150. int availability = sqlite3_column_int(stmt, 0);
  4151. // 库存小于购买量为缺货
  4152. if (availability < item_qty || availability <= 0) {
  4153. number_of_outOfStock++;
  4154. needContinue = YES;
  4155. }
  4156. }];
  4157. if (needContinue) {
  4158. continue;
  4159. }
  4160. }
  4161. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4162. NSString* sql=nil;
  4163. sqlite3_stmt *stmt = nil;
  4164. BOOL shouldStep = NO;
  4165. if(_id<0)
  4166. {
  4167. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4168. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4169. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4170. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4171. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4172. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4173. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4174. shouldStep = YES;
  4175. }
  4176. else
  4177. {
  4178. if (qty) { // wish list move to cart
  4179. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4180. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4181. sqlite3_bind_int(stmt, 1, _id);
  4182. shouldStep = YES;
  4183. } else {
  4184. 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];
  4185. __block BOOL update = YES;
  4186. if (![params[@"can_create_backorder"] boolValue]) {
  4187. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4188. int newQTY = sqlite3_column_int(stmt, 0);
  4189. int availability = sqlite3_column_int(stmt, 1);
  4190. if (newQTY > availability) { // 库存不够
  4191. update = NO;
  4192. number_of_outOfStock++;
  4193. }
  4194. }];
  4195. }
  4196. if (update) {
  4197. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4198. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4199. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4200. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4201. shouldStep = YES;
  4202. }
  4203. }
  4204. }
  4205. if (shouldStep) {
  4206. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4207. [iSalesDB execSql:@"ROLLBACK" db:db];
  4208. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4209. [iSalesDB close_db:db];
  4210. DebugLog(@"add to cart error");
  4211. return [RAConvertor dict2data:ret];
  4212. }
  4213. }
  4214. }
  4215. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4216. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4217. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4218. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4219. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4220. if (![params[@"can_create_backorder"] boolValue]) {
  4221. if (number_of_outOfStock > 0) {
  4222. ret[@"result"]=[NSNumber numberWithInt:8];
  4223. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4224. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4225. }
  4226. }
  4227. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4228. [iSalesDB close_db:db];
  4229. return [RAConvertor dict2data:ret];
  4230. }
  4231. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4232. {
  4233. // UIApplication * app = [UIApplication sharedApplication];
  4234. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4235. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4236. 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];
  4237. sqlite3_stmt * statement;
  4238. int count=0;
  4239. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4240. {
  4241. while (sqlite3_step(statement) == SQLITE_ROW)
  4242. {
  4243. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4244. int bitem_id = sqlite3_column_int(statement, 0);
  4245. int bitem_qty = sqlite3_column_int(statement, 1);
  4246. char *name = (char*)sqlite3_column_text(statement, 2);
  4247. if(name==nil)
  4248. name="";
  4249. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4250. char *description = (char*)sqlite3_column_text(statement, 3);
  4251. if(description==nil)
  4252. description="";
  4253. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4254. double unit_price = sqlite3_column_double(statement, 4);
  4255. int use_unitprice = sqlite3_column_int(statement, 5);
  4256. if(use_unitprice!=1)
  4257. {
  4258. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4259. }
  4260. itemjson[@"model"]=nsname;
  4261. itemjson[@"description"]=nsdescription;
  4262. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4263. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4264. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4265. if(compute)
  4266. {
  4267. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4268. }
  4269. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4270. count++;
  4271. }
  4272. sqlite3_finalize(statement);
  4273. }
  4274. ret[@"count"]=@(count);
  4275. if(count==0)
  4276. return nil;
  4277. else
  4278. return ret;
  4279. }
  4280. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4281. {
  4282. //compute: add part to subtotal;
  4283. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4284. dict_item[@(item_id)]=@"1";
  4285. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4286. double cuft=0;
  4287. double weight=0;
  4288. int carton=0;
  4289. 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];
  4290. sqlite3_stmt * statement;
  4291. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4292. {
  4293. if (sqlite3_step(statement) == SQLITE_ROW)
  4294. {
  4295. double ulength = sqlite3_column_double(statement, 0);
  4296. double uwidth = sqlite3_column_double(statement, 1);
  4297. double uheight = sqlite3_column_double(statement, 2);
  4298. double uweight = sqlite3_column_double(statement, 3);
  4299. double mlength = sqlite3_column_double(statement, 4);
  4300. double mwidth = sqlite3_column_double(statement, 5);
  4301. double mheight = sqlite3_column_double(statement, 6);
  4302. double mweight = sqlite3_column_double(statement, 7);
  4303. double ilength = sqlite3_column_double(statement, 8);
  4304. double iwidth = sqlite3_column_double(statement, 9);
  4305. double iheight = sqlite3_column_double(statement, 10);
  4306. double iweight = sqlite3_column_double(statement, 11);
  4307. // int pcs = sqlite3_column_int(statement,12);
  4308. int mpack = sqlite3_column_int(statement, 13);
  4309. int ipack = sqlite3_column_int(statement, 14);
  4310. double ucbf = sqlite3_column_double(statement, 15);
  4311. // double icbf = sqlite3_column_double(statement, 16);
  4312. // double mcbf = sqlite3_column_double(statement, 17);
  4313. if(ipack==0)
  4314. {
  4315. carton= count/mpack ;
  4316. weight = mweight*carton;
  4317. cuft= carton*(mlength*mwidth*mheight);
  4318. int remain=count%mpack;
  4319. if(remain==0)
  4320. {
  4321. //do nothing;
  4322. }
  4323. else
  4324. {
  4325. carton++;
  4326. weight += uweight*remain;
  4327. cuft += (ulength*uwidth*uheight)*remain;
  4328. }
  4329. }
  4330. else
  4331. {
  4332. carton = count/(mpack*ipack);
  4333. weight = mweight*carton;
  4334. cuft= carton*(mlength*mwidth*mheight);
  4335. int remain=count%(mpack*ipack);
  4336. if(remain==0)
  4337. {
  4338. // do nothing;
  4339. }
  4340. else
  4341. {
  4342. carton++;
  4343. int icarton =remain/ipack;
  4344. int iremain=remain%ipack;
  4345. weight += iweight*icarton;
  4346. cuft += (ilength*iwidth*iheight)*icarton;
  4347. if(iremain==0)
  4348. {
  4349. //do nothing;
  4350. }
  4351. else
  4352. {
  4353. weight += uweight*iremain;
  4354. cuft += (ulength*uwidth*uheight)*iremain;
  4355. }
  4356. }
  4357. }
  4358. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4359. cuft=ucbf*count;
  4360. weight= uweight*count;
  4361. #endif
  4362. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4363. cuft=ucbf*count;
  4364. weight= uweight*count;
  4365. #endif
  4366. }
  4367. sqlite3_finalize(statement);
  4368. }
  4369. if(compute)
  4370. {
  4371. NSArray * arr_count=nil;
  4372. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4373. for(int i=0;i<arr_bundle.count;i++)
  4374. {
  4375. dict_item[arr_bundle[i]]=@"1";
  4376. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4377. cuft+=[bundlejson[@"cuft"] doubleValue];
  4378. weight+=[bundlejson[@"weight"] doubleValue];
  4379. carton+=[bundlejson[@"carton"] intValue];
  4380. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4381. }
  4382. }
  4383. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4384. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4385. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4386. ret[@"items"]=dict_item;
  4387. return ret;
  4388. }
  4389. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4390. {
  4391. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4392. // 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 ];
  4393. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4394. DebugLog(@"offline_login sql:%@",sqlQuery);
  4395. sqlite3_stmt * statement;
  4396. int cart_count=0;
  4397. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4398. if ( dbresult== SQLITE_OK)
  4399. {
  4400. while (sqlite3_step(statement) == SQLITE_ROW)
  4401. {
  4402. int item_id = sqlite3_column_int(statement, 0);
  4403. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4404. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4405. }
  4406. sqlite3_finalize(statement);
  4407. }
  4408. return cart_count;
  4409. }
  4410. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4411. //{
  4412. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4413. // sqlite3 *db = [iSalesDB get_db];
  4414. // UIApplication * app = [UIApplication sharedApplication];
  4415. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4416. //
  4417. // NSString* orderCode=params[@"orderCode"];
  4418. //
  4419. // int sort = [[params objectForKey:@"sort"] intValue];
  4420. // NSString *sort_str = @"";
  4421. // switch (sort) {
  4422. // case 0:{
  4423. // sort_str = @"order by c.modify_time desc";
  4424. // }
  4425. // break;
  4426. // case 1:{
  4427. // sort_str = @"order by c.modify_time asc";
  4428. // }
  4429. // break;
  4430. // case 2:{
  4431. // sort_str = @"order by m.name asc";
  4432. // }
  4433. // break;
  4434. // case 3:{
  4435. // sort_str = @"order by m.name desc";
  4436. // }
  4437. // break;
  4438. // case 4:{
  4439. // sort_str = @"order by m.description asc";
  4440. // }
  4441. // break;
  4442. //
  4443. // default:
  4444. // break;
  4445. //
  4446. // }
  4447. //
  4448. //
  4449. //
  4450. // 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 ];
  4451. //
  4452. //
  4453. //// 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 ];
  4454. //
  4455. //
  4456. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4457. //
  4458. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4459. // sqlite3_stmt * statement;
  4460. //
  4461. //
  4462. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4463. //
  4464. // NSDate *date1 = [NSDate date];
  4465. //
  4466. // int count=0;
  4467. // int cart_count=0;
  4468. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4469. // if ( dbresult== SQLITE_OK)
  4470. // {
  4471. //
  4472. //
  4473. // while (sqlite3_step(statement) == SQLITE_ROW)
  4474. // {
  4475. //// NSDate *row_date = [NSDate date];
  4476. //
  4477. //
  4478. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4479. //
  4480. //
  4481. //
  4482. //
  4483. // int product_id = sqlite3_column_int(statement, 0);
  4484. //
  4485. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4486. //
  4487. // int item_id = sqlite3_column_int(statement, 7);
  4488. //
  4489. // NSString* Price=nil;
  4490. // if(str_price==nil)
  4491. // {
  4492. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4493. //// NSDate *price_date = [NSDate date];
  4494. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4495. //// DebugLog(@"price time interval");
  4496. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4497. //
  4498. // if(price==nil)
  4499. // Price=@"No Price.";
  4500. // else
  4501. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4502. // }
  4503. // else
  4504. // {
  4505. //
  4506. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4507. // }
  4508. //
  4509. //
  4510. // double discount = sqlite3_column_double(statement, 2);
  4511. // int item_count = sqlite3_column_int(statement, 3);
  4512. //
  4513. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4514. // NSString *nsline_note=nil;
  4515. // if(line_note!=nil)
  4516. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4517. //
  4518. //
  4519. // char *name = (char*)sqlite3_column_text(statement, 5);
  4520. // NSString *nsname=nil;
  4521. // if(name!=nil)
  4522. // nsname= [[NSString alloc]initWithUTF8String:name];
  4523. //
  4524. // char *description = (char*)sqlite3_column_text(statement, 6);
  4525. // NSString *nsdescription=nil;
  4526. // if(description!=nil)
  4527. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4528. //
  4529. //
  4530. //
  4531. // int stockUom = sqlite3_column_int(statement, 8);
  4532. // int _id = sqlite3_column_int(statement, 9);
  4533. // int availability = sqlite3_column_int(statement, 10);
  4534. //
  4535. //// NSDate *subtotal_date = [NSDate date];
  4536. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4537. //// DebugLog(@"subtotal_date time interval");
  4538. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4539. //
  4540. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4541. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4542. // int carton=[bsubtotaljson[@"carton"] intValue];
  4543. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4544. //
  4545. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4546. //// NSDate *img_date = [NSDate date];
  4547. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4548. //// DebugLog(@"img_date time interval");
  4549. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4550. //
  4551. // itemjson[@"model"]=nsname;
  4552. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4553. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4554. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4555. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4556. // itemjson[@"check"]=@"true";
  4557. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4558. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4559. // itemjson[@"unit_price"]=Price;
  4560. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4561. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4562. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4563. // itemjson[@"note"]=nsline_note;
  4564. // if (!appDelegate.can_create_backorder) {
  4565. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4566. // }
  4567. //// NSDate *date2 = [NSDate date];
  4568. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4569. //// DebugLog(@"model_bundle time interval");
  4570. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4571. //
  4572. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4573. // count++;
  4574. //
  4575. //// DebugLog(@"row time interval");
  4576. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4577. // }
  4578. //
  4579. //
  4580. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4581. // sqlite3_finalize(statement);
  4582. // }
  4583. //
  4584. //
  4585. // DebugLog(@"request cart total time interval");
  4586. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4587. //
  4588. //
  4589. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4590. //
  4591. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4592. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4593. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4594. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4595. //
  4596. // ret[@"mode"]=@"Regular Mode";
  4597. //
  4598. // [iSalesDB close_db:db];
  4599. //
  4600. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4601. //
  4602. // DebugLog(@"general notes :%@",general_note);
  4603. //
  4604. // ret[@"general_note"]= general_note;
  4605. //
  4606. // return [RAConvertor dict2data:ret];
  4607. //}
  4608. +(NSData*) offline_login :(NSMutableDictionary *) params
  4609. {
  4610. NSString* user = [params valueForKey:@"user"];
  4611. NSString* password = [params valueForKey:@"password"];
  4612. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4613. sqlite3 *db = [iSalesDB get_db];
  4614. 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"]];
  4615. DebugLog(@"offline_login sql:%@",sqlQuery);
  4616. sqlite3_stmt * statement;
  4617. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4618. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4619. {
  4620. if (sqlite3_step(statement) == SQLITE_ROW)
  4621. {
  4622. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4623. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4624. int can_show_price = sqlite3_column_int(statement, 0);
  4625. int can_see_price = sqlite3_column_int(statement, 1);
  4626. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4627. if(contact_id==nil)
  4628. contact_id="";
  4629. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4630. int user_type = sqlite3_column_int(statement, 3);
  4631. int can_cancel_order = sqlite3_column_int(statement, 4);
  4632. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4633. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4634. int can_delete_order = sqlite3_column_int(statement, 7);
  4635. int can_submit_order = sqlite3_column_int(statement, 8);
  4636. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4637. int can_create_order = sqlite3_column_int(statement, 10);
  4638. char *mode = (char*)sqlite3_column_text(statement, 11);
  4639. if(mode==nil)
  4640. mode="";
  4641. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4642. char *username = (char*)sqlite3_column_text(statement, 12);
  4643. if(username==nil)
  4644. username="";
  4645. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4646. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4647. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4648. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4649. [header setValue:nscontact_id forKey:@"contact_id"];
  4650. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4651. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4652. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4653. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4654. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4655. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4656. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4657. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4658. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4659. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4660. [header setValue:nsusername forKey:@"username"];
  4661. NSError* error=nil;
  4662. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4663. [header setValue:statusFilter forKey:@"statusFilter"];
  4664. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4665. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4666. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4667. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4668. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4669. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4670. [ret setObject:header forKey:@"header"];
  4671. [ret setValue:nsmode forKey:@"mode"];
  4672. }
  4673. sqlite3_finalize(statement);
  4674. }
  4675. [iSalesDB close_db:db];
  4676. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4677. return [RAConvertor dict2data:ret];
  4678. }
  4679. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4680. {
  4681. assert(params[@"mode"]!=nil);
  4682. NSString* contactId = [params valueForKey:@"contactId"];
  4683. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4684. sqlite3 *db = [iSalesDB get_db];
  4685. NSString *sqlQuery = nil;
  4686. {
  4687. 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];
  4688. }
  4689. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4690. sqlite3_stmt * statement;
  4691. [ret setValue:@"2" forKey:@"result"];
  4692. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4693. {
  4694. //int i = 0;
  4695. if (sqlite3_step(statement) == SQLITE_ROW)
  4696. {
  4697. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4698. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4699. // int editable = sqlite3_column_int(statement, 0);
  4700. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4701. NSString *nscompany_name =nil;
  4702. if(company_name==nil)
  4703. nscompany_name=@"";
  4704. else
  4705. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4706. char *country = (char*)sqlite3_column_text(statement, 2);
  4707. if(country==nil)
  4708. country="";
  4709. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4710. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4711. // if(addr==nil)
  4712. // addr="";
  4713. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4714. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4715. if(zipcode==nil)
  4716. zipcode="";
  4717. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4718. char *state = (char*)sqlite3_column_text(statement, 5);
  4719. if(state==nil)
  4720. state="";
  4721. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4722. char *city = (char*)sqlite3_column_text(statement, 6);
  4723. if(city==nil)
  4724. city="";
  4725. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4726. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4727. // NSString *nscontact_name = nil;
  4728. // if(contact_name==nil)
  4729. // nscontact_name=@"";
  4730. // else
  4731. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4732. char *phone = (char*)sqlite3_column_text(statement, 8);
  4733. NSString *nsphone = nil;
  4734. if(phone==nil)
  4735. nsphone=@"";
  4736. else
  4737. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4738. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4739. // if(contact_id==nil)
  4740. // contact_id="";
  4741. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4742. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4743. if(addr_1==nil)
  4744. addr_1="";
  4745. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4746. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4747. if(addr_2==nil)
  4748. addr_2="";
  4749. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4750. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4751. if(addr_3==nil)
  4752. addr_3="";
  4753. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4754. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4755. if(addr_4==nil)
  4756. addr_4="";
  4757. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4758. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4759. if(first_name==nil)
  4760. first_name="";
  4761. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4762. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4763. if(last_name==nil)
  4764. last_name="";
  4765. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4766. char *fax = (char*)sqlite3_column_text(statement, 16);
  4767. NSString *nsfax = nil;
  4768. if(fax==nil)
  4769. nsfax=@"";
  4770. else
  4771. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4772. char *email = (char*)sqlite3_column_text(statement, 17);
  4773. NSString *nsemail = nil;
  4774. if(email==nil)
  4775. nsemail=@"";
  4776. else
  4777. nsemail= [[NSString alloc]initWithUTF8String:email];
  4778. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4779. NSString *nsimg_0 = nil;
  4780. if(img_0==nil)
  4781. nsimg_0=@"";
  4782. else
  4783. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4784. [self copy_bcardImg:nsimg_0];
  4785. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4786. NSString *nsimg_1 = nil;
  4787. if(img_1==nil)
  4788. nsimg_1=@"";
  4789. else
  4790. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4791. [self copy_bcardImg:nsimg_1];
  4792. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4793. NSString *nsimg_2 = nil;
  4794. if(img_2==nil)
  4795. nsimg_2=@"";
  4796. else
  4797. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4798. [self copy_bcardImg:nsimg_2];
  4799. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4800. NSString *nsprice_type = nil;
  4801. if(price_type==nil)
  4802. nsprice_type=@"";
  4803. else
  4804. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4805. char *notes = (char*)sqlite3_column_text(statement, 22);
  4806. NSString *nsnotes = nil;
  4807. if(notes==nil)
  4808. nsnotes=@"";
  4809. else
  4810. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4811. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4812. NSString *nssalesrep = nil;
  4813. if(salesrep==nil)
  4814. nssalesrep=@"";
  4815. else
  4816. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4817. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4818. {
  4819. // decrypt
  4820. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4821. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4822. nsphone=[AESCrypt fastdecrypt:nsphone];
  4823. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4824. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4825. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4826. }
  4827. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4828. [arr_name addObject:nsfirst_name];
  4829. [arr_name addObject:nslast_name];
  4830. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4831. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4832. [arr_ext addObject:nsaddr_1];
  4833. [arr_ext addObject:nsaddr_2];
  4834. [arr_ext addObject:nsaddr_3];
  4835. [arr_ext addObject:nsaddr_4];
  4836. [arr_ext addObject:@"\r\n"];
  4837. [arr_ext addObject:nscity];
  4838. [arr_ext addObject:nsstate];
  4839. [arr_ext addObject:nszipcode];
  4840. [arr_ext addObject:nscountry];
  4841. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4842. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4843. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4844. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4845. [item setValue:nscountry forKey:@"customer_country"];
  4846. [item setValue:nsphone forKey:@"customer_phone"];
  4847. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4848. [item setValue:nscompany_name forKey:@"customer_name"];
  4849. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4850. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4851. [item setValue:nsext forKey:@"customer_contact_ext"];
  4852. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4853. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4854. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4855. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4856. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4857. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4858. [item setValue:nslast_name forKey:@"customer_last_name"];
  4859. [item setValue:nscity forKey:@"customer_city"];
  4860. [item setValue:nsstate forKey:@"customer_state"];
  4861. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4862. [item setValue:contactId forKey:@"customer_cid"];
  4863. [item setValue:nscontact_name forKey:@"customer_contact"];
  4864. [item setValue:nsfax forKey:@"customer_fax"];
  4865. [item setValue:nsemail forKey:@"customer_email"];
  4866. [item setValue:contact_type forKey:@"customer_contact_type"];
  4867. [ret setObject:item forKey:@"customerInfo"];
  4868. // i++;
  4869. }
  4870. // UIApplication * app = [UIApplication sharedApplication];
  4871. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4872. [ret setValue:params[@"mode"] forKey:@"mode"];
  4873. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4874. sqlite3_finalize(statement);
  4875. }
  4876. [iSalesDB close_db:db];
  4877. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4878. return ret;
  4879. }
  4880. + (bool) copy_bcardImg:(NSString*) filename
  4881. {
  4882. if(filename.length==0)
  4883. return false;
  4884. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4885. bool ret=false;
  4886. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4887. NSString *cachefolder = [paths objectAtIndex:0];
  4888. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4889. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4890. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4891. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4892. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4893. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4894. //
  4895. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4896. NSFileManager* fileManager = [NSFileManager defaultManager];
  4897. BOOL bdir=NO;
  4898. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4899. {
  4900. NSError *error = nil;
  4901. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4902. {
  4903. ret=false;
  4904. }
  4905. else
  4906. {
  4907. ret=true;
  4908. }
  4909. // NSError *error = nil;
  4910. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4911. //
  4912. // if(!bsuccess)
  4913. // {
  4914. // DebugLog(@"Create offline_createimg folder failed");
  4915. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4916. // return [RAConvertor dict2data:ret];
  4917. // }
  4918. // if(bsuccess)
  4919. // {
  4920. // sqlite3 *db = [self get_db];
  4921. //
  4922. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4923. // [iSalesDB close_db:db];
  4924. // }
  4925. }
  4926. return ret;
  4927. //
  4928. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4929. // if(bsuccess)
  4930. // {
  4931. // NSError *error = nil;
  4932. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4933. // {
  4934. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4935. // }
  4936. // else
  4937. // {
  4938. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4939. //
  4940. // ret[@"img_url_aname"]=filename;
  4941. // ret[@"img_url"]=savedImagePath;
  4942. // }
  4943. // }
  4944. }
  4945. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4946. {
  4947. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4948. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4949. NSString *cachefolder = [paths objectAtIndex:0];
  4950. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4951. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4952. NSFileManager* fileManager = [NSFileManager defaultManager];
  4953. BOOL bdir=YES;
  4954. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4955. {
  4956. NSError *error = nil;
  4957. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4958. if(!bsuccess)
  4959. {
  4960. DebugLog(@"Create offline_createimg folder failed");
  4961. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4962. return [RAConvertor dict2data:ret];
  4963. }
  4964. // if(bsuccess)
  4965. // {
  4966. // sqlite3 *db = [self get_db];
  4967. //
  4968. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4969. // [iSalesDB close_db:db];
  4970. // }
  4971. }
  4972. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4973. {
  4974. NSError *error = nil;
  4975. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4976. if(!bsuccess)
  4977. {
  4978. DebugLog(@"Create img_cache folder failed");
  4979. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4980. return [RAConvertor dict2data:ret];
  4981. }
  4982. // if(bsuccess)
  4983. // {
  4984. // sqlite3 *db = [self get_db];
  4985. //
  4986. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4987. // [iSalesDB close_db:db];
  4988. // }
  4989. }
  4990. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4991. //JEPG格式
  4992. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4993. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4994. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4995. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4996. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4997. if(bsuccess)
  4998. {
  4999. NSError *error = nil;
  5000. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5001. {
  5002. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5003. }
  5004. else
  5005. {
  5006. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5007. ret[@"img_url_aname"]=filename;
  5008. ret[@"img_url"]=filename;
  5009. }
  5010. }
  5011. else
  5012. {
  5013. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5014. }
  5015. return [RAConvertor dict2data:ret];
  5016. }
  5017. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  5018. {
  5019. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  5020. category = [category substringToIndex:3];
  5021. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5022. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  5023. params[@"category"]= category;
  5024. ret[@"params"]= params;
  5025. [ret setValue:@"detail" forKey:@"target"];
  5026. [ret setValue:@"popup" forKey:@"action"];
  5027. [ret setValue:@"content" forKey:@"type"];
  5028. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  5029. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  5030. [ret setValue:@"true" forKey:@"single_row"];
  5031. [ret setValue:@"true" forKey:@"partial_refresh"];
  5032. // sqlite3 *db = [iSalesDB get_db];
  5033. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  5034. NSString *sqlQuery =nil;
  5035. #ifdef BUILD_NPD
  5036. 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 ;
  5037. #else
  5038. 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];
  5039. #endif
  5040. sqlite3_stmt * statement;
  5041. int count = 0;
  5042. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  5043. // int count=0;
  5044. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5045. {
  5046. int i=0;
  5047. while (sqlite3_step(statement) == SQLITE_ROW)
  5048. {
  5049. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5050. // char *name = (char*)sqlite3_column_text(statement, 1);
  5051. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5052. char *name = (char*)sqlite3_column_text(statement, 0);
  5053. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5054. int product_id = sqlite3_column_int(statement, 1);
  5055. char *url = (char*)sqlite3_column_text(statement, 2);
  5056. if(url==nil)
  5057. url="";
  5058. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5059. [item setValue:nsurl forKey:@"picture_path"];
  5060. [item setValue:nsname forKey:@"fash_name"];
  5061. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5062. [item setValue:category forKey:@"category"];
  5063. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5064. i++;
  5065. }
  5066. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5067. sqlite3_finalize(statement);
  5068. }
  5069. DebugLog(@"count:%d",count);
  5070. // [iSalesDB close_db:db];
  5071. return ret;
  5072. }
  5073. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  5074. {
  5075. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5076. [ret setValue:key forKey:@"key"];
  5077. [ret setValue:value forKey:@"val"];
  5078. [ret setValue:@"price" forKey:@"type"];
  5079. return ret;
  5080. }
  5081. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  5082. {
  5083. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5084. [ret setValue:key forKey:@"key"];
  5085. [ret setValue:value forKey:@"val"];
  5086. return ret;
  5087. }
  5088. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  5089. {
  5090. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5091. [ret setValue:@"0" forKey:@"img_count"];
  5092. // sqlite3 *db = [iSalesDB get_db];
  5093. 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 ;
  5094. sqlite3_stmt * statement;
  5095. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5096. {
  5097. int i=0;
  5098. if (sqlite3_step(statement) == SQLITE_ROW)
  5099. {
  5100. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5101. // char *name = (char*)sqlite3_column_text(statement, 1);
  5102. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5103. char *value = (char*)sqlite3_column_text(statement, 0);
  5104. if(value==nil)
  5105. value="";
  5106. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5107. char *key = (char*)sqlite3_column_text(statement, 1);
  5108. if(key==nil)
  5109. key="";
  5110. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5111. [item setValue:nsvalue forKey:@"val"];
  5112. [item setValue:nskey forKey:@"key"];
  5113. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5114. [ret setValue:@"1" forKey:@"count"];
  5115. i++;
  5116. }
  5117. sqlite3_finalize(statement);
  5118. }
  5119. // [iSalesDB close_db:db];
  5120. return ret;
  5121. }
  5122. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5123. {
  5124. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5125. [ret setValue:@"0" forKey:@"count"];
  5126. // sqlite3 *db = [iSalesDB get_db];
  5127. 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;
  5128. sqlite3_stmt * statement;
  5129. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5130. {
  5131. int i=0;
  5132. while (sqlite3_step(statement) == SQLITE_ROW)
  5133. {
  5134. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5135. // char *name = (char*)sqlite3_column_text(statement, 1);
  5136. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5137. char *value = (char*)sqlite3_column_text(statement, 0);
  5138. if(value==nil)
  5139. value="";
  5140. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5141. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5142. if(selector_display==nil)
  5143. selector_display="";
  5144. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5145. int product_id = sqlite3_column_int(statement, 2);
  5146. char *category = (char*)sqlite3_column_text(statement, 3);
  5147. if(category==nil)
  5148. category="";
  5149. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5150. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5151. [item setValue:nsvalue forKey:@"title"];
  5152. [item setValue:url forKey:@"pic_url"];
  5153. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5154. [params setValue:@"2" forKey:@"count"];
  5155. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5156. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5157. [param0 setValue:@"product_id" forKey:@"name"];
  5158. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5159. [param1 setValue:nscategory forKey:@"val"];
  5160. [param1 setValue:@"category" forKey:@"name"];
  5161. [params setObject:param0 forKey:@"param_0"];
  5162. [params setObject:param1 forKey:@"param_1"];
  5163. [item setObject:params forKey:@"params"];
  5164. [ret setValue:nsselector_display forKey:@"name"];
  5165. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5166. i++;
  5167. }
  5168. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5169. [ret setValue:@"switch" forKey:@"action"];
  5170. sqlite3_finalize(statement);
  5171. }
  5172. // [iSalesDB close_db:db];
  5173. return ret;
  5174. }
  5175. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5176. {
  5177. // model 在 category search 显示的图片。
  5178. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5179. // sqlite3 *db = [iSalesDB get_db];
  5180. 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];
  5181. sqlite3_stmt * statement;
  5182. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5183. {
  5184. while (sqlite3_step(statement) == SQLITE_ROW)
  5185. {
  5186. char *url = (char*)sqlite3_column_text(statement, 0);
  5187. if(url==nil)
  5188. url="";
  5189. int type = sqlite3_column_int(statement, 1);
  5190. if(type==0)
  5191. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5192. else
  5193. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5194. }
  5195. sqlite3_finalize(statement);
  5196. }
  5197. // [iSalesDB close_db:db];
  5198. return ret;
  5199. }
  5200. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5201. {
  5202. int item_id=-1;
  5203. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5204. sqlite3_stmt * statement;
  5205. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5206. {
  5207. if (sqlite3_step(statement) == SQLITE_ROW)
  5208. {
  5209. item_id = sqlite3_column_int(statement, 0);
  5210. }
  5211. sqlite3_finalize(statement);
  5212. }
  5213. return item_id;
  5214. }
  5215. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5216. {
  5217. // NSString* ret = @"";
  5218. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5219. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5220. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5221. sqlite3_stmt * statement;
  5222. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5223. {
  5224. while (sqlite3_step(statement) == SQLITE_ROW)
  5225. {
  5226. int bitem_id = sqlite3_column_int(statement, 0);
  5227. int bitem_qty = sqlite3_column_int(statement, 1);
  5228. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5229. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5230. }
  5231. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5232. sqlite3_finalize(statement);
  5233. }
  5234. // if(ret==nil)
  5235. // ret=@"";
  5236. *count=arr_count;
  5237. return arr_bundle;
  5238. }
  5239. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5240. {
  5241. // get default sold qty, return -1 if model not found;
  5242. int ret = -1;
  5243. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5244. sqlite3_stmt * statement;
  5245. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5246. {
  5247. if (sqlite3_step(statement) == SQLITE_ROW)
  5248. {
  5249. ret = sqlite3_column_int(statement, 0);
  5250. }
  5251. sqlite3_finalize(statement);
  5252. }
  5253. return ret;
  5254. }
  5255. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5256. {
  5257. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5258. [ret setValue:@"0" forKey:@"img_count"];
  5259. // sqlite3 *db = [iSalesDB get_db];
  5260. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5261. sqlite3_stmt * statement;
  5262. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5263. {
  5264. int i=0;
  5265. while (sqlite3_step(statement) == SQLITE_ROW)
  5266. {
  5267. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5268. // char *name = (char*)sqlite3_column_text(statement, 1);
  5269. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5270. char *url = (char*)sqlite3_column_text(statement, 0);
  5271. if(url==nil)
  5272. url="";
  5273. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5274. [item setValue:nsurl forKey:@"s"];
  5275. [item setValue:nsurl forKey:@"l"];
  5276. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5277. i++;
  5278. }
  5279. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5280. sqlite3_finalize(statement);
  5281. }
  5282. // [iSalesDB close_db:db];
  5283. return ret;
  5284. }
  5285. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5286. // UIApplication * app = [UIApplication sharedApplication];
  5287. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5288. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5289. sqlite3 *db = [iSalesDB get_db];
  5290. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5291. NSString* product_id=params[@"product_id"];
  5292. NSString *item_count_str = params[@"item_count"];
  5293. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5294. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5295. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5296. // NSString *sql = @"";
  5297. for(int i=0;i<arr.count;i++)
  5298. {
  5299. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5300. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5301. __block int cart_count = 0;
  5302. if (!item_count_str) {
  5303. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5304. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5305. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5306. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5307. cart_count = [[model_set_components lastObject] intValue];
  5308. }];
  5309. }
  5310. if(count==0)
  5311. {
  5312. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5313. sqlite3_stmt *stmt;
  5314. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5315. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5316. if (item_count_arr) {
  5317. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5318. } else {
  5319. sqlite3_bind_int(stmt,2,cart_count);
  5320. }
  5321. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5322. [iSalesDB execSql:@"ROLLBACK" db:db];
  5323. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5324. [iSalesDB close_db:db];
  5325. DebugLog(@"add to wishlist error");
  5326. return ret;
  5327. }
  5328. } else {
  5329. int qty = 0;
  5330. if (item_count_arr) {
  5331. qty = [item_count_arr[i] intValue];
  5332. } else {
  5333. qty = cart_count;
  5334. }
  5335. 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]];
  5336. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5337. [iSalesDB execSql:@"ROLLBACK" db:db];
  5338. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5339. [iSalesDB close_db:db];
  5340. DebugLog(@"add to wishlist error");
  5341. return ret;
  5342. }
  5343. }
  5344. }
  5345. // [iSalesDB execSql:sql db:db];
  5346. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5347. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5348. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5349. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5350. [iSalesDB close_db:db];
  5351. ret[@"wish_count"]=@(count);
  5352. ret[@"result"]= [NSNumber numberWithInt:2];
  5353. return ret;
  5354. }
  5355. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5356. // 0 category
  5357. // 1 search
  5358. // 2 itemsearch
  5359. NSData *ret = nil;
  5360. NSDictionary *items = nil;
  5361. switch (from) {
  5362. case 0:{
  5363. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5364. }
  5365. break;
  5366. case 1:{
  5367. items = [[self search:params limited:NO] objectForKey:@"items"];
  5368. }
  5369. break;
  5370. case 2:{
  5371. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5372. }
  5373. break;
  5374. default:
  5375. break;
  5376. }
  5377. if (!items) {
  5378. return ret;
  5379. }
  5380. int count = [[items objectForKey:@"count"] intValue];
  5381. NSMutableString *product_id_str = [@"" mutableCopy];
  5382. for (int i = 0; i < count; i++) {
  5383. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5384. NSDictionary *item = [items objectForKey:key];
  5385. NSString *product_id = [item objectForKey:@"product_id"];
  5386. if (i == 0) {
  5387. [product_id_str appendString:product_id];
  5388. } else {
  5389. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5390. }
  5391. }
  5392. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5393. if ([add_to isEqualToString:@"cart"]) {
  5394. NSString *order_code = [params objectForKey:@"orderCode"];
  5395. if (order_code.length) {
  5396. NSDictionary *newParams = @{
  5397. @"product_id" : product_id_str,
  5398. @"orderCode" : order_code,
  5399. @"can_create_backorder":params[@"can_create_backorder"]
  5400. };
  5401. ret = [self offline_add2cart:newParams.mutableCopy];
  5402. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5403. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5404. ret = [RAConvertor dict2data:retDic];
  5405. }
  5406. } else if([add_to isEqualToString:@"wishlist"]) {
  5407. NSDictionary *newParams = @{
  5408. @"product_id" : product_id_str
  5409. };
  5410. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5411. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5412. ret = [RAConvertor dict2data:retDic];
  5413. } else if([add_to isEqualToString:@"portfolio"]) {
  5414. NSDictionary *newParams = @{
  5415. @"product_id" : product_id_str
  5416. };
  5417. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5418. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5419. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5420. ret = [RAConvertor dict2data:retDic];
  5421. }
  5422. return ret;
  5423. }
  5424. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5425. {
  5426. return [self addAll:params from:0];
  5427. }
  5428. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5429. {
  5430. return [self addAll:params from:1];
  5431. }
  5432. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5433. {
  5434. return [self addAll:params from:2];
  5435. }
  5436. #pragma mark - Jack
  5437. #warning 做SQL操作时转义!!
  5438. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5439. // "val_227" : {
  5440. // "check" : 1,
  5441. // "value" : "US United States",
  5442. // "value_id" : "228"
  5443. // },
  5444. if (!countryCode) {
  5445. countryCode = @"US";
  5446. }
  5447. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5448. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5449. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5450. int code_id = sqlite3_column_int(stmt, 3); // id
  5451. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5452. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5453. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5454. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5455. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5456. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5457. }
  5458. long n = *count;
  5459. *count = n + 1;
  5460. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5461. [container setValue:countryDic forKey:key];
  5462. }] mutableCopy];
  5463. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5464. return ret;
  5465. }
  5466. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5467. countryCode = [self translateSingleQuote:countryCode];
  5468. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5469. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5470. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5471. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5472. if (name == NULL) {
  5473. name = "";
  5474. }
  5475. if (code == NULL) {
  5476. code = "";
  5477. }
  5478. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5479. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5480. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5481. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5482. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5483. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5484. }
  5485. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5486. [container setValue:stateDic forKey:key];
  5487. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5488. DebugLog(@"query all state error: %@",err_msg);
  5489. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5490. // [stateDic setValue:@"Other" forKey:@"value"];
  5491. // [stateDic setValue:@"" forKey:@"value_id"];
  5492. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5493. //
  5494. // if (state_code && [@"" isEqualToString:state_code]) {
  5495. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5496. // }
  5497. //
  5498. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5499. // [container setValue:stateDic forKey:key];
  5500. }] mutableCopy];
  5501. [ret removeObjectForKey:@"result"];
  5502. // failure 可以不用了,一样的
  5503. if (ret.allKeys.count == 0) {
  5504. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5505. [stateDic setValue:@"Other" forKey:@"value"];
  5506. [stateDic setValue:@"" forKey:@"value_id"];
  5507. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5508. if (state_code && [@"" isEqualToString:state_code]) {
  5509. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5510. }
  5511. NSString *key = [NSString stringWithFormat:@"val_0"];
  5512. [ret setValue:stateDic forKey:key];
  5513. }
  5514. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5515. return ret;
  5516. }
  5517. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5518. codeId = [self translateSingleQuote:codeId];
  5519. 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];
  5520. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5521. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5522. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5523. if (name == NULL) {
  5524. name = "";
  5525. }
  5526. if (code == NULL) {
  5527. code = "";
  5528. }
  5529. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5530. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5531. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5532. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5533. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5534. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5535. }
  5536. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5537. [container setValue:stateDic forKey:key];
  5538. }] mutableCopy];
  5539. [ret removeObjectForKey:@"result"];
  5540. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5541. return ret;
  5542. }
  5543. + (NSDictionary *)offline_getPrice {
  5544. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5545. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5546. char *name = (char *) sqlite3_column_text(stmt, 1);
  5547. int type = sqlite3_column_int(stmt, 2);
  5548. int orderBy = sqlite3_column_int(stmt, 3);
  5549. if (name == NULL) {
  5550. name = "";
  5551. }
  5552. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5553. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5554. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5555. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5556. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5557. if (orderBy == 0) {
  5558. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5559. }
  5560. [container setValue:priceDic forKey:key];
  5561. }] mutableCopy];
  5562. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5563. return ret;
  5564. }
  5565. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5566. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5567. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5568. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5569. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5570. // int _id = sqlite3_column_int(stmt, 0);
  5571. NSString *name = [self textAtColumn:1 statement:stmt];
  5572. NSDictionary *typeDic = @{
  5573. @"value_id" : name,
  5574. @"value" : name,
  5575. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5576. };
  5577. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5578. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5579. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5580. }];
  5581. return ret;
  5582. }
  5583. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5584. // 首先从offline_login表中取出sales_code
  5585. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5586. // NSString *user = app.user;
  5587. user = [self translateSingleQuote:user];
  5588. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5589. __block NSString *user_code = @"";
  5590. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5591. char *code = (char *)sqlite3_column_text(stmt, 0);
  5592. if (code == NULL) {
  5593. code = "";
  5594. }
  5595. user_code = [NSString stringWithUTF8String:code];
  5596. }];
  5597. // 再取所有salesRep
  5598. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5599. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5600. // 1 name 2 code 3 salesrep_id
  5601. char *name = (char *)sqlite3_column_text(stmt, 1);
  5602. char *code = (char *)sqlite3_column_text(stmt, 2);
  5603. int salesrep_id = sqlite3_column_int(stmt, 3);
  5604. if (name == NULL) {
  5605. name = "";
  5606. }
  5607. if (code == NULL) {
  5608. code = "";
  5609. }
  5610. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5611. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5612. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5613. // 比较code 相等则check
  5614. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5615. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5616. }
  5617. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5618. }] mutableCopy];
  5619. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5620. return ret;
  5621. }
  5622. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5623. zipcode = [self translateSingleQuote:zipcode];
  5624. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5625. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5626. char *country = (char *)sqlite3_column_text(stmt, 0);
  5627. char *state = (char *)sqlite3_column_text(stmt, 1);
  5628. char *city = (char *)sqlite3_column_text(stmt, 2);
  5629. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5630. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5631. if (country == NULL) {
  5632. country = "";
  5633. }
  5634. if (state == NULL) {
  5635. state = "";
  5636. }
  5637. if (city == NULL) {
  5638. city = "";
  5639. }
  5640. if (country_code == NULL) {
  5641. country_code = "";
  5642. }
  5643. if (state_code == NULL) {
  5644. state_code = "";
  5645. }
  5646. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5647. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5648. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5649. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5650. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5651. }] mutableCopy];
  5652. return ret;
  5653. }
  5654. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5655. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5656. [item setValue:value forKey:valueKey];
  5657. [dic setValue:item forKey:itemKey];
  5658. }
  5659. + (NSString *)countryCodeByid:(NSString *)code_id {
  5660. NSString *ret = nil;
  5661. code_id = [self translateSingleQuote:code_id];
  5662. sqlite3 *db = [iSalesDB get_db];
  5663. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5664. sqlite3_stmt * statement;
  5665. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5666. while (sqlite3_step(statement) == SQLITE_ROW) {
  5667. char *code = (char *)sqlite3_column_text(statement, 0);
  5668. if (code == NULL) {
  5669. code = "";
  5670. }
  5671. ret = [NSString stringWithUTF8String:code];
  5672. }
  5673. sqlite3_finalize(statement);
  5674. }
  5675. [iSalesDB close_db:db];
  5676. return ret;
  5677. }
  5678. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5679. NSString *ret = nil;
  5680. code = [self translateSingleQuote:code];
  5681. sqlite3 *db = [iSalesDB get_db];
  5682. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5683. sqlite3_stmt * statement;
  5684. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5685. while (sqlite3_step(statement) == SQLITE_ROW) {
  5686. char *_id = (char *)sqlite3_column_text(statement, 0);
  5687. if (_id == NULL) {
  5688. _id = "";
  5689. }
  5690. ret = [NSString stringWithFormat:@"%s",_id];
  5691. }
  5692. sqlite3_finalize(statement);
  5693. }
  5694. [iSalesDB close_db:db];
  5695. return ret;
  5696. }
  5697. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5698. NSString *name = nil;
  5699. codeId = [self translateSingleQuote:codeId];
  5700. sqlite3 *db = [iSalesDB get_db];
  5701. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5702. sqlite3_stmt * statement;
  5703. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5704. while (sqlite3_step(statement) == SQLITE_ROW) {
  5705. char *value = (char *)sqlite3_column_text(statement, 0);
  5706. if (value == NULL) {
  5707. value = "";
  5708. }
  5709. name = [NSString stringWithUTF8String:value];
  5710. }
  5711. sqlite3_finalize(statement);
  5712. }
  5713. [iSalesDB close_db:db];
  5714. return name;
  5715. }
  5716. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5717. NSString *ret = nil;
  5718. sqlite3 *db = [iSalesDB get_db];
  5719. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5720. sqlite3_stmt * statement;
  5721. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5722. while (sqlite3_step(statement) == SQLITE_ROW) {
  5723. char *name = (char *)sqlite3_column_text(statement, 0);
  5724. if (name == NULL) {
  5725. name = "";
  5726. }
  5727. ret = [NSString stringWithUTF8String:name];
  5728. }
  5729. sqlite3_finalize(statement);
  5730. }
  5731. [iSalesDB close_db:db];
  5732. return ret;
  5733. }
  5734. + (NSString *)salesRepCodeById:(NSString *)_id {
  5735. NSString *ret = nil;
  5736. _id = [self translateSingleQuote:_id];
  5737. sqlite3 *db = [iSalesDB get_db];
  5738. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5739. sqlite3_stmt * statement;
  5740. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5741. while (sqlite3_step(statement) == SQLITE_ROW) {
  5742. char *rep = (char *)sqlite3_column_text(statement, 0);
  5743. if (rep == NULL) {
  5744. rep = "";
  5745. }
  5746. ret = [NSString stringWithUTF8String:rep];
  5747. }
  5748. sqlite3_finalize(statement);
  5749. }
  5750. [iSalesDB close_db:db];
  5751. return ret;
  5752. }
  5753. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5754. char *tx = (char *)sqlite3_column_text(stmt, col);
  5755. if (tx == NULL) {
  5756. tx = "";
  5757. }
  5758. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5759. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5760. if (!text) {
  5761. text = @"";
  5762. }
  5763. // 将字符全部为' '的字符串干掉
  5764. int spaceCount = 0;
  5765. for (int i = 0; i < text.length; i++) {
  5766. if ([text characterAtIndex:i] == ' ') {
  5767. spaceCount++;
  5768. }
  5769. }
  5770. if (spaceCount == text.length) {
  5771. text = @"";
  5772. }
  5773. return text;
  5774. }
  5775. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5776. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5777. NSData *data = [NSData dataWithContentsOfFile:path];
  5778. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5779. return ret;
  5780. }
  5781. + (NSString *)textFileName:(NSString *)name {
  5782. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5783. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5784. if (!text) {
  5785. text = @"";
  5786. }
  5787. return text;
  5788. }
  5789. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5790. return [[dic objectForKey:key] mutableCopy];
  5791. }
  5792. + (id)translateSingleQuote:(NSString *)string {
  5793. if ([string isKindOfClass:[NSString class]])
  5794. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5795. return string;
  5796. }
  5797. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5798. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5799. }
  5800. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5801. NSString* ret= nil;
  5802. NSString *sqlQuery = nil;
  5803. // 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
  5804. 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];
  5805. sqlite3_stmt * statement;
  5806. // int count=0;
  5807. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5808. {
  5809. if (sqlite3_step(statement) == SQLITE_ROW)
  5810. {
  5811. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5812. if(imgurl==nil)
  5813. imgurl="";
  5814. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5815. ret=nsimgurl;
  5816. }
  5817. sqlite3_finalize(statement);
  5818. }
  5819. else
  5820. {
  5821. [ret setValue:@"8" forKey:@"result"];
  5822. }
  5823. // [iSalesDB close_db:db];
  5824. // DebugLog(@"data string: %@",ret );
  5825. return ret;
  5826. }
  5827. #pragma mark contact Advanced search
  5828. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5829. {
  5830. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5831. return [RAConvertor dict2data:contactAdvanceDic];
  5832. }
  5833. #pragma mark create new contact
  5834. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5835. assert(params[@"user"]!=nil);
  5836. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5837. NSData *data = [NSData dataWithContentsOfFile:path];
  5838. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5839. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5840. NSString *countryCode = nil;
  5841. NSString *countryCode_id = nil;
  5842. NSString *stateCode = nil;
  5843. NSString *city = nil;
  5844. NSString *zipCode = nil;
  5845. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5846. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5847. NSString *code_id = params[@"country"];
  5848. countryCode_id = code_id;
  5849. countryCode = [self countryCodeByid:code_id];
  5850. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5851. NSString *zip_code = params[@"zipcode"];
  5852. // 剔除全部为空格
  5853. int spaceCount = 0;
  5854. for (int i = 0; i < zip_code.length; i++) {
  5855. if ([zip_code characterAtIndex:i] == ' ') {
  5856. spaceCount++;
  5857. }
  5858. }
  5859. if (spaceCount == zip_code.length) {
  5860. zip_code = @"";
  5861. }
  5862. zipCode = zip_code;
  5863. if (zipCode.length > 0) {
  5864. countryCode_id = params[@"country"];
  5865. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5866. countryCode = [dic valueForKey:@"country_code"];
  5867. if (!countryCode) {
  5868. // countryCode = @"US";
  5869. NSString *code_id = params[@"country"];
  5870. countryCode = [self countryCodeByid:code_id];
  5871. }
  5872. stateCode = [dic valueForKey:@"state_code"];
  5873. if (!stateCode.length) {
  5874. stateCode = params[@"state"];
  5875. }
  5876. city = [dic valueForKey:@"city"];
  5877. if (!city.length) {
  5878. city = params[@"city"];
  5879. }
  5880. // zip code
  5881. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5882. [zipDic setValue:zipCode forKey:@"value"];
  5883. [section_0 setValue:zipDic forKey:@"item_11"];
  5884. } else {
  5885. NSString *code_id = params[@"country"];
  5886. countryCode = [self countryCodeByid:code_id];
  5887. stateCode = params[@"state"];
  5888. city = params[@"city"];
  5889. }
  5890. }
  5891. } else {
  5892. // default: US United States
  5893. countryCode = @"US";
  5894. countryCode_id = @"228";
  5895. }
  5896. // country
  5897. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5898. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5899. // state
  5900. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5901. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5902. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5903. // city
  5904. if (city) {
  5905. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5906. [cityDic setValue:city forKey:@"value"];
  5907. [section_0 setValue:cityDic forKey:@"item_13"];
  5908. }
  5909. // price type
  5910. NSDictionary *priceDic = [self offline_getPrice];
  5911. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5912. // contact type
  5913. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5914. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5915. // Sales Rep
  5916. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5917. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5918. [ret setValue:section_0 forKey:@"section_0"];
  5919. return [RAConvertor dict2data:ret];
  5920. }
  5921. #pragma mark save
  5922. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5923. NSString *addr = nil;
  5924. NSString *contact_name = nil;
  5925. NSString *companyName = [params objectForKey:@"company"];
  5926. if (companyName) {
  5927. companyName = [AESCrypt fastencrypt:companyName];
  5928. } else {
  5929. companyName = @"";
  5930. }
  5931. DebugLog(@"company");
  5932. companyName = [self translateSingleQuote:companyName];
  5933. NSString *addr1 = [params objectForKey:@"address"];
  5934. NSString *addr2 = [params objectForKey:@"address2"];
  5935. NSString *addr3 = [params objectForKey:@"address_3"];
  5936. NSString *addr4 = [params objectForKey:@"address_4"];
  5937. if (!addr2) {
  5938. addr2 = @"";
  5939. }
  5940. if (!addr3) {
  5941. addr3 = @"";
  5942. }
  5943. if (!addr4) {
  5944. addr4 = @"";
  5945. }
  5946. if (!addr1) {
  5947. addr1 = @"";
  5948. }
  5949. DebugLog(@"addr");
  5950. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5951. addr = [AESCrypt fastencrypt:addr];
  5952. addr = [self translateSingleQuote:addr];
  5953. if (addr1 && ![addr1 isEqualToString:@""]) {
  5954. addr1 = [AESCrypt fastencrypt:addr1];
  5955. }
  5956. addr1 = [self translateSingleQuote:addr1];
  5957. addr2 = [self translateSingleQuote:addr2];
  5958. addr3 = [self translateSingleQuote:addr3];
  5959. addr4 = [self translateSingleQuote:addr4];
  5960. NSString *country = [params objectForKey:@"country"];
  5961. if (country) {
  5962. country = [self countryNameByCountryCodeId:country];
  5963. } else {
  5964. country = @"";
  5965. }
  5966. DebugLog(@"country");
  5967. country = [self translateSingleQuote:country];
  5968. NSString *state = [params objectForKey:@"state"];
  5969. if (!state) {
  5970. state = @"";
  5971. }
  5972. DebugLog(@"state");
  5973. state = [self translateSingleQuote:state];
  5974. NSString *city = [params objectForKey:@"city"];
  5975. if (!city) {
  5976. city = @"";
  5977. }
  5978. city = [self translateSingleQuote:city];
  5979. NSString *zipcode = [params objectForKey:@"zipcode"];
  5980. if (!zipcode) {
  5981. zipcode = @"";
  5982. }
  5983. DebugLog(@"zip");
  5984. zipcode = [self translateSingleQuote:zipcode];
  5985. NSString *fistName = [params objectForKey:@"firstname"];
  5986. if (!fistName) {
  5987. fistName = @"";
  5988. }
  5989. NSString *lastName = [params objectForKey:@"lastname"];
  5990. if (!lastName) {
  5991. lastName = @"";
  5992. }
  5993. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5994. DebugLog(@"contact_name");
  5995. contact_name = [self translateSingleQuote:contact_name];
  5996. fistName = [self translateSingleQuote:fistName];
  5997. lastName = [self translateSingleQuote:lastName];
  5998. NSString *phone = [params objectForKey:@"phone"];
  5999. if (phone) {
  6000. phone = [AESCrypt fastencrypt:phone];
  6001. } else {
  6002. phone = @"";
  6003. }
  6004. DebugLog(@"PHONE");
  6005. phone = [self translateSingleQuote:phone];
  6006. NSString *fax = [params objectForKey:@"fax"];
  6007. if (!fax) {
  6008. fax = @"";
  6009. }
  6010. DebugLog(@"FAX");
  6011. fax = [self translateSingleQuote:fax];
  6012. NSString *email = [params objectForKey:@"email"];
  6013. if (!email) {
  6014. email = @"";
  6015. }
  6016. DebugLog(@"EMAIL:%@",email);
  6017. email = [self translateSingleQuote:email];
  6018. NSString *notes = [params objectForKey:@"contact_notes"];
  6019. if (!notes) {
  6020. notes = @"";
  6021. }
  6022. DebugLog(@"NOTE:%@",notes);
  6023. notes = [self translateSingleQuote:notes];
  6024. NSString *price = [params objectForKey:@"price_name"];
  6025. if (price) {
  6026. price = [self priceNameByPriceId:price];
  6027. } else {
  6028. price = @"";
  6029. }
  6030. DebugLog(@"PRICE");
  6031. price = [self translateSingleQuote:price];
  6032. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6033. if (salesRep) {
  6034. salesRep = [self salesRepCodeById:salesRep];
  6035. } else {
  6036. salesRep = @"";
  6037. }
  6038. salesRep = [self translateSingleQuote:salesRep];
  6039. NSString *img = [params objectForKey:@"business_card"];
  6040. NSArray *array = [img componentsSeparatedByString:@","];
  6041. NSString *img_0 = array[0];
  6042. if (!img_0) {
  6043. img_0 = @"";
  6044. }
  6045. img_0 = [self translateSingleQuote:img_0];
  6046. NSString *img_1 = array[1];
  6047. if (!img_1) {
  6048. img_1 = @"";
  6049. }
  6050. img_1 = [self translateSingleQuote:img_1];
  6051. NSString *img_2 = array[2];
  6052. if (!img_2) {
  6053. img_2 = @"";
  6054. }
  6055. img_2 = [self translateSingleQuote:img_2];
  6056. NSString *contact_id = [NSUUID UUID].UUIDString;
  6057. NSString *contact_type = [params objectForKey:@"type_name"];
  6058. if (!contact_type) {
  6059. contact_type = @"";
  6060. }
  6061. contact_type = [self translateSingleQuote:contact_type];
  6062. // 判断更新时是否为customer
  6063. if (update) {
  6064. contact_id = [params objectForKey:@"contact_id"];
  6065. if (!contact_id) {
  6066. contact_id = @"";
  6067. }
  6068. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6069. __block int customer = 0;
  6070. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6071. customer = sqlite3_column_int(stmt, 0);
  6072. }];
  6073. isCustomer = customer ? YES : NO;
  6074. }
  6075. NSMutableDictionary *sync_dic = [params mutableCopy];
  6076. if (isCustomer) {
  6077. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6078. } else {
  6079. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6080. }
  6081. NSString *sync_data = nil;
  6082. NSString *sql = nil;
  6083. if (update){
  6084. contact_id = [params objectForKey:@"contact_id"];
  6085. if (!contact_id) {
  6086. contact_id = @"";
  6087. }
  6088. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6089. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6090. sync_data = [RAConvertor dict2string:sync_dic];
  6091. sync_data = [self translateSingleQuote:sync_data];
  6092. 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];
  6093. } else {
  6094. contact_id = [self translateSingleQuote:contact_id];
  6095. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6096. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6097. sync_data = [RAConvertor dict2string:sync_dic];
  6098. sync_data = [self translateSingleQuote:sync_data];
  6099. 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];
  6100. }
  6101. int result = [iSalesDB execSql:sql];
  6102. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6103. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6104. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6105. }
  6106. #pragma mark save new contact
  6107. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6108. {
  6109. return [self offline_saveContact:params update:NO isCustomer:YES];
  6110. }
  6111. #pragma mark edit contact
  6112. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6113. {
  6114. assert(params[@"user"]!=nil);
  6115. // {
  6116. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6117. // password = 123456;
  6118. // user = EvanK;
  6119. // }
  6120. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6121. NSData *data = [NSData dataWithContentsOfFile:path];
  6122. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6123. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6124. NSString *countryCode = nil;
  6125. NSString *countryCode_id = nil;
  6126. NSString *stateCode = nil;
  6127. /*------contact infor------*/
  6128. __block NSString *country = nil;
  6129. __block NSString *company_name = nil;
  6130. __block NSString *contact_id = params[@"contact_id"];
  6131. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6132. __block NSString *zipcode = nil;
  6133. __block NSString *state = nil; // state_code
  6134. __block NSString *city = nil; //
  6135. __block NSString *firt_name,*last_name;
  6136. __block NSString *phone,*fax,*email;
  6137. __block NSString *notes,*price_type,*sales_rep;
  6138. __block NSString *img_0,*img_1,*img_2;
  6139. __block NSString *contact_type;
  6140. contact_id = [self translateSingleQuote:contact_id];
  6141. 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];
  6142. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6143. country = [self textAtColumn:0 statement:stmt]; // country name
  6144. company_name = [self textAtColumn:1 statement:stmt];
  6145. addr_1 = [self textAtColumn:2 statement:stmt];
  6146. addr_2 = [self textAtColumn:3 statement:stmt];
  6147. addr_3 = [self textAtColumn:4 statement:stmt];
  6148. addr_4 = [self textAtColumn:5 statement:stmt];
  6149. zipcode = [self textAtColumn:6 statement:stmt];
  6150. state = [self textAtColumn:7 statement:stmt]; // state code
  6151. city = [self textAtColumn:8 statement:stmt];
  6152. firt_name = [self textAtColumn:9 statement:stmt];
  6153. last_name = [self textAtColumn:10 statement:stmt];
  6154. phone = [self textAtColumn:11 statement:stmt];
  6155. fax = [self textAtColumn:12 statement:stmt];
  6156. email = [self textAtColumn:13 statement:stmt];
  6157. notes = [self textAtColumn:14 statement:stmt];
  6158. price_type = [self textAtColumn:15 statement:stmt]; // name
  6159. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6160. img_0 = [self textAtColumn:17 statement:stmt];
  6161. img_1 = [self textAtColumn:18 statement:stmt];
  6162. img_2 = [self textAtColumn:19 statement:stmt];
  6163. contact_type = [self textAtColumn:20 statement:stmt];
  6164. }];
  6165. // decrypt
  6166. if (company_name) {
  6167. company_name = [AESCrypt fastdecrypt:company_name];
  6168. }
  6169. if (addr_1) {
  6170. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6171. }
  6172. if (phone) {
  6173. phone = [AESCrypt fastdecrypt:phone];
  6174. }
  6175. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6176. countryCode = [iSalesDB jk_queryText:countrySql];
  6177. stateCode = state;
  6178. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6179. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6180. NSString *code_id = params[@"country"];
  6181. countryCode_id = code_id;
  6182. countryCode = [self countryCodeByid:code_id];
  6183. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6184. NSString *zip_code = params[@"zipcode"];
  6185. // 剔除全部为空格
  6186. int spaceCount = 0;
  6187. for (int i = 0; i < zip_code.length; i++) {
  6188. if ([zip_code characterAtIndex:i] == ' ') {
  6189. spaceCount++;
  6190. }
  6191. }
  6192. if (spaceCount == zip_code.length) {
  6193. zip_code = @"";
  6194. }
  6195. if (zipcode.length > 0) {
  6196. zipcode = zip_code;
  6197. countryCode_id = params[@"country"];
  6198. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6199. countryCode = [dic valueForKey:@"country_code"];
  6200. if (!countryCode) {
  6201. NSString *code_id = params[@"country"];
  6202. countryCode = [self countryCodeByid:code_id];
  6203. }
  6204. stateCode = [dic valueForKey:@"state_code"];
  6205. if (!stateCode.length) {
  6206. stateCode = params[@"state"];
  6207. }
  6208. city = [dic valueForKey:@"city"];
  6209. if (!city.length) {
  6210. city = params[@"city"];
  6211. }
  6212. // zip code
  6213. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6214. [zipDic setValue:zipcode forKey:@"value"];
  6215. [section_0 setValue:zipDic forKey:@"item_8"];
  6216. } else {
  6217. NSString *code_id = params[@"country"];
  6218. countryCode = [self countryCodeByid:code_id];
  6219. stateCode = params[@"state"];
  6220. city = params[@"city"];
  6221. }
  6222. }
  6223. }
  6224. // 0 Country
  6225. // 1 Company Name
  6226. // 2 Contact ID
  6227. // 3 Picture
  6228. // 4 Address 1
  6229. // 5 Address 2
  6230. // 6 Address 3
  6231. // 7 Address 4
  6232. // 8 Zip Code
  6233. // 9 State/Province
  6234. // 10 City
  6235. // 11 Contact First Name
  6236. // 12 Contact Last Name
  6237. // 13 Phone
  6238. // 14 Fax
  6239. // 15 Email
  6240. // 16 Contact Notes
  6241. // 17 Price Type
  6242. // 18 Contact Type
  6243. // 19 Sales Rep
  6244. // country
  6245. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6246. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6247. // company
  6248. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6249. // contact_id
  6250. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6251. // picture
  6252. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6253. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6254. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6255. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6256. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6257. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6258. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6259. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6260. // addr 1 2 3 4
  6261. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6262. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6263. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6264. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6265. // zip code
  6266. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6267. // state
  6268. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6269. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6270. // city
  6271. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6272. // first last
  6273. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6274. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6275. // phone fax email
  6276. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6277. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6278. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6279. // notes
  6280. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6281. // price
  6282. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6283. for (NSString *key in priceDic.allKeys) {
  6284. if ([key containsString:@"val_"]) {
  6285. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6286. if ([dic[@"value"] isEqualToString:price_type]) {
  6287. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6288. [priceDic setValue:dic forKey:key];
  6289. }
  6290. }
  6291. }
  6292. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6293. // Contact Rep
  6294. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6295. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6296. // Sales Rep
  6297. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6298. for (NSString *key in repDic.allKeys) {
  6299. if ([key containsString:@"val_"]) {
  6300. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6301. NSString *value = dic[@"value"];
  6302. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6303. if (code && [code isEqualToString:sales_rep]) {
  6304. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6305. [repDic setValue:dic forKey:key];
  6306. }
  6307. }
  6308. }
  6309. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6310. [ret setValue:section_0 forKey:@"section_0"];
  6311. return [RAConvertor dict2data:ret];
  6312. }
  6313. #pragma mark save contact
  6314. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6315. {
  6316. return [self offline_saveContact:params update:YES isCustomer:YES];
  6317. }
  6318. #pragma mark category
  6319. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6320. if (ck) {
  6321. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6322. for (NSString *key in res.allKeys) {
  6323. if (![key isEqualToString:@"count"]) {
  6324. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6325. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6326. if ([val[@"value"] isEqualToString:ck]) {
  6327. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6328. }
  6329. [res setValue:val forKey:key];
  6330. }
  6331. }
  6332. [dic setValue:res forKey:valueKey];
  6333. }
  6334. }
  6335. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6336. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6337. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6338. NSString* category = [params valueForKey:@"category"];
  6339. if (!category || [category isEqualToString:@""]) {
  6340. category = @"%";
  6341. }
  6342. category = [self translateSingleQuote:category];
  6343. int limit = [[params valueForKey:@"limit"] intValue];
  6344. int offset = [[params valueForKey:@"offset"] intValue];
  6345. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6346. NSString *limit_str = @"";
  6347. if (limited) {
  6348. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6349. }
  6350. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6351. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6352. sqlite3 *db = [iSalesDB get_db];
  6353. // [iSalesDB AddExFunction:db];
  6354. int count;
  6355. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6356. 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];
  6357. double price_min = 0;
  6358. double price_max = 0;
  6359. if ([params.allKeys containsObject:@"alert"]) {
  6360. // alert
  6361. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6362. NSString *alert = params[@"alert"];
  6363. if ([alert isEqualToString:@"Display All"]) {
  6364. alert = [NSString stringWithFormat:@""];
  6365. } else {
  6366. alert = [self translateSingleQuote:alert];
  6367. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6368. }
  6369. // available
  6370. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6371. NSString *available = params[@"available"];
  6372. NSString *available_condition;
  6373. if ([available isEqualToString:@"Display All"]) {
  6374. available_condition = @"";
  6375. } else if ([available isEqualToString:@"Available Now"]) {
  6376. available_condition = @"and availability > 0";
  6377. } else {
  6378. available_condition = @"and availability == 0";
  6379. }
  6380. // best seller
  6381. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6382. NSString *best_seller = @"";
  6383. NSString *order_best_seller = @"m.name asc";
  6384. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6385. best_seller = @"and best_seller > 0";
  6386. order_best_seller = @"m.best_seller desc,m.name asc";
  6387. }
  6388. // price
  6389. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6390. NSString *price = params[@"price"];
  6391. price_min = 0;
  6392. price_max = MAXFLOAT;
  6393. if (params[@"user"] && price != nil) {
  6394. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6395. NSMutableString *priceName = [NSMutableString string];
  6396. for (int i = 0; i < priceTypeArray.count; i++) {
  6397. NSString *pricetype = priceTypeArray[i];
  6398. pricetype = [self translateSingleQuote:pricetype];
  6399. if (i == 0) {
  6400. [priceName appendFormat:@"'%@'",pricetype];
  6401. } else {
  6402. [priceName appendFormat:@",'%@'",pricetype];
  6403. }
  6404. }
  6405. if ([price isEqualToString:@"Display All"]) {
  6406. price = [NSString stringWithFormat:@""];
  6407. } else if([price containsString:@"+"]){
  6408. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6409. price_min = [price doubleValue];
  6410. 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];
  6411. } else {
  6412. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6413. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6414. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6415. 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];
  6416. }
  6417. } else {
  6418. price = @"";
  6419. }
  6420. // sold_by_qty : Sold in quantities of %@
  6421. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6422. NSString *qty = params[@"sold_by_qty"];
  6423. if ([qty isEqualToString:@"Display All"]) {
  6424. qty = @"";
  6425. } else {
  6426. qty = [self translateSingleQuote:qty];
  6427. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6428. }
  6429. // model name;
  6430. NSString* modelname =params[@"modelName"];
  6431. if(modelname.length==0)
  6432. {
  6433. modelname=@"";
  6434. }
  6435. else
  6436. {
  6437. modelname = modelname.lowercaseString;
  6438. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6439. }
  6440. //modelDescription
  6441. NSString* modelDescription =params[@"modelDescription"];
  6442. if(modelDescription.length==0)
  6443. {
  6444. modelDescription=@"";
  6445. }
  6446. else
  6447. {
  6448. modelDescription = modelDescription.lowercaseString;
  6449. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6450. }
  6451. // cate
  6452. // category = [self translateSingleQuote:category];
  6453. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6454. // cate mutiple selection
  6455. NSString *category_id = params[@"category"];
  6456. NSMutableArray *cate_id_array = nil;
  6457. NSMutableString *cateWhere = [NSMutableString string];
  6458. if ([category_id isEqualToString:@""] || !category_id) {
  6459. [cateWhere appendString:@"1 = 1"];
  6460. } else {
  6461. if ([category_id containsString:@","]) {
  6462. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6463. } else {
  6464. cate_id_array = [@[category_id] mutableCopy];
  6465. }
  6466. [cateWhere appendString:@"("];
  6467. for (int i = 0; i < cate_id_array.count; i++) {
  6468. if (i == 0) {
  6469. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6470. } else {
  6471. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6472. }
  6473. }
  6474. [cateWhere appendString:@")"];
  6475. }
  6476. // where bestseller > 0 order by bestseller desc
  6477. // sql query: alert availability(int) best_seller(int) price qty
  6478. 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];
  6479. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6480. }
  6481. DebugLog(@"offline category where: %@",where);
  6482. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6483. if (!params[@"user"]) {
  6484. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6485. }
  6486. [ret setValue:filter forKey:@"filter"];
  6487. DebugLog(@"offline_category sql:%@",sqlQuery);
  6488. sqlite3_stmt * statement;
  6489. [ret setValue:@"2" forKey:@"result"];
  6490. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6491. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6492. // int count=0;
  6493. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6494. {
  6495. int i=0;
  6496. while (sqlite3_step(statement) == SQLITE_ROW)
  6497. {
  6498. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6499. char *name = (char*)sqlite3_column_text(statement, 0);
  6500. if(name==nil)
  6501. name="";
  6502. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6503. char *description = (char*)sqlite3_column_text(statement, 1);
  6504. if(description==nil)
  6505. description="";
  6506. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6507. int product_id = sqlite3_column_int(statement, 2);
  6508. int wid = sqlite3_column_int(statement, 3);
  6509. int closeout = sqlite3_column_int(statement, 4);
  6510. int cid = sqlite3_column_int(statement, 5);
  6511. int wisdelete = sqlite3_column_int(statement, 6);
  6512. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6513. int more_color = sqlite3_column_int(statement, 8);
  6514. // Defaul Category ID
  6515. __block NSString *categoryID = nil;
  6516. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6517. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6518. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6519. if(default_category==nil)
  6520. default_category="";
  6521. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6522. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6523. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6524. categoryID = nsdefault_category;
  6525. }];
  6526. if (!categoryID.length) {
  6527. NSString *cateIDs = params[@"category"];
  6528. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6529. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6530. for (NSString *cateID in requestCategoryArr) {
  6531. BOOL needBreak = NO;
  6532. for (NSString *itemCateIDBox in itemCategoryArr) {
  6533. if (itemCateIDBox.length > 4) {
  6534. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6535. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6536. if ([itemCategoryID isEqualToString:cateID]) {
  6537. needBreak = YES;
  6538. categoryID = itemCategoryID;
  6539. break;
  6540. }
  6541. }
  6542. }
  6543. if (needBreak) {
  6544. break;
  6545. }
  6546. }
  6547. }
  6548. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6549. if(wid !=0 && wisdelete != 1)
  6550. [item setValue:@"true" forKey:@"wish_exists"];
  6551. else
  6552. [item setValue:@"false" forKey:@"wish_exists"];
  6553. if(closeout==0)
  6554. [item setValue:@"false" forKey:@"is_closeout"];
  6555. else
  6556. [item setValue:@"true" forKey:@"is_closeout"];
  6557. if(cid==0)
  6558. [item setValue:@"false" forKey:@"cart_exists"];
  6559. else
  6560. [item setValue:@"true" forKey:@"cart_exists"];
  6561. if (more_color == 0) {
  6562. [item setObject:@(false) forKey:@"more_color"];
  6563. } else if (more_color == 1) {
  6564. [item setObject:@(true) forKey:@"more_color"];
  6565. }
  6566. [item addEntriesFromDictionary:imgjson];
  6567. // [item setValue:nsurl forKey:@"img"];
  6568. [item setValue:nsname forKey:@"name"];
  6569. [item setValue:nsdescription forKey:@"description"];
  6570. if (categoryID) {
  6571. [item setValue:categoryID forKey:@"item_category_id"];
  6572. }
  6573. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6574. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6575. i++;
  6576. }
  6577. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6578. [ret setObject:items forKey:@"items"];
  6579. sqlite3_finalize(statement);
  6580. } else {
  6581. DebugLog(@"nothing...");
  6582. }
  6583. DebugLog(@"count:%d",count);
  6584. [iSalesDB close_db:db];
  6585. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6586. return ret;
  6587. }
  6588. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6589. {
  6590. return [self categoryList:params limited:YES];
  6591. }
  6592. # pragma mark item search
  6593. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6594. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6595. // assert(params[@"order_code"]);
  6596. // params[@"user"]
  6597. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6598. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6599. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6600. // category
  6601. NSDictionary *category_menu = [self offline_category_menu];
  6602. [filter setValue:category_menu forKey:@"category"];
  6603. NSString* where= nil;
  6604. NSString* orderby= @"m.name";
  6605. if (!filterSearch) {
  6606. int covertype = [[params valueForKey:@"covertype"] intValue];
  6607. switch (covertype) {
  6608. case 0:
  6609. {
  6610. where=@"m.category like'%%#005#%%'";
  6611. break;
  6612. }
  6613. case 1:
  6614. {
  6615. where=@"m.alert like '%QS%'";
  6616. break;
  6617. }
  6618. case 2:
  6619. {
  6620. where=@"m.availability>0";
  6621. break;
  6622. }
  6623. case 3:
  6624. {
  6625. where=@"m.best_seller>0";
  6626. orderby=@"m.best_seller desc,m.name asc";
  6627. break;
  6628. }
  6629. default:
  6630. where=@"1=1";
  6631. break;
  6632. }
  6633. }
  6634. int limit = [[params valueForKey:@"limit"] intValue];
  6635. int offset = [[params valueForKey:@"offset"] intValue];
  6636. NSString *limit_str = @"";
  6637. if (limited) {
  6638. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6639. }
  6640. sqlite3 *db = [iSalesDB get_db];
  6641. // [iSalesDB AddExFunction:db];
  6642. int count;
  6643. NSString *sqlQuery = nil;
  6644. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6645. 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];
  6646. double price_min = 0;
  6647. double price_max = 0;
  6648. if (filterSearch) {
  6649. // alert
  6650. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6651. NSString *alert = params[@"alert"];
  6652. if ([alert isEqualToString:@"Display All"]) {
  6653. alert = [NSString stringWithFormat:@""];
  6654. } else {
  6655. alert = [self translateSingleQuote:alert];
  6656. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6657. }
  6658. // available
  6659. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6660. NSString *available = params[@"available"];
  6661. NSString *available_condition;
  6662. if ([available isEqualToString:@"Display All"]) {
  6663. available_condition = @"";
  6664. } else if ([available isEqualToString:@"Available Now"]) {
  6665. available_condition = @"and availability > 0";
  6666. } else {
  6667. available_condition = @"and availability == 0";
  6668. }
  6669. // best seller
  6670. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6671. NSString *best_seller = @"";
  6672. NSString *order_best_seller = @"m.name asc";
  6673. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6674. best_seller = @"and best_seller > 0";
  6675. order_best_seller = @"m.best_seller desc,m.name asc";
  6676. }
  6677. // price
  6678. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6679. NSString *price = params[@"price"];
  6680. price_min = 0;
  6681. price_max = MAXFLOAT;
  6682. if (params[@"user"]) {
  6683. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6684. NSMutableString *priceName = [NSMutableString string];
  6685. for (int i = 0; i < priceTypeArray.count; i++) {
  6686. NSString *pricetype = priceTypeArray[i];
  6687. pricetype = [self translateSingleQuote:pricetype];
  6688. if (i == 0) {
  6689. [priceName appendFormat:@"'%@'",pricetype];
  6690. } else {
  6691. [priceName appendFormat:@",'%@'",pricetype];
  6692. }
  6693. }
  6694. if ([price isEqualToString:@"Display All"]) {
  6695. price = [NSString stringWithFormat:@""];
  6696. } else if([price containsString:@"+"]){
  6697. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6698. price_min = [price doubleValue];
  6699. 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];
  6700. } else {
  6701. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6702. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6703. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6704. 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];
  6705. }
  6706. } else {
  6707. price = @"";
  6708. }
  6709. // sold_by_qty : Sold in quantities of %@
  6710. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6711. NSString *qty = params[@"sold_by_qty"];
  6712. if ([qty isEqualToString:@"Display All"]) {
  6713. qty = @"";
  6714. } else {
  6715. qty = [self translateSingleQuote:qty];
  6716. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6717. }
  6718. // model name;
  6719. NSString* modelname =params[@"modelName"];
  6720. if(modelname.length==0)
  6721. {
  6722. modelname=@"";
  6723. }
  6724. else
  6725. {
  6726. modelname = modelname.lowercaseString;
  6727. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6728. }
  6729. //modelDescription
  6730. NSString* modelDescription =params[@"modelDescription"];
  6731. if(modelDescription.length==0)
  6732. {
  6733. modelDescription=@"";
  6734. }
  6735. else
  6736. {
  6737. modelDescription = modelDescription.lowercaseString;
  6738. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6739. }
  6740. //lower(description) like'%%%@%%'
  6741. // category
  6742. NSString *category_id = params[@"ctgId"];
  6743. NSMutableArray *cate_id_array = nil;
  6744. NSMutableString *cateWhere = [NSMutableString string];
  6745. if ([category_id isEqualToString:@""] || !category_id) {
  6746. [cateWhere appendString:@"1 = 1"];
  6747. } else {
  6748. if ([category_id containsString:@","]) {
  6749. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6750. } else {
  6751. cate_id_array = [@[category_id] mutableCopy];
  6752. }
  6753. /*-----------*/
  6754. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6755. [cateWhere appendString:@"("];
  6756. for (int i = 0; i < cate_id_array.count; i++) {
  6757. for (NSString *key0 in cateDic.allKeys) {
  6758. if ([key0 containsString:@"category_"]) {
  6759. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6760. for (NSString *key1 in category0.allKeys) {
  6761. if ([key1 containsString:@"category_"]) {
  6762. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6763. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6764. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6765. cate_id_array[i] = [category1 objectForKey:@"id"];
  6766. if (i == 0) {
  6767. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6768. } else {
  6769. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6770. }
  6771. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6772. [category0 setValue:category1 forKey:key1];
  6773. [cateDic setValue:category0 forKey:key0];
  6774. }
  6775. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6776. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6777. cate_id_array[i] = [category0 objectForKey:@"id"];
  6778. if (i == 0) {
  6779. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6780. } else {
  6781. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6782. }
  6783. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6784. [cateDic setValue:category0 forKey:key0];
  6785. }
  6786. }
  6787. }
  6788. }
  6789. }
  6790. }
  6791. [cateWhere appendString:@")"];
  6792. [filter setValue:cateDic forKey:@"category"];
  6793. }
  6794. // where bestseller > 0 order by bestseller desc
  6795. // sql query: alert availability(int) best_seller(int) price qty
  6796. 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];
  6797. // count
  6798. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6799. }
  6800. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6801. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6802. if (!params[@"user"]) {
  6803. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6804. }
  6805. [ret setValue:filter forKey:@"filter"];
  6806. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6807. sqlite3_stmt * statement;
  6808. [ret setValue:@"2" forKey:@"result"];
  6809. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6810. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6811. // int count=0;
  6812. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6813. {
  6814. int i=0;
  6815. while (sqlite3_step(statement) == SQLITE_ROW)
  6816. {
  6817. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6818. char *name = (char*)sqlite3_column_text(statement, 0);
  6819. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6820. char *description = (char*)sqlite3_column_text(statement, 1);
  6821. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6822. int product_id = sqlite3_column_int(statement, 2);
  6823. int wid = sqlite3_column_int(statement, 3);
  6824. int closeout = sqlite3_column_int(statement, 4);
  6825. int cid = sqlite3_column_int(statement, 5);
  6826. int wisdelete = sqlite3_column_int(statement, 6);
  6827. int more_color = sqlite3_column_int(statement, 7);
  6828. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6829. if(wid !=0 && wisdelete != 1)
  6830. [item setValue:@"true" forKey:@"wish_exists"];
  6831. else
  6832. [item setValue:@"false" forKey:@"wish_exists"];
  6833. if(closeout==0)
  6834. [item setValue:@"false" forKey:@"is_closeout"];
  6835. else
  6836. [item setValue:@"true" forKey:@"is_closeout"];
  6837. if(cid==0)
  6838. [item setValue:@"false" forKey:@"cart_exists"];
  6839. else
  6840. [item setValue:@"true" forKey:@"cart_exists"];
  6841. if (more_color == 0) {
  6842. [item setObject:@(false) forKey:@"more_color"];
  6843. } else if (more_color == 1) {
  6844. [item setObject:@(true) forKey:@"more_color"];
  6845. }
  6846. [item addEntriesFromDictionary:imgjson];
  6847. // [item setValue:nsurl forKey:@"img"];
  6848. [item setValue:nsname forKey:@"fash_name"];
  6849. [item setValue:nsdescription forKey:@"description"];
  6850. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6851. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6852. i++;
  6853. }
  6854. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6855. [ret setObject:items forKey:@"items"];
  6856. sqlite3_finalize(statement);
  6857. }
  6858. [iSalesDB close_db:db];
  6859. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6860. return ret;
  6861. }
  6862. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6863. {
  6864. return [self itemsearch:params limited:YES];
  6865. }
  6866. #pragma mark order detail
  6867. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6868. if (str1.length == 0) {
  6869. str1 = @"&nbsp";
  6870. }
  6871. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6872. return str;
  6873. }
  6874. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6875. {
  6876. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6877. [fromformatter setDateFormat:from];
  6878. NSDate *date = [fromformatter dateFromString:datetime];
  6879. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6880. [toformatter setDateFormat:to];
  6881. NSString * ret = [toformatter stringFromDate:date];
  6882. return ret;
  6883. }
  6884. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6885. // 把毫秒去掉
  6886. if ([dateTime containsString:@"."]) {
  6887. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6888. }
  6889. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6890. formatter.dateFormat = formate;
  6891. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6892. NSDate *date = [formatter dateFromString:dateTime];
  6893. formatter.dateFormat = newFormate;
  6894. NSString *result = [formatter stringFromDate:date];
  6895. return result ? result : @"";
  6896. }
  6897. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6898. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6899. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6900. }
  6901. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6902. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6903. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6904. }
  6905. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6906. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6907. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6908. }
  6909. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6910. {
  6911. assert(params[@"mode"]!=nil);
  6912. assert(params[@"user"]!=nil);
  6913. DebugLog(@"offline oderdetail params: %@",params);
  6914. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6915. int orderId = [params[@"orderId"] intValue];
  6916. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6917. // decrypt card number and card security code
  6918. // 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 ];
  6919. 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];
  6920. sqlite3 *db = [iSalesDB get_db];
  6921. sqlite3_stmt * statement;
  6922. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6923. NSString *nssoid = @"";
  6924. NSString* orderinfo = @"";
  6925. NSString *moreInfo = @"";
  6926. double handlingFee = 0;
  6927. double payments_and_credist = 0;
  6928. double totalPrice = 0;
  6929. double shippingFee = 0;
  6930. double lift_gate = 0;
  6931. NSString *customer_contact = @"";
  6932. NSString *customer_email = @"";
  6933. NSString *customer_fax = @"";
  6934. NSString *customer_phone = @"";
  6935. NSString *customer_city = @"";
  6936. NSString *customer_state = @"";
  6937. NSString *customer_zipcode = @"";
  6938. NSString *customer_country = @"";
  6939. BOOL must_call = NO;
  6940. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6941. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6942. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6943. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6944. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6945. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6946. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6947. {
  6948. if (sqlite3_step(statement) == SQLITE_ROW)
  6949. {
  6950. // int order_id = sqlite3_column_int(statement, 0);
  6951. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6952. // ret[@"sign_url"] = sign_url;
  6953. section_1 = @{
  6954. @"data":sign_url,
  6955. @"title":@"Signature",
  6956. @"type":@"sign_url"
  6957. }.mutableCopy;
  6958. [ret setObject:section_1 forKey:@"section_1"];
  6959. customer_contact = [self textAtColumn:52 statement:statement];
  6960. customer_email = [self textAtColumn:53 statement:statement];
  6961. customer_phone = [self textAtColumn:54 statement:statement];
  6962. customer_fax = [self textAtColumn:55 statement:statement];
  6963. customer_city = [self textAtColumn:60 statement:statement];
  6964. customer_state = [self textAtColumn:61 statement:statement];
  6965. customer_zipcode = [self textAtColumn:62 statement:statement];
  6966. customer_country = [self textAtColumn:63 statement:statement];
  6967. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6968. int offline_edit=sqlite3_column_int(statement, 56);
  6969. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6970. char *soid = (char*)sqlite3_column_text(statement, 1);
  6971. if(soid==nil)
  6972. soid= "";
  6973. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6974. // so#
  6975. ret[@"so#"] = nssoid;
  6976. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6977. if(poNumber==nil)
  6978. poNumber= "";
  6979. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6980. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6981. if(create_time==nil)
  6982. create_time= "";
  6983. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6984. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6985. int status = sqlite3_column_int(statement, 4);
  6986. int erpStatus = sqlite3_column_int(statement, 49);
  6987. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6988. // status
  6989. if (status > 1 && status != 3) {
  6990. status = erpStatus;
  6991. } else if (status == 3) {
  6992. status = 15;
  6993. }
  6994. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6995. ret[@"order_status"] = nsstatus;
  6996. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6997. if(company_name==nil)
  6998. company_name= "";
  6999. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7000. // company name
  7001. ret[@"company_name"] = nscompany_name;
  7002. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7003. if(customer_contact==nil)
  7004. customer_contact= "";
  7005. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7006. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7007. if(addr_1==nil)
  7008. addr_1="";
  7009. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7010. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7011. if(addr_2==nil)
  7012. addr_2="";
  7013. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7014. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7015. if(addr_3==nil)
  7016. addr_3="";
  7017. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7018. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7019. if(addr_4==nil)
  7020. addr_4="";
  7021. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7022. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7023. [arr_addr addObject:nsaddr_1];
  7024. [arr_addr addObject:nsaddr_2];
  7025. [arr_addr addObject:nsaddr_3];
  7026. [arr_addr addObject:nsaddr_4];
  7027. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7028. [arr_addr addObject:customer_state];
  7029. [arr_addr addObject:customer_zipcode];
  7030. [arr_addr addObject:customer_country];
  7031. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7032. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7033. char *logist = (char*)sqlite3_column_text(statement, 11);
  7034. if(logist==nil)
  7035. logist= "";
  7036. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7037. if (/*status == -11 || */status == 10 || status == 11) {
  7038. nslogist = [self textAtColumn:59 statement:statement];
  7039. };
  7040. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7041. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7042. shipping = @"Shipping To Be Quoted";
  7043. } else {
  7044. shippingFee = sqlite3_column_double(statement, 12);
  7045. }
  7046. // Shipping
  7047. // ret[@"Shipping"] = shipping;
  7048. NSDictionary *shipping_item = @{
  7049. @"title":@"Shipping",
  7050. @"value":shipping
  7051. };
  7052. [price_data setObject:shipping_item forKey:@"item_1"];
  7053. int have_lift_gate = sqlite3_column_int(statement, 17);
  7054. lift_gate = sqlite3_column_double(statement, 13);
  7055. // Liftgate Fee(No loading dock)
  7056. if (!have_lift_gate) {
  7057. lift_gate = 0;
  7058. }
  7059. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7060. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7061. if (sqlite3_column_int(statement, 57)) {
  7062. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7063. liftgate_value = @"Shipping To Be Quoted";
  7064. }
  7065. NSDictionary *liftgate_item = @{
  7066. @"title":@"Liftgate Fee(No loading dock)",
  7067. @"value":liftgate_value
  7068. };
  7069. [price_data setObject:liftgate_item forKey:@"item_2"];
  7070. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7071. // [ret removeObjectForKey:@"Shipping"];
  7072. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7073. // }
  7074. //
  7075. // if (have_lift_gate) {
  7076. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7077. // }
  7078. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7079. if(general_notes==nil)
  7080. general_notes= "";
  7081. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7082. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7083. if(internal_notes==nil)
  7084. internal_notes= "";
  7085. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7086. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7087. if(payment_type==nil)
  7088. payment_type= "";
  7089. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7090. // order info
  7091. orderinfo = [self textFileName:@"order_info.html"];
  7092. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7093. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7094. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7095. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7096. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7097. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7098. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7099. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7100. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7101. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7102. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7103. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7104. NSString *payment = nil;
  7105. // id -> show
  7106. __block NSString *show_pay_type = nspayment_type;
  7107. [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) {
  7108. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7109. if (show_typ_ch != NULL) {
  7110. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7111. }
  7112. }];
  7113. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7114. {
  7115. payment = [self textFileName:@"creditcardpayment.html"];
  7116. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7117. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7118. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7119. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7120. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7121. NSString *card_type = [self textAtColumn:42 statement:statement];
  7122. if (card_type.length > 0) { // 显示星号
  7123. card_type = @"****";
  7124. }
  7125. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7126. if (card_number.length > 0 && card_number.length > 4) {
  7127. for (int i = 0; i < card_number.length - 4; i++) {
  7128. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7129. }
  7130. } else {
  7131. card_number = @"";
  7132. }
  7133. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7134. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7135. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7136. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7137. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7138. card_expiration = @"****";
  7139. }
  7140. NSString *card_city = [self textAtColumn:46 statement:statement];
  7141. NSString *card_state = [self textAtColumn:47 statement:statement];
  7142. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7143. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7144. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7145. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7146. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7147. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7148. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7149. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7150. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7151. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7152. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7153. }
  7154. else
  7155. {
  7156. payment=[self textFileName:@"normalpayment.html"];
  7157. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7158. }
  7159. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7160. ret[@"result"]= [NSNumber numberWithInt:2];
  7161. // more info
  7162. moreInfo = [self textFileName:@"more_info.html"];
  7163. /*****ship to******/
  7164. // ShipToCompany_or_&nbsp
  7165. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7166. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7167. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7168. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7169. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7170. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7171. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7172. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7173. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7174. /*****ship from******/
  7175. // ShipFromCompany_or_&nbsp
  7176. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7177. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7178. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7179. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7180. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7181. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7182. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7183. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7184. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7185. /*****freight to******/
  7186. // FreightBillToCompany_or_&nbsp
  7187. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7188. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7189. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7190. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7191. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7192. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7193. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7194. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7195. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7196. /*****merchandise to******/
  7197. // MerchandiseBillToCompany_or_&nbsp
  7198. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7199. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7200. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7201. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7202. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7203. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7204. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7205. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7206. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7207. /*****return to******/
  7208. // ReturnToCompany_or_&nbsp
  7209. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7210. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7211. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7212. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7213. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7214. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7215. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7216. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7217. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7218. //
  7219. // DebugLog(@"more info : %@",moreInfo);
  7220. // handling fee
  7221. handlingFee = sqlite3_column_double(statement, 33);
  7222. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7223. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7224. if (sqlite3_column_int(statement, 58)) {
  7225. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7226. handling_fee_value = @"Shipping To Be Quoted";
  7227. }
  7228. NSDictionary *handling_fee_item = @{
  7229. @"title":@"Handling Fee",
  7230. @"value":handling_fee_value
  7231. };
  7232. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7233. //
  7234. // customer info
  7235. customerID = [self textAtColumn:36 statement:statement];
  7236. // mode
  7237. ret[@"mode"] = params[@"mode"];
  7238. // model_count
  7239. ret[@"model_count"] = @(0);
  7240. }
  7241. sqlite3_finalize(statement);
  7242. }
  7243. [iSalesDB close_db:db];
  7244. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7245. // "customer_email" = "Shui Hu";
  7246. // "customer_fax" = "";
  7247. // "customer_first_name" = F;
  7248. // "customer_last_name" = L;
  7249. // "customer_name" = ",da He Xiang Dong Liu A";
  7250. // "customer_phone" = "Hey Xuan Feng";
  7251. contactInfo[@"customer_phone"] = customer_phone;
  7252. contactInfo[@"customer_fax"] = customer_fax;
  7253. contactInfo[@"customer_email"] = customer_email;
  7254. NSString *first_name = @"";
  7255. NSString *last_name = @"";
  7256. if ([customer_contact isEqualToString:@""]) {
  7257. } else if ([customer_contact containsString:@" "]) {
  7258. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7259. first_name = [customer_contact substringToIndex:first_space_index];
  7260. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7261. }
  7262. contactInfo[@"customer_first_name"] = first_name;
  7263. contactInfo[@"customer_last_name"] = last_name;
  7264. ret[@"customerInfo"] = contactInfo;
  7265. // models
  7266. if (nssoid) {
  7267. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7268. __block double TotalCuft = 0;
  7269. __block double TotalWeight = 0;
  7270. __block int TotalCarton = 0;
  7271. __block double allItemPrice = 0;
  7272. 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];
  7273. sqlite3 *db1 = [iSalesDB get_db];
  7274. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7275. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7276. int product_id = sqlite3_column_int(stmt, 0);
  7277. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7278. int item_id = sqlite3_column_int(stmt, 7);
  7279. NSString* Price=nil;
  7280. if(str_price==nil)
  7281. {
  7282. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7283. if(price==nil)
  7284. Price=@"No Price.";
  7285. else
  7286. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7287. }
  7288. else
  7289. {
  7290. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7291. }
  7292. double discount = sqlite3_column_double(stmt, 2);
  7293. int item_count = sqlite3_column_int(stmt, 3);
  7294. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7295. NSString *nsline_note=nil;
  7296. if(line_note!=nil)
  7297. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7298. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7299. // NSString *nsname = nil;
  7300. // if(name!=nil)
  7301. // nsname= [[NSString alloc]initWithUTF8String:name];
  7302. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7303. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7304. // NSString *nsdescription=nil;
  7305. // if(description!=nil)
  7306. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7307. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7308. // int stockUom = sqlite3_column_int(stmt, 8);
  7309. // int _id = sqlite3_column_int(stmt, 9);
  7310. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7311. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7312. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7313. int carton=[bsubtotaljson[@"carton"] intValue];
  7314. TotalCuft += cuft;
  7315. TotalWeight += weight;
  7316. TotalCarton += carton;
  7317. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7318. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7319. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7320. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7321. itemjson[@"note"]=nsline_note;
  7322. itemjson[@"origin_price"] = Price;
  7323. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7324. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7325. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7326. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7327. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7328. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7329. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7330. if(itemjson[@"combine"] != nil)
  7331. {
  7332. // int citem=0;
  7333. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7334. for(int bc=0;bc<bcount;bc++)
  7335. {
  7336. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7337. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7338. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7339. subTotal += uprice * modulus * item_count;
  7340. }
  7341. }
  7342. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7343. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7344. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7345. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7346. itemjson[@"type"] = @"order_item";
  7347. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7348. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7349. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7350. allItemPrice += subTotal;
  7351. }];
  7352. section_3[@"data"] = model_data;
  7353. section_3[@"type"] = @"sub_order";
  7354. section_3[@"title"] = @"Models";
  7355. section_3[@"switch"] = @(false);
  7356. ret[@"section_3"] = section_3;
  7357. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7358. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7359. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7360. // payments/Credits
  7361. // payments_and_credist = sqlite3_column_double(statement, 34);
  7362. payments_and_credist = allItemPrice;
  7363. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7364. NSDictionary *sub_total_item = @{
  7365. @"title":@"Sub-Total",
  7366. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7367. };
  7368. [price_data setObject:sub_total_item forKey:@"item_0"];
  7369. // // total
  7370. // totalPrice = sqlite3_column_double(statement, 35);
  7371. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7372. } else {
  7373. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7374. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7375. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7376. // payments/Credits
  7377. payments_and_credist = 0;
  7378. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7379. NSDictionary *sub_total_item = @{
  7380. @"title":@"Sub-Total",
  7381. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7382. };
  7383. [price_data setObject:sub_total_item forKey:@"item_0"];
  7384. }
  7385. // total
  7386. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7387. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7388. NSDictionary *total_item = @{
  7389. @"title":@"Total",
  7390. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7391. };
  7392. [price_data setObject:total_item forKey:@"item_4"];
  7393. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7394. section_4 = @{
  7395. @"data":price_data,
  7396. @"title":@"Price Info",
  7397. @"type":@"price_info"
  7398. }.mutableCopy;
  7399. ret[@"section_4"] = section_4;
  7400. // ret[@"order_info"]= orderinfo;
  7401. section_0 = @{
  7402. @"data":orderinfo,
  7403. @"title":@"Order Info",
  7404. @"type":@"order_info"
  7405. }.mutableCopy;
  7406. ret[@"section_0"] = section_0;
  7407. // ret[@"more_order_info"] = moreInfo;
  7408. section_2 = @{
  7409. @"data":moreInfo,
  7410. @"title":@"More Info",
  7411. @"type":@"more_order_info"
  7412. }.mutableCopy;
  7413. ret[@"section_2"] = section_2;
  7414. ret[@"count"] = @(5);
  7415. return [RAConvertor dict2data:ret];
  7416. }
  7417. #pragma mark order list
  7418. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7419. NSString *nsstatus = @"";
  7420. switch (status) {
  7421. case 0:
  7422. {
  7423. nsstatus=@"Temp Order";
  7424. break;
  7425. }
  7426. case 1:
  7427. {
  7428. nsstatus=@"Saved Order";
  7429. break;
  7430. }
  7431. case 2: {
  7432. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7433. break;
  7434. }
  7435. case 3:
  7436. case 15:
  7437. {
  7438. nsstatus=@"Cancelled";
  7439. break;
  7440. }
  7441. case 10:
  7442. {
  7443. nsstatus=@"Quote Submitted";
  7444. break;
  7445. }
  7446. case 11:
  7447. {
  7448. nsstatus=@"Sales Order Submitted";
  7449. break;
  7450. }
  7451. case 12:
  7452. {
  7453. nsstatus=@"Processing";
  7454. break;
  7455. }
  7456. case 13:
  7457. {
  7458. nsstatus=@"Shipped";
  7459. break;
  7460. }
  7461. case 14:
  7462. {
  7463. nsstatus=@"Closed";
  7464. break;
  7465. }
  7466. case -11:
  7467. {
  7468. nsstatus = @"Ready For Submit";
  7469. break;
  7470. }
  7471. default:
  7472. break;
  7473. }
  7474. return nsstatus;
  7475. }
  7476. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7477. {
  7478. double total = 0;
  7479. __block double payments_and_credist = 0;
  7480. __block double allItemPrice = 0;
  7481. // sqlite3 *db1 = [iSalesDB get_db];
  7482. if (so_id) {
  7483. // 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];
  7484. 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];
  7485. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7486. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7487. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7488. int product_id = sqlite3_column_int(stmt, 0);
  7489. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7490. int discount = sqlite3_column_int(stmt, 2);
  7491. int item_count = sqlite3_column_int(stmt, 3);
  7492. // int item_id = sqlite3_column_int(stmt, 7);
  7493. int item_id = sqlite3_column_int(stmt, 4);
  7494. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7495. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7496. NSString* Price=nil;
  7497. if(str_price==nil)
  7498. {
  7499. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7500. if(price==nil)
  7501. Price=@"No Price.";
  7502. else
  7503. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7504. }
  7505. else
  7506. {
  7507. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7508. }
  7509. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7510. itemjson[@"The unit price"]=Price;
  7511. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7512. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7513. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7514. if(itemjson[@"combine"] != nil)
  7515. {
  7516. // int citem=0;
  7517. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7518. for(int bc=0;bc<bcount;bc++)
  7519. {
  7520. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7521. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7522. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7523. subTotal += uprice * modulus * item_count;
  7524. }
  7525. }
  7526. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7527. allItemPrice += subTotal;
  7528. }];
  7529. payments_and_credist = allItemPrice;
  7530. } else {
  7531. // payments/Credits
  7532. payments_and_credist = 0;
  7533. }
  7534. // lift_gate handlingFee shippingFee
  7535. __block double lift_gate = 0;
  7536. __block double handlingFee = 0;
  7537. __block double shippingFee = 0;
  7538. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7539. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7540. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7541. if (have_lift_gate) {
  7542. lift_gate = sqlite3_column_double(stmt, 1);
  7543. }
  7544. handlingFee = sqlite3_column_double(stmt, 2);
  7545. shippingFee = sqlite3_column_double(stmt, 3);
  7546. }];
  7547. // total
  7548. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7549. if (close) {
  7550. [iSalesDB close_db:db1];
  7551. }
  7552. return total;
  7553. }
  7554. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7555. {
  7556. assert(params[@"user"]!=nil);
  7557. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7558. int limit = [[params valueForKey:@"limit"] intValue];
  7559. int offset = [[params valueForKey:@"offset"] intValue];
  7560. NSString* keyword = [params valueForKey:@"keyWord"];
  7561. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7562. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7563. NSString* where=@"1 = 1";
  7564. if(keyword.length>0)
  7565. 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]];
  7566. if (orderStatus.length > 0) {
  7567. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7568. if (order_status_array.count == 1) {
  7569. int status_value = [[order_status_array firstObject] intValue];
  7570. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7571. if (status_value == 15 || status_value == 3) {
  7572. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7573. } else {
  7574. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7575. }
  7576. } else {
  7577. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7578. }
  7579. } else if (order_status_array.count > 1) {
  7580. for (int i = 0; i < order_status_array.count;i++) {
  7581. NSString *status = order_status_array[i];
  7582. NSString *condition = @" or";
  7583. if (i == 0) {
  7584. condition = @" and (";
  7585. }
  7586. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7587. int status_value = [status intValue];
  7588. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7589. if (status_value == 15 || status_value == 3) {
  7590. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7591. } else {
  7592. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7593. }
  7594. } else {
  7595. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7596. }
  7597. }
  7598. where = [where stringByAppendingString:@" )"];
  7599. }
  7600. }
  7601. 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];
  7602. // DebugLog(@"order list sql: %@",sqlQuery);
  7603. sqlite3 *db = [iSalesDB get_db];
  7604. sqlite3_stmt * statement;
  7605. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7606. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7607. {
  7608. int count=0;
  7609. while (sqlite3_step(statement) == SQLITE_ROW)
  7610. {
  7611. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7612. int order_id = sqlite3_column_double(statement, 0);
  7613. char *soid = (char*)sqlite3_column_text(statement, 1);
  7614. if(soid==nil)
  7615. soid= "";
  7616. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7617. int status = sqlite3_column_double(statement, 2);
  7618. int erpStatus = sqlite3_column_double(statement, 9);
  7619. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7620. if(sales_rep==nil)
  7621. sales_rep= "";
  7622. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7623. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7624. if(create_by==nil)
  7625. create_by= "";
  7626. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7627. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7628. if(company_name==nil)
  7629. company_name= "";
  7630. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7631. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7632. if(create_time==nil)
  7633. create_time= "";
  7634. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7635. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7636. // double total_price = sqlite3_column_double(statement, 7);
  7637. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7638. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7639. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7640. int offline_edit = sqlite3_column_int(statement, 10);
  7641. // ": "JH",
  7642. // "": "$8307.00",
  7643. // "": "MOB1608050001",
  7644. // "": "ArpithaT",
  7645. // "": "1st Stage Property Transformations",
  7646. // "": "JANICE SUTTON",
  7647. // "": 2255,
  7648. // "": "08/02/2016 09:49:18",
  7649. // "": 1,
  7650. // "": "Saved Order"
  7651. // "": "1470384050483",
  7652. // "model_count": "6 / 28"
  7653. item[@"sales_rep"]= nssales_rep;
  7654. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7655. item[@"so#"]= nssoid;
  7656. item[@"create_by"]= nscreate_by;
  7657. item[@"customer_name"]= nscompany_name;
  7658. item[@"customer_contact"] = customer_contact;
  7659. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7660. item[@"purchase_time"]= nscreate_time;
  7661. int statusCode = status;
  7662. if (statusCode == 2) {
  7663. statusCode = erpStatus;
  7664. } else if (statusCode == 3) {
  7665. statusCode = 15;
  7666. }
  7667. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7668. item[@"order_status"]= nsstatus;
  7669. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7670. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7671. // item[@"model_count"]
  7672. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7673. count++;
  7674. }
  7675. ret[@"count"]= [NSNumber numberWithInt:count];
  7676. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7677. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7678. ret[@"result"]= [NSNumber numberWithInt:2];
  7679. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7680. ret[@"time_zone"] = @"PST";
  7681. sqlite3_finalize(statement);
  7682. }
  7683. 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];
  7684. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7685. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7686. [iSalesDB close_db:db];
  7687. return [RAConvertor dict2data:ret];
  7688. }
  7689. #pragma mark update gnotes
  7690. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7691. {
  7692. DebugLog(@"params: %@",params);
  7693. // comments = Meyoyoyoyoyoyoy;
  7694. // orderCode = MOB1608110001;
  7695. // password = 123456;
  7696. // user = EvanK;
  7697. 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]];
  7698. int ret = [iSalesDB execSql:sql];
  7699. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7700. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7701. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7702. // [dic setValue:@"160409" forKey:@"min_ver"];
  7703. return [RAConvertor dict2data:dic];
  7704. }
  7705. #pragma mark move to wishlist
  7706. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7707. {
  7708. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7709. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7710. _id = [NSString stringWithFormat:@"(%@)",_id];
  7711. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7712. sqlite3 *db = [iSalesDB get_db];
  7713. __block NSString *product_id = @"";
  7714. __block NSString *item_count_str = @"";
  7715. // __block NSString *item_id = nil;
  7716. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7717. // product_id = [self textAtColumn:0 statement:stmt];
  7718. int item_count = sqlite3_column_int(stmt, 1);
  7719. // item_id = [self textAtColumn:2 statement:stmt];
  7720. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7721. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7722. if (p_id.length) {
  7723. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7724. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7725. }
  7726. }];
  7727. [iSalesDB close_db:db];
  7728. // 去除第一个,
  7729. if (product_id.length > 1) {
  7730. product_id = [product_id substringFromIndex:1];
  7731. }
  7732. if (item_count_str.length > 1) {
  7733. item_count_str = [item_count_str substringFromIndex:1];
  7734. }
  7735. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7736. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7737. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7738. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7739. sqlite3 *db1 = [iSalesDB get_db];
  7740. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7741. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7742. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7743. // 删除
  7744. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7745. int ret = [iSalesDB execSql:deleteSql db:db1];
  7746. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7747. [iSalesDB close_db:db1];
  7748. return [RAConvertor dict2data:dic];
  7749. }
  7750. #pragma mark cart delete
  7751. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7752. {
  7753. // cartItemId = 548;
  7754. // orderCode = MOB1608110001;
  7755. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7756. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7757. _id = [NSString stringWithFormat:@"(%@)",_id];
  7758. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7759. int ret = [iSalesDB execSql:sql];
  7760. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7761. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7762. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7763. // [dic setValue:@"160409" forKey:@"min_ver"];
  7764. return [RAConvertor dict2data:dic];
  7765. }
  7766. #pragma mark set price
  7767. +(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7768. {
  7769. DebugLog(@"cart set price params: %@",params);
  7770. // "cartitem_id" = 1;
  7771. // discount = "0.000000";
  7772. // "item_note" = "";
  7773. // price = "269.000000";
  7774. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7775. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7776. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7777. // NSString *price = [self valueInParams:params key:@"price"];
  7778. // bool badd_price_changed=false;
  7779. // sqlite3* db=[iSalesDB get_db];
  7780. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7781. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7782. // NSRange range;
  7783. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7784. //
  7785. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7786. // badd_price_changed=true;
  7787. // [iSalesDB close_db:db];
  7788. //
  7789. // if(badd_price_changed)
  7790. // {
  7791. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7792. // }
  7793. //
  7794. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7795. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7796. int ret = [iSalesDB execSql:sql];
  7797. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7798. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7799. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7800. // [dic setValue:@"160409" forKey:@"min_ver"];
  7801. // return [RAConvertor dict2data:dic];
  7802. return dic;
  7803. }
  7804. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7805. {
  7806. DebugLog(@"cart set price params: %@",params);
  7807. // "cartitem_id" = 1;
  7808. // discount = "0.000000";
  7809. // "item_note" = "";
  7810. // price = "269.000000";
  7811. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7812. NSString *notes = [self valueInParams:params key:@"item_note"];
  7813. NSString *discount = [self valueInParams:params key:@"discount"];
  7814. NSString *price = [self valueInParams:params key:@"price"];
  7815. // bool badd_price_changed=false;
  7816. // sqlite3* db=[iSalesDB get_db];
  7817. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7818. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7819. // NSRange range;
  7820. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7821. //
  7822. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7823. // badd_price_changed=true;
  7824. // [iSalesDB close_db:db];
  7825. //
  7826. // if(badd_price_changed)
  7827. // {
  7828. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7829. // }
  7830. //
  7831. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7832. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7833. int ret = [iSalesDB execSql:sql];
  7834. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7835. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7836. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7837. // [dic setValue:@"160409" forKey:@"min_ver"];
  7838. return [RAConvertor dict2data:dic];
  7839. }
  7840. #pragma mark set line notes
  7841. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7842. {
  7843. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7844. NSString *notes = [self valueInParams:params key:@"notes"];
  7845. notes = [self translateSingleQuote:notes];
  7846. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7847. int ret = [iSalesDB execSql:sql];
  7848. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7849. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7850. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7851. // [dic setValue:@"160409" forKey:@"min_ver"];
  7852. return [RAConvertor dict2data:dic];
  7853. }
  7854. #pragma mark set qty
  7855. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7856. {
  7857. assert(params[@"can_create_backorder"]!=nil);
  7858. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7859. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7860. int item_count = [params[@"inputInt"] intValue];
  7861. // 购买检查数量大于库存
  7862. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7863. if (!params[@"can_create_backorder"]) {
  7864. 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];
  7865. __block BOOL out_of_stock = NO;
  7866. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7867. int availability = sqlite3_column_int(stmt, 0);
  7868. if (availability < item_count) {
  7869. out_of_stock = YES;
  7870. }
  7871. }];
  7872. if (out_of_stock) { // 缺货
  7873. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7874. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7875. return [RAConvertor dict2data:dic];
  7876. }
  7877. }
  7878. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7879. int ret = [iSalesDB execSql:sql];
  7880. __block int item_id = 0;
  7881. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7882. item_id = sqlite3_column_int(stmt, 0);
  7883. }];
  7884. sqlite3 *db = [iSalesDB get_db];
  7885. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7886. [iSalesDB close_db:db];
  7887. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7888. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7889. // [dic setValue:@"160409" forKey:@"min_ver"];
  7890. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7891. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7892. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7893. return [RAConvertor dict2data:dic];
  7894. }
  7895. #pragma mark place order
  7896. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7897. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7898. if (aname) {
  7899. [dic setValue:aname forKey:@"aname"];
  7900. }
  7901. if (control) {
  7902. [dic setValue:control forKey:@"control"];
  7903. }
  7904. if (keyboard) {
  7905. [dic setValue:keyboard forKey:@"keyboard"];
  7906. }
  7907. if (name) {
  7908. [dic setValue:name forKey:@"name"];
  7909. }
  7910. if (value) {
  7911. [dic setValue:value forKey:@"value"];
  7912. }
  7913. return dic;
  7914. }
  7915. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7916. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7917. orderCode = [self translateSingleQuote:orderCode];
  7918. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7919. 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];
  7920. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7921. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7922. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7923. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7924. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7925. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7926. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7927. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7928. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7929. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7930. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7931. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7932. NSString *email = [self textAtColumn:11 statement:stmt];
  7933. NSString *phone = [self textAtColumn:12 statement:stmt];
  7934. NSString *fax = [self textAtColumn:13 statement:stmt];
  7935. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7936. NSString *city = [self textAtColumn:15 statement:stmt];
  7937. NSString *state = [self textAtColumn:16 statement:stmt];
  7938. NSString *country = [self textAtColumn:17 statement:stmt];
  7939. NSString *name = [self textAtColumn:18 statement:stmt];
  7940. // contact id
  7941. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7942. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7943. [contact_id_dic setValue:@"text" forKey:@"control"];
  7944. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7945. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7946. [contact_id_dic setValue:@"true" forKey:@"required"];
  7947. [contact_id_dic setValue:contact_id forKey:@"value"];
  7948. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7949. // business card
  7950. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7951. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7952. [business_card_dic setValue:@"img" forKey:@"control"];
  7953. [business_card_dic setValue:@"1" forKey:@"disable"];
  7954. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7955. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7956. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7957. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7958. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7959. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7960. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7961. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7962. // fax
  7963. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7964. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7965. // zipcode
  7966. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7967. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7968. // city
  7969. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7970. [customer_dic setValue:city_dic forKey:@"item_12"];
  7971. // state
  7972. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7973. [customer_dic setValue:state_dic forKey:@"item_13"];
  7974. // country
  7975. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7976. [customer_dic setValue:country_dic forKey:@"item_14"];
  7977. // company name
  7978. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7979. [customer_dic setValue:company_dic forKey:@"item_2"];
  7980. // addr_1
  7981. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7982. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7983. // addr_2
  7984. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7985. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7986. // addr_3
  7987. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7988. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7989. // addr_4
  7990. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7991. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7992. // Contact
  7993. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7994. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7995. // email
  7996. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7997. [customer_dic setValue:email_dic forKey:@"item_8"];
  7998. // phone
  7999. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8000. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8001. // title
  8002. [customer_dic setValue:@"Customer" forKey:@"title"];
  8003. // count
  8004. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8005. }];
  8006. // setting
  8007. NSDictionary *setting = params[@"setting"];
  8008. NSNumber *hide = setting[@"CustomerHide"];
  8009. [customer_dic setValue:hide forKey:@"hide"];
  8010. return customer_dic;
  8011. }
  8012. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8013. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8014. // setting
  8015. NSDictionary *setting = params[@"setting"];
  8016. NSNumber *hide = setting[@"ShipToHide"];
  8017. [dic setValue:hide forKey:@"hide"];
  8018. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8019. orderCode = [self translateSingleQuote:orderCode];
  8020. 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];
  8021. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8022. NSString *cid = [self textAtColumn:0 statement:stmt];
  8023. NSString *name = [self textAtColumn:1 statement:stmt];
  8024. NSString *ext = [self textAtColumn:2 statement:stmt];
  8025. NSString *contact = [self textAtColumn:3 statement:stmt];
  8026. NSString *email = [self textAtColumn:4 statement:stmt];
  8027. NSString *fax = [self textAtColumn:5 statement:stmt];
  8028. NSString *phone = [self textAtColumn:6 statement:stmt];
  8029. // count
  8030. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8031. // title
  8032. [dic setValue:@"Ship To" forKey:@"title"];
  8033. // choose
  8034. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8035. [choose_dic setValue:@"choose" forKey:@"aname"];
  8036. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8037. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8038. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8039. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8040. @"receive_contact" : @"customer_contact",
  8041. @"receive_email" : @"customer_email",
  8042. @"receive_ext" : @"customer_contact_ext",
  8043. @"receive_fax" : @"customer_fax",
  8044. @"receive_name" : @"customer_name",
  8045. @"receive_phone" : @"customer_phone"},
  8046. @"refresh" : [NSNumber numberWithInteger:1],
  8047. @"type" : @"pull"};
  8048. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8049. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8050. @"name" : @"Add new address",
  8051. @"refresh" : [NSNumber numberWithInteger:1],
  8052. @"value" : @"new_addr"
  8053. };
  8054. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8055. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8056. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8057. @"receive_contact" : @"customer_contact",
  8058. @"receive_email" : @"customer_email",
  8059. @"receive_ext" : @"customer_contact_ext",
  8060. @"receive_fax" : @"customer_fax",
  8061. @"receive_name" : @"customer_name",
  8062. @"receive_phone" : @"customer_phone"},
  8063. @"name" : @"select_ship_to",
  8064. @"refresh" : [NSNumber numberWithInteger:1],
  8065. @"value" : @"Sales_Order_Ship_To"};
  8066. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8067. [dic setValue:choose_dic forKey:@"item_0"];
  8068. // contact id
  8069. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8070. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8071. [contact_id_dic setValue:@"true" forKey:@"required"];
  8072. [dic setValue:contact_id_dic forKey:@"item_1"];
  8073. // company name
  8074. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8075. [dic setValue:company_name_dic forKey:@"item_2"];
  8076. // address
  8077. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8078. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8079. if ([required integerValue]) {
  8080. [ext_dic setValue:@"true" forKey:@"required"];
  8081. }
  8082. [dic setValue:ext_dic forKey:@"item_3"];
  8083. // contact
  8084. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8085. [dic setValue:contact_dic forKey:@"item_4"];
  8086. // phone
  8087. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8088. [dic setValue:phone_dic forKey:@"item_5"];
  8089. // fax
  8090. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8091. [dic setValue:fax_dic forKey:@"item_6"];
  8092. // email
  8093. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8094. [dic setValue:email_dic forKey:@"item_7"];
  8095. }];
  8096. return dic;
  8097. }
  8098. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8099. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8100. // setting
  8101. NSDictionary *setting = params[@"setting"];
  8102. NSNumber *hide = setting[@"ShipFromHide"];
  8103. [dic setValue:hide forKey:@"hide"];
  8104. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8105. orderCode = [self translateSingleQuote:orderCode];
  8106. 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];
  8107. 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';";
  8108. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8109. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8110. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8111. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8112. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8113. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8114. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8115. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8116. if (!cid.length) {
  8117. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8118. cid = [self textAtColumn:0 statement:statment];
  8119. name = [self textAtColumn:1 statement:statment];
  8120. ext = [self textAtColumn:2 statement:statment];
  8121. contact = [self textAtColumn:3 statement:statment];
  8122. email = [self textAtColumn:4 statement:statment];
  8123. fax = [self textAtColumn:5 statement:statment];
  8124. phone = [self textAtColumn:6 statement:statment];
  8125. }];
  8126. }
  8127. // count
  8128. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8129. // title
  8130. [dic setValue:@"Ship From" forKey:@"title"];
  8131. // hide
  8132. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8133. // choose
  8134. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8135. [choose_dic setValue:@"choose" forKey:@"aname"];
  8136. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8137. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8138. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8139. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8140. @"sender_contact" : @"customer_contact",
  8141. @"sender_email" : @"customer_email",
  8142. @"sender_ext" : @"customer_contact_ext",
  8143. @"sender_fax" : @"customer_fax",
  8144. @"sender_name" : @"customer_name",
  8145. @"sender_phone" : @"customer_phone"},
  8146. @"name" : @"select_cid",
  8147. @"refresh" : [NSNumber numberWithInteger:0],
  8148. @"value" : @"Sales_Order_Ship_From"};
  8149. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8150. [dic setValue:choose_dic forKey:@"item_0"];
  8151. // contact id
  8152. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8153. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8154. [contact_id_dic setValue:@"true" forKey:@"required"];
  8155. [dic setValue:contact_id_dic forKey:@"item_1"];
  8156. // company name
  8157. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8158. [dic setValue:company_name_dic forKey:@"item_2"];
  8159. // address
  8160. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8161. [dic setValue:ext_dic forKey:@"item_3"];
  8162. // contact
  8163. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8164. [dic setValue:contact_dic forKey:@"item_4"];
  8165. // phone
  8166. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8167. [dic setValue:phone_dic forKey:@"item_5"];
  8168. // fax
  8169. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8170. [dic setValue:fax_dic forKey:@"item_6"];
  8171. // email
  8172. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8173. [dic setValue:email_dic forKey:@"item_7"];
  8174. }];
  8175. return dic;
  8176. }
  8177. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8178. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8179. // setting
  8180. NSDictionary *setting = params[@"setting"];
  8181. NSNumber *hide = setting[@"FreightBillToHide"];
  8182. [dic setValue:hide forKey:@"hide"];
  8183. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8184. orderCode = [self translateSingleQuote:orderCode];
  8185. 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];
  8186. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8187. NSString *cid = [self textAtColumn:0 statement:stmt];
  8188. NSString *name = [self textAtColumn:1 statement:stmt];
  8189. NSString *ext = [self textAtColumn:2 statement:stmt];
  8190. NSString *contact = [self textAtColumn:3 statement:stmt];
  8191. NSString *email = [self textAtColumn:4 statement:stmt];
  8192. NSString *fax = [self textAtColumn:5 statement:stmt];
  8193. NSString *phone = [self textAtColumn:6 statement:stmt];
  8194. // count
  8195. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8196. // title
  8197. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8198. // hide
  8199. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8200. // choose
  8201. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8202. [choose_dic setValue:@"choose" forKey:@"aname"];
  8203. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8204. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8205. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8206. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8207. @"shipping_billto_contact" : @"receive_contact",
  8208. @"shipping_billto_email" : @"receive_email",
  8209. @"shipping_billto_ext" : @"receive_ext",
  8210. @"shipping_billto_fax" : @"receive_fax",
  8211. @"shipping_billto_name" : @"receive_name",
  8212. @"shipping_billto_phone" : @"receive_phone"},
  8213. @"type" : @"pull"};
  8214. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8215. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8216. @"type" : @"pull",
  8217. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8218. @"shipping_billto_contact" : @"customer_contact",
  8219. @"shipping_billto_email" : @"customer_email",
  8220. @"shipping_billto_ext" : @"customer_contact_ext",
  8221. @"shipping_billto_fax" : @"customer_fax",
  8222. @"shipping_billto_name" : @"customer_name",
  8223. @"shipping_billto_phone" : @"customer_phone"}
  8224. };
  8225. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8226. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8227. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8228. @"shipping_billto_contact" : @"sender_contact",
  8229. @"shipping_billto_email" : @"sender_email",
  8230. @"shipping_billto_ext" : @"sender_ext",
  8231. @"shipping_billto_fax" : @"sender_fax",
  8232. @"shipping_billto_name" : @"sender_name",
  8233. @"shipping_billto_phone" : @"sender_phone"},
  8234. @"type" : @"pull"
  8235. };
  8236. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8237. NSDictionary *select_freight_bill_to_dic = @{
  8238. @"aname" : @"Select freight bill to",
  8239. @"name" : @"select_cid",
  8240. @"refresh" : [NSNumber numberWithInteger:0],
  8241. @"value" : @"Sales_Order_Freight_Bill_To",
  8242. @"key_map" : @{
  8243. @"shipping_billto_cid" : @"customer_cid",
  8244. @"shipping_billto_contact" : @"customer_contact",
  8245. @"shipping_billto_email" : @"customer_email",
  8246. @"shipping_billto_ext" : @"customer_contact_ext",
  8247. @"shipping_billto_fax" : @"customer_fax",
  8248. @"shipping_billto_name" : @"customer_name",
  8249. @"shipping_billto_phone" : @"customer_phone"
  8250. }
  8251. };
  8252. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8253. [dic setValue:choose_dic forKey:@"item_0"];
  8254. // contact id
  8255. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8256. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8257. [contact_id_dic setValue:@"true" forKey:@"required"];
  8258. [dic setValue:contact_id_dic forKey:@"item_1"];
  8259. // company name
  8260. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8261. [dic setValue:company_name_dic forKey:@"item_2"];
  8262. // address
  8263. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8264. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8265. if ([ext_required integerValue]) {
  8266. [ext_dic setValue:@"true" forKey:@"required"];
  8267. }
  8268. [dic setValue:ext_dic forKey:@"item_3"];
  8269. // contact
  8270. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8271. [dic setValue:contact_dic forKey:@"item_4"];
  8272. // phone
  8273. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8274. [dic setValue:phone_dic forKey:@"item_5"];
  8275. // fax
  8276. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8277. [dic setValue:fax_dic forKey:@"item_6"];
  8278. // email
  8279. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8280. [dic setValue:email_dic forKey:@"item_7"];
  8281. }];
  8282. return dic;
  8283. }
  8284. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8285. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8286. // setting
  8287. NSDictionary *setting = params[@"setting"];
  8288. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8289. [dic setValue:hide forKey:@"hide"];
  8290. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8291. orderCode = [self translateSingleQuote:orderCode];
  8292. 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];
  8293. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8294. NSString *cid = [self textAtColumn:0 statement:stmt];
  8295. NSString *name = [self textAtColumn:1 statement:stmt];
  8296. NSString *ext = [self textAtColumn:2 statement:stmt];
  8297. NSString *contact = [self textAtColumn:3 statement:stmt];
  8298. NSString *email = [self textAtColumn:4 statement:stmt];
  8299. NSString *fax = [self textAtColumn:5 statement:stmt];
  8300. NSString *phone = [self textAtColumn:6 statement:stmt];
  8301. // count
  8302. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8303. // title
  8304. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8305. // hide
  8306. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8307. // choose
  8308. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8309. [choose_dic setValue:@"choose" forKey:@"aname"];
  8310. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8311. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8312. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8313. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8314. @"billing_contact" : @"receive_contact",
  8315. @"billing_email" : @"receive_email",
  8316. @"billing_ext" : @"receive_ext",
  8317. @"billing_fax" : @"receive_fax",
  8318. @"billing_name" : @"receive_name",
  8319. @"billing_phone" : @"receive_phone"},
  8320. @"type" : @"pull"};
  8321. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8322. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8323. @"type" : @"pull",
  8324. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8325. @"billing_contact" : @"customer_contact",
  8326. @"billing_email" : @"customer_email",
  8327. @"billing_ext" : @"customer_contact_ext",
  8328. @"billing_fax" : @"customer_fax",
  8329. @"billing_name" : @"customer_name",
  8330. @"billing_phone" : @"customer_phone"}
  8331. };
  8332. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8333. NSDictionary *select_bill_to_dic = @{
  8334. @"aname" : @"Select bill to",
  8335. @"name" : @"select_cid",
  8336. @"refresh" : [NSNumber numberWithInteger:0],
  8337. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8338. @"key_map" : @{
  8339. @"billing_cid" : @"customer_cid",
  8340. @"billing_contact" : @"customer_contact",
  8341. @"billing_email" : @"customer_email",
  8342. @"billing_ext" : @"customer_contact_ext",
  8343. @"billing_fax" : @"customer_fax",
  8344. @"billing_name" : @"customer_name",
  8345. @"billing_phone" : @"customer_phone"
  8346. }
  8347. };
  8348. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8349. [dic setValue:choose_dic forKey:@"item_0"];
  8350. // contact id
  8351. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8352. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8353. [contact_id_dic setValue:@"true" forKey:@"required"];
  8354. [dic setValue:contact_id_dic forKey:@"item_1"];
  8355. // company name
  8356. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8357. [dic setValue:company_name_dic forKey:@"item_2"];
  8358. // address
  8359. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8360. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8361. if ([ext_required integerValue]) {
  8362. [ext_dic setValue:@"true" forKey:@"required"];
  8363. }
  8364. [dic setValue:ext_dic forKey:@"item_3"];
  8365. // contact
  8366. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8367. [dic setValue:contact_dic forKey:@"item_4"];
  8368. // phone
  8369. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8370. [dic setValue:phone_dic forKey:@"item_5"];
  8371. // fax
  8372. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8373. [dic setValue:fax_dic forKey:@"item_6"];
  8374. // email
  8375. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8376. [dic setValue:email_dic forKey:@"item_7"];
  8377. }];
  8378. return dic;
  8379. }
  8380. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8381. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8382. // setting
  8383. NSDictionary *setting = params[@"setting"];
  8384. NSNumber *hide = setting[@"ReturnToHide"];
  8385. [dic setValue:hide forKey:@"hide"];
  8386. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8387. orderCode = [self translateSingleQuote:orderCode];
  8388. 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];
  8389. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8390. NSString *cid = [self textAtColumn:0 statement:stmt];
  8391. NSString *name = [self textAtColumn:1 statement:stmt];
  8392. NSString *ext = [self textAtColumn:2 statement:stmt];
  8393. NSString *contact = [self textAtColumn:3 statement:stmt];
  8394. NSString *email = [self textAtColumn:4 statement:stmt];
  8395. NSString *fax = [self textAtColumn:5 statement:stmt];
  8396. NSString *phone = [self textAtColumn:6 statement:stmt];
  8397. // count
  8398. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8399. // title
  8400. [dic setValue:@"Return To" forKey:@"title"];
  8401. // hide
  8402. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8403. // choose
  8404. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8405. [choose_dic setValue:@"choose" forKey:@"aname"];
  8406. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8407. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8408. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8409. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8410. @"returnto_contact" : @"sender_contact",
  8411. @"returnto_email" : @"sender_email",
  8412. @"returnto_ext" : @"sender_ext",
  8413. @"returnto_fax" : @"sender_fax",
  8414. @"returnto_name" : @"sender_name",
  8415. @"returnto_phone" : @"sender_phone"},
  8416. @"type" : @"pull"};
  8417. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8418. NSDictionary *select_return_to_dic = @{
  8419. @"aname" : @"Select return to",
  8420. @"name" : @"select_cid",
  8421. @"refresh" : [NSNumber numberWithInteger:0],
  8422. @"value" : @"Contact_Return_To",
  8423. @"key_map" : @{
  8424. @"returnto_cid" : @"customer_cid",
  8425. @"returnto_contact" : @"customer_contact",
  8426. @"returnto_email" : @"customer_email",
  8427. @"returnto_ext" : @"customer_contact_ext",
  8428. @"returnto_fax" : @"customer_fax",
  8429. @"returnto_name" : @"customer_name",
  8430. @"returnto_phone" : @"customer_phone"
  8431. }
  8432. };
  8433. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8434. [dic setValue:choose_dic forKey:@"item_0"];
  8435. // contact id
  8436. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8437. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8438. [contact_id_dic setValue:@"true" forKey:@"required"];
  8439. [dic setValue:contact_id_dic forKey:@"item_1"];
  8440. // company name
  8441. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8442. [dic setValue:company_name_dic forKey:@"item_2"];
  8443. // address
  8444. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8445. [dic setValue:ext_dic forKey:@"item_3"];
  8446. // contact
  8447. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8448. [dic setValue:contact_dic forKey:@"item_4"];
  8449. // phone
  8450. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8451. [dic setValue:phone_dic forKey:@"item_5"];
  8452. // fax
  8453. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8454. [dic setValue:fax_dic forKey:@"item_6"];
  8455. // email
  8456. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8457. [dic setValue:email_dic forKey:@"item_7"];
  8458. }];
  8459. return dic;
  8460. }
  8461. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8462. {
  8463. assert(params[@"user"]!=nil);
  8464. assert(params[@"contact_id"]!=nil);
  8465. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8466. orderCode = [self translateSingleQuote:orderCode];
  8467. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8468. __block double TotalCuft = 0;
  8469. __block double TotalWeight = 0;
  8470. __block int TotalCarton = 0;
  8471. __block double payments = 0;
  8472. // setting
  8473. NSDictionary *setting = params[@"setting"];
  8474. NSNumber *hide = setting[@"ModelInformationHide"];
  8475. [dic setValue:hide forKey:@"hide"];
  8476. 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];
  8477. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8478. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8479. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8480. // item id
  8481. int item_id = sqlite3_column_int(stmt, 0);
  8482. // count
  8483. int item_count = sqlite3_column_int(stmt, 1);
  8484. // stockUom
  8485. int stockUom = sqlite3_column_int(stmt, 2);
  8486. // unit price
  8487. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8488. NSString* Price=nil;
  8489. if([str_price isEqualToString:@""])
  8490. {
  8491. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8492. if(price==nil)
  8493. Price=@"No Price.";
  8494. else
  8495. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8496. }
  8497. else
  8498. {
  8499. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8500. }
  8501. // discount
  8502. double discount = sqlite3_column_double(stmt, 4);
  8503. // name
  8504. NSString *name = [self textAtColumn:5 statement:stmt];
  8505. // description
  8506. NSString *description = [self textAtColumn:6 statement:stmt];
  8507. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8508. // line note
  8509. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8510. int avaulability = sqlite3_column_int(stmt, 8);
  8511. // img
  8512. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8513. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8514. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8515. if(combine != nil)
  8516. {
  8517. // int citem=0;
  8518. int bcount=[[combine valueForKey:@"count"] intValue];
  8519. for(int bc=0;bc<bcount;bc++)
  8520. {
  8521. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8522. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8523. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8524. subTotal += uprice * modulus * item_count;
  8525. }
  8526. }
  8527. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8528. [model_dic setValue:@"model" forKey:@"control"];
  8529. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8530. [model_dic setValue:description forKey:@"description"];
  8531. [model_dic setValue:line_note forKey:@"note"];
  8532. [model_dic setValue:img forKey:@"img_url"];
  8533. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8534. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8535. [model_dic setValue:Price forKey:@"unit_price"];
  8536. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8537. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8538. if (combine) {
  8539. [model_dic setValue:combine forKey:@"combine"];
  8540. }
  8541. // well,what under the row is the info for total
  8542. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8543. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8544. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8545. int carton=[bsubtotaljson[@"carton"] intValue];
  8546. TotalCuft += cuft;
  8547. TotalWeight += weight;
  8548. TotalCarton += carton;
  8549. payments += subTotal;
  8550. //---------------------------
  8551. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8552. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8553. }];
  8554. [dic setValue:@"Model Information" forKey:@"title"];
  8555. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8556. return dic;
  8557. }
  8558. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8559. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8560. [dic setValue:@"Remarks Content" forKey:@"title"];
  8561. // setting
  8562. NSDictionary *setting = params[@"setting"];
  8563. NSNumber *hide = setting[@"RemarksContentHide"];
  8564. [dic setValue:hide forKey:@"hide"];
  8565. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8566. orderCode = [self translateSingleQuote:orderCode];
  8567. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8568. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8569. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8570. int mustCall = sqlite3_column_int(stmt, 1);
  8571. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8572. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8573. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8574. NSDictionary *po_dic = @{
  8575. @"aname" : @"PO#",
  8576. @"control" : @"edit",
  8577. @"keyboard" : @"text",
  8578. @"name" : @"poNumber",
  8579. @"value" : poNumber
  8580. };
  8581. #ifdef BUILD_CONTRAST
  8582. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8583. NSDictionary *schedule_dict = @{
  8584. @"aname" : @"Schdule Date",
  8585. @"control" : @"datepicker",
  8586. @"type": @"date",
  8587. @"required": @"true",
  8588. @"name" : @"schedule_date_str",
  8589. @"value" : nsdate
  8590. };
  8591. #endif
  8592. NSDictionary *must_call_dic = @{
  8593. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8594. @"control" : @"switch",
  8595. @"name" : @"must_call",
  8596. @"value" : mustCall ? @"true" : @"false"
  8597. };
  8598. NSDictionary *general_notes_dic = @{
  8599. @"aname" : @"General notes",
  8600. @"control" : @"text_view",
  8601. @"keyboard" : @"text",
  8602. @"name" : @"comments",
  8603. @"value" : generalNotes
  8604. };
  8605. // NSDictionary *internal_notes_dic = @{
  8606. // @"aname" : @"Internal notes",
  8607. // @"control" : @"text_view",
  8608. // @"keyboard" : @"text",
  8609. // @"name" : @"internal_notes",
  8610. // @"value" : internalNotes
  8611. // };
  8612. #ifdef BUILD_CONTRAST
  8613. [dic setValue:po_dic forKey:@"item_0"];
  8614. [dic setValue:schedule_dict forKey:@"item_1"];
  8615. [dic setValue:must_call_dic forKey:@"item_2"];
  8616. [dic setValue:general_notes_dic forKey:@"item_3"];
  8617. #else
  8618. [dic setValue:po_dic forKey:@"item_0"];
  8619. [dic setValue:must_call_dic forKey:@"item_1"];
  8620. [dic setValue:general_notes_dic forKey:@"item_2"];
  8621. #endif
  8622. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8623. }];
  8624. #ifdef BUILD_CONTRAST
  8625. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8626. #else
  8627. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8628. #endif
  8629. return dic;
  8630. }
  8631. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8632. // params
  8633. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8634. orderCode = [self translateSingleQuote:orderCode];
  8635. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8636. // setting
  8637. NSDictionary *setting = params[@"setting"];
  8638. NSNumber *hide = setting[@"OrderTotalHide"];
  8639. [dic setValue:hide forKey:@"hide"];
  8640. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8641. __block double lift_gate_value = 0;
  8642. __block double handling_fee = 0;
  8643. __block double shipping = 0;
  8644. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8645. int lift_gate = sqlite3_column_int(stmt, 0);
  8646. lift_gate_value = sqlite3_column_double(stmt, 1);
  8647. shipping = sqlite3_column_double(stmt, 2);
  8648. handling_fee = sqlite3_column_double(stmt, 3);
  8649. if (!lift_gate) {
  8650. lift_gate_value = 0;
  8651. }
  8652. }];
  8653. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8654. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8655. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8656. double payments = [[total valueForKey:@"payments"] doubleValue];
  8657. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8658. double totalPrice = payments;
  8659. [dic setValue:@"Order Total" forKey:@"title"];
  8660. NSDictionary *payments_dic = @{
  8661. @"align" : @"right",
  8662. @"aname" : @"Payments/Credits",
  8663. @"control" : @"text",
  8664. @"name" : @"paymentsAndCredits",
  8665. @"type" : @"price",
  8666. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8667. };
  8668. [dic setValue:payments_dic forKey:@"item_0"];
  8669. // version 1.71 remove
  8670. // NSDictionary *handling_fee_dic = @{
  8671. // @"align" : @"right",
  8672. // @"aname" : @"Handling Fee",
  8673. // @"control" : @"text",
  8674. // @"name" : @"handling_fee_value",
  8675. // @"required" : @"true",
  8676. // @"type" : @"price",
  8677. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8678. // };
  8679. NSDictionary *shipping_dic = @{
  8680. @"align" : @"right",
  8681. @"aname" : @"Shipping*",
  8682. @"control" : @"text",
  8683. @"name" : @"shipping",
  8684. @"required" : @"true",
  8685. @"type" : @"price",
  8686. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8687. };
  8688. NSDictionary *lift_gate_dic = @{
  8689. @"align" : @"right",
  8690. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8691. @"control" : @"text",
  8692. @"name" : @"lift_gate_value",
  8693. @"required" : @"true",
  8694. @"type" : @"price",
  8695. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8696. };
  8697. int item_count = 1;
  8698. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8699. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8700. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8701. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8702. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8703. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8704. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8705. } else {
  8706. }
  8707. }
  8708. // version 1.71 remove
  8709. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8710. NSDictionary *total_price_dic = @{
  8711. @"align" : @"right",
  8712. @"aname" : @"Total",
  8713. @"control" : @"text",
  8714. @"name" : @"totalPrice",
  8715. @"type" : @"price",
  8716. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8717. };
  8718. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8719. NSDictionary *total_cuft_dic = @{
  8720. @"align" : @"right",
  8721. @"aname" : @"Total Cuft",
  8722. @"control" : @"text",
  8723. @"name" : @"",
  8724. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8725. };
  8726. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8727. NSDictionary *total_weight_dic = @{
  8728. @"align" : @"right",
  8729. @"aname" : @"Total Weight",
  8730. @"control" : @"text",
  8731. @"name" : @"",
  8732. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8733. };
  8734. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8735. NSDictionary *total_carton_dic = @{
  8736. @"align" : @"right",
  8737. @"aname" : @"Total Carton",
  8738. @"control" : @"text",
  8739. @"name" : @"",
  8740. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8741. };
  8742. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8743. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8744. return dic;
  8745. }
  8746. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8747. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8748. orderCode = [self translateSingleQuote:orderCode];
  8749. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8750. // setting
  8751. NSDictionary *setting = params[@"setting"];
  8752. NSNumber *hide = setting[@"SignatureHide"];
  8753. [dic setValue:hide forKey:@"hide"];
  8754. [dic setValue:@"Signature" forKey:@"title"];
  8755. __block NSString *pic_path = @"";
  8756. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8757. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8758. pic_path = [self textAtColumn:0 statement:stmt];
  8759. }];
  8760. NSDictionary *pic_dic = @{
  8761. @"aname" : @"Signature",
  8762. @"avalue" :pic_path,
  8763. @"control" : @"signature",
  8764. @"name" : @"sign_picpath",
  8765. @"value" : pic_path
  8766. };
  8767. [dic setValue:pic_dic forKey:@"item_0"];
  8768. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8769. return dic;
  8770. }
  8771. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8772. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8773. // setting
  8774. NSDictionary *setting = params[@"setting"];
  8775. NSNumber *hide = setting[@"ShippingMethodHide"];
  8776. [dic setValue:hide forKey:@"hide"];
  8777. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8778. orderCode = [self translateSingleQuote:orderCode];
  8779. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8780. __block NSString *logist = @"";
  8781. __block NSString *lift_gate = @"";
  8782. __block int lift_gate_integer = 0;
  8783. __block NSString *logistic_note = @"";
  8784. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8785. logist = [self textAtColumn:0 statement:stmt];
  8786. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8787. logistic_note = [self textAtColumn:2 statement:stmt];
  8788. }];
  8789. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8790. [dic setValue:@"Shipping Method" forKey:@"title"];
  8791. // val_0
  8792. int PERSONAL_PICK_UP_check = 0;
  8793. int USE_MY_CARRIER_check = 0;
  8794. NSString *logistic_note_text = @"";
  8795. int will_call_check = 0;
  8796. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8797. will_call_check = 1;
  8798. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8799. PERSONAL_PICK_UP_check = 1;
  8800. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8801. USE_MY_CARRIER_check = 1;
  8802. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8803. logistic_note = [logistic_note_array firstObject];
  8804. if (logistic_note_array.count > 1) {
  8805. logistic_note_text = [logistic_note_array lastObject];
  8806. }
  8807. }
  8808. }
  8809. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8810. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8811. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8812. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8813. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8814. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8815. NSDictionary *val0_subItem_item0 = @{
  8816. @"aname" : @"Option",
  8817. @"cadedate" : @{
  8818. @"count" : [NSNumber numberWithInteger:2],
  8819. @"val_0" : @{
  8820. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8821. @"refresh" : [NSNumber numberWithInteger:0],
  8822. @"value" : @"PERSONAL PICK UP",
  8823. @"value_id" : @"PERSONAL PICK UP"
  8824. },
  8825. @"val_1" : @{
  8826. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8827. @"refresh" : [NSNumber numberWithInteger:0],
  8828. @"sub_item" : @{
  8829. @"count" : [NSNumber numberWithInteger:1],
  8830. @"item_0" : @{
  8831. @"aname" : @"BOL",
  8832. @"control" : @"edit",
  8833. @"keyboard" : @"text",
  8834. @"name" : @"logist_note_text",
  8835. @"refresh" : [NSNumber numberWithInteger:0],
  8836. @"required" : @"false",
  8837. @"value" : logistic_note_text
  8838. }
  8839. },
  8840. @"value" : @"USE MY CARRIER",
  8841. @"value_id" : @"USE MY CARRIER"
  8842. }
  8843. },
  8844. @"control" : @"enum",
  8845. @"name" : @"logistic_note",
  8846. @"required" : @"true",
  8847. @"single_select" : @"true"
  8848. };
  8849. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8850. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8851. // val_1
  8852. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8853. if([lift_gate isEqualToString:@""]) {
  8854. if (lift_gate_integer == 1) {
  8855. lift_gate = @"true";
  8856. } else {
  8857. lift_gate = @"false";
  8858. }
  8859. }
  8860. int common_carrier_check = 0;
  8861. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8862. common_carrier_check = 1;
  8863. [params setValue:lift_gate forKey:@"lift_gate"];
  8864. }
  8865. NSDictionary *val_1 = @{
  8866. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8867. @"sub_item" : @{
  8868. @"count" : [NSNumber numberWithInteger:1],
  8869. @"item_0" : @{
  8870. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8871. @"control" : @"switch",
  8872. @"name" : @"lift_gate",
  8873. @"refresh" : [NSNumber numberWithInteger:1],
  8874. @"required" : @"true",
  8875. @"value" : lift_gate
  8876. }
  8877. },
  8878. @"value" : @"COMMON CARRIER",
  8879. @"value_id" : @"COMMON CARRIER"
  8880. };
  8881. // cadedate
  8882. NSDictionary *cadedate = @{
  8883. @"count" : [NSNumber numberWithInteger:2],
  8884. @"val_0" : val_0,
  8885. @"val_1" : val_1
  8886. };
  8887. // item_0
  8888. NSMutableDictionary *item_0 = @{
  8889. @"aname" : @"Shipping",
  8890. @"cadedate" : cadedate,
  8891. @"control" : @"enum",
  8892. @"name" : @"logist",
  8893. @"refresh" : [NSNumber numberWithInteger:1],
  8894. @"single_select" : @"true",
  8895. }.mutableCopy;
  8896. NSNumber *required = setting[@"ShippingMethodRequire"];
  8897. if ([required integerValue]) {
  8898. [item_0 setValue:@"true" forKey:@"required"];
  8899. }
  8900. [dic setValue:item_0 forKey:@"item_0"];
  8901. if ([logist isEqualToString:@"WILL CALL"]) {
  8902. [dic removeObjectForKey:@"lift_gate"];
  8903. }
  8904. return dic;
  8905. }
  8906. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8907. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8908. orderCode = [self translateSingleQuote:orderCode];
  8909. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8910. __block int submit_as_integer = 0;
  8911. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8912. submit_as_integer = sqlite3_column_int(stmt, 0);
  8913. }];
  8914. int check11 = 0;
  8915. int check10 = 0;
  8916. if (submit_as_integer == 11) {
  8917. check11 = 1;
  8918. }
  8919. if (submit_as_integer == 10) {
  8920. check10 = 1;
  8921. }
  8922. // section_0
  8923. NSMutableDictionary *dic = @{
  8924. @"count" : @(1),
  8925. @"item_0" : @{
  8926. @"aname" : @"Submit Order As",
  8927. @"cadedate" : @{
  8928. @"count" : @(2),
  8929. @"val_0" : @{
  8930. @"check" : [NSNumber numberWithInteger:check11],
  8931. @"value" : @"Sales Order",
  8932. @"value_id" : @(11)
  8933. },
  8934. @"val_1" : @{
  8935. @"check" : [NSNumber numberWithInteger:check10],
  8936. @"value" : @"Quote",
  8937. @"value_id" : @(10)
  8938. }
  8939. },
  8940. @"control" : @"enum",
  8941. @"name" : @"erpOrderStatus",
  8942. @"required" : @"true",
  8943. @"single_select" : @"true"
  8944. },
  8945. @"title" : @"Order Type"
  8946. }.mutableCopy;
  8947. // setting
  8948. NSDictionary *setting = params[@"setting"];
  8949. NSNumber *hide = setting[@"OrderTypeHide"];
  8950. [dic setValue:hide forKey:@"hide"];
  8951. return dic;
  8952. }
  8953. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8954. countryCode = [self translateSingleQuote:countryCode];
  8955. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8956. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8957. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8958. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8959. if (name == NULL) {
  8960. name = "";
  8961. }
  8962. if (code == NULL) {
  8963. code = "";
  8964. }
  8965. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8966. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8967. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8968. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8969. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8970. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8971. }
  8972. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8973. [container setValue:stateDic forKey:key];
  8974. }] mutableCopy];
  8975. [ret removeObjectForKey:@"result"];
  8976. // failure 可以不用了,一样的
  8977. if (ret.allKeys.count == 0) {
  8978. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8979. [stateDic setValue:@"Other" forKey:@"value"];
  8980. [stateDic setValue:@"" forKey:@"value_id"];
  8981. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8982. if (state_code && [@"" isEqualToString:state_code]) {
  8983. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8984. }
  8985. NSString *key = [NSString stringWithFormat:@"val_0"];
  8986. [ret setValue:stateDic forKey:key];
  8987. }
  8988. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8989. return ret;
  8990. }
  8991. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8992. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8993. orderCode = [self translateSingleQuote:orderCode];
  8994. 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];
  8995. __block NSString *payType = @"";
  8996. __block NSString *firstName = @"";
  8997. __block NSString *lastName = @"";
  8998. __block NSString *addr1 = @"";
  8999. __block NSString *addr2 = @"";
  9000. __block NSString *zipcode = @"";
  9001. __block NSString *cardType = @"";
  9002. __block NSString *cardNumber = @"";
  9003. __block NSString *securityCode = @"";
  9004. __block NSString *month = @"";
  9005. __block NSString *year = @"";
  9006. __block NSString *city = @"";
  9007. __block NSString *state = @"";
  9008. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9009. payType = [self textAtColumn:0 statement:stmt];
  9010. firstName = [self textAtColumn:1 statement:stmt];
  9011. lastName = [self textAtColumn:2 statement:stmt];
  9012. addr1 = [self textAtColumn:3 statement:stmt];
  9013. addr2 = [self textAtColumn:4 statement:stmt];
  9014. zipcode = [self textAtColumn:5 statement:stmt];
  9015. cardType = [self textAtColumn:6 statement:stmt];
  9016. cardNumber = [self textAtColumn:7 statement:stmt];
  9017. securityCode = [self textAtColumn:8 statement:stmt];
  9018. month = [self textAtColumn:9 statement:stmt];
  9019. year = [self textAtColumn:10 statement:stmt];
  9020. city = [self textAtColumn:11 statement:stmt];
  9021. state = [self textAtColumn:12 statement:stmt];
  9022. }];
  9023. NSString *required = @"true";
  9024. // setting
  9025. NSDictionary *setting = params[@"setting"];
  9026. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9027. if ([requiredNumber integerValue]) {
  9028. required = @"true";
  9029. } else {
  9030. required = @"false";
  9031. }
  9032. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9033. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9034. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9035. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9036. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9037. NSString *type = [self textAtColumn:1 statement:stmt];
  9038. NSMutableDictionary *val = @{
  9039. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9040. @"value" : type,
  9041. @"value_id" : type_id
  9042. }.mutableCopy;
  9043. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9044. NSDictionary *sub_item =nil;
  9045. #ifdef BUILD_CONTRAST
  9046. sub_item = @{
  9047. @"count" : @(3),
  9048. @"item_0" : @{
  9049. @"aname" : @"Number",
  9050. @"control" : @"edit",
  9051. @"keyboard" : @"int",
  9052. @"length" : @"16",
  9053. @"name" : @"credit_card_number",
  9054. @"required" : @"true",
  9055. @"value" : cardNumber
  9056. },
  9057. @"item_1" : @{
  9058. @"aname" : @"Expiration Date",
  9059. @"control" : @"monthpicker",
  9060. @"name" : @"credit_card_expiration",
  9061. @"required" : @"true",
  9062. @"type" : @"date",
  9063. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9064. },
  9065. @"item_2" : @{
  9066. @"aname" : @"Security Code",
  9067. @"control" : @"edit",
  9068. @"keyboard" : @"int",
  9069. @"length" : @"4",
  9070. @"name" : @"credit_card_security_code",
  9071. @"required" : @"true",
  9072. @"value" : securityCode
  9073. },
  9074. };
  9075. #else
  9076. sub_item= @{
  9077. @"count" : @(3),
  9078. @"item_0" : @{
  9079. @"aname" : @"choose",
  9080. @"control" : @"multi_action",
  9081. @"count" : @(1),
  9082. @"item_0" : @{
  9083. @"aname" : @"Same as customer",
  9084. @"key_map" : @{
  9085. @"credit_card_address1" : @"customer_address1",
  9086. @"credit_card_address2" : @"customer_address2",
  9087. @"credit_card_city" : @"customer_city",
  9088. @"credit_card_first_name" : @"customer_first_name",
  9089. @"credit_card_last_name" : @"customer_last_name",
  9090. @"credit_card_state" : @"customer_state",
  9091. @"credit_card_zipcode" : @"customer_zipcode"
  9092. },
  9093. @"type" : @"pull"
  9094. }
  9095. },
  9096. @"item_1" : @{
  9097. @"aname" : @"",
  9098. @"color" : @"red",
  9099. @"control" : @"text",
  9100. @"name" : @"",
  9101. @"value" : @"USA Credit cards only"
  9102. },
  9103. @"item_2" : @{
  9104. @"aname" : @"Fill",
  9105. @"cadedate" : @{
  9106. @"count" : @(2),
  9107. @"val_0" : @{
  9108. @"check" : @(1),
  9109. @"sub_item" : @{
  9110. @"count" : @(11),
  9111. @"item_0" : @{
  9112. @"aname" : @"Type",
  9113. @"cadedate" : @{
  9114. @"count" : @(2),
  9115. @"val_0" : @{
  9116. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9117. @"value" : @"VISA",
  9118. @"value_id" : @"VISA"/*@(0)*/
  9119. },
  9120. @"val_1" : @{
  9121. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9122. @"value" : @"MASTER CARD",
  9123. @"value_id" : @"MASTER CARD"/*@(1)*/
  9124. }
  9125. },
  9126. @"control" : @"enum",
  9127. @"name" : @"credit_card_type",
  9128. @"required" : @"true",
  9129. @"single_select" : @"true"
  9130. },
  9131. @"item_1" : @{
  9132. @"aname" : @"Number",
  9133. @"control" : @"edit",
  9134. @"keyboard" : @"int",
  9135. @"length" : @"16",
  9136. @"name" : @"credit_card_number",
  9137. @"required" : @"true",
  9138. @"value" : cardNumber
  9139. },
  9140. @"item_10" : @{
  9141. @"aname" : @"State",
  9142. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9143. @"control" : @"enum",
  9144. @"enum" : @"true",
  9145. @"name" : @"credit_card_state",
  9146. @"required" : @"true",
  9147. @"single_select" : @"true"
  9148. },
  9149. @"item_2" : @{
  9150. @"aname" : @"Expiration Date",
  9151. @"control" : @"monthpicker",
  9152. @"name" : @"credit_card_expiration",
  9153. @"required" : @"true",
  9154. @"type" : @"date",
  9155. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9156. },
  9157. @"item_3" : @{
  9158. @"aname" : @"Security Code",
  9159. @"control" : @"edit",
  9160. @"keyboard" : @"int",
  9161. @"length" : @"3",
  9162. @"name" : @"credit_card_security_code",
  9163. @"required" : @"true",
  9164. @"value" : securityCode
  9165. },
  9166. @"item_4" : @{
  9167. @"aname" : @"First Name",
  9168. @"control" : @"edit",
  9169. @"keyboard" : @"text",
  9170. @"name" : @"credit_card_first_name",
  9171. @"required" : @"true",
  9172. @"value" : firstName
  9173. },
  9174. @"item_5" : @{
  9175. @"aname" : @"Last Name",
  9176. @"control" : @"edit",
  9177. @"keyboard" : @"text",
  9178. @"name" : @"credit_card_last_name",
  9179. @"required" : @"true",
  9180. @"value" : lastName
  9181. },
  9182. @"item_6" : @{
  9183. @"aname" : @"Address 1",
  9184. @"control" : @"edit",
  9185. @"keyboard" : @"text",
  9186. @"name" : @"credit_card_address1",
  9187. @"required" : @"true",
  9188. @"value" : addr1
  9189. },
  9190. @"item_7" : @{
  9191. @"aname" : @"Address 2",
  9192. @"control" : @"edit",
  9193. @"keyboard" : @"text",
  9194. @"name" : @"credit_card_address2",
  9195. @"value" : addr2
  9196. },
  9197. @"item_8" : @{
  9198. @"aname" : @"zip code",
  9199. @"control" : @"edit",
  9200. @"keyboard" : @"text",
  9201. @"name" : @"credit_card_zipcode",
  9202. @"required" : @"true",
  9203. @"value" : zipcode
  9204. },
  9205. @"item_9" : @{
  9206. @"aname" : @"City",
  9207. @"control" : @"edit",
  9208. @"keyboard" : @"text",
  9209. @"name" : @"credit_card_city",
  9210. @"required" : @"true",
  9211. @"value" : city
  9212. }
  9213. },
  9214. @"value" : @"Fill Now",
  9215. @"value_id" : @""
  9216. },
  9217. @"val_1" : @{
  9218. @"check" : @(0),
  9219. @"value" : @"Fill Later",
  9220. @"value_id" : @""
  9221. }
  9222. },
  9223. @"control" : @"enum",
  9224. @"name" : @"",
  9225. @"single_select" : @"true"
  9226. }
  9227. };
  9228. #endif;
  9229. [val setObject:sub_item forKey:@"sub_item"];
  9230. }
  9231. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9232. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9233. }];
  9234. // "section_2"
  9235. NSMutableDictionary *dic = @{
  9236. @"count" : @(1),
  9237. @"item_0" : @{
  9238. @"aname" : @"Payment",
  9239. @"required" : required,
  9240. @"cadedate" : cadedate,
  9241. // @{
  9242. // @"count" : @(6),
  9243. // @"val_3" : @{
  9244. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9245. // @"value" : @"Check",
  9246. // @"value_id" : @"Check"
  9247. // },
  9248. // @"val_2" : @{
  9249. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9250. // @"value" : @"Cash",
  9251. // @"value_id" : @"Cash"
  9252. // },
  9253. // @"val_1" : @{
  9254. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9255. // @"value" : @"NET 60",
  9256. // @"value_id" : @"NET 30"
  9257. // },
  9258. // @"val_4" : @{
  9259. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9260. // @"value" : @"Wire Transfer",
  9261. // @"value_id" : @"Wire Transfer"
  9262. // },
  9263. // @"val_0" : @{
  9264. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9265. // @"sub_item" : @{
  9266. // @"count" : @(3),
  9267. // @"item_0" : @{
  9268. // @"aname" : @"choose",
  9269. // @"control" : @"multi_action",
  9270. // @"count" : @(1),
  9271. // @"item_0" : @{
  9272. // @"aname" : @"Same as customer",
  9273. // @"key_map" : @{
  9274. // @"credit_card_address1" : @"customer_address1",
  9275. // @"credit_card_address2" : @"customer_address2",
  9276. // @"credit_card_city" : @"customer_city",
  9277. // @"credit_card_first_name" : @"customer_first_name",
  9278. // @"credit_card_last_name" : @"customer_last_name",
  9279. // @"credit_card_state" : @"customer_state",
  9280. // @"credit_card_zipcode" : @"customer_zipcode"
  9281. // },
  9282. // @"type" : @"pull"
  9283. // }
  9284. // },
  9285. // @"item_1" : @{
  9286. // @"aname" : @"",
  9287. // @"color" : @"red",
  9288. // @"control" : @"text",
  9289. // @"name" : @"",
  9290. // @"value" : @"USA Credit cards only"
  9291. // },
  9292. // @"item_2" : @{
  9293. // @"aname" : @"Fill",
  9294. // @"cadedate" : @{
  9295. // @"count" : @(2),
  9296. // @"val_0" : @{
  9297. // @"check" : @(1),
  9298. // @"sub_item" : @{
  9299. // @"count" : @(11),
  9300. // @"item_0" : @{
  9301. // @"aname" : @"Type",
  9302. // @"cadedate" : @{
  9303. // @"count" : @(2),
  9304. // @"val_0" : @{
  9305. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9306. // @"value" : @"VISA",
  9307. // @"value_id" : @(0)
  9308. // },
  9309. // @"val_1" : @{
  9310. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9311. // @"value" : @"MASTER CARD",
  9312. // @"value_id" : @(1)
  9313. // }
  9314. // },
  9315. // @"control" : @"enum",
  9316. // @"name" : @"credit_card_type",
  9317. // @"required" : @"true",
  9318. // @"single_select" : @"true"
  9319. // },
  9320. // @"item_1" : @{
  9321. // @"aname" : @"Number",
  9322. // @"control" : @"edit",
  9323. // @"keyboard" : @"int",
  9324. // @"length" : @"16",
  9325. // @"name" : @"credit_card_number",
  9326. // @"required" : @"true",
  9327. // @"value" : cardNumber
  9328. // },
  9329. // @"item_10" : @{
  9330. // @"aname" : @"State",
  9331. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9332. // @"control" : @"enum",
  9333. // @"enum" : @"true",
  9334. // @"name" : @"credit_card_state",
  9335. // @"required" : @"true",
  9336. // @"single_select" : @"true"
  9337. // },
  9338. // @"item_2" : @{
  9339. // @"aname" : @"Expiration Date",
  9340. // @"control" : @"monthpicker",
  9341. // @"name" : @"credit_card_expiration",
  9342. // @"required" : @"true",
  9343. // @"type" : @"date",
  9344. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9345. // },
  9346. // @"item_3" : @{
  9347. // @"aname" : @"Security Code",
  9348. // @"control" : @"edit",
  9349. // @"keyboard" : @"int",
  9350. // @"length" : @"3",
  9351. // @"name" : @"credit_card_security_code",
  9352. // @"required" : @"true",
  9353. // @"value" : securityCode
  9354. // },
  9355. // @"item_4" : @{
  9356. // @"aname" : @"First Name",
  9357. // @"control" : @"edit",
  9358. // @"keyboard" : @"text",
  9359. // @"name" : @"credit_card_first_name",
  9360. // @"required" : @"true",
  9361. // @"value" : firstName
  9362. // },
  9363. // @"item_5" : @{
  9364. // @"aname" : @"Last Name",
  9365. // @"control" : @"edit",
  9366. // @"keyboard" : @"text",
  9367. // @"name" : @"credit_card_last_name",
  9368. // @"required" : @"true",
  9369. // @"value" : lastName
  9370. // },
  9371. // @"item_6" : @{
  9372. // @"aname" : @"Address 1",
  9373. // @"control" : @"edit",
  9374. // @"keyboard" : @"text",
  9375. // @"name" : @"credit_card_address1",
  9376. // @"required" : @"true",
  9377. // @"value" : addr1
  9378. // },
  9379. // @"item_7" : @{
  9380. // @"aname" : @"Address 2",
  9381. // @"control" : @"edit",
  9382. // @"keyboard" : @"text",
  9383. // @"name" : @"credit_card_address2",
  9384. // @"value" : addr2
  9385. // },
  9386. // @"item_8" : @{
  9387. // @"aname" : @"zip code",
  9388. // @"control" : @"edit",
  9389. // @"keyboard" : @"text",
  9390. // @"name" : @"credit_card_zipcode",
  9391. // @"required" : @"true",
  9392. // @"value" : zipcode
  9393. // },
  9394. // @"item_9" : @{
  9395. // @"aname" : @"City",
  9396. // @"control" : @"edit",
  9397. // @"keyboard" : @"text",
  9398. // @"name" : @"credit_card_city",
  9399. // @"required" : @"true",
  9400. // @"value" : city
  9401. // }
  9402. // },
  9403. // @"value" : @"Fill Now",
  9404. // @"value_id" : @""
  9405. // },
  9406. // @"val_1" : @{
  9407. // @"check" : @(0),
  9408. // @"value" : @"Fill Later",
  9409. // @"value_id" : @""
  9410. // }
  9411. // },
  9412. // @"control" : @"enum",
  9413. // @"name" : @"",
  9414. // @"single_select" : @"true"
  9415. // }
  9416. // },
  9417. // @"value" : @"Visa/Master",
  9418. // @"value_id" : @"Credit Card"
  9419. // },
  9420. // @"val_5" : @{
  9421. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9422. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9423. // @"value_id" : @"FOLLOW EXISTING"
  9424. // }
  9425. // },
  9426. @"control" : @"enum",
  9427. @"name" : @"paymentType",
  9428. @"single_select" : @"true"
  9429. },
  9430. @"title" : @"Payment Information"
  9431. }.mutableCopy;
  9432. NSNumber *hide = setting[@"PaymentInformationHide"];
  9433. [dic setValue:hide forKey:@"hide"];
  9434. return dic;
  9435. }
  9436. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9437. // params
  9438. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9439. orderCode = [self translateSingleQuote:orderCode];
  9440. // 缺货检查
  9441. 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];
  9442. __block BOOL outOfStock = NO;
  9443. sqlite3 *database = db;
  9444. if (!db) {
  9445. database = [iSalesDB get_db];
  9446. }
  9447. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9448. int availability = sqlite3_column_int(stmt, 0);
  9449. int item_qty = sqlite3_column_int(stmt, 1);
  9450. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9451. }];
  9452. if (!db) {
  9453. [iSalesDB close_db:database];
  9454. }
  9455. return outOfStock;
  9456. }
  9457. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9458. {
  9459. assert(params[@"user"]!=nil);
  9460. assert(params[@"contact_id"]!=nil);
  9461. assert(params[@"can_create_backorder"]!=nil);
  9462. sqlite3 *db = [iSalesDB get_db];
  9463. // params
  9464. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9465. orderCode = [self translateSingleQuote:orderCode];
  9466. // 缺货检查
  9467. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9468. if (![params[@"can_create_backorder"] boolValue]) {
  9469. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9470. if (out_of_stock) {
  9471. [iSalesDB close_db:db];
  9472. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9473. [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"];
  9474. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9475. return [RAConvertor dict2data:resultDic];
  9476. }
  9477. }
  9478. // UISetting
  9479. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9480. NSString *cachefolder = [paths objectAtIndex:0];
  9481. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9482. NSData* json =nil;
  9483. json=[NSData dataWithContentsOfFile:img_cache];
  9484. NSError *error=nil;
  9485. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9486. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9487. [params setObject:setting forKey:@"setting"];
  9488. int section_count = 0;
  9489. // 0 Order Type 1 Shipping Method 2 Payment Information
  9490. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9491. // 0 Order Type
  9492. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9493. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9494. [ret setValue:order_type_dic forKey:key0];
  9495. // 1 Shipping Method
  9496. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9497. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9498. [ret setValue:shipping_method_dic forKey:key1];
  9499. // 2 Payment Information
  9500. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9501. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9502. [ret setValue:payment_info_dic forKey:key2];
  9503. // 3 Customer
  9504. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9505. NSDictionary *customer_dic = [self customerDic:params db:db];
  9506. [ret setValue:customer_dic forKey:key3];
  9507. // 4 Ship To
  9508. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9509. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9510. [ret setValue:ship_to_dic forKey:key4];
  9511. // 5 Ship From
  9512. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9513. if (![shipFromDisable integerValue]) {
  9514. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9515. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9516. [ret setValue:ship_from_dic forKey:key5];
  9517. }
  9518. // 6 Freight Bill To
  9519. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9520. if (![freightBillToDisable integerValue]) {
  9521. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9522. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9523. [ret setValue:freight_bill_to forKey:key6];
  9524. }
  9525. // 7 Merchandise Bill To
  9526. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9527. if (![merchandiseBillToDisable integerValue]) {
  9528. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9529. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9530. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9531. }
  9532. // 8 Return To
  9533. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9534. if (![returnToDisable integerValue]) {
  9535. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9536. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9537. [ret setValue:return_to_dic forKey:key8];
  9538. }
  9539. // 9 Model Information
  9540. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9541. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9542. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9543. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9544. [ret setValue:model_info_dic forKey:key9];
  9545. // 10 Remarks Content
  9546. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9547. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9548. [ret setValue:remarks_content_dic forKey:key10];
  9549. // 11 Order Total
  9550. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9551. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9552. [ret setValue:order_total_dic forKey:key11];
  9553. // 12 Signature
  9554. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9555. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9556. [ret setValue:sign_dic forKey:key12];
  9557. [ret setValue:@(section_count) forKey:@"section_count"];
  9558. [iSalesDB close_db:db];
  9559. return [RAConvertor dict2data:ret];
  9560. // return nil;
  9561. }
  9562. #pragma mark addr editor
  9563. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9564. NSMutableDictionary *new_item = [item mutableCopy];
  9565. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9566. return new_item;
  9567. }
  9568. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9569. {
  9570. // "is_subaction" = true;
  9571. // orderCode = MOB1608240002;
  9572. // password = 123456;
  9573. // "subaction_tag" = 1;
  9574. // user = EvanK;
  9575. // {
  9576. // "is_subaction" = true;
  9577. // orderCode = MOB1608240002;
  9578. // password = 123456;
  9579. // "refresh_trigger" = zipcode;
  9580. // "subaction_tag" = 1;
  9581. // user = EvanK;
  9582. // }
  9583. NSString *country_code = nil;
  9584. NSString *zipCode = nil;
  9585. NSString *stateCode = nil;
  9586. NSString *city = nil;
  9587. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9588. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9589. NSString *code_id = params[@"country"];
  9590. country_code = [self countryCodeByid:code_id];
  9591. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9592. NSString *zip_code = params[@"zipcode"];
  9593. // 剔除全部为空格
  9594. int spaceCount = 0;
  9595. for (int i = 0; i < zip_code.length; i++) {
  9596. if ([zip_code characterAtIndex:i] == ' ') {
  9597. spaceCount++;
  9598. }
  9599. }
  9600. if (spaceCount == zip_code.length) {
  9601. zip_code = @"";
  9602. }
  9603. zipCode = zip_code;
  9604. if (zipCode.length > 0) {
  9605. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9606. country_code = [dic valueForKey:@"country_code"];
  9607. if (!country_code) {
  9608. // country_code = @"US";
  9609. NSString *code_id = params[@"country"];
  9610. country_code = [self countryCodeByid:code_id];
  9611. }
  9612. stateCode = [dic valueForKey:@"state_code"];
  9613. if(!stateCode.length) {
  9614. stateCode = params[@"state"];
  9615. }
  9616. city = [dic valueForKey:@"city"];
  9617. if (!city.length) {
  9618. city = params[@"city"];
  9619. }
  9620. // zip code
  9621. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9622. // [zipDic setValue:zipCode forKey:@"value"];
  9623. // [section_0 setValue:zipDic forKey:@"item_11"];
  9624. } else {
  9625. NSString *code_id = params[@"country"];
  9626. country_code = [self countryCodeByid:code_id];
  9627. stateCode = params[@"state"];
  9628. city = params[@"city"];
  9629. }
  9630. }
  9631. }
  9632. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9633. // [ret removeObjectForKey:@"up_params"];
  9634. [ret setObject:@"New Address" forKey:@"title"];
  9635. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9636. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9637. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9638. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9639. // [country_dic removeObjectForKey:@"refresh"];
  9640. // [country_dic removeObjectForKey:@"restore"];
  9641. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9642. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9643. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9644. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9645. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9646. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9647. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9648. // [zip_code_dic removeObjectForKey:@"refresh"];
  9649. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9650. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9651. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9652. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9653. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9654. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9655. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9656. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9657. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9658. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9659. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9660. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9661. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9662. // country
  9663. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9664. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9665. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9666. // state
  9667. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9668. // NSDictionary *tmpDic = @{
  9669. // @"value" : @"Other",
  9670. // @"value_id" : @"",
  9671. // @"check" : [NSNumber numberWithInteger:0]
  9672. // };
  9673. //
  9674. // for (int i = 0; i < allState.allKeys.count; i++) {
  9675. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9676. //
  9677. // NSDictionary *tmp = allState[key];
  9678. // [allState setValue:tmpDic forKey:key];
  9679. // tmpDic = tmp;
  9680. // }
  9681. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9682. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9683. [ret setValue:new_section_0 forKey:@"section_0"];
  9684. return [RAConvertor dict2data:ret];
  9685. }
  9686. #pragma mark save addr
  9687. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9688. {
  9689. // NSString *companyName = [self valueInParams:params key:@"company"];
  9690. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9691. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9692. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9693. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9694. // NSString *countryId = [self valueInParams:params key:@"country"];
  9695. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9696. // NSString *city = [self valueInParams:params key:@"city"];
  9697. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9698. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9699. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9700. // NSString *phone = [self valueInParams:params key:@"phone"];
  9701. // NSString *fax = [self valueInParams:params key:@"fax"];
  9702. // NSString *email = [self valueInParams:params key:@"email"];
  9703. return [self offline_saveContact:params update:NO isCustomer:NO];
  9704. }
  9705. #pragma mark cancel order
  9706. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9707. {
  9708. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9709. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9710. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9711. if (order_id.length) {
  9712. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9713. }
  9714. int ret = [iSalesDB execSql:sql];
  9715. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9716. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9717. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9718. // [dic setValue:@"160409" forKey:@"min_ver"];
  9719. return [RAConvertor dict2data:dic];
  9720. }
  9721. #pragma mark sign order
  9722. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9723. {
  9724. //参考 offline_saveBusinesscard
  9725. DebugLog(@"sign order params: %@",params);
  9726. // orderCode = MOB1608240002;
  9727. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9728. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9729. orderCode = [self translateSingleQuote:orderCode];
  9730. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9731. picPath = [self translateSingleQuote:picPath];
  9732. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9733. int ret = [iSalesDB execSql:sql];
  9734. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9735. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9736. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9737. // [dic setValue:@"160409" forKey:@"min_ver"];
  9738. return [RAConvertor dict2data:dic];
  9739. }
  9740. #pragma mark save order
  9741. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9742. NSString *ret = [self valueInParams:params key:key];
  9743. if (translate) {
  9744. ret = [self translateSingleQuote:ret];
  9745. }
  9746. return ret;
  9747. }
  9748. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9749. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9750. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9751. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9752. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9753. if (schedule_date.length==0 ) {
  9754. schedule_date = @"";
  9755. } else {
  9756. schedule_date = [self rchangeDateFormate:schedule_date];
  9757. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9758. }
  9759. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9760. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9761. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9762. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9763. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9764. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9765. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9766. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9767. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9768. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9769. if (![total_price isEqualToString:@""]) {
  9770. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9771. } else {
  9772. total_price = @"";
  9773. }
  9774. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9775. if ([paymentsAndCredits isEqualToString:@""]) {
  9776. paymentsAndCredits = @"";
  9777. } else {
  9778. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9779. }
  9780. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9781. if ([handling_fee_value isEqualToString:@""]) {
  9782. handling_fee_value = @"";
  9783. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9784. handling_fee_value = @"";
  9785. } else {
  9786. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9787. }
  9788. NSString *handling_fee_placeholder = handling_fee_value;
  9789. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9790. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9791. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9792. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9793. NSString *lift_gate_placeholder = @"";
  9794. if ([lift_gate_value isEqualToString:@""]) {
  9795. lift_gate_placeholder = @"";
  9796. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9797. lift_gate_placeholder = @"";
  9798. } else {
  9799. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9800. }
  9801. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9802. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9803. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9804. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9805. 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];
  9806. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9807. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9808. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9809. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9810. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9811. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9812. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9813. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9814. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9815. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9816. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9817. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9818. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9819. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9820. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9821. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9822. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9823. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9824. if (!number_nil) {
  9825. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9826. }
  9827. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9828. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9829. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9830. if (!security_code_nil) {
  9831. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9832. }
  9833. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9834. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9835. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9836. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9837. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9838. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9839. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9840. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9841. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9842. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9843. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9844. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9845. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9846. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9847. NSString *contact_id = customer_cid;
  9848. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9849. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9850. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9851. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9852. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9853. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9854. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9855. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9856. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9857. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9858. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9859. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9860. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9861. //
  9862. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9863. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9864. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9865. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9866. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9867. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9868. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9869. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9870. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9871. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9872. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9873. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9874. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9875. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9876. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9877. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9878. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9879. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9880. 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];
  9881. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9882. if (receive_cid.length) {
  9883. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9884. }
  9885. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9886. if (receive_name.length) {
  9887. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9888. }
  9889. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9890. if (receive_ext.length) {
  9891. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9892. }
  9893. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9894. if (receive_contact.length) {
  9895. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9896. }
  9897. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9898. if (receive_phone.length) {
  9899. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9900. }
  9901. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9902. if (receive_email.length) {
  9903. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9904. }
  9905. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9906. if (receive_fax.length) {
  9907. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9908. }
  9909. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9910. if (sender_cid.length) {
  9911. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9912. }
  9913. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9914. if (sender_name.length) {
  9915. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9916. }
  9917. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9918. if (sender_ext.length) {
  9919. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9920. }
  9921. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9922. if(sender_contact.length) {
  9923. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9924. }
  9925. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9926. if (sender_phone.length) {
  9927. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9928. }
  9929. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9930. if (sender_fax.length) {
  9931. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9932. }
  9933. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9934. if (sender_email.length) {
  9935. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9936. }
  9937. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9938. if (shipping_billto_cid.length) {
  9939. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9940. }
  9941. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9942. if (shipping_billto_name.length) {
  9943. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9944. }
  9945. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9946. if (shipping_billto_ext.length) {
  9947. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9948. }
  9949. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9950. if (shipping_billto_contact.length) {
  9951. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9952. }
  9953. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9954. if (shipping_billto_phone.length) {
  9955. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9956. }
  9957. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9958. if (shipping_billto_fax.length) {
  9959. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9960. }
  9961. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9962. if (shipping_billto_email.length) {
  9963. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9964. }
  9965. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9966. if (billing_cid.length) {
  9967. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9968. }
  9969. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9970. if (billing_name.length) {
  9971. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9972. }
  9973. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9974. if (billing_ext.length) {
  9975. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9976. }
  9977. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9978. if (billing_contact.length) {
  9979. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9980. }
  9981. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9982. if (billing_phone.length) {
  9983. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9984. }
  9985. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9986. if (billing_fax.length) {
  9987. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9988. }
  9989. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9990. if (billing_email.length) {
  9991. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9992. }
  9993. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9994. if (returnto_cid.length) {
  9995. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9996. }
  9997. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9998. if (returnto_name.length) {
  9999. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10000. }
  10001. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10002. if (returnto_ext.length) {
  10003. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10004. }
  10005. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10006. if (returnto_contact.length) {
  10007. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10008. }
  10009. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10010. if (returnto_phone.length) {
  10011. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10012. }
  10013. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10014. if (returnto_fax.length) {
  10015. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10016. }
  10017. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10018. if (returnto_email.length) {
  10019. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10020. }
  10021. NSString *order_status = @"status = 1,";
  10022. if (submit) {
  10023. order_status = @"status = -11,";
  10024. }
  10025. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10026. NSString *sync_sql = @"";
  10027. if (submit) {
  10028. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10029. [param setValue:sales_rep forKey:@"sales_rep"];
  10030. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10031. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10032. }
  10033. 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];
  10034. DebugLog(@"save order contactSql: %@",contactSql);
  10035. DebugLog(@"save order orderSql: %@",orderSql);
  10036. // int contact_ret = [iSalesDB execSql:contactSql];
  10037. int order_ret = [iSalesDB execSql:orderSql];
  10038. int ret = order_ret;
  10039. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10040. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10041. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10042. // [dic setValue:@"160409" forKey:@"min_ver"];
  10043. [dic setObject:so_id forKey:@"so#"];
  10044. return [RAConvertor dict2data:dic];
  10045. }
  10046. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10047. {
  10048. // id foo = nil;
  10049. // NSMutableArray *a = @[].mutableCopy;
  10050. // [a addObject:foo];
  10051. return [self saveorder:param submit:NO];
  10052. }
  10053. #pragma mark add to cart by name
  10054. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10055. {
  10056. NSString *orderCode = [params objectForKey:@"orderCode"];
  10057. NSDictionary *newParams = @{
  10058. @"product_id" : params[@"product_id_string"],
  10059. @"orderCode" : orderCode,
  10060. @"can_create_backorder":params[@"can_create_backorder"]
  10061. };
  10062. return [self offline_add2cart:[newParams mutableCopy]];
  10063. }
  10064. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10065. {
  10066. NSString *orderCode = [params objectForKey:@"orderCode"];
  10067. NSString *upccode = [params objectForKey:@"upc_code"];
  10068. // product_name = [self translateSingleQuote:product_name];
  10069. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10070. // bool search_upc = [params[@"search_upc"] boolValue];
  10071. sqlite3 *db = [iSalesDB get_db];
  10072. __block NSMutableString *product_id_string = [NSMutableString string];
  10073. // NSString *name = [product_name_array objectAtIndex:i];
  10074. NSString *sql =nil;
  10075. 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];
  10076. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10077. int product_id = sqlite3_column_int(stmt, 0);
  10078. [product_id_string appendFormat:@"%d",product_id];
  10079. }];
  10080. if (!orderCode) {
  10081. orderCode = @"";
  10082. }
  10083. NSDictionary *newParams = @{
  10084. @"product_id" : product_id_string,
  10085. @"orderCode" : orderCode,
  10086. @"can_create_backorder":params[@"can_create_backorder"]
  10087. };
  10088. [iSalesDB close_db:db];
  10089. return [self offline_add2cart:[newParams mutableCopy]];
  10090. }
  10091. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10092. {
  10093. NSString *orderCode = [params objectForKey:@"orderCode"];
  10094. NSString *product_name = [params objectForKey:@"product_name"];
  10095. product_name = [self translateSingleQuote:product_name];
  10096. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10097. bool search_upc = [params[@"search_upc"] boolValue];
  10098. sqlite3 *db = [iSalesDB get_db];
  10099. __block NSMutableString *product_id_string = [NSMutableString string];
  10100. for (int i = 0; i < product_name_array.count; i++) {
  10101. NSString *name = [product_name_array objectAtIndex:i];
  10102. NSString *sql =nil;
  10103. if(search_upc)
  10104. 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];
  10105. else
  10106. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10107. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10108. int product_id = sqlite3_column_int(stmt, 0);
  10109. if (i == product_name_array.count - 1) {
  10110. [product_id_string appendFormat:@"%d",product_id];
  10111. } else {
  10112. [product_id_string appendFormat:@"%d,",product_id];
  10113. }
  10114. }];
  10115. }
  10116. if (!orderCode) {
  10117. orderCode = @"";
  10118. }
  10119. NSDictionary *newParams = @{
  10120. @"product_id" : product_id_string,
  10121. @"orderCode" : orderCode,
  10122. @"can_create_backorder":params[@"can_create_backorder"]
  10123. };
  10124. [iSalesDB close_db:db];
  10125. return [self offline_add2cart:[newParams mutableCopy]];
  10126. }
  10127. #pragma mark reset order
  10128. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10129. {
  10130. // UIApplication * app = [UIApplication sharedApplication];
  10131. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10132. [iSalesDB disable_trigger];
  10133. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10134. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10135. [iSalesDB enable_trigger];
  10136. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10137. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10138. return [RAConvertor dict2data:dic];
  10139. }
  10140. #pragma mark submit order
  10141. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10142. {
  10143. return [self saveorder:params submit:YES];
  10144. }
  10145. #pragma mark copy order
  10146. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10147. {
  10148. NSMutableDictionary *ret = @{}.mutableCopy;
  10149. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10150. sqlite3 *db = [iSalesDB get_db];
  10151. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10152. // 首先查看联系人是否active
  10153. 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];
  10154. __block int customer_is_active = 1;
  10155. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10156. customer_is_active = sqlite3_column_int(stmt, 0);
  10157. }];
  10158. if (!customer_is_active) {
  10159. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10160. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10161. [iSalesDB close_db:db];
  10162. return [RAConvertor dict2data:ret];
  10163. }
  10164. // new order
  10165. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10166. NSString *new_order_code = [self get_offline_soid:db];
  10167. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10168. user = [self translateSingleQuote:user];
  10169. 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
  10170. __block int result = 1;
  10171. result = [iSalesDB execSql:insert_order_sql db:db];
  10172. if (!result) {
  10173. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10174. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10175. [iSalesDB close_db:db];
  10176. return [RAConvertor dict2data:ret];
  10177. }
  10178. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10179. // __block NSString *product_id = @"";
  10180. __weak typeof(self) weakSelf = self;
  10181. 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];
  10182. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10183. int is_active = sqlite3_column_int(stmt, 1);
  10184. if (is_active) {
  10185. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10186. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10187. 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];
  10188. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10189. }
  10190. }];
  10191. // product_id = [product_id substringFromIndex:1];
  10192. //
  10193. // [self offline_add2cart:@{}.mutableCopy];
  10194. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10195. [iSalesDB close_db:db];
  10196. if (result) {
  10197. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10198. } else {
  10199. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10200. }
  10201. [ret setObject:new_order_code forKey:@"so_id"];
  10202. return [RAConvertor dict2data:ret];
  10203. }
  10204. #pragma mark move wish list to cart
  10205. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10206. assert(params[@"can_create_backorder"]!=nil);
  10207. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10208. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10209. NSString *collectId = params[@"collectId"];
  10210. 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];
  10211. __block NSString *product_id = @"";
  10212. __block NSString *qty = @"";
  10213. __block int number_of_outOfStock = 0;
  10214. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10215. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10216. int productId = sqlite3_column_int(stmt, 0);
  10217. int item_qty = sqlite3_column_int(stmt, 1);
  10218. int availability = sqlite3_column_int(stmt, 2);
  10219. int _id = sqlite3_column_int(stmt, 3);
  10220. if (![params[@"can_create_backorder"] boolValue]) {
  10221. // 库存小于购买量为缺货
  10222. if (availability >= item_qty) {
  10223. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10224. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10225. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10226. } else {
  10227. number_of_outOfStock++;
  10228. }
  10229. } else {
  10230. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10231. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10232. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10233. }
  10234. }];
  10235. NSMutableDictionary *retDic = nil;
  10236. if (![params[@"can_create_backorder"] boolValue]) {
  10237. if (delete_collectId.count == 0) {
  10238. retDic = [NSMutableDictionary dictionary];
  10239. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10240. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10241. return [RAConvertor dict2data:retDic];
  10242. }
  10243. }
  10244. if (product_id.length > 1) {
  10245. product_id = [product_id substringFromIndex:1];
  10246. }
  10247. if (qty.length > 1) {
  10248. qty = [qty substringFromIndex:1];
  10249. }
  10250. NSString *orderCode = params[@"orderCode"];
  10251. if (!orderCode) {
  10252. orderCode = @"";
  10253. }
  10254. NSDictionary *newParams = @{
  10255. @"product_id" : product_id,
  10256. @"orderCode" : orderCode,
  10257. @"qty" : qty,
  10258. @"can_create_backorder":params[@"can_create_backorder"]
  10259. };
  10260. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10261. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10262. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10263. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10264. NSInteger ret = [wish_return[@"result"] intValue];
  10265. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10266. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10267. }
  10268. if (![params[@"can_create_backorder"] boolValue]) {
  10269. if (number_of_outOfStock > 0) {
  10270. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10271. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10272. }
  10273. }
  10274. return [RAConvertor dict2data:retDic];
  10275. }
  10276. #pragma mark - portfolio
  10277. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10278. // assert(params[@"contact_id"]!=nil);
  10279. assert(params[@"user"]!=nil);
  10280. assert(params[@"can_see_price"]!=nil);
  10281. int sort = [[params valueForKey:@"sort"] intValue];
  10282. int offset = [[params valueForKey:@"offset"] intValue];
  10283. int limit = [[params valueForKey:@"limit"] intValue];
  10284. NSString *orderBy = @"";
  10285. switch (sort) {
  10286. case 0:{
  10287. orderBy = @"p.modify_time desc";
  10288. }
  10289. break;
  10290. case 1:{
  10291. orderBy = @"modify_time asc";
  10292. }
  10293. break;
  10294. case 2:{
  10295. orderBy = @"p.name asc";
  10296. }
  10297. break;
  10298. case 3:{
  10299. orderBy = @"p.name desc";
  10300. }
  10301. break;
  10302. case 4:{
  10303. orderBy = @"p.description asc";
  10304. }
  10305. break;
  10306. case 5: {
  10307. orderBy = @"m.default_category asc";
  10308. }
  10309. default:
  10310. break;
  10311. }
  10312. // 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];
  10313. 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];
  10314. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10315. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10316. sqlite3 *db = [iSalesDB get_db];
  10317. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10318. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10319. int product_id = sqlite3_column_int(stmt, 0);
  10320. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10321. NSString *name = [self textAtColumn:1 statement:stmt];
  10322. NSString *description = [self textAtColumn:2 statement:stmt];
  10323. double price = sqlite3_column_double(stmt, 3);
  10324. double discount = sqlite3_column_double(stmt,4);
  10325. int qty = sqlite3_column_int(stmt, 5);
  10326. int percentage = sqlite3_column_int(stmt, 6);
  10327. int item_id = sqlite3_column_int(stmt, 7);
  10328. // int fashion_id = sqlite3_column_int(stmt, 8);
  10329. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10330. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10331. double percent = sqlite3_column_double(stmt, 11);
  10332. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10333. if ([price_null isEqualToString:@"null"]) {
  10334. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10335. }
  10336. NSMutableDictionary *item = @{
  10337. @"linenotes": line_note,
  10338. @"check": @(1),
  10339. @"product_id": product_id_string,
  10340. @"available_qty": @(qty),
  10341. @"available_percent" : @(percent),
  10342. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10343. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10344. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10345. @"img": img_path,
  10346. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10347. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10348. }.mutableCopy;
  10349. if (percentage) {
  10350. [item removeObjectForKey:@"available_qty"];
  10351. } else {
  10352. [item removeObjectForKey:@"available_percent"];
  10353. }
  10354. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10355. if ([qty_null isEqualToString:@"null"]) {
  10356. [item removeObjectForKey:@"available_qty"];
  10357. }
  10358. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10359. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10360. }];
  10361. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10362. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10363. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10364. }
  10365. [iSalesDB close_db:db];
  10366. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10367. [dic setValue:@"" forKey:@"email_content"];
  10368. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10369. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10370. return [RAConvertor dict2data:dic];
  10371. }
  10372. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10373. __block int qty = 0;
  10374. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10375. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10376. qty = sqlite3_column_int(stmt, 0);
  10377. }];
  10378. return qty;
  10379. }
  10380. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10381. {
  10382. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10383. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10384. // NSMutableDictionary * values = params[@"replaceValue"];
  10385. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10386. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10387. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10388. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10389. NSString *pdf_path = @"";
  10390. if (direct) {
  10391. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10392. } else {
  10393. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10394. }
  10395. NSString *create_user = [self valueInParams:params key:@"user"];
  10396. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10397. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10398. // model info
  10399. // 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];
  10400. // V1.90 more color
  10401. 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];
  10402. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10403. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10404. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10405. sqlite3 *db = [iSalesDB get_db];
  10406. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10407. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10408. int product_id = sqlite3_column_int(stmt, 0);
  10409. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10410. double price = sqlite3_column_double(stmt, 1);
  10411. double discount = sqlite3_column_double(stmt,2);
  10412. int qty = sqlite3_column_int(stmt, 3);
  10413. int percentage = sqlite3_column_int(stmt, 4);
  10414. int item_id = sqlite3_column_int(stmt, 5);
  10415. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10416. double percent = sqlite3_column_double(stmt, 7);
  10417. int more_color = sqlite3_column_int(stmt, 8);
  10418. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10419. /* if ([price_null isEqualToString:@"null"]) {
  10420. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10421. }
  10422. */
  10423. [product_ids_string appendFormat:@"%@,",product_id_string];
  10424. // Regular Price
  10425. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10426. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10427. // QTY
  10428. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10429. if ([qty_null isEqualToString:@"null"]) {
  10430. // 查available
  10431. qty = [self model_QTY:product_id_string db:db];
  10432. }
  10433. if (percentage) {
  10434. qty = qty * percent / 100;
  10435. }
  10436. // Special Price
  10437. if ([price_null isEqualToString:@"null"]) {
  10438. // price = regular price
  10439. price = [regular_price_str doubleValue];
  10440. }
  10441. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10442. if (![discount_null isEqualToString:@"null"]) {
  10443. price = price * (1 - discount / 100.0);
  10444. }
  10445. NSMutableDictionary *item = @{
  10446. @"line_note": line_note,
  10447. @"product_id": product_id_string,
  10448. @"available_qty": @(qty),
  10449. @"more_color":@(more_color),
  10450. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10451. @"regular_price" : regular_price_str,
  10452. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10453. }.mutableCopy;
  10454. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10455. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10456. }];
  10457. [iSalesDB close_db:db];
  10458. if (product_ids_string.length > 0) {
  10459. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10460. }
  10461. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10462. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10463. return [RAConvertor dict2data:resultDictionary];
  10464. }
  10465. NSString *model_info = [RAConvertor dict2string:dic];
  10466. // 创建PDF
  10467. // 在preview情况下保存,则不需要新建了
  10468. if (direct) {
  10469. NSMutableDictionary *tear_sheet_params = params;
  10470. // if (tear_sheet_id) {
  10471. // tear_sheet_params = values;
  10472. // }
  10473. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10474. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10475. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10476. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10477. resultDictionary = pdfInfo.mutableCopy;
  10478. } else { // 创建PDF失败
  10479. return pdfData;
  10480. }
  10481. } else {
  10482. // pdf_path 就是本地路径
  10483. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10484. }
  10485. // 将文件移动到PDF Cache文件夹
  10486. NSString *newPath = [pdf_path lastPathComponent];
  10487. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10488. NSString *cachefolder = [paths objectAtIndex:0];
  10489. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10490. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10491. NSFileManager *fileManager = [NSFileManager defaultManager];
  10492. NSError *error = nil;
  10493. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10494. if (error) { // 移动文件失败
  10495. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10496. return [RAConvertor dict2data:resultDictionary];
  10497. }
  10498. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10499. pdf_path = [newPath lastPathComponent];
  10500. // 保存信息
  10501. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10502. NSString *off_params = [RAConvertor dict2string:params];
  10503. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10504. pdf_path = [self translateSingleQuote:pdf_path];
  10505. create_user = [self translateSingleQuote:create_user];
  10506. tear_note = [self translateSingleQuote:tear_note];
  10507. tear_name = [self translateSingleQuote:tear_name];
  10508. model_info = [self translateSingleQuote:model_info];
  10509. configureParams = [self translateSingleQuote:configureParams];
  10510. off_params = [self translateSingleQuote:off_params];
  10511. 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];
  10512. if (tear_sheet_id) {
  10513. int _id = [tear_sheet_id intValue];
  10514. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10515. }
  10516. int result = [iSalesDB execSql:save_pdf_sql];
  10517. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10518. //
  10519. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10520. if (remove_Item) {
  10521. // portfolioId
  10522. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10523. }
  10524. return [RAConvertor dict2data:resultDictionary];
  10525. }
  10526. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10527. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10528. }
  10529. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10530. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10531. }
  10532. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10533. int offset = [[params valueForKey:@"offset"] intValue];
  10534. int limit = [[params valueForKey:@"limit"] intValue];
  10535. NSString *keyword = [params valueForKey:@"keyWord"];
  10536. NSString *where = @"where is_delete is null or is_delete = 0";
  10537. if (keyword.length) {
  10538. keyword = [self translateSingleQuote:keyword];
  10539. 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];
  10540. }
  10541. 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
  10542. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10543. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10544. NSString *cachefolder = [paths objectAtIndex:0];
  10545. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10546. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10547. NSString *name = [self textAtColumn:0 statement:stmt];
  10548. NSString *note = [self textAtColumn:1 statement:stmt];
  10549. NSString *time = [self textAtColumn:2 statement:stmt];
  10550. NSString *user = [self textAtColumn:3 statement:stmt];
  10551. NSString *path = [self textAtColumn:4 statement:stmt];
  10552. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10553. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10554. path = [pdfFolder stringByAppendingPathComponent:path];
  10555. BOOL bdir=NO;
  10556. NSFileManager* fileManager = [NSFileManager defaultManager];
  10557. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10558. {
  10559. //pdf文件不存在
  10560. path=nil;
  10561. }
  10562. time = [self changeDateTimeFormate:time];
  10563. time = [time stringByAppendingString:@" PST"];
  10564. int sheet_id = sqlite3_column_int(stmt, 5);
  10565. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10566. item[@"tearsheetsId"]=@(sheet_id);
  10567. item[@"pdf_path"]=path;
  10568. item[@"create_time"]=time;
  10569. item[@"create_user"]=user;
  10570. item[@"tear_note"]=note;
  10571. item[@"tear_name"]=name;
  10572. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10573. item[@"model_info"]=model_info;
  10574. item[@"off_params"]=off_params;
  10575. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10576. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10577. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10578. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10579. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10580. }];
  10581. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10582. return [RAConvertor dict2data:dic];
  10583. }
  10584. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10585. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10586. // NSString *user = [params objectForKey:@"user"];
  10587. // 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];
  10588. 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];
  10589. int result = [iSalesDB execSql:sql];
  10590. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10591. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10592. if (result == RESULT_TRUE) {
  10593. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10594. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10595. sqlite3 *db = [iSalesDB get_db];
  10596. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10597. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10598. [iSalesDB close_db:db];
  10599. }
  10600. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10601. return [RAConvertor dict2data:dic];
  10602. }
  10603. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10604. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10605. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10606. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10607. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10608. NSDictionary *company_item = @{
  10609. @"control": @"text",
  10610. @"keyboard": @"text",
  10611. @"name": @"company_name",
  10612. @"value": COMPANY_FULL_NAME,
  10613. @"aname": @"Company Name"
  10614. };
  10615. [section_0 setObject:company_item forKey:@"item_0"];
  10616. [dic setObject:section_0 forKey:@"section_0"];
  10617. // Regurlar Price
  10618. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10619. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10620. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10621. __block long val_count = 0;
  10622. NSString *sql = @"select name,type,order_by from price order by order_by";
  10623. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10624. NSString *name = [self textAtColumn:0 statement:stmt];
  10625. int type = sqlite3_column_int(stmt, 1);
  10626. int order_by = sqlite3_column_int(stmt, 2);
  10627. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10628. NSDictionary *price_dic = @{
  10629. @"value" : name,
  10630. @"value_id" : [NSNumber numberWithInteger:type],
  10631. @"check" : order_by == 0 ? @(1) : @(0)
  10632. };
  10633. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10634. val_count = ++(*count);
  10635. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10636. val_count = 0;
  10637. }];
  10638. [cadedate setObject:@{@"value" : @"None",
  10639. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10640. val_count++;
  10641. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10642. [price setObject:cadedate forKey:@"cadedate"];
  10643. [section1 setObject:price forKey:@"item_2"];
  10644. [dic setObject:section1 forKey:@"section_1"];
  10645. return [RAConvertor dict2data:dic];
  10646. }
  10647. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10648. NSString *product_id = [params objectForKey:@"fashionId"];
  10649. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10650. __block int result = RESULT_TRUE;
  10651. __block int qty = 0;
  10652. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10653. qty = sqlite3_column_int(stmt, 0);
  10654. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10655. result = RESULT_FALSE;
  10656. }];
  10657. NSMutableDictionary *dic = @{
  10658. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10659. @"mode" : @"Regular Mode",
  10660. @"quantity_available" : @(qty),
  10661. @"result" : @(result),
  10662. }.mutableCopy;
  10663. return [RAConvertor dict2data:dic];
  10664. }
  10665. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10666. NSString *item_ids = [params objectForKey:@"item_id"];
  10667. NSString *line_notes = [params objectForKey:@"notes"];
  10668. NSString *price_str = [params objectForKey:@"price"];
  10669. NSString *discount_str = [params objectForKey:@"discount"];
  10670. NSString *percent = [params objectForKey:@"available_percent"];
  10671. NSString *qty = [params objectForKey:@"available_qty"];
  10672. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10673. int dot = 0;
  10674. if (line_notes) {
  10675. line_notes = [self translateSingleQuote:line_notes];
  10676. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10677. sql = [sql stringByAppendingString:line_notes];
  10678. dot = 1;
  10679. } else {
  10680. line_notes = @"";
  10681. }
  10682. if (price_str) {
  10683. if (dot) {
  10684. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10685. } else {
  10686. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10687. dot = 1;
  10688. }
  10689. sql = [sql stringByAppendingString:price_str];
  10690. } else {
  10691. price_str = @"";
  10692. }
  10693. if (discount_str) {
  10694. if (dot) {
  10695. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10696. } else {
  10697. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10698. dot = 1;
  10699. }
  10700. sql = [sql stringByAppendingString:discount_str];
  10701. } else {
  10702. discount_str = @"";
  10703. }
  10704. if (percent) {
  10705. if (dot) {
  10706. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10707. } else {
  10708. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10709. dot = 1;
  10710. }
  10711. sql = [sql stringByAppendingString:percent];
  10712. } else {
  10713. percent = @"";
  10714. }
  10715. if (qty) {
  10716. if (dot) {
  10717. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10718. } else {
  10719. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10720. dot = 1;
  10721. }
  10722. sql = [sql stringByAppendingString:qty];
  10723. } else {
  10724. qty = @"";
  10725. }
  10726. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10727. sql = [sql stringByAppendingString:where];
  10728. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10729. int result = [iSalesDB execSql:sql];
  10730. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10731. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10732. return [RAConvertor dict2data:dic];
  10733. }
  10734. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10735. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10737. sqlite3 *db = [iSalesDB get_db];
  10738. 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];
  10739. int result = [iSalesDB execSql:sql db:db];
  10740. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10741. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10742. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10743. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10744. [iSalesDB close_db:db];
  10745. return [RAConvertor dict2data:dic];
  10746. }
  10747. + (void)offline_removePDFWithName:(NSString *)name {
  10748. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10749. NSString *cachefolder = [paths objectAtIndex:0];
  10750. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10751. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10752. NSFileManager *fileManager = [NSFileManager defaultManager];
  10753. [fileManager removeItemAtPath:path error:nil];
  10754. }
  10755. + (void)offline_clear_PDFCache {
  10756. NSFileManager *fileManager = [NSFileManager defaultManager];
  10757. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10758. NSString *cachefolder = [paths objectAtIndex:0];
  10759. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10760. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10761. for (NSString *path in pdf_files) {
  10762. [self offline_removePDFWithName:[path lastPathComponent]];
  10763. }
  10764. }
  10765. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10766. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10767. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10768. NSString *user = [params objectForKey:@"user"];
  10769. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10770. if (![create_user isEqualToString:user]) {
  10771. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10772. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10773. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10774. return [RAConvertor dict2data:dic];
  10775. }
  10776. 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]];
  10777. __block int is_local = 0;
  10778. __block NSString *path = @"";
  10779. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10780. is_local = sqlite3_column_int(stmt, 0);
  10781. path = [self textAtColumn:1 statement:stmt];
  10782. }];
  10783. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10784. if (is_local == 1) {
  10785. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10786. // 删除文件
  10787. [self offline_removePDFWithName:path];
  10788. }
  10789. int result = [iSalesDB execSql:sql];
  10790. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10791. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10792. return [RAConvertor dict2data:dic];
  10793. }
  10794. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10795. {
  10796. // assert(add_params[@"contact_id"]!=nil);
  10797. assert(add_params[@"user"]!=nil);
  10798. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10799. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10800. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10801. NSString* where=@"1=1";
  10802. if (ver!=nil) {
  10803. where=@"is_dirty=1";
  10804. }
  10805. 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];
  10806. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10807. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10808. sqlite3 *db = [iSalesDB get_db];
  10809. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10810. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10811. NSInteger _id = sqlite3_column_int(stmt, 0);
  10812. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10813. NSString *name = [self textAtColumn:2 statement:stmt];
  10814. NSString *desc = [self textAtColumn:3 statement:stmt];
  10815. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10816. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10817. NSInteger qty = sqlite3_column_int(stmt, 6);
  10818. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10819. double percent = sqlite3_column_double(stmt, 8);
  10820. double price = sqlite3_column_double(stmt, 9);
  10821. double discount = sqlite3_column_double(stmt, 10);
  10822. NSString *img = [self textAtColumn:11 statement:stmt];
  10823. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10824. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10825. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10826. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10827. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10828. if ([price_null isEqualToString:@"null"]) {
  10829. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10830. }
  10831. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10832. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10833. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10834. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10835. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10836. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10837. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10838. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10839. if ([qty_null isEqualToString:@"null"]) {
  10840. // [item setValue:@"null" forKey:@"available_qty"];
  10841. } else {
  10842. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10843. }
  10844. if ([is_percent_null isEqualToString:@"null"]) {
  10845. // [item setValue:@"null" forKey:@"percentage"];
  10846. } else {
  10847. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10848. }
  10849. if ([percent_null isEqualToString:@"null"]) {
  10850. // [item setValue:@"null" forKey:@"percent"];
  10851. } else {
  10852. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10853. }
  10854. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10855. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10856. [item setValue:name forKey:@"name"];
  10857. [item setValue:desc forKey:@"description"];
  10858. [item setValue:img forKey:@"img"];
  10859. [item setValue:line_note forKey:@"line_note"];
  10860. [item setValue:create_time forKey:@"createtime"];
  10861. [item setValue:modify_time forKey:@"modifytime"];
  10862. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10863. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10864. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10865. }];
  10866. [iSalesDB close_db:db];
  10867. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10868. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10869. }
  10870. return ret;
  10871. }
  10872. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10873. {
  10874. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10875. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10876. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10877. NSString* where=@"1=1";
  10878. if (ver!=nil) {
  10879. where=@"is_dirty=1";
  10880. }
  10881. 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];
  10882. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10883. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10884. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10885. NSInteger _id = sqlite3_column_int(stmt, 0);
  10886. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10887. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10888. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10889. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10890. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10891. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10892. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10893. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10894. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10895. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10896. NSString *uuid = [NSUUID UUID].UUIDString;
  10897. int is_delete = sqlite3_column_int(stmt, 11);
  10898. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10899. int is_local = sqlite3_column_int(stmt, 12);
  10900. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10901. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10902. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10903. }
  10904. [item setObject:pdf_path forKey:@"pdf_path"];
  10905. [item setObject:create_user forKey:@"create_user"];
  10906. [item setObject:tear_note forKey:@"tear_note"];
  10907. [item setObject:tear_name forKey:@"tear_name"];
  10908. [item setObject:model_info forKey:@"model_info"];
  10909. [item setObject:createtime forKey:@"createtime"];
  10910. [item setObject:modifytime forKey:@"modifytime"];
  10911. [item setObject:urlParams forKey:@"urlParams"];
  10912. [item setObject:off_params forKey:@"off_params"];
  10913. [item setObject:uuid forKey:@"pdf_token"];
  10914. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10915. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10916. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10917. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10918. } else {
  10919. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10920. }
  10921. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10922. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10923. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10924. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10925. }];
  10926. return ret;
  10927. }
  10928. #pragma mark 2020
  10929. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10930. {
  10931. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10932. sqlite3 *db = [iSalesDB get_db];
  10933. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10934. if (contactType) {
  10935. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10936. } else {
  10937. contactType = @"1 = 1";
  10938. }
  10939. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10940. DebugLog(@"offline contact list keyword: %@",keyword);
  10941. // advanced search
  10942. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10943. if (contact_name) {
  10944. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10945. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10946. } else {
  10947. contact_name = @"";
  10948. }
  10949. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10950. if (customer_phone) {
  10951. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10952. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10953. } else {
  10954. customer_phone = @"";
  10955. }
  10956. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10957. if (customer_fax) {
  10958. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10959. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10960. } else {
  10961. customer_fax = @"";
  10962. }
  10963. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10964. if (customer_zipcode) {
  10965. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10966. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10967. } else {
  10968. customer_zipcode = @"";
  10969. }
  10970. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10971. if (customer_sales_rep) {
  10972. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10973. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10974. } else {
  10975. customer_sales_rep = @"";
  10976. }
  10977. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10978. if (customer_state) {
  10979. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10980. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10981. } else {
  10982. customer_state = @"";
  10983. }
  10984. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10985. if (customer_name) {
  10986. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10987. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10988. } else {
  10989. customer_name = @"";
  10990. }
  10991. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10992. if (customer_country) {
  10993. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10994. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10995. } else {
  10996. customer_country = @"";
  10997. }
  10998. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10999. if (customer_cid) {
  11000. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11001. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11002. } else {
  11003. customer_cid = @"";
  11004. }
  11005. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11006. if (customer_city) {
  11007. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11008. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11009. } else {
  11010. customer_city = @"";
  11011. }
  11012. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11013. if (customer_address) {
  11014. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11015. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11016. } else {
  11017. customer_address = @"";
  11018. }
  11019. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11020. if (customer_email) {
  11021. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11022. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11023. } else {
  11024. customer_email = @"";
  11025. }
  11026. NSString *price_name = [params valueForKey:@"price_name"];
  11027. if (price_name) {
  11028. if ([price_name containsString:@","]) {
  11029. // 首先从 price表中查处name
  11030. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11031. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11032. for (int i = 1;i < pArray.count;i++) {
  11033. NSString *p = pArray[i];
  11034. [mutablePStr appendFormat:@" or type = %@ ",p];
  11035. }
  11036. [mutablePStr appendString:@";"];
  11037. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11038. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11039. char *name = (char *)sqlite3_column_text(stmt, 0);
  11040. if (!name)
  11041. name = "";
  11042. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11043. }];
  11044. // 再根据name 拼sql
  11045. NSMutableString *mutable_price_name = [NSMutableString string];
  11046. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11047. for (int i = 1; i < price_name_array.count; i++) {
  11048. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11049. }
  11050. [mutable_price_name appendString:@")"];
  11051. price_name = mutable_price_name;
  11052. } else {
  11053. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11054. if ([price_name isEqualToString:@""]) {
  11055. price_name = @"";
  11056. } else {
  11057. __block NSString *price;
  11058. price_name = [self translateSingleQuote:price_name];
  11059. [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) {
  11060. char *p = (char *)sqlite3_column_text(stmt, 0);
  11061. if (p == NULL) {
  11062. p = "";
  11063. }
  11064. price = [NSString stringWithUTF8String:p];
  11065. }];
  11066. if ([price isEqualToString:@""]) {
  11067. price_name = @"";
  11068. } else {
  11069. price = [self translateSingleQuote:price];
  11070. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11071. }
  11072. }
  11073. }
  11074. } else {
  11075. price_name = @"";
  11076. }
  11077. int limit = [[params valueForKey:@"limit"] intValue];
  11078. int offset = [[params valueForKey:@"offset"] intValue];
  11079. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11080. 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];
  11081. 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];
  11082. // int result= [iSalesDB AddExFunction:db];
  11083. int count =0;
  11084. NSString *sqlQuery = nil;
  11085. if(keyword.length==0)
  11086. {
  11087. // 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];
  11088. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11089. sqlQuery = sql;
  11090. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11091. }
  11092. else
  11093. {
  11094. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11095. keyword = keyword.lowercaseString;
  11096. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11097. 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];
  11098. 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]];
  11099. }
  11100. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11101. sqlite3_stmt * statement;
  11102. [ret setValue:@"2" forKey:@"result"];
  11103. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11104. // 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";
  11105. int i = 0;
  11106. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11107. {
  11108. while (sqlite3_step(statement) == SQLITE_ROW)
  11109. {
  11110. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11111. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11112. int editable = sqlite3_column_int(statement, 0);
  11113. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11114. NSString *nscompany_name =nil;
  11115. if(company_name==nil)
  11116. nscompany_name=@"";
  11117. else
  11118. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11119. char *country = (char*)sqlite3_column_text(statement, 2);
  11120. if(country==nil)
  11121. country="";
  11122. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11123. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11124. // if(addr==nil)
  11125. // addr="";
  11126. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11127. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11128. if(zipcode==nil)
  11129. zipcode="";
  11130. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11131. char *state = (char*)sqlite3_column_text(statement, 5);
  11132. if(state==nil)
  11133. state="";
  11134. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11135. char *city = (char*)sqlite3_column_text(statement, 6);
  11136. if(city==nil)
  11137. city="";
  11138. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11139. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11140. // NSString *nscontact_name = nil;
  11141. // if(contact_name==nil)
  11142. // nscontact_name=@"";
  11143. // else
  11144. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11145. char *phone = (char*)sqlite3_column_text(statement, 8);
  11146. NSString *nsphone = nil;
  11147. if(phone==nil)
  11148. nsphone=@"";
  11149. else
  11150. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11151. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11152. if(contact_id==nil)
  11153. contact_id="";
  11154. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11155. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11156. if(addr_1==nil)
  11157. addr_1="";
  11158. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11159. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11160. if(addr_2==nil)
  11161. addr_2="";
  11162. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11163. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11164. if(addr_3==nil)
  11165. addr_3="";
  11166. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11167. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11168. if(addr_4==nil)
  11169. addr_4="";
  11170. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11171. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11172. if(first_name==nil)
  11173. first_name="";
  11174. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11175. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11176. if(last_name==nil)
  11177. last_name="";
  11178. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11179. char *fax = (char*)sqlite3_column_text(statement, 16);
  11180. NSString *nsfax = nil;
  11181. if(fax==nil)
  11182. nsfax=@"";
  11183. else
  11184. {
  11185. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11186. if(nsfax.length>0)
  11187. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11188. }
  11189. char *email = (char*)sqlite3_column_text(statement, 17);
  11190. NSString *nsemail = nil;
  11191. if(email==nil)
  11192. nsemail=@"";
  11193. else
  11194. {
  11195. nsemail= [[NSString alloc]initWithUTF8String:email];
  11196. if(nsemail.length>0)
  11197. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11198. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11199. }
  11200. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11201. [arr_name addObject:nsfirst_name];
  11202. [arr_name addObject:nslast_name];
  11203. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11204. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11205. {
  11206. // decrypt
  11207. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11208. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11209. nsphone=[AESCrypt fastdecrypt:nsphone];
  11210. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11211. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11212. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11213. }
  11214. [arr_addr addObject:nscompany_name];
  11215. [arr_addr addObject:nscontact_name];
  11216. [arr_addr addObject:@"<br>"];
  11217. [arr_addr addObject:nsaddr_1];
  11218. [arr_addr addObject:nsaddr_2];
  11219. [arr_addr addObject:nsaddr_3];
  11220. [arr_addr addObject:nsaddr_4];
  11221. //[arr_addr addObject:nsaddr];
  11222. [arr_addr addObject:nszipcode];
  11223. [arr_addr addObject:nscity];
  11224. [arr_addr addObject:nsstate];
  11225. [arr_addr addObject:nscountry];
  11226. [arr_addr addObject:@"<br>"];
  11227. [arr_addr addObject:nsphone];
  11228. [arr_addr addObject:nsfax];
  11229. [arr_addr addObject:nsemail];
  11230. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11231. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11232. [item setValue:name forKey:@"name"];
  11233. [item setValue:nscontact_id forKey:@"contact_id"];
  11234. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11235. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11236. i++;
  11237. }
  11238. sqlite3_finalize(statement);
  11239. }
  11240. [iSalesDB close_db:db];
  11241. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11242. dispatch_async(dispatch_get_main_queue(), ^{
  11243. UIApplication * app = [UIApplication sharedApplication];
  11244. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11245. [ret setValue:appDelegate.mode forKey:@"mode"];
  11246. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11247. result(ret);
  11248. });
  11249. return ;
  11250. });
  11251. }
  11252. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11253. //{
  11254. //
  11255. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11256. //
  11257. // NSString* contactId = [params valueForKey:@"contactId"];
  11258. //
  11259. //
  11260. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11261. //
  11262. //
  11263. //
  11264. //
  11265. //
  11266. //
  11267. // sqlite3 *db = [iSalesDB get_db];
  11268. //
  11269. //
  11270. //
  11271. //
  11272. //
  11273. // NSString *sqlQuery = nil;
  11274. //
  11275. //
  11276. // {
  11277. // 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];
  11278. //
  11279. // }
  11280. //
  11281. //
  11282. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11283. // sqlite3_stmt * statement;
  11284. //
  11285. //
  11286. // [ret setValue:@"2" forKey:@"result"];
  11287. //
  11288. //
  11289. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11290. // {
  11291. //
  11292. // //int i = 0;
  11293. // if (sqlite3_step(statement) == SQLITE_ROW)
  11294. // {
  11295. //
  11296. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11297. //
  11298. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11299. //
  11300. //
  11301. // // int editable = sqlite3_column_int(statement, 0);
  11302. //
  11303. //
  11304. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11305. // NSString *nscompany_name =nil;
  11306. // if(company_name==nil)
  11307. // nscompany_name=@"";
  11308. // else
  11309. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11310. //
  11311. //
  11312. // char *country = (char*)sqlite3_column_text(statement, 2);
  11313. // if(country==nil)
  11314. // country="";
  11315. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11316. //
  11317. //
  11318. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11319. // // if(addr==nil)
  11320. // // addr="";
  11321. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11322. //
  11323. //
  11324. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11325. // if(zipcode==nil)
  11326. // zipcode="";
  11327. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11328. //
  11329. //
  11330. // char *state = (char*)sqlite3_column_text(statement, 5);
  11331. // if(state==nil)
  11332. // state="";
  11333. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11334. //
  11335. // char *city = (char*)sqlite3_column_text(statement, 6);
  11336. // if(city==nil)
  11337. // city="";
  11338. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11339. //
  11340. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11341. // // NSString *nscontact_name = nil;
  11342. // // if(contact_name==nil)
  11343. // // nscontact_name=@"";
  11344. // // else
  11345. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11346. //
  11347. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11348. // NSString *nsphone = nil;
  11349. // if(phone==nil)
  11350. // nsphone=@"";
  11351. // else
  11352. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11353. //
  11354. //
  11355. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11356. // // if(contact_id==nil)
  11357. // // contact_id="";
  11358. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11359. //
  11360. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11361. // if(addr_1==nil)
  11362. // addr_1="";
  11363. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11364. //
  11365. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11366. // if(addr_2==nil)
  11367. // addr_2="";
  11368. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11369. //
  11370. //
  11371. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11372. // if(addr_3==nil)
  11373. // addr_3="";
  11374. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11375. //
  11376. //
  11377. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11378. // if(addr_4==nil)
  11379. // addr_4="";
  11380. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11381. //
  11382. //
  11383. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11384. // if(first_name==nil)
  11385. // first_name="";
  11386. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11387. //
  11388. //
  11389. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11390. // if(last_name==nil)
  11391. // last_name="";
  11392. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11393. //
  11394. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11395. // NSString *nsfax = nil;
  11396. // if(fax==nil)
  11397. // nsfax=@"";
  11398. // else
  11399. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11400. //
  11401. // char *email = (char*)sqlite3_column_text(statement, 17);
  11402. // NSString *nsemail = nil;
  11403. // if(email==nil)
  11404. // nsemail=@"";
  11405. // else
  11406. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11407. //
  11408. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11409. // NSString *nsimg_0 = nil;
  11410. // if(img_0==nil)
  11411. // nsimg_0=@"";
  11412. // else
  11413. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11414. //
  11415. // [self copy_bcardImg:nsimg_0];
  11416. //
  11417. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11418. // NSString *nsimg_1 = nil;
  11419. // if(img_1==nil)
  11420. // nsimg_1=@"";
  11421. // else
  11422. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11423. // [self copy_bcardImg:nsimg_1];
  11424. //
  11425. //
  11426. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11427. // NSString *nsimg_2 = nil;
  11428. // if(img_2==nil)
  11429. // nsimg_2=@"";
  11430. // else
  11431. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11432. // [self copy_bcardImg:nsimg_2];
  11433. //
  11434. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11435. // NSString *nsprice_type = nil;
  11436. // if(price_type==nil)
  11437. // nsprice_type=@"";
  11438. // else
  11439. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11440. //
  11441. //
  11442. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11443. // NSString *nsnotes = nil;
  11444. // if(notes==nil)
  11445. // nsnotes=@"";
  11446. // else
  11447. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11448. //
  11449. //
  11450. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11451. // NSString *nssalesrep = nil;
  11452. // if(salesrep==nil)
  11453. // nssalesrep=@"";
  11454. // else
  11455. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11456. //
  11457. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11458. //
  11459. //
  11460. // {
  11461. // // decrypt
  11462. //
  11463. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11464. //
  11465. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11466. //
  11467. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11468. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11469. //
  11470. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11471. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11472. //
  11473. // }
  11474. //
  11475. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11476. //
  11477. // [arr_name addObject:nsfirst_name];
  11478. // [arr_name addObject:nslast_name];
  11479. //
  11480. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11481. //
  11482. //
  11483. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11484. //
  11485. // [arr_ext addObject:nsaddr_1];
  11486. // [arr_ext addObject:nsaddr_2];
  11487. // [arr_ext addObject:nsaddr_3];
  11488. // [arr_ext addObject:nsaddr_4];
  11489. // [arr_ext addObject:@"\r\n"];
  11490. //
  11491. // [arr_ext addObject:nscity];
  11492. // [arr_ext addObject:nsstate];
  11493. // [arr_ext addObject:nszipcode];
  11494. // [arr_ext addObject:nscountry];
  11495. //
  11496. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11497. //
  11498. //
  11499. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11500. //
  11501. //
  11502. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11503. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11504. // [item setValue:nscountry forKey:@"customer_country"];
  11505. // [item setValue:nsphone forKey:@"customer_phone"];
  11506. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11507. // [item setValue:nscompany_name forKey:@"customer_name"];
  11508. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11509. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11510. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11511. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11512. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11513. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11514. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11515. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11516. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11517. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11518. // [item setValue:nscity forKey:@"customer_city"];
  11519. // [item setValue:nsstate forKey:@"customer_state"];
  11520. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11521. // [item setValue:contactId forKey:@"customer_cid"];
  11522. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11523. // [item setValue:nsfax forKey:@"customer_fax"];
  11524. // [item setValue:nsemail forKey:@"customer_email"];
  11525. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11526. //
  11527. //
  11528. // [ret setObject:item forKey:@"customerInfo"];
  11529. // // i++;
  11530. //
  11531. //
  11532. //
  11533. // }
  11534. //
  11535. //
  11536. //
  11537. //
  11538. // sqlite3_finalize(statement);
  11539. // }
  11540. //
  11541. //
  11542. //
  11543. // [iSalesDB close_db:db];
  11544. //
  11545. //
  11546. //
  11547. //
  11548. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11549. // dispatch_async(dispatch_get_main_queue(), ^{
  11550. // UIApplication * app = [UIApplication sharedApplication];
  11551. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11552. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11553. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11554. // result(ret);
  11555. // });
  11556. //
  11557. // return ;
  11558. // });
  11559. //}
  11560. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11561. {
  11562. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11563. NSString* order_code= appDelegate.order_code;
  11564. NSString* user = appDelegate.user;
  11565. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11566. NSString* category = [params valueForKey:@"category"];
  11567. if (!category || [category isEqualToString:@""]) {
  11568. category = @"%";
  11569. }
  11570. category = [self translateSingleQuote:category];
  11571. int limit = [[params valueForKey:@"limit"] intValue];
  11572. int offset = [[params valueForKey:@"offset"] intValue];
  11573. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11574. NSString *limit_str = @"";
  11575. if (limited) {
  11576. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11577. }
  11578. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11579. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11580. sqlite3 *db = [iSalesDB get_db];
  11581. // [iSalesDB AddExFunction:db];
  11582. int count;
  11583. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11584. 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];
  11585. double price_min = 0;
  11586. double price_max = 0;
  11587. if ([params.allKeys containsObject:@"alert"]) {
  11588. // alert
  11589. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11590. NSString *alert = params[@"alert"];
  11591. if ([alert isEqualToString:@"Display All"]) {
  11592. alert = [NSString stringWithFormat:@""];
  11593. } else {
  11594. alert = [self translateSingleQuote:alert];
  11595. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11596. }
  11597. // available
  11598. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11599. NSString *available = params[@"available"];
  11600. NSString *available_condition;
  11601. if ([available isEqualToString:@"Display All"]) {
  11602. available_condition = @"";
  11603. } else if ([available isEqualToString:@"Available Now"]) {
  11604. available_condition = @"and availability > 0";
  11605. } else {
  11606. available_condition = @"and availability == 0";
  11607. }
  11608. // best seller
  11609. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11610. NSString *best_seller = @"";
  11611. NSString *order_best_seller = @"m.name asc";
  11612. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11613. best_seller = @"and best_seller > 0";
  11614. order_best_seller = @"m.best_seller desc,m.name asc";
  11615. }
  11616. // price
  11617. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11618. NSString *price = params[@"price"];
  11619. price_min = 0;
  11620. price_max = MAXFLOAT;
  11621. if (user && price != nil) {
  11622. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11623. NSMutableString *priceName = [NSMutableString string];
  11624. for (int i = 0; i < priceTypeArray.count; i++) {
  11625. NSString *pricetype = priceTypeArray[i];
  11626. pricetype = [self translateSingleQuote:pricetype];
  11627. if (i == 0) {
  11628. [priceName appendFormat:@"'%@'",pricetype];
  11629. } else {
  11630. [priceName appendFormat:@",'%@'",pricetype];
  11631. }
  11632. }
  11633. if ([price isEqualToString:@"Display All"]) {
  11634. price = [NSString stringWithFormat:@""];
  11635. } else if([price containsString:@"+"]){
  11636. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11637. price_min = [price doubleValue];
  11638. 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];
  11639. } else {
  11640. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11641. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11642. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11643. 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];
  11644. }
  11645. } else {
  11646. price = @"";
  11647. }
  11648. // sold_by_qty : Sold in quantities of %@
  11649. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11650. NSString *qty = params[@"sold_by_qty"];
  11651. if ([qty isEqualToString:@"Display All"]) {
  11652. qty = @"";
  11653. } else {
  11654. qty = [self translateSingleQuote:qty];
  11655. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11656. }
  11657. // cate
  11658. // category = [self translateSingleQuote:category];
  11659. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11660. // cate mutiple selection
  11661. NSString *category_id = params[@"category"];
  11662. NSMutableArray *cate_id_array = nil;
  11663. NSMutableString *cateWhere = [NSMutableString string];
  11664. if ([category_id isEqualToString:@""] || !category_id) {
  11665. [cateWhere appendString:@"1 = 1"];
  11666. } else {
  11667. if ([category_id containsString:@","]) {
  11668. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11669. } else {
  11670. cate_id_array = [@[category_id] mutableCopy];
  11671. }
  11672. [cateWhere appendString:@"("];
  11673. for (int i = 0; i < cate_id_array.count; i++) {
  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. }
  11680. [cateWhere appendString:@")"];
  11681. }
  11682. // where bestseller > 0 order by bestseller desc
  11683. // sql query: alert availability(int) best_seller(int) price qty
  11684. 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];
  11685. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11686. }
  11687. DebugLog(@"offline category where: %@",where);
  11688. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11689. if (!user) {
  11690. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11691. }
  11692. [ret setValue:filter forKey:@"filter"];
  11693. DebugLog(@"offline_category sql:%@",sqlQuery);
  11694. sqlite3_stmt * statement;
  11695. [ret setValue:@"2" forKey:@"result"];
  11696. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11697. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11698. // int count=0;
  11699. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11700. {
  11701. int i=0;
  11702. while (sqlite3_step(statement) == SQLITE_ROW)
  11703. {
  11704. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11705. char *name = (char*)sqlite3_column_text(statement, 0);
  11706. if(name==nil)
  11707. name="";
  11708. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11709. char *description = (char*)sqlite3_column_text(statement, 1);
  11710. if(description==nil)
  11711. description="";
  11712. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11713. int product_id = sqlite3_column_int(statement, 2);
  11714. int wid = sqlite3_column_int(statement, 3);
  11715. int closeout = sqlite3_column_int(statement, 4);
  11716. int cid = sqlite3_column_int(statement, 5);
  11717. int wisdelete = sqlite3_column_int(statement, 6);
  11718. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11719. int more_color = sqlite3_column_int(statement, 8);
  11720. // Defaul Category ID
  11721. __block NSString *categoryID = nil;
  11722. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11723. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11724. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11725. if(default_category==nil)
  11726. default_category="";
  11727. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11728. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11729. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11730. categoryID = nsdefault_category;
  11731. }];
  11732. if (!categoryID.length) {
  11733. NSString *cateIDs = params[@"category"];
  11734. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11735. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11736. for (NSString *cateID in requestCategoryArr) {
  11737. BOOL needBreak = NO;
  11738. for (NSString *itemCateIDBox in itemCategoryArr) {
  11739. if (itemCateIDBox.length > 4) {
  11740. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11741. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11742. if ([itemCategoryID isEqualToString:cateID]) {
  11743. needBreak = YES;
  11744. categoryID = itemCategoryID;
  11745. break;
  11746. }
  11747. }
  11748. }
  11749. if (needBreak) {
  11750. break;
  11751. }
  11752. }
  11753. }
  11754. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11755. if(wid !=0 && wisdelete != 1)
  11756. [item setValue:@"true" forKey:@"wish_exists"];
  11757. else
  11758. [item setValue:@"false" forKey:@"wish_exists"];
  11759. if(closeout==0)
  11760. [item setValue:@"false" forKey:@"is_closeout"];
  11761. else
  11762. [item setValue:@"true" forKey:@"is_closeout"];
  11763. if(cid==0)
  11764. [item setValue:@"false" forKey:@"cart_exists"];
  11765. else
  11766. [item setValue:@"true" forKey:@"cart_exists"];
  11767. if (more_color == 0) {
  11768. [item setObject:@(false) forKey:@"more_color"];
  11769. } else if (more_color == 1) {
  11770. [item setObject:@(true) forKey:@"more_color"];
  11771. }
  11772. [item addEntriesFromDictionary:imgjson];
  11773. // [item setValue:nsurl forKey:@"img"];
  11774. [item setValue:nsname forKey:@"name"];
  11775. [item setValue:nsdescription forKey:@"description"];
  11776. if (categoryID) {
  11777. [item setValue:categoryID forKey:@"item_category_id"];
  11778. }
  11779. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11780. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11781. i++;
  11782. }
  11783. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11784. [ret setObject:items forKey:@"items"];
  11785. sqlite3_finalize(statement);
  11786. } else {
  11787. DebugLog(@"nothing...");
  11788. }
  11789. DebugLog(@"count:%d",count);
  11790. [iSalesDB close_db:db];
  11791. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11792. dispatch_async(dispatch_get_main_queue(), ^{
  11793. // UIApplication * app = [UIApplication sharedApplication];
  11794. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11795. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11796. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11797. result(ret);
  11798. });
  11799. });
  11800. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11801. }
  11802. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11803. {
  11804. [self categoryList:params limited:YES completionHandler:result];
  11805. }
  11806. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11807. {
  11808. UIApplication * app = [UIApplication sharedApplication];
  11809. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11810. NSString* order_code = appDelegate.order_code;
  11811. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11812. NSString* keyword = [params valueForKey:@"keyword"];
  11813. keyword=keyword.lowercaseString;
  11814. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11815. int limit = [[params valueForKey:@"limit"] intValue];
  11816. int offset = [[params valueForKey:@"offset"] intValue];
  11817. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11818. NSString *limit_str = @"";
  11819. if (limited) {
  11820. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11821. }
  11822. sqlite3 *db = [iSalesDB get_db];
  11823. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11824. NSString *sqlQuery = nil;
  11825. if(exactMatch )
  11826. 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 ;
  11827. else
  11828. 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
  11829. DebugLog(@"offline_search sql:%@",sqlQuery);
  11830. sqlite3_stmt * statement;
  11831. [ret setValue:@"2" forKey:@"result"];
  11832. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11833. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11834. // int count=0;
  11835. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11836. {
  11837. int i=0;
  11838. while (sqlite3_step(statement) == SQLITE_ROW)
  11839. {
  11840. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11841. // char *name = (char*)sqlite3_column_text(statement, 1);
  11842. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11843. char *name = (char*)sqlite3_column_text(statement, 0);
  11844. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11845. char *description = (char*)sqlite3_column_text(statement, 1);
  11846. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11847. int product_id = sqlite3_column_int(statement, 2);
  11848. // char *url = (char*)sqlite3_column_text(statement, 3);
  11849. // if(url==nil)
  11850. // url="";
  11851. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11852. int wid = sqlite3_column_int(statement, 3);
  11853. int closeout = sqlite3_column_int(statement, 4);
  11854. int cid = sqlite3_column_int(statement, 5);
  11855. int wisdelete = sqlite3_column_int(statement, 6);
  11856. int more_color = sqlite3_column_int(statement, 7);
  11857. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11858. if(wid !=0 && wisdelete != 1)
  11859. [item setValue:@"true" forKey:@"wish_exists"];
  11860. else
  11861. [item setValue:@"false" forKey:@"wish_exists"];
  11862. if(closeout==0)
  11863. [item setValue:@"false" forKey:@"is_closeout"];
  11864. else
  11865. [item setValue:@"true" forKey:@"is_closeout"];
  11866. if(cid==0)
  11867. [item setValue:@"false" forKey:@"cart_exists"];
  11868. else
  11869. [item setValue:@"true" forKey:@"cart_exists"];
  11870. if (more_color == 0) {
  11871. [item setObject:@(false) forKey:@"more_color"];
  11872. } else if (more_color == 1) {
  11873. [item setObject:@(true) forKey:@"more_color"];
  11874. }
  11875. [item addEntriesFromDictionary:imgjson];
  11876. // [item setValue:nsurl forKey:@"img"];
  11877. [item setValue:nsname forKey:@"fash_name"];
  11878. [item setValue:nsdescription forKey:@"description"];
  11879. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11880. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11881. i++;
  11882. }
  11883. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11884. [ret setObject:items forKey:@"items"];
  11885. sqlite3_finalize(statement);
  11886. }
  11887. DebugLog(@"count:%d",count);
  11888. [iSalesDB close_db:db];
  11889. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11890. dispatch_async(dispatch_get_main_queue(), ^{
  11891. result(ret);
  11892. });
  11893. });
  11894. }
  11895. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11896. {
  11897. [self search:params limited:YES completionHandler:result];
  11898. }
  11899. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11900. //{
  11901. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11902. // NSString* order_code = appDelegate.order_code;
  11903. // NSString* user = appDelegate.user;
  11904. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11905. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11906. //
  11907. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11908. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11909. //
  11910. // // category
  11911. // NSDictionary *category_menu = [self offline_category_menu];
  11912. // [filter setValue:category_menu forKey:@"category"];
  11913. //
  11914. // NSString* where= nil;
  11915. // NSString* orderby= @"m.name";
  11916. // if (!filterSearch) {
  11917. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11918. //
  11919. // switch (covertype) {
  11920. // case 0:
  11921. // {
  11922. // where=@"m.category like'%%#005#%%'";
  11923. // break;
  11924. // }
  11925. // case 1:
  11926. // {
  11927. // where=@"m.alert like '%QS%'";
  11928. // break;
  11929. // }
  11930. // case 2:
  11931. // {
  11932. // where=@"m.availability>0";
  11933. // break;
  11934. // }
  11935. // case 3:
  11936. // {
  11937. // where=@"m.best_seller>0";
  11938. // orderby=@"m.best_seller desc,m.name asc";
  11939. // break;
  11940. // }
  11941. // default:
  11942. // where=@"1=1";
  11943. // break;
  11944. // }
  11945. //
  11946. // }
  11947. //
  11948. //
  11949. // int limit = [[params valueForKey:@"limit"] intValue];
  11950. // int offset = [[params valueForKey:@"offset"] intValue];
  11951. //
  11952. // NSString *limit_str = @"";
  11953. // if (limited) {
  11954. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11955. // }
  11956. //
  11957. //
  11958. // sqlite3 *db = [iSalesDB get_db];
  11959. // // [iSalesDB AddExFunction:db];
  11960. //
  11961. // int count;
  11962. //
  11963. // NSString *sqlQuery = nil;
  11964. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11965. // 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];
  11966. //
  11967. //
  11968. // double price_min = 0;
  11969. // double price_max = 0;
  11970. // if (filterSearch) {
  11971. // // alert
  11972. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11973. // NSString *alert = params[@"alert"];
  11974. // if ([alert isEqualToString:@"Display All"]) {
  11975. // alert = [NSString stringWithFormat:@""];
  11976. // } else {
  11977. // alert = [self translateSingleQuote:alert];
  11978. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11979. // }
  11980. //
  11981. // // available
  11982. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11983. // NSString *available = params[@"available"];
  11984. // NSString *available_condition;
  11985. // if ([available isEqualToString:@"Display All"]) {
  11986. // available_condition = @"";
  11987. // } else if ([available isEqualToString:@"Available Now"]) {
  11988. // available_condition = @"and availability > 0";
  11989. // } else {
  11990. // available_condition = @"and availability == 0";
  11991. // }
  11992. //
  11993. // // best seller
  11994. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11995. // NSString *best_seller = @"";
  11996. // NSString *order_best_seller = @"m.name asc";
  11997. //
  11998. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11999. // best_seller = @"and best_seller > 0";
  12000. // order_best_seller = @"m.best_seller desc,m.name asc";
  12001. // }
  12002. //
  12003. // // price
  12004. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12005. // NSString *price = params[@"price"];
  12006. // price_min = 0;
  12007. // price_max = MAXFLOAT;
  12008. // if (user) {
  12009. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12010. // NSMutableString *priceName = [NSMutableString string];
  12011. // for (int i = 0; i < priceTypeArray.count; i++) {
  12012. // NSString *pricetype = priceTypeArray[i];
  12013. // pricetype = [self translateSingleQuote:pricetype];
  12014. // if (i == 0) {
  12015. // [priceName appendFormat:@"'%@'",pricetype];
  12016. // } else {
  12017. // [priceName appendFormat:@",'%@'",pricetype];
  12018. // }
  12019. // }
  12020. //
  12021. // if ([price isEqualToString:@"Display All"]) {
  12022. // price = [NSString stringWithFormat:@""];
  12023. // } else if([price containsString:@"+"]){
  12024. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12025. // price_min = [price doubleValue];
  12026. // 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];
  12027. // } else {
  12028. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12029. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12030. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12031. // 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];
  12032. // }
  12033. // } else {
  12034. // price = @"";
  12035. // }
  12036. //
  12037. // // sold_by_qty : Sold in quantities of %@
  12038. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12039. // NSString *qty = params[@"sold_by_qty"];
  12040. // if ([qty isEqualToString:@"Display All"]) {
  12041. // qty = @"";
  12042. // } else {
  12043. // qty = [self translateSingleQuote:qty];
  12044. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12045. // }
  12046. //
  12047. // // category
  12048. // NSString *category_id = params[@"ctgId"];
  12049. // NSMutableArray *cate_id_array = nil;
  12050. // NSMutableString *cateWhere = [NSMutableString string];
  12051. //
  12052. // if ([category_id isEqualToString:@""] || !category_id) {
  12053. // [cateWhere appendString:@"1 = 1"];
  12054. // } else {
  12055. //
  12056. // if ([category_id containsString:@","]) {
  12057. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12058. // } else {
  12059. // cate_id_array = [@[category_id] mutableCopy];
  12060. // }
  12061. // /*-----------*/
  12062. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12063. //
  12064. // [cateWhere appendString:@"("];
  12065. // for (int i = 0; i < cate_id_array.count; i++) {
  12066. //
  12067. // for (NSString *key0 in cateDic.allKeys) {
  12068. //
  12069. // if ([key0 containsString:@"category_"]) {
  12070. //
  12071. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12072. //
  12073. // for (NSString *key1 in category0.allKeys) {
  12074. //
  12075. // if ([key1 containsString:@"category_"]) {
  12076. //
  12077. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12078. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12079. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12080. //
  12081. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12082. // if (i == 0) {
  12083. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12084. // } else {
  12085. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12086. // }
  12087. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12088. // [category0 setValue:category1 forKey:key1];
  12089. // [cateDic setValue:category0 forKey:key0];
  12090. //
  12091. // }
  12092. //
  12093. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12094. //
  12095. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12096. //
  12097. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12098. // if (i == 0) {
  12099. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12100. // } else {
  12101. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12102. // }
  12103. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12104. // [cateDic setValue:category0 forKey:key0];
  12105. //
  12106. // }
  12107. //
  12108. // }
  12109. //
  12110. // }
  12111. //
  12112. // }
  12113. //
  12114. // }
  12115. //
  12116. // }
  12117. // [cateWhere appendString:@")"];
  12118. //
  12119. // [filter setValue:cateDic forKey:@"category"];
  12120. // }
  12121. //
  12122. // // where bestseller > 0 order by bestseller desc
  12123. // // sql query: alert availability(int) best_seller(int) price qty
  12124. //
  12125. // 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];
  12126. //
  12127. //
  12128. // // count
  12129. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12130. //
  12131. // }
  12132. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12133. //
  12134. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12135. //
  12136. //
  12137. //
  12138. //
  12139. // if (!user) {
  12140. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12141. // }
  12142. //
  12143. // [ret setValue:filter forKey:@"filter"];
  12144. //
  12145. //
  12146. //
  12147. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12148. //
  12149. // sqlite3_stmt * statement;
  12150. // [ret setValue:@"2" forKey:@"result"];
  12151. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12152. //
  12153. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12154. // // int count=0;
  12155. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12156. // {
  12157. //
  12158. // int i=0;
  12159. // while (sqlite3_step(statement) == SQLITE_ROW)
  12160. // {
  12161. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12162. //
  12163. //
  12164. // char *name = (char*)sqlite3_column_text(statement, 0);
  12165. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12166. //
  12167. // char *description = (char*)sqlite3_column_text(statement, 1);
  12168. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12169. //
  12170. //
  12171. //
  12172. // int product_id = sqlite3_column_int(statement, 2);
  12173. //
  12174. //
  12175. // int wid = sqlite3_column_int(statement, 3);
  12176. // int closeout = sqlite3_column_int(statement, 4);
  12177. // int cid = sqlite3_column_int(statement, 5);
  12178. // int wisdelete = sqlite3_column_int(statement, 6);
  12179. // int more_color = sqlite3_column_int(statement, 7);
  12180. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12181. //
  12182. // if(wid !=0 && wisdelete != 1)
  12183. // [item setValue:@"true" forKey:@"wish_exists"];
  12184. // else
  12185. // [item setValue:@"false" forKey:@"wish_exists"];
  12186. //
  12187. // if(closeout==0)
  12188. // [item setValue:@"false" forKey:@"is_closeout"];
  12189. // else
  12190. // [item setValue:@"true" forKey:@"is_closeout"];
  12191. //
  12192. // if(cid==0)
  12193. // [item setValue:@"false" forKey:@"cart_exists"];
  12194. // else
  12195. // [item setValue:@"true" forKey:@"cart_exists"];
  12196. //
  12197. // if (more_color == 0) {
  12198. // [item setObject:@(false) forKey:@"more_color"];
  12199. // } else if (more_color == 1) {
  12200. // [item setObject:@(true) forKey:@"more_color"];
  12201. // }
  12202. //
  12203. // [item addEntriesFromDictionary:imgjson];
  12204. //
  12205. //
  12206. // // [item setValue:nsurl forKey:@"img"];
  12207. // [item setValue:nsname forKey:@"fash_name"];
  12208. // [item setValue:nsdescription forKey:@"description"];
  12209. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12210. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12211. // i++;
  12212. // }
  12213. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12214. // [ret setObject:items forKey:@"items"];
  12215. // sqlite3_finalize(statement);
  12216. // }
  12217. //
  12218. // [iSalesDB close_db:db];
  12219. //
  12220. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12221. //
  12222. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12223. // dispatch_async(dispatch_get_main_queue(), ^{
  12224. //
  12225. // result(ret);
  12226. // });
  12227. //
  12228. // });
  12229. //
  12230. //
  12231. //
  12232. //}
  12233. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12234. //{
  12235. // [self itemsearch:params limited:YES completionHandler:result];
  12236. //}
  12237. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12238. {
  12239. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12240. NSString* offline_command=params[@"offline_Command"];
  12241. NSMutableDictionary* ret=nil;
  12242. if([offline_command isEqualToString:@"model_NIYMAL"])
  12243. {
  12244. NSString* category = params[@"category"];
  12245. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12246. }
  12247. dispatch_async(dispatch_get_main_queue(), ^{
  12248. result(ret);
  12249. });
  12250. });
  12251. }
  12252. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12253. {
  12254. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12255. int sort = [[params objectForKey:@"sort"] intValue];
  12256. NSString *sort_str = @"";
  12257. switch (sort) {
  12258. case 0:{
  12259. sort_str = @"order by w.modify_time desc";
  12260. }
  12261. break;
  12262. case 1:{
  12263. sort_str = @"order by w.modify_time asc";
  12264. }
  12265. break;
  12266. case 2:{
  12267. sort_str = @"order by m.name asc";
  12268. }
  12269. break;
  12270. case 3:{
  12271. sort_str = @"order by m.name desc";
  12272. }
  12273. break;
  12274. case 4:{
  12275. sort_str = @"order by m.description asc";
  12276. }
  12277. break;
  12278. default:
  12279. break;
  12280. }
  12281. // NSString* user = appDelegate.user;
  12282. sqlite3 *db = [iSalesDB get_db];
  12283. // order by w.create_time
  12284. 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];
  12285. // 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];
  12286. sqlite3_stmt * statement;
  12287. NSDate *date1 = [NSDate date];
  12288. // NSDate *date2 = nil;
  12289. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12290. int count=0;
  12291. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12292. {
  12293. while (sqlite3_step(statement) == SQLITE_ROW)
  12294. {
  12295. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12296. int product_id = sqlite3_column_double(statement, 0);
  12297. char *description = (char*)sqlite3_column_text(statement, 1);
  12298. if(description==nil)
  12299. description= "";
  12300. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12301. int item_id = sqlite3_column_double(statement, 2);
  12302. NSDate *date_image = [NSDate date];
  12303. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12304. printf("image : ");
  12305. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12306. // char *url = (char*)sqlite3_column_text(statement, 3);
  12307. // if(url==nil)
  12308. // url="";
  12309. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12310. int qty = sqlite3_column_int(statement, 3);
  12311. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12312. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12313. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12314. item[@"description"]= nsdescription;
  12315. item[@"img"]= nsurl;
  12316. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12317. count++;
  12318. }
  12319. printf("total time:");
  12320. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12321. ret[@"count"]= [NSNumber numberWithInt:count];
  12322. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12323. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12324. ret[@"result"]= [NSNumber numberWithInt:2];
  12325. sqlite3_finalize(statement);
  12326. }
  12327. [iSalesDB close_db:db];
  12328. dispatch_async(dispatch_get_main_queue(), ^{
  12329. UIApplication * app = [UIApplication sharedApplication];
  12330. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12331. appDelegate.wish_count =count;
  12332. [appDelegate update_count_mark];
  12333. result(ret);
  12334. });
  12335. });
  12336. }
  12337. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12338. {
  12339. // UIApplication * app = [UIApplication sharedApplication];
  12340. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12341. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12342. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12343. sqlite3 *db = [iSalesDB get_db];
  12344. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12345. NSString* product_id=params[@"product_id"];
  12346. NSString *item_count_str = params[@"item_count"];
  12347. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12348. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12349. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12350. // NSString *sql = @"";
  12351. for(int i=0;i<arr.count;i++)
  12352. {
  12353. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12354. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12355. __block int cart_count = 0;
  12356. if (!item_count_str) {
  12357. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12358. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12359. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12360. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12361. cart_count = [[model_set_components lastObject] intValue];
  12362. }];
  12363. }
  12364. if(count==0)
  12365. {
  12366. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12367. sqlite3_stmt *stmt;
  12368. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12369. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12370. if (item_count_arr) {
  12371. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12372. } else {
  12373. sqlite3_bind_int(stmt,2,cart_count);
  12374. }
  12375. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12376. [iSalesDB execSql:@"ROLLBACK" db:db];
  12377. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12378. [iSalesDB close_db:db];
  12379. DebugLog(@"add to wishlist error");
  12380. dispatch_async(dispatch_get_main_queue(), ^{
  12381. result(ret);
  12382. });
  12383. }
  12384. } else {
  12385. int qty = 0;
  12386. if (item_count_arr) {
  12387. qty = [item_count_arr[i] intValue];
  12388. } else {
  12389. qty = cart_count;
  12390. }
  12391. 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]];
  12392. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12393. [iSalesDB execSql:@"ROLLBACK" db:db];
  12394. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12395. [iSalesDB close_db:db];
  12396. DebugLog(@"add to wishlist error");
  12397. dispatch_async(dispatch_get_main_queue(), ^{
  12398. result(ret);
  12399. });
  12400. }
  12401. }
  12402. }
  12403. // [iSalesDB execSql:sql db:db];
  12404. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12405. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12406. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12407. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12408. [iSalesDB close_db:db];
  12409. ret[@"result"]= [NSNumber numberWithInt:2];
  12410. dispatch_async(dispatch_get_main_queue(), ^{
  12411. UIApplication * app = [UIApplication sharedApplication];
  12412. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12413. appDelegate.wish_count =count;
  12414. [appDelegate update_count_mark];
  12415. result(ret);
  12416. });
  12417. });
  12418. }
  12419. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12420. {
  12421. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12422. bool bLogin = appDelegate.bLogin;
  12423. __block NSString* contact_id = appDelegate.contact_id;
  12424. __block NSString* user = appDelegate.user;
  12425. __block NSString* order_code = appDelegate.order_code;
  12426. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12427. NSString* model_name = [params valueForKey:@"product_name"];
  12428. NSString* product_id = [params valueForKey:@"product_id"];
  12429. NSString* upc_code = [params valueForKey:@"upc_code"];
  12430. NSString* category = [params valueForKey:@"category"];
  12431. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12432. if(category==nil) {
  12433. category = default_category_id;
  12434. } else {
  12435. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12436. // 参数重有多个category id
  12437. if (arr_0.count > 1) {
  12438. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12439. // 取交集
  12440. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12441. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12442. [set_0 intersectSet:set_1];
  12443. if (set_0.count == 1) {
  12444. category = (NSString *)[set_0 anyObject];
  12445. } else {
  12446. if ([set_0 containsObject:default_category_id]) {
  12447. category = default_category_id;
  12448. } else {
  12449. category = (NSString *)[set_0 anyObject];
  12450. }
  12451. }
  12452. }
  12453. }
  12454. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12455. sqlite3 *db = [iSalesDB get_db];
  12456. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12457. NSString *sqlQuery = nil;
  12458. if(product_id==nil)
  12459. if(model_name==nil)
  12460. {
  12461. 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='%@';
  12462. }
  12463. else
  12464. {
  12465. 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='%@';
  12466. }
  12467. else
  12468. 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=%@;
  12469. sqlite3_stmt * statement;
  12470. [ret setValue:@"2" forKey:@"result"];
  12471. [ret setValue:@"3" forKey:@"detail_section_count"];
  12472. // int count=0;
  12473. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12474. {
  12475. if (sqlite3_step(statement) == SQLITE_ROW)
  12476. {
  12477. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12478. char *name = (char*)sqlite3_column_text(statement, 0);
  12479. if(name==nil)
  12480. name="";
  12481. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12482. char *description = (char*)sqlite3_column_text(statement, 1);
  12483. if(description==nil)
  12484. description="";
  12485. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12486. int product_id = sqlite3_column_int(statement, 2);
  12487. char *color = (char*)sqlite3_column_text(statement, 3);
  12488. if(color==nil)
  12489. color="";
  12490. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12491. //
  12492. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12493. // if(legcolor==nil)
  12494. // legcolor="";
  12495. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12496. //
  12497. //
  12498. int availability = sqlite3_column_int(statement, 5);
  12499. //
  12500. int incoming_stock = sqlite3_column_int(statement, 6);
  12501. char *demension = (char*)sqlite3_column_text(statement, 7);
  12502. if(demension==nil)
  12503. demension="";
  12504. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12505. // ,,,,,,,,,
  12506. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12507. if(seat_height==nil)
  12508. seat_height="";
  12509. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12510. char *material = (char*)sqlite3_column_text(statement, 9);
  12511. if(material==nil)
  12512. material="";
  12513. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12514. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12515. if(box_dim==nil)
  12516. box_dim="";
  12517. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12518. char *volume = (char*)sqlite3_column_text(statement, 11);
  12519. if(volume==nil)
  12520. volume="";
  12521. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12522. double weight = sqlite3_column_double(statement, 12);
  12523. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12524. if(model_set==nil)
  12525. model_set="";
  12526. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12527. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12528. if(load_ability==nil)
  12529. load_ability="";
  12530. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12531. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12532. // if(default_category==nil)
  12533. // default_category="";
  12534. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12535. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12536. if(fabric_content==nil)
  12537. fabric_content="";
  12538. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12539. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12540. if(assembling==nil)
  12541. assembling="";
  12542. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12543. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12544. if(made_in==nil)
  12545. made_in="";
  12546. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12547. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12548. if(special_remarks==nil)
  12549. special_remarks="";
  12550. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12551. int stockUcom = sqlite3_column_double(statement, 20);
  12552. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12553. if(product_group==nil)
  12554. product_group="";
  12555. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12556. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12557. // if(fashion_selector==nil)
  12558. // fashion_selector="";
  12559. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12560. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12561. if(selector_field==nil)
  12562. selector_field="";
  12563. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12564. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12565. if(property_field==nil)
  12566. property_field="";
  12567. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12568. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12569. if(packaging==nil)
  12570. packaging="";
  12571. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12572. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12573. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12574. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12575. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12576. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12577. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12578. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12579. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12580. NSString* Availability=nil;
  12581. if(availability>0)
  12582. Availability=[NSString stringWithFormat:@"%d",availability];
  12583. else
  12584. Availability = @"Out of Stock";
  12585. [img_section setValue:Availability forKey:@"Availability"];
  12586. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12587. char *eta = (char*)sqlite3_column_text(statement, 25);
  12588. if(eta==nil)
  12589. eta="";
  12590. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12591. if ([nseta isEqualToString:@"null"]) {
  12592. nseta = @"";
  12593. }
  12594. if (availability <= 0) {
  12595. [img_section setValue:nseta forKey:@"ETA"];
  12596. }
  12597. int item_id = sqlite3_column_int(statement, 26);
  12598. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12599. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12600. NSString* Price=nil;
  12601. if(bLogin==false)
  12602. Price=@"Must Sign in.";
  12603. else
  12604. {
  12605. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12606. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12607. if(price==nil)
  12608. Price=@"No Price.";
  12609. else
  12610. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12611. }
  12612. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12613. [img_section setValue:Price forKey:@"price"];
  12614. [img_section setValue:nsname forKey:@"model_name"];
  12615. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12616. if (order_code) { // 离线order code即so#
  12617. 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];
  12618. __block int cartQTY = 0;
  12619. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12620. cartQTY = sqlite3_column_int(stmt, 0);
  12621. }];
  12622. if (cartQTY > 0) {
  12623. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12624. }
  12625. }
  12626. [ret setObject:img_section forKey:@"img_section"];
  12627. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12628. int detail0_item_count=0;
  12629. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12630. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12631. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12632. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12633. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12634. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12635. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12636. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12637. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12638. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12639. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12640. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12641. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12642. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12643. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12644. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12645. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12646. {
  12647. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12648. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12649. }
  12650. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12651. [detail0_section setValue:@"kv" forKey:@"type"];
  12652. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12653. [ret setObject:detail0_section forKey:@"detail_0"];
  12654. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12655. // [detail1_section setValue:@"detail" forKey:@"target"];
  12656. // [detail1_section setValue:@"popup" forKey:@"action"];
  12657. // [detail1_section setValue:@"content" forKey:@"type"];
  12658. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12659. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12660. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12661. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12662. [ret setObject:detail1_section forKey:@"detail_1"];
  12663. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12664. [detail2_section setValue:@"detail" forKey:@"target"];
  12665. [detail2_section setValue:@"popup" forKey:@"action"];
  12666. [detail2_section setValue:@"content" forKey:@"type"];
  12667. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12668. [detail2_section setValue:@"true" forKey:@"single_row"];
  12669. [detail2_section setValue:@"local" forKey:@"data"];
  12670. [ret setObject:detail2_section forKey:@"detail_2"];
  12671. }
  12672. sqlite3_finalize(statement);
  12673. }
  12674. else
  12675. {
  12676. [ret setValue:@"8" forKey:@"result"];
  12677. }
  12678. // DebugLog(@"count:%d",count);
  12679. [iSalesDB close_db:db];
  12680. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12681. dispatch_async(dispatch_get_main_queue(), ^{
  12682. result(ret);
  12683. });
  12684. });
  12685. }
  12686. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12687. //{
  12688. //
  12689. // UIApplication * app = [UIApplication sharedApplication];
  12690. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12691. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12692. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12693. // sqlite3 *db = [iSalesDB get_db];
  12694. // NSString* collectId=params[@"collectId"];
  12695. //
  12696. //
  12697. //
  12698. //
  12699. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12700. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12701. // [iSalesDB execSql:sqlQuery db:db];
  12702. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12703. // [iSalesDB close_db:db];
  12704. //
  12705. //
  12706. // ret[@"result"]= [NSNumber numberWithInt:2];
  12707. // dispatch_async(dispatch_get_main_queue(), ^{
  12708. //
  12709. // appDelegate.wish_count =count;
  12710. //
  12711. // [appDelegate update_count_mark];
  12712. // result(ret);
  12713. // });
  12714. //
  12715. // });
  12716. //}
  12717. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12718. {
  12719. UIApplication * app = [UIApplication sharedApplication];
  12720. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12721. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12722. [iSalesDB disable_trigger];
  12723. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12724. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12725. [iSalesDB enable_trigger];
  12726. dispatch_async(dispatch_get_main_queue(), ^{
  12727. //
  12728. // NSString* user = [params valueForKey:@"user"];
  12729. //
  12730. // NSString* password = [params valueForKey:@"password"];
  12731. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12732. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12733. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12734. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12735. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12736. [appDelegate update_count_mark];
  12737. appDelegate.can_show_price =false;
  12738. appDelegate.can_see_price =false;
  12739. appDelegate.can_create_portfolio =false;
  12740. appDelegate.can_create_order =false;
  12741. appDelegate.can_cancel_order =false;
  12742. appDelegate.can_set_cart_price =false;
  12743. appDelegate.can_delete_order =false;
  12744. appDelegate.can_submit_order =false;
  12745. appDelegate.can_set_tearsheet_price =false;
  12746. appDelegate.can_update_contact_info = false;
  12747. appDelegate.save_order_logout = false;
  12748. appDelegate.submit_order_logout = false;
  12749. appDelegate.alert_sold_in_quantities = false;
  12750. appDelegate.ipad_perm =nil ;
  12751. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12752. appDelegate.OrderFilter= nil;
  12753. [appDelegate SetSo:nil];
  12754. [appDelegate set_main_button_panel];
  12755. result(ret);
  12756. });
  12757. });
  12758. }
  12759. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12760. {
  12761. // iSalesDB.
  12762. // UIApplication * app = [UIApplication sharedApplication];
  12763. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12764. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12765. [iSalesDB disable_trigger];
  12766. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12767. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12768. [iSalesDB enable_trigger];
  12769. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12770. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12771. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12772. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12773. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12774. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12775. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12776. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12777. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12778. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12779. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12780. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12781. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12782. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12783. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12784. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12785. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12786. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12787. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12788. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12789. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12790. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12791. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12792. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12793. [arr_name addObject:customer_first_name];
  12794. [arr_name addObject:customer_last_name];
  12795. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12796. // default ship from
  12797. 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';";
  12798. __block NSString *cid = @"";
  12799. __block NSString *name = @"";
  12800. __block NSString *ext = @"";
  12801. __block NSString *contact = @"";
  12802. __block NSString *email = @"";
  12803. __block NSString *fax = @"";
  12804. __block NSString *phone = @"";
  12805. sqlite3 *db = [iSalesDB get_db];
  12806. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12807. cid = [self textAtColumn:0 statement:statment];
  12808. name = [self textAtColumn:1 statement:statment];
  12809. ext = [self textAtColumn:2 statement:statment];
  12810. contact = [self textAtColumn:3 statement:statment];
  12811. email = [self textAtColumn:4 statement:statment];
  12812. fax = [self textAtColumn:5 statement:statment];
  12813. phone = [self textAtColumn:6 statement:statment];
  12814. }];
  12815. NSString* so_id = [self get_offline_soid:db];
  12816. if(so_id==nil)
  12817. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12818. 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];
  12819. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12820. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12821. //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'
  12822. //soId
  12823. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12824. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12825. sqlite3_stmt * statement;
  12826. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12827. {
  12828. if (sqlite3_step(statement) == SQLITE_ROW)
  12829. {
  12830. // char *name = (char*)sqlite3_column_text(statement, 1);
  12831. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12832. //ret = sqlite3_column_int(statement, 0);
  12833. char *soId = (char*)sqlite3_column_text(statement, 0);
  12834. if(soId==nil)
  12835. soId="";
  12836. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12837. [ret setValue:nssoId forKey:@"soId"];
  12838. [ret setValue:nssoId forKey:@"orderCode"];
  12839. }
  12840. sqlite3_finalize(statement);
  12841. }
  12842. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12843. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12844. [iSalesDB close_db:db];
  12845. dispatch_async(dispatch_get_main_queue(), ^{
  12846. result(ret);
  12847. });
  12848. });
  12849. }
  12850. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12851. {
  12852. UIApplication * app = [UIApplication sharedApplication];
  12853. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12854. bool can_create_backorder= appDelegate.can_create_backorder;
  12855. NSString* user = appDelegate.user;
  12856. NSString* contact_id = appDelegate.contact_id;
  12857. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12858. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12859. sqlite3 *db = [iSalesDB get_db];
  12860. NSString* orderCode=params[@"orderCode"];
  12861. int sort = [[params objectForKey:@"sort"] intValue];
  12862. NSString *sort_str = @"";
  12863. switch (sort) {
  12864. case 0:{
  12865. sort_str = @"order by c.modify_time desc";
  12866. }
  12867. break;
  12868. case 1:{
  12869. sort_str = @"order by c.modify_time asc";
  12870. }
  12871. break;
  12872. case 2:{
  12873. sort_str = @"order by m.name asc";
  12874. }
  12875. break;
  12876. case 3:{
  12877. sort_str = @"order by m.name desc";
  12878. }
  12879. break;
  12880. case 4:{
  12881. sort_str = @"order by m.description asc";
  12882. }
  12883. break;
  12884. default:
  12885. break;
  12886. }
  12887. 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 ];
  12888. // 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 ];
  12889. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12890. DebugLog(@"offline_login sql:%@",sqlQuery);
  12891. sqlite3_stmt * statement;
  12892. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12893. NSDate *date1 = [NSDate date];
  12894. int count=0;
  12895. int cart_count=0;
  12896. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12897. if ( dbresult== SQLITE_OK)
  12898. {
  12899. while (sqlite3_step(statement) == SQLITE_ROW)
  12900. {
  12901. // NSDate *row_date = [NSDate date];
  12902. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12903. int product_id = sqlite3_column_int(statement, 0);
  12904. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12905. int item_id = sqlite3_column_int(statement, 7);
  12906. NSString* Price=nil;
  12907. if(str_price==nil)
  12908. {
  12909. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12910. // NSDate *price_date = [NSDate date];
  12911. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12912. // DebugLog(@"price time interval");
  12913. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12914. if(price==nil)
  12915. Price=@"No Price.";
  12916. else
  12917. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12918. }
  12919. else
  12920. {
  12921. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12922. }
  12923. double discount = sqlite3_column_double(statement, 2);
  12924. int item_count = sqlite3_column_int(statement, 3);
  12925. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12926. NSString *nsline_note=nil;
  12927. if(line_note!=nil)
  12928. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12929. char *name = (char*)sqlite3_column_text(statement, 5);
  12930. NSString *nsname=nil;
  12931. if(name!=nil)
  12932. nsname= [[NSString alloc]initWithUTF8String:name];
  12933. char *description = (char*)sqlite3_column_text(statement, 6);
  12934. NSString *nsdescription=nil;
  12935. if(description!=nil)
  12936. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12937. int stockUom = sqlite3_column_int(statement, 8);
  12938. int _id = sqlite3_column_int(statement, 9);
  12939. int availability = sqlite3_column_int(statement, 10);
  12940. // NSDate *subtotal_date = [NSDate date];
  12941. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12942. // DebugLog(@"subtotal_date time interval");
  12943. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12944. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12945. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12946. int carton=[bsubtotaljson[@"carton"] intValue];
  12947. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12948. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12949. // NSDate *img_date = [NSDate date];
  12950. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12951. // DebugLog(@"img_date time interval");
  12952. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12953. itemjson[@"model"]=nsname;
  12954. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12955. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12956. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12957. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12958. itemjson[@"check"]=@"true";
  12959. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12960. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12961. itemjson[@"unit_price"]=Price;
  12962. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12963. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12964. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12965. itemjson[@"note"]=nsline_note;
  12966. if (!can_create_backorder) {
  12967. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12968. }
  12969. // NSDate *date2 = [NSDate date];
  12970. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12971. // DebugLog(@"model_bundle time interval");
  12972. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12973. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12974. count++;
  12975. // DebugLog(@"row time interval");
  12976. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12977. }
  12978. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12979. sqlite3_finalize(statement);
  12980. }
  12981. DebugLog(@"request cart total time interval");
  12982. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12983. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12984. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12985. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12986. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12987. ret[@"count"]=[NSNumber numberWithInt:count ];
  12988. ret[@"mode"]=@"Regular Mode";
  12989. [iSalesDB close_db:db];
  12990. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12991. DebugLog(@"general notes :%@",general_note);
  12992. ret[@"general_note"]= general_note;
  12993. dispatch_async(dispatch_get_main_queue(), ^{
  12994. result(ret);
  12995. });
  12996. });
  12997. }
  12998. @end