OLDataProvider.m 711 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862
  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 "AppDelegate.h"
  15. #import "pdfCreator.h"
  16. #import "RASingleton.h"
  17. #import "QRCodeGenerator.h"
  18. #import "config.h"
  19. #import "ImageUtils.h"
  20. #import "RAConvertor.h"
  21. #import "ActiveViewController.h"
  22. #import "RANetwork.h"
  23. #import "RADataProvider.h"
  24. @interface OLDataProvider ()
  25. @end
  26. @implementation OLDataProvider
  27. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  28. {
  29. 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];
  30. NSString* ret=@"No Price.";
  31. sqlite3_stmt * statement;
  32. // int count=0;
  33. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  34. {
  35. if (sqlite3_step(statement) == SQLITE_ROW)
  36. {
  37. char *price = (char*)sqlite3_column_text(statement, 0);
  38. if(price==nil)
  39. price="";
  40. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  41. double dp= [nsprice doubleValue];
  42. ret=[NSString stringWithFormat:@"%.2f",dp];
  43. }
  44. sqlite3_finalize(statement);
  45. }
  46. return ret;
  47. // // [iSalesDB close_db:db];
  48. //
  49. // return nil;
  50. //// if(dprice==DBL_MAX)
  51. //// ret= nil;
  52. //// else
  53. //// ret= [NSNumber numberWithDouble:dprice];
  54. //// return ret;
  55. }
  56. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  57. {
  58. NSString* ret= nil;
  59. NSString *sqlQuery = nil;
  60. // 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
  61. 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];
  62. sqlite3_stmt * statement;
  63. // int count=0;
  64. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  65. {
  66. if (sqlite3_step(statement) == SQLITE_ROW)
  67. {
  68. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  69. if(imgurl==nil)
  70. imgurl="";
  71. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  72. ret=nsimgurl;
  73. }
  74. sqlite3_finalize(statement);
  75. }
  76. else
  77. {
  78. ret=@"";
  79. }
  80. // [iSalesDB close_db:db];
  81. // DebugLog(@"data string: %@",ret );
  82. return ret;
  83. }
  84. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  85. {
  86. // assert(params[@"contact_id"]!=nil);
  87. assert(params[@"user"]!=nil);
  88. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  89. //生成portfolio pdf需要的数据
  90. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  91. data[@"npd_url"]=COMPANY_WEB;
  92. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  93. [formatter setDateFormat:@"MMMM yyyy"];
  94. NSString* date = [formatter stringFromDate:[NSDate date]];
  95. data[@"create_month"]=date;
  96. data[@"company_name"]=COMPANY_FULL_NAME;
  97. data[@"catalog_name"]=params[@"catalog_name"];
  98. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  99. data[@"TOTAL_PAGE"]=0;
  100. data[@"CURRENT_PAGE"]=0;
  101. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  102. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  103. bool show_stockout=[params[@"show_stockout"] boolValue];
  104. NSString *sql = nil;
  105. // v1.90 more color
  106. NSString *group_by = @"";
  107. if(values==nil)
  108. {
  109. if (show_group_by) {
  110. group_by = @"group by b.product_group ";
  111. }
  112. 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];
  113. }
  114. else
  115. {
  116. if (show_group_by) {
  117. group_by = @"group by product_group ";
  118. }
  119. 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];
  120. }
  121. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  122. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  123. sqlite3 *db = [iSalesDB get_db];
  124. // NSDictionary *resultDic =
  125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  126. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  127. int product_id = sqlite3_column_int(stmt, 0);
  128. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  129. NSString *name = [self textAtColumn:1 statement:stmt];
  130. NSString *description = [self textAtColumn:2 statement:stmt];
  131. double price = sqlite3_column_double(stmt, 3);
  132. NSString *s_price = [self textAtColumn:3 statement:stmt];
  133. double discount = sqlite3_column_double(stmt,4);
  134. int qty = sqlite3_column_int(stmt, 5);
  135. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  136. int is_percentage = sqlite3_column_int(stmt, 6);
  137. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  138. int item_id = sqlite3_column_int(stmt, 7);
  139. // int fashion_id = sqlite3_column_int(stmt, 8);
  140. NSString *line_note = [self textAtColumn:9 statement:stmt];
  141. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  142. double percent = sqlite3_column_double(stmt, 10);
  143. // int portfolio_id = sqlite3_column_int(stmt, 11);
  144. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  145. int availability = sqlite3_column_int(stmt, 12);
  146. NSString *color = [self textAtColumn:13 statement:stmt];
  147. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  148. NSString *demension = [self textAtColumn:15 statement:stmt];
  149. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  150. NSString *material = [self textAtColumn:17 statement:stmt];
  151. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  152. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  153. double volume = sqlite3_column_double(stmt, 19);
  154. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  155. double weight = sqlite3_column_double(stmt, 20);
  156. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  157. int model_set = sqlite3_column_int(stmt, 21);
  158. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  159. double load_ability = sqlite3_column_double(stmt, 22);
  160. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  161. NSString *assembling = [self textAtColumn:24 statement:stmt];
  162. NSString *made_in = [self textAtColumn:25 statement:stmt];
  163. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  164. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  165. float bprice=0;
  166. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  167. for(int b=0;b< [bundle[@"count"] intValue];b++)
  168. {
  169. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  170. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  171. }
  172. if(gprice!=nil)
  173. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  174. if(s_percent.length==0||is_percentage==0)
  175. {
  176. percent=100.0;
  177. if([s_qty isEqualToString:@"null"])
  178. qty=availability;
  179. }
  180. else
  181. {
  182. qty=availability;
  183. }
  184. qty=qty*percent/100+0.5;
  185. for(int i=0;i<[values[@"count"] intValue];i++)
  186. {
  187. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  188. if([valueitem[@"product_id"] intValue]==product_id)
  189. {
  190. line_note=valueitem[@"line_note"];
  191. qty=[valueitem[@"available_qty"] intValue];
  192. if([[valueitem allKeys] containsObject:@"available_qty"])
  193. s_qty=[NSString stringWithFormat:@"%d",qty];
  194. else
  195. s_qty=@"null";
  196. if(valueitem[@"regular_price"]!=nil)
  197. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  198. s_price=valueitem[@"special_price"];
  199. price=[valueitem[@"special_price"] floatValue];
  200. discount=0;
  201. // v1.90
  202. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  203. }
  204. }
  205. if(is_percentage==0&&qty==0&&!show_stockout)
  206. {
  207. return;
  208. }
  209. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  210. {
  211. return;
  212. }
  213. NSString* set_price=@"";
  214. if([params[@"entered_price"] boolValue])
  215. {
  216. if (s_price==nil ) {
  217. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  218. }
  219. else
  220. {
  221. if(price*(1-discount/100.0)!=gprice.floatValue)
  222. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  223. }
  224. }
  225. NSString* get_price=@"";
  226. {
  227. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  228. // DebugLog(@"price time interval");
  229. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  230. if(gprice==nil)
  231. get_price=@"Price:No Price.";
  232. else
  233. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  234. }
  235. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  236. // if ([qty_null isEqualToString:@"null"]) {
  237. // qty=availability;
  238. // }
  239. //
  240. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  241. // if ([qty_percent_null isEqualToString:@"null"]) {
  242. // percentage=1;
  243. // }
  244. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  245. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  246. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  247. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  248. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  249. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  250. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  251. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  252. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  253. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  254. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  255. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  256. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  257. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  258. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  259. if([params[@"availability"] boolValue]==false)
  260. str_availability=@"";
  261. else
  262. {
  263. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  264. str_availability=@"<b>Availability:</b> In Production";
  265. }
  266. if([params[@"color"] boolValue]==false || color.length==0 )
  267. str_color=@"";
  268. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  269. str_model_set=@"";
  270. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  271. str_legcolor=@"";
  272. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  273. str_demension=@"";
  274. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  275. str_seat_height=@"";
  276. if([params[@"material"] boolValue]==false || material.length==0 )
  277. str_meterial=@"";
  278. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  279. str_box_dim=@"";
  280. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  281. str_volume=@"";
  282. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  283. str_weight=@"";
  284. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  285. str_load_ability=@"";
  286. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  287. str_fabric_content=@"";
  288. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  289. str_assembling=@"";
  290. if([params[@"made"] boolValue]==false || made_in.length==0 )
  291. str_made_in=@"";
  292. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  293. str_line_note=@"";
  294. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  295. [arr_detail addObject:str_availability];
  296. [arr_detail addObject:str_color];
  297. [arr_detail addObject:str_model_set];
  298. [arr_detail addObject:str_legcolor];
  299. [arr_detail addObject:str_demension];
  300. [arr_detail addObject:str_seat_height];
  301. [arr_detail addObject:str_meterial];
  302. [arr_detail addObject:str_box_dim];
  303. [arr_detail addObject:str_volume];
  304. [arr_detail addObject:str_weight];
  305. [arr_detail addObject:str_load_ability];
  306. [arr_detail addObject:str_fabric_content];
  307. [arr_detail addObject:str_assembling];
  308. [arr_detail addObject:str_made_in];
  309. [arr_detail addObject:str_line_note];
  310. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  311. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  312. //model image;
  313. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  314. // qr image
  315. NSString* qrpath=nil;
  316. if([params[@"show_barcode"] boolValue])
  317. {
  318. NSString* temp = NSTemporaryDirectory();
  319. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  320. qrpath=[temp stringByAppendingPathComponent:filename];
  321. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  322. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  323. }
  324. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  325. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  326. item[@"img"]=img_path;
  327. item[@"img_qr"]=qrpath;
  328. item[@"special_price"]=set_price;
  329. item[@"price"]=get_price;
  330. item[@"name"]=name;
  331. item[@"description"]=description;
  332. item[@"detail"]=detail;
  333. item[@"img"]=img_path;
  334. if (show_group_by && more_color_int != 0) {
  335. item[@"more_color"] = more_color_path;
  336. }
  337. // @{
  338. // //@"linenotes": line_note,
  339. //// @"product_id": product_id_string,
  340. //// @"available_qty": @(qty),
  341. //// @"available_percent" : @(percent),
  342. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  343. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  344. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"":
  352. // }.mutableCopy;
  353. // if (percentage) {
  354. // [item removeObjectForKey:@"available_qty"];
  355. // } else {
  356. // [item removeObjectForKey:@"available_percent"];
  357. // }
  358. //
  359. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  360. // if ([qty_null isEqualToString:@"null"]) {
  361. // [item removeObjectForKey:@"available_qty"];
  362. // }
  363. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  364. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  365. }];
  366. [iSalesDB close_db:db];
  367. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  368. // [dic setValue:@"" forKey:@"email_content"];
  369. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  370. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  371. //
  372. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  373. // cell[@"count"]=[NSNumber numberWithInt:10];
  374. grid[@"cell0"]=dic;
  375. data[@"grid0"]=grid;
  376. return data;
  377. }
  378. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  379. {
  380. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  381. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  382. NSString *cache_folder=[paths objectAtIndex:0];
  383. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  384. BOOL bdir=NO;
  385. NSFileManager* fileManager = [NSFileManager defaultManager];
  386. if(PDF_DEBUG)
  387. {
  388. NSData *data = [NSData dataWithContentsOfFile:default_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  393. {
  394. NSError * error=nil;
  395. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  396. {
  397. return nil;
  398. }
  399. }
  400. NSData *data = [NSData dataWithContentsOfFile:template_path];
  401. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  402. return ret;
  403. }
  404. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  405. {
  406. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  407. 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 ];
  408. DebugLog(@"offline_login sql:%@",sqlQuery);
  409. sqlite3_stmt * statement;
  410. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  411. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  412. {
  413. if (sqlite3_step(statement) == SQLITE_ROW)
  414. {
  415. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  416. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  417. // int can_show_price = sqlite3_column_int(statement, 0);
  418. // int can_see_price = sqlite3_column_int(statement, 1);
  419. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  420. // if(contact_id==nil)
  421. // contact_id="";
  422. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  423. // int user_type = sqlite3_column_int(statement, 3);
  424. // int can_cancel_order = sqlite3_column_int(statement, 4);
  425. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  426. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  427. // int can_delete_order = sqlite3_column_int(statement, 7);
  428. // int can_submit_order = sqlite3_column_int(statement, 8);
  429. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  430. // int can_create_order = sqlite3_column_int(statement, 10);
  431. // char *mode = (char*)sqlite3_column_text(statement, 11);
  432. // if(mode==nil)
  433. // mode="";
  434. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  435. // char *username = (char*)sqlite3_column_text(statement, 12);
  436. // if(username==nil)
  437. // username="";
  438. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  439. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  440. char *first_name = (char*)sqlite3_column_text(statement, 14);
  441. if(first_name==nil)
  442. first_name="";
  443. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  444. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  445. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  446. // [header setValue:nscontact_id forKey:@"contact_id"];
  447. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  448. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  449. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  450. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  451. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  452. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  454. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  455. //
  456. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  457. //
  458. // [header setValue:nsusername forKey:@"username"];
  459. //
  460. // NSError* error=nil;
  461. //
  462. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  463. // [header setValue:statusFilter forKey:@"statusFilter"];
  464. //
  465. //
  466. //
  467. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  468. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  469. //
  470. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  471. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  472. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  473. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  474. //
  475. // [ret setObject:header forKey:@"header"];
  476. [ret setValue:nsfirst_name forKey:@"first_name"];
  477. }
  478. sqlite3_finalize(statement);
  479. }
  480. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  481. return ret;
  482. }
  483. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  484. {
  485. assert(user!=nil);
  486. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  487. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  488. data[@"npd_url"]=COMPANY_WEB;
  489. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  490. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  491. NSString* date = [formatter stringFromDate:[NSDate date]];
  492. data[@"print_date"]=date;
  493. data[@"company_name"]=COMPANY_FULL_NAME;
  494. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  495. data[@"TOTAL_PAGE"]=0;
  496. data[@"CURRENT_PAGE"]=0;
  497. // [formatter setDateFormat:@"MM/dd/yyyy"];
  498. // date = [formatter stringFromDate:[NSDate date]];
  499. // data[@"creat_date"]=date;
  500. NSString* temp = NSTemporaryDirectory();
  501. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  502. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  503. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  504. // bool bwrite=
  505. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  506. data[@"img_barcode"]=qrpath;
  507. data[@"so_id"]=order_code;
  508. data[@"company"]=COMPANY_FULL_NAME;
  509. // 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];
  510. 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];
  511. sqlite3 *db = [iSalesDB get_db];
  512. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  513. sqlite3_stmt * statement;
  514. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  515. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  516. row0[@"count"]=[NSNumber numberWithInt:1];
  517. datasource[@"row0"]=row0;
  518. NSString* print_status=@"";
  519. NSString* order_type=@"Offline Quote";
  520. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  521. {
  522. if (sqlite3_step(statement) == SQLITE_ROW)
  523. {
  524. int erpOrderStatus = sqlite3_column_int(statement, 0);
  525. int status = sqlite3_column_int(statement, 26);
  526. if(status==2)
  527. {
  528. switch (erpOrderStatus) {
  529. case 10:
  530. // order_type=@"Quoted Order";
  531. print_status=@"Quote";
  532. break;
  533. case 11:
  534. print_status=@"Booked";
  535. break;
  536. case 12:
  537. print_status=@"Picked";
  538. break;
  539. case 13:
  540. print_status=@"Shipped";
  541. break;
  542. case 14:
  543. print_status=@"Closed";
  544. break;
  545. case 15:
  546. print_status=@"Canceled";
  547. break;
  548. case 16:
  549. print_status=@"All";
  550. break;
  551. case 18:
  552. print_status=@"Processing";
  553. break;
  554. default:
  555. break;
  556. }
  557. }
  558. else{
  559. switch (status) {
  560. case 0:
  561. {
  562. print_status=@"Quote";
  563. break;
  564. }
  565. case 1:
  566. {
  567. print_status=@"Saved";
  568. break;
  569. }
  570. case 3:
  571. {
  572. //canceled
  573. print_status=@"Canceled";
  574. break;
  575. }
  576. default:
  577. break;
  578. }
  579. }
  580. NSString* sender_name = [self textAtColumn:1 statement:statement];
  581. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  582. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  583. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  584. NSString* sender_email = [self textAtColumn:5 statement:statement];
  585. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  586. NSString* billing_name = [self textAtColumn:7 statement:statement];
  587. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  588. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  589. if(billing_phone.length>0)
  590. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  591. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  592. if(billing_fax.length>0)
  593. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  594. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  595. [billto_arr0 addObject:billing_phone];
  596. [billto_arr0 addObject:billing_fax];
  597. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  598. NSString* billing_email = [self textAtColumn:11 statement:statement];
  599. if(billing_email.length>0)
  600. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  601. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  602. if(billing_contact.length>0)
  603. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  604. NSString* receive_name = [self textAtColumn:12 statement:statement];
  605. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  606. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  607. if(receive_phone.length>0)
  608. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  609. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  610. if(receive_fax.length>0)
  611. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  612. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  613. [receive_arr0 addObject:receive_phone];
  614. [receive_arr0 addObject:receive_fax];
  615. NSString* receive_email = [self textAtColumn:16 statement:statement];
  616. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  617. if(receive_email.length>0)
  618. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  619. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  620. if(receive_contact.length>0)
  621. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  622. NSString* poNumber = [self textAtColumn:17 statement:statement];
  623. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  624. NSString* carrier = [self textAtColumn:19 statement:statement];
  625. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  626. NSString* terms = [self textAtColumn:21 statement:statement];
  627. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  628. NSString* port_destination = [self textAtColumn:23 statement:statement];
  629. NSString* etd = [self textAtColumn:24 statement:statement];
  630. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  631. if(sign_picpath.length==0)
  632. sign_picpath=nil;
  633. NSString* create_date = [self textAtColumn:27 statement:statement];
  634. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  635. NSString* general_notes=[self textAtColumn:33 statement:statement];
  636. NSString *customerID = [self textAtColumn:34 statement:statement];
  637. if (customerID.length == 0) {
  638. customerID = nil;
  639. }
  640. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  641. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  642. if(general_notes.length>0)
  643. {
  644. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  645. row2[@"item_0"]=@{@"g_notes":general_notes };
  646. row2[@"count"]=[NSNumber numberWithInt:1];
  647. datasource[@"row2"]=row2;
  648. }
  649. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  650. [billto_arr addObject:billing_ext];
  651. [billto_arr addObject:billing_pf];
  652. [billto_arr addObject:billing_contact];
  653. [billto_arr addObject:billing_email];
  654. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  655. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  656. [receive_arr addObject:receive_ext];
  657. [receive_arr addObject:receive_pf];
  658. [receive_arr addObject:receive_contact];
  659. [receive_arr addObject:receive_email];
  660. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  661. float shipping = sqlite3_column_double(statement, 28);
  662. float lift_gate_value = sqlite3_column_double(statement, 29);
  663. float handling_fee_value = sqlite3_column_double(statement, 30);
  664. data[@"company"]=sender_name;
  665. data[@"shipfrom_addr"]=sender_ext;
  666. data[@"shipfrom_phone"]=sender_phone;
  667. data[@"shipfrom_fax"]=sender_fax;
  668. data[@"shipfrom_email"]=sender_email;
  669. data[@"warehouse_name"]=warehouse_name;
  670. data[@"bill_to_name"]=billing_name;
  671. data[@"bill_to_ext"]=billto;
  672. data[@"ship_to_name"]=receive_name;
  673. data[@"ship_to_ext"]=receive;
  674. data[@"po_no"]=poNumber;
  675. data[@"rep"]=sales_rep;
  676. data[@"e_schdule"]=etd;
  677. data[@"sales_terms"]=sales_terms;
  678. data[@"port_destination"]=port_destination;
  679. data[@"terms"]=terms;
  680. data[@"carrier"]=carrier;
  681. data[@"vendor_no"]=vendor_no;
  682. data[@"customer_sign"]=sign_picpath;
  683. data[@"print_status"]=print_status;
  684. data[@"create_date"]=create_date;
  685. 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];;
  686. sqlite3_stmt * statement_cart;
  687. // NSDate *date1 = [NSDate date];
  688. int count=0;
  689. // int cart_count=0;
  690. float credits=0;
  691. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  692. if ( dbresult== SQLITE_OK)
  693. {
  694. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  695. int row1count=0;
  696. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  697. {
  698. // NSDate *row_date = [NSDate date];
  699. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  700. // int product_id = sqlite3_column_int(statement_cart, 0);
  701. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  702. int item_id = sqlite3_column_int(statement_cart, 7);
  703. double discount = sqlite3_column_double(statement_cart, 2);
  704. NSString* Price=nil;
  705. if(str_price==nil)
  706. {
  707. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  708. // NSDate *price_date = [NSDate date];
  709. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  710. // DebugLog(@"price time interval");
  711. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  712. if(price==nil)
  713. Price=@"No Price.";
  714. else
  715. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  716. }
  717. else
  718. {
  719. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  720. }
  721. float dunitprice=0;
  722. int item_count = sqlite3_column_int(statement_cart, 3);
  723. NSString* Amount=@"";
  724. if(![Price isEqualToString:@"No Price."])
  725. {
  726. dunitprice=[Price floatValue];
  727. dunitprice=dunitprice* (1-discount/100.0);
  728. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  729. credits+=dunitprice*item_count;
  730. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  731. }
  732. else
  733. {
  734. Price=@"";
  735. }
  736. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  737. // NSString *nsline_note=nil;
  738. // if(line_note!=nil)
  739. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  740. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  741. NSString *nsname=nil;
  742. if(name!=nil)
  743. nsname= [[NSString alloc]initWithUTF8String:name];
  744. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  745. NSString *nsdescription=nil;
  746. if(description!=nil)
  747. nsdescription= [[NSString alloc]initWithUTF8String:description];
  748. // int stockUom = sqlite3_column_int(statement_cart, 8);
  749. // int _id = sqlite3_column_int(statement_cart, 9);
  750. // NSDate *subtotal_date = [NSDate date];
  751. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  752. // DebugLog(@"subtotal_date time interval");
  753. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  754. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  755. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  756. // int carton=[bsubtotaljson[@"carton"] intValue];
  757. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  758. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  759. // NSDate *img_date = [NSDate date];
  760. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  761. // DebugLog(@"img_date time interval");
  762. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  763. itemjson[@"model"]=nsname;
  764. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  765. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  766. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  767. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  768. // itemjson[@"check"]=@"true";
  769. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  770. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  771. itemjson[@"unit_price"]=Price;
  772. itemjson[@"amount"]=Amount;
  773. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  774. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  775. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  776. // itemjson[@"note"]=nsline_note;
  777. // NSDate *date2 = [NSDate date];
  778. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  779. // DebugLog(@"model_bundle time interval");
  780. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  781. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  782. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  783. row1count++;
  784. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  785. for(int c=0;c<[combine[@"count"] intValue];c++)
  786. {
  787. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  788. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  789. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  790. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  791. credits+=combine_amount;
  792. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  793. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  794. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  795. row1count++;
  796. }
  797. count++;
  798. // DebugLog(@"row time interval");
  799. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  800. }
  801. row1[@"count"]=[NSNumber numberWithInt:row1count];
  802. datasource[@"row1"]=row1;
  803. data[@"table_model"]=datasource;
  804. sqlite3_finalize(statement_cart);
  805. }
  806. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  807. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  808. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  809. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  810. // if(credits>0)
  811. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  812. // else
  813. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  814. if(shipping>0)
  815. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  816. else
  817. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  818. if(lift_gate_value>0)
  819. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  820. else
  821. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  822. if(handling_fee_value>0)
  823. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  824. else
  825. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  826. float total = credits+shipping+lift_gate_value+handling_fee_value;
  827. if(total>0)
  828. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  829. else
  830. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  831. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  832. fee[@"row0"]=fee_row0;
  833. data[@"table_fee"]=fee;
  834. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  835. // ret[@"sign_url"] = sign_url;
  836. //
  837. // customer_contact = [self textAtColumn:52 statement:statement];
  838. // customer_email = [self textAtColumn:53 statement:statement];
  839. // customer_phone = [self textAtColumn:54 statement:statement];
  840. // customer_fax = [self textAtColumn:55 statement:statement];
  841. //
  842. //
  843. // int offline_edit=sqlite3_column_int(statement, 56);
  844. //
  845. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  846. //
  847. // char *soid = (char*)sqlite3_column_text(statement, 1);
  848. // if(soid==nil)
  849. // soid= "";
  850. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  851. // // so#
  852. // ret[@"so#"] = nssoid;
  853. //
  854. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  855. // if(poNumber==nil)
  856. // poNumber= "";
  857. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  858. //
  859. //
  860. //
  861. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  862. // if(create_time==nil)
  863. // create_time= "";
  864. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  865. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  866. //
  867. // int status = sqlite3_column_int(statement, 4);
  868. // int erpStatus = sqlite3_column_int(statement, 49);
  869. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  870. //
  871. // // status
  872. // if (status > 1 && status != 3) {
  873. // status = erpStatus;
  874. // } else if (status == 3) {
  875. // status = 15;
  876. // }
  877. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  878. // ret[@"order_status"] = nsstatus;
  879. //
  880. //
  881. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  882. // if(company_name==nil)
  883. // company_name= "";
  884. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  885. // // company name
  886. // ret[@"company_name"] = nscompany_name;
  887. //
  888. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  889. // if(customer_contact==nil)
  890. // customer_contact= "";
  891. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  892. //
  893. //
  894. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  895. // if(addr_1==nil)
  896. // addr_1="";
  897. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  898. //
  899. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  900. // if(addr_2==nil)
  901. // addr_2="";
  902. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  903. //
  904. //
  905. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  906. // if(addr_3==nil)
  907. // addr_3="";
  908. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  909. //
  910. //
  911. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  912. // if(addr_4==nil)
  913. // addr_4="";
  914. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  915. //
  916. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  917. // [arr_addr addObject:nsaddr_1];
  918. // [arr_addr addObject:nsaddr_2];
  919. // [arr_addr addObject:nsaddr_3];
  920. // [arr_addr addObject:nsaddr_4];
  921. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  922. //
  923. //
  924. // char *logist = (char*)sqlite3_column_text(statement, 11);
  925. // if(logist==nil)
  926. // logist= "";
  927. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  928. // if (status == -11 || status == 10 || status == 11) {
  929. // nslogist = [self textAtColumn:59 statement:statement];
  930. // };
  931. //
  932. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  933. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  934. // shipping = @"Shipping To Be Quoted";
  935. // } else {
  936. // shippingFee = sqlite3_column_double(statement, 12);
  937. // }
  938. //
  939. // // Shipping
  940. // ret[@"Shipping"] = shipping;
  941. //
  942. // int have_lift_gate = sqlite3_column_int(statement, 17);
  943. // lift_gate = sqlite3_column_double(statement, 13);
  944. // // Liftgate Fee(No loading dock)
  945. // if (!have_lift_gate) {
  946. // lift_gate = 0;
  947. // }
  948. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  949. // if (sqlite3_column_int(statement, 57)) {
  950. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  951. // }
  952. //
  953. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  954. // if(general_notes==nil)
  955. // general_notes= "";
  956. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  957. //
  958. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  959. // if(internal_notes==nil)
  960. // internal_notes= "";
  961. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  962. //
  963. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  964. // if(payment_type==nil)
  965. // payment_type= "";
  966. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  967. //
  968. //
  969. //
  970. // // order info
  971. // orderinfo = [self textFileName:@"order_info.html"];
  972. //
  973. //
  974. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  975. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  978. //
  979. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  986. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  987. //
  988. // NSString *payment = nil;
  989. // if([nspayment_type isEqualToString:@"Credit Card"])
  990. // {
  991. // payment = [self textFileName:@"creditcardpayment.html"];
  992. //
  993. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  994. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  995. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  996. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  997. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  998. // NSString *card_type = [self textAtColumn:42 statement:statement];
  999. // if (card_type.length > 0) { // 显示星号
  1000. // card_type = @"****";
  1001. // }
  1002. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1003. // if (card_number.length > 0 && card_number.length > 4) {
  1004. // for (int i = 0; i < card_number.length - 4; i++) {
  1005. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1006. // }
  1007. // } else {
  1008. // card_number = @"";
  1009. // }
  1010. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1011. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1012. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1013. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1014. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1015. // card_expiration = @"****";
  1016. // }
  1017. //
  1018. //
  1019. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1020. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1021. //
  1022. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1023. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1024. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1025. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1026. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1027. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1028. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1029. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1030. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1031. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1032. //
  1033. //
  1034. // }
  1035. // else
  1036. // {
  1037. // payment=[self textFileName:@"normalpayment.html"];
  1038. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1039. // }
  1040. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1041. //
  1042. // ret[@"result"]= [NSNumber numberWithInt:2];
  1043. //
  1044. // // more info
  1045. // moreInfo = [self textFileName:@"more_info.html"];
  1046. //
  1047. //
  1048. // // ShipToCompany_or_&nbsp
  1049. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1051. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1053. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1055. //
  1056. //
  1057. // // ShipFromCompany_or_&nbsp
  1058. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1060. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1062. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1064. //
  1065. //
  1066. // // FreightBillToCompany_or_&nbsp
  1067. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1069. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1071. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1073. //
  1074. //
  1075. // // MerchandiseBillToCompany_or_&nbsp
  1076. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1077. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1078. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1079. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1080. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1081. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1082. //
  1083. //
  1084. // // ReturnToCompany_or_&nbsp
  1085. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1086. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1087. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1088. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1089. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1090. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1091. // //
  1092. //
  1093. // // DebugLog(@"more info : %@",moreInfo);
  1094. //
  1095. // // handling fee
  1096. // handlingFee = sqlite3_column_double(statement, 33);
  1097. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1098. // if (sqlite3_column_int(statement, 58)) {
  1099. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1100. // }
  1101. // //
  1102. //
  1103. // // customer info
  1104. // customerID = [self textAtColumn:36 statement:statement];
  1105. //
  1106. // // mode
  1107. // ret[@"mode"] = appDelegate.mode;
  1108. //
  1109. // // model_count
  1110. // ret[@"model_count"] = @(0);
  1111. }
  1112. sqlite3_finalize(statement);
  1113. }
  1114. [iSalesDB close_db:db];
  1115. data[@"order_type"]=order_type;
  1116. /*
  1117. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1118. // "customer_email" = "Shui Hu";
  1119. // "customer_fax" = "";
  1120. // "customer_first_name" = F;
  1121. // "customer_last_name" = L;
  1122. // "customer_name" = ",da He Xiang Dong Liu A";
  1123. // "customer_phone" = "Hey Xuan Feng";
  1124. contactInfo[@"customer_phone"] = customer_phone;
  1125. contactInfo[@"customer_fax"] = customer_fax;
  1126. contactInfo[@"customer_email"] = customer_email;
  1127. NSString *first_name = @"";
  1128. NSString *last_name = @"";
  1129. if ([customer_contact isEqualToString:@""]) {
  1130. } else if ([customer_contact containsString:@" "]) {
  1131. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1132. first_name = [customer_contact substringToIndex:first_space_index];
  1133. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1134. }
  1135. contactInfo[@"customer_first_name"] = first_name;
  1136. contactInfo[@"customer_last_name"] = last_name;
  1137. ret[@"customerInfo"] = contactInfo;
  1138. // models
  1139. if (nssoid) {
  1140. __block double TotalCuft = 0;
  1141. __block double TotalWeight = 0;
  1142. __block int TotalCarton = 0;
  1143. __block double allItemPrice = 0;
  1144. 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];
  1145. sqlite3 *db1 = [iSalesDB get_db];
  1146. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1147. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1148. int product_id = sqlite3_column_int(stmt, 0);
  1149. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1150. int item_id = sqlite3_column_int(stmt, 7);
  1151. NSString* Price=nil;
  1152. if(str_price==nil)
  1153. {
  1154. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1155. if(price==nil)
  1156. Price=@"No Price.";
  1157. else
  1158. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1159. }
  1160. else
  1161. {
  1162. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1163. }
  1164. double discount = sqlite3_column_double(stmt, 2);
  1165. int item_count = sqlite3_column_int(stmt, 3);
  1166. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1167. NSString *nsline_note=nil;
  1168. if(line_note!=nil)
  1169. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1170. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1171. // NSString *nsname = nil;
  1172. // if(name!=nil)
  1173. // nsname= [[NSString alloc]initWithUTF8String:name];
  1174. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1175. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1176. // NSString *nsdescription=nil;
  1177. // if(description!=nil)
  1178. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1179. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1180. // int stockUom = sqlite3_column_int(stmt, 8);
  1181. // int _id = sqlite3_column_int(stmt, 9);
  1182. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1183. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1184. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1185. int carton=[bsubtotaljson[@"carton"] intValue];
  1186. TotalCuft += cuft;
  1187. TotalWeight += weight;
  1188. TotalCarton += carton;
  1189. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1190. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1191. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1192. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1193. itemjson[@"note"]=nsline_note;
  1194. itemjson[@"origin_price"] = Price;
  1195. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1196. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1197. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1198. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1199. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1200. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1201. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1202. if(itemjson[@"combine"] != nil)
  1203. {
  1204. // int citem=0;
  1205. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1206. for(int bc=0;bc<bcount;bc++)
  1207. {
  1208. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1209. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1210. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1211. subTotal += uprice * modulus * item_count;
  1212. }
  1213. }
  1214. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1215. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1216. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1217. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1218. allItemPrice += subTotal;
  1219. }];
  1220. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1222. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1223. // payments/Credits
  1224. // payments_and_credist = sqlite3_column_double(statement, 34);
  1225. payments_and_credist = allItemPrice;
  1226. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1227. // // total
  1228. // totalPrice = sqlite3_column_double(statement, 35);
  1229. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1230. } else {
  1231. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1233. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1234. // payments/Credits
  1235. payments_and_credist = 0;
  1236. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1237. }
  1238. // total
  1239. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1240. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1241. ret[@"order_info"]= orderinfo;
  1242. ret[@"more_order_info"] = moreInfo;
  1243. return [RAConvertor dict2data:ret];
  1244. */
  1245. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1246. return data;
  1247. }
  1248. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1249. {
  1250. assert(params[@"user"]!=nil);
  1251. NSString* template_file=nil;
  1252. switch([params[@"thumb"] intValue])
  1253. {
  1254. case 0:
  1255. template_file= @"so.json";
  1256. break;
  1257. case 1:
  1258. template_file= @"so_thumb.json";
  1259. break;
  1260. default:
  1261. template_file= @"so.json";
  1262. }
  1263. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1264. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1265. if(data[@"table_model"][@"row2"]==nil)
  1266. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1267. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1268. DebugLog(@"%@",file);
  1269. // return nil;
  1270. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1271. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1272. if (file) {
  1273. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1274. [dic setObject:file forKey:@"pdf_path"];
  1275. dic[@"isLocalFile"]=@"true";
  1276. return [RAConvertor dict2data:dic];
  1277. }
  1278. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1279. dic[@"isLocalFile"]=@"true";
  1280. return [RAConvertor dict2data:dic];
  1281. }
  1282. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1283. {
  1284. // assert(params[@"contact_id"]!=nil);
  1285. // assert(params[@"user"]!=nil);
  1286. if(params[@"user"]==nil)
  1287. {
  1288. AppDelegate *appDelegate = nil;
  1289. //some UI methods ej
  1290. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1291. if(appDelegate.user!=nil)
  1292. [params setValue:appDelegate.user forKey:@"user"];
  1293. }
  1294. NSString* template_file=nil;
  1295. switch([params[@"pdf_style"] intValue])
  1296. {
  1297. case 0:
  1298. template_file= @"portfolio_2x3.json";
  1299. break;
  1300. case 1:
  1301. template_file= @"portfolio_3x2.json";
  1302. break;
  1303. default:
  1304. template_file= @"portfolio_3x2.json";
  1305. }
  1306. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1307. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1308. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1309. DebugLog(@"%@",file);
  1310. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1311. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1312. if (file) {
  1313. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1314. [dic setObject:file forKey:@"pdf_path"];
  1315. dic[@"isLocalFile"]=@"true";
  1316. return [RAConvertor dict2data:dic];
  1317. }
  1318. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1319. dic[@"isLocalFile"]=@"true";
  1320. return [RAConvertor dict2data:dic];
  1321. }
  1322. +(NSString*) get_offline_soid:(sqlite3*)db
  1323. {
  1324. NSString* soid=nil;
  1325. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1326. [formatter setDateFormat:@"yyMMdd"];
  1327. NSString* date = [formatter stringFromDate:[NSDate date]];
  1328. for(int i=1;i<999;i++)
  1329. {
  1330. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1331. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1332. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1333. if(count==0)
  1334. return soid;
  1335. }
  1336. return nil;
  1337. }
  1338. +(NSArray*) enumOfflineOrder
  1339. {
  1340. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1341. sqlite3 *db = [iSalesDB get_db];
  1342. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1343. sqlite3_stmt * statement;
  1344. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1345. if ( dbresult== SQLITE_OK)
  1346. {
  1347. while (sqlite3_step(statement) == SQLITE_ROW)
  1348. {
  1349. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1350. NSString* nsso_id=nil;
  1351. if(so_id!=nil)
  1352. {
  1353. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1354. }
  1355. [ret addObject:nsso_id];
  1356. }
  1357. sqlite3_finalize(statement);
  1358. }
  1359. [iSalesDB close_db:db];
  1360. return ret;
  1361. }
  1362. //+(void) uploadFile:(NSString*) file
  1363. //{
  1364. // NSData* data = [NSData dataWithContentsOfFile: file];
  1365. // UIApplication * app = [UIApplication sharedApplication];
  1366. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1367. //
  1368. //
  1369. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1370. //
  1371. //
  1372. //
  1373. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1374. //
  1375. // if(appDelegate.user!=nil)
  1376. // [params setValue:appDelegate.user forKey:@"user"];
  1377. // // if(appDelegate.contact_id!=nil)
  1378. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1379. // if(appDelegate.password!=nil)
  1380. // [params setValue:appDelegate.password forKey:@"password"];
  1381. //
  1382. //
  1383. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1384. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1385. // } error:nil];
  1386. //
  1387. //
  1388. //
  1389. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1390. //
  1391. // NSProgress *progress = nil;
  1392. //
  1393. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1394. //
  1395. //
  1396. //
  1397. // if (error) {
  1398. //
  1399. // NSString* err_msg = [error localizedDescription];
  1400. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1401. //
  1402. //
  1403. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1404. //
  1405. // DebugLog(@"data string: %@",str);
  1406. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1407. //
  1408. // } else {
  1409. // DebugLog(@"response ");
  1410. //
  1411. //
  1412. //
  1413. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1414. //
  1415. // // 再将NSData转为字符串
  1416. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1417. //
  1418. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1419. //
  1420. // DebugLog(@"data string: %@",jsonStr);
  1421. //
  1422. // NSDictionary* json = responseObject;
  1423. //
  1424. //
  1425. // if([[json valueForKey:@"result"] intValue]==2)
  1426. // {
  1427. //// NSString* img_url_down = json[@"img_url_aname"];
  1428. //// NSString* img_url_up = json[@"img_url"];
  1429. //
  1430. //
  1431. // }
  1432. // else
  1433. // {
  1434. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1435. // }
  1436. //
  1437. //
  1438. // }
  1439. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1440. // }];
  1441. //
  1442. //
  1443. //
  1444. //
  1445. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1446. // //
  1447. //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. //
  1453. // [uploadTask resume];
  1454. //
  1455. //}
  1456. //+(void) saveScanOrder:(NSMutableDictionary*) ordercontent
  1457. //{
  1458. //
  1459. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1460. // NSString *documents = [paths objectAtIndex:0];
  1461. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1462. //
  1463. //
  1464. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1465. //
  1466. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1467. //
  1468. //
  1469. //
  1470. // [RAUtils dicttofile:orderPath dict:ordercontent];
  1471. //
  1472. //
  1473. //
  1474. //
  1475. //
  1476. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1477. //
  1478. //
  1479. //
  1480. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1481. //
  1482. //
  1483. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1484. //
  1485. //
  1486. //
  1487. // [RAUtils dicttofile:cartpath dict:RASingleton.sharedInstance.scan_cart];
  1488. //
  1489. //
  1490. //
  1491. //
  1492. // NSMutableDictionary* orderlist = nil;
  1493. // NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1494. //
  1495. //
  1496. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1497. //
  1498. //
  1499. // orderlist = [NSMutableDictionary new];
  1500. //
  1501. //// [orderlist addObject:appDelegate.order_code];
  1502. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1503. //
  1504. // }
  1505. // else
  1506. // {
  1507. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1508. //
  1509. //// [orderlist insertObject:appDelegate.order_code atIndex:0];
  1510. //
  1511. //// [RAUtils dicttofile:orderlistPath dict:orderlist];
  1512. // }
  1513. //
  1514. // NSMutableDictionary* orderitem = [NSMutableDictionary new];
  1515. // orderitem[@"create_by"] = appDelegate.user;
  1516. // orderitem[@"price"] = upparams[@"total_price"];
  1517. // orderitem[@"customer_name"] = upparams[@"customer_contact"];
  1518. // orderitem[@"purchase_time"] = [RAUtils current_date_time];
  1519. // orderitem[@"order_status"] = @"Saved Order";
  1520. // orderitem[@"order_code"] = appDelegate.order_code;
  1521. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1522. //
  1523. //
  1524. // int count = [orderlist[@"count"] intValue];
  1525. //
  1526. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1527. //
  1528. // newlist[@"item_0"]=orderitem;
  1529. // int newcount = 1;
  1530. // for(int i=0;i<count;i++)
  1531. // {
  1532. // NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1533. // if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1534. // continue;
  1535. // else
  1536. // {
  1537. // newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1538. // newcount++;
  1539. // }
  1540. // }
  1541. // newlist[@"count"] = @(newcount);
  1542. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1543. //
  1544. // return;
  1545. //}
  1546. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1547. {
  1548. if(submitlist==nil)
  1549. return;
  1550. NSString *userdir = [OLDataProvider getUserPath];
  1551. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1552. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1553. }
  1554. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1555. {
  1556. if(orderlist==nil)
  1557. return;
  1558. NSString *userdir = [OLDataProvider getUserPath];
  1559. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1560. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1561. }
  1562. +(NSMutableDictionary*) getScanSubmitList
  1563. {
  1564. NSString *userdir = [OLDataProvider getUserPath];
  1565. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1566. NSMutableDictionary* submitlist = nil;
  1567. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1568. submitlist = [NSMutableDictionary new];
  1569. }
  1570. else
  1571. {
  1572. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1573. }
  1574. return submitlist;
  1575. }
  1576. +(NSMutableDictionary*) getScanOrderList{
  1577. NSString *userdir = [OLDataProvider getUserPath];
  1578. NSMutableDictionary* orderlist = nil;
  1579. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1580. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1581. orderlist = [NSMutableDictionary new];
  1582. // [orderlist addObject:appDelegate.order_code];
  1583. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1584. }
  1585. else
  1586. {
  1587. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1588. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1589. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1590. }
  1591. return orderlist;
  1592. }
  1593. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1594. {
  1595. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1596. // NSString *documents = [paths objectAtIndex:0];
  1597. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1598. // NSString *userdir = [OLDataProvider getUserPath];
  1599. NSMutableDictionary* orderlist = [self getScanOrderList];
  1600. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1601. int count = [orderlist[@"count"] intValue];
  1602. if(count==0)
  1603. {
  1604. // order list 为空,不用维护
  1605. #ifdef RA_NOTIFICATION
  1606. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1607. #else
  1608. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1609. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1610. #endif
  1611. return;
  1612. }
  1613. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1614. //
  1615. // // order list 为空,不用维护
  1616. //
  1617. //
  1618. // // [orderlist addObject:appDelegate.order_code];
  1619. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1620. //
  1621. // }
  1622. // else
  1623. // {
  1624. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1625. //
  1626. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1627. //
  1628. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1629. // }
  1630. //==================== order list 删除
  1631. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1632. NSMutableDictionary* submititem =nil;
  1633. int newcount = 0;
  1634. for(int i=0;i<count;i++)
  1635. {
  1636. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1637. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1638. {
  1639. submititem = item;
  1640. continue;
  1641. }
  1642. else
  1643. {
  1644. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1645. newcount++;
  1646. }
  1647. }
  1648. if(submititem==nil)
  1649. {
  1650. //order list 里没有和当前打开的order code一致的order
  1651. //这种情况往往是因为重复点提交按钮,order已关闭。
  1652. return;
  1653. }
  1654. newlist[@"count"] = @(newcount);
  1655. [self saveScanOrderList:newlist];
  1656. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1657. //////////////////---=============================== submitlist 添加
  1658. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1659. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1660. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1661. //
  1662. // submitlist = [NSMutableDictionary new];
  1663. //
  1664. //
  1665. // }
  1666. // else
  1667. // {
  1668. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1669. //
  1670. //
  1671. // }
  1672. int submitcount =[submitlist[@"count"] intValue];
  1673. submititem[@"order_status"] = soid;//@"Submitted Order";
  1674. submititem[@"customer_name"] = company;//@"Submitted Order";
  1675. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1676. submitlist[@"count"] = @(submitcount+1);
  1677. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1678. [self saveScanSubmitList:submitlist];
  1679. #ifdef RA_NOTIFICATION
  1680. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1681. #else
  1682. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1683. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1684. #endif
  1685. }
  1686. +(void) save2submitScanOrder1
  1687. {
  1688. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1689. // NSString *documents = [paths objectAtIndex:0];
  1690. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1691. // NSString *userdir = [OLDataProvider getUserPath];
  1692. NSMutableDictionary* orderlist = [self getScanOrderList];
  1693. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1694. int count = [orderlist[@"count"] intValue];
  1695. if(count==0)
  1696. {
  1697. // order list 为空,不用维护
  1698. #ifdef RA_NOTIFICATION
  1699. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1700. #else
  1701. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1702. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1703. #endif
  1704. return;
  1705. return;
  1706. }
  1707. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1708. int newcount = 0;
  1709. for(int i=0;i<count;i++)
  1710. {
  1711. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1712. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1713. continue;
  1714. else
  1715. {
  1716. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1717. newcount++;
  1718. }
  1719. }
  1720. newlist[@"count"] = @(newcount);
  1721. [self saveScanOrderList:newlist];
  1722. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1723. #ifdef RA_NOTIFICATION
  1724. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1725. #else
  1726. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1727. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1728. #endif
  1729. }
  1730. //+(void) createScanTempFolder
  1731. //{
  1732. // return;
  1733. //// NSString* scanFilePath = [self getScanTempPath];
  1734. //// BOOL bdir=YES;
  1735. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1736. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1737. //// {
  1738. ////
  1739. //// NSError *error = nil;
  1740. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1741. ////
  1742. //// if(!bsuccess)
  1743. //// DebugLog(@"Create cache folder failed");
  1744. ////
  1745. //// // if(bsuccess)
  1746. //// // {
  1747. //// // sqlite3 *db = [self get_db];
  1748. //// //
  1749. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1750. //// // [iSalesDB close_db:db];
  1751. //// // }
  1752. //// }
  1753. //}
  1754. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1755. {
  1756. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1757. NSString *documents = [paths objectAtIndex:0];
  1758. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1759. NSString* servername = addressDic[@"name"];
  1760. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1761. NSString* user = appDelegate.user;
  1762. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1763. orderpath = [orderpath stringByAppendingPathComponent:user];
  1764. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1765. BOOL bdir=YES;
  1766. NSFileManager* fileManager = [NSFileManager defaultManager];
  1767. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1768. {
  1769. NSError *error = nil;
  1770. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1771. if(!bsuccess)
  1772. DebugLog(@"Create cache folder failed");
  1773. // if(bsuccess)
  1774. // {
  1775. // sqlite3 *db = [self get_db];
  1776. //
  1777. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1778. // [iSalesDB close_db:db];
  1779. // }
  1780. }
  1781. return orderpath;
  1782. }
  1783. +(NSString*) getScanOrderPath
  1784. {
  1785. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1786. NSString *documents = [paths objectAtIndex:0];
  1787. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1788. NSString* servername = addressDic[@"name"];
  1789. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1790. NSString* user = appDelegate.user;
  1791. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1792. orderpath = [orderpath stringByAppendingPathComponent:user];
  1793. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1794. BOOL bdir=YES;
  1795. NSFileManager* fileManager = [NSFileManager defaultManager];
  1796. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1797. {
  1798. NSError *error = nil;
  1799. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1800. if(!bsuccess)
  1801. DebugLog(@"Create cache folder failed");
  1802. // if(bsuccess)
  1803. // {
  1804. // sqlite3 *db = [self get_db];
  1805. //
  1806. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1807. // [iSalesDB close_db:db];
  1808. // }
  1809. }
  1810. return orderpath;
  1811. }
  1812. //+(NSString*) getScanTempPath
  1813. //{
  1814. // return nil;
  1815. ////
  1816. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1817. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1818. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1819. //}
  1820. //+(void) moveScanTemp2Order
  1821. //{
  1822. // return;
  1823. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1824. ////
  1825. //// NSError *error = nil;
  1826. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1827. ////
  1828. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1829. //// // for(int i=0;i<arr_files.count;i++)
  1830. //// // {
  1831. //// // NSString* file=arr_files[i];
  1832. //// //
  1833. //// //
  1834. //// // }
  1835. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1836. ////
  1837. //
  1838. //}
  1839. +(NSString*) getUserPath
  1840. {
  1841. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1842. NSString *documents = [paths objectAtIndex:0];
  1843. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1844. NSString* servername = addressDic[@"name"];
  1845. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1846. NSString* user = appDelegate.user;
  1847. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1848. userpath = [userpath stringByAppendingPathComponent:user];
  1849. return userpath;
  1850. }
  1851. +(bool) isSavedScanOrder:(NSString*) order_code
  1852. {
  1853. if(order_code == nil)
  1854. return false;
  1855. NSMutableDictionary * orderlist=[self getScanOrderList];
  1856. int count = [orderlist[@"count"] intValue];
  1857. for(int i=0;i<count;i++)
  1858. {
  1859. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1860. if([item[@"order_code"] isEqualToString: order_code ])
  1861. {
  1862. return true;
  1863. }
  1864. }
  1865. return false;
  1866. }
  1867. +(NSString*) getScanPath
  1868. {
  1869. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1870. if(appDelegate.order_code==nil)
  1871. {
  1872. appDelegate.order_code=[[NSUUID new] UUIDString];
  1873. }
  1874. return [self getScanOrderPath];
  1875. // else
  1876. // {
  1877. // [self createScanTempFolder];
  1878. // return [self getScanTempPath];
  1879. // }
  1880. }
  1881. +(void) scanCloseOrder
  1882. {
  1883. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1884. // if(appDelegate.user.length<=0)
  1885. // {
  1886. // [RAUtils message_alert:@"Please login first." title:@"Warring" controller:self];
  1887. // return;
  1888. // }
  1889. // if(appDelegate.order_code.length>0)
  1890. {
  1891. // 当前订单先关闭
  1892. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1893. appDelegate.order_code = nil;
  1894. // RASingleton.sharedInstance.scan_temp_code = nil;
  1895. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1896. { appDelegate.customerInfo = nil;
  1897. appDelegate.contact_id = nil;
  1898. }
  1899. RASingleton.sharedInstance.scan_cart = nil;
  1900. RASingleton.sharedInstance.price_type = 1;
  1901. // [appDelegate updateScanButton:false];
  1902. [appDelegate update_count_mark];
  1903. [appDelegate closeOrder];
  1904. }
  1905. }
  1906. +(NSString*) createScanTempCode
  1907. {
  1908. return [[NSUUID new] UUIDString];
  1909. }
  1910. +(NSString*) scanTemplatePath:(NSString*)file
  1911. {
  1912. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1913. NSString *documents = [paths objectAtIndex:0];
  1914. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1915. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1916. return templatefile;
  1917. }
  1918. +(void) realguestLogin
  1919. {
  1920. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1921. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1922. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1923. }
  1924. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1925. {
  1926. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1927. NSString *documents = [paths objectAtIndex:0];
  1928. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1929. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1930. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1931. return [dict mutableCopy];
  1932. }
  1933. +(void) deleteSubmittedOrder:(NSString*) order_code
  1934. {
  1935. NSString *userdir = [OLDataProvider getUserPath];
  1936. if(order_code.length==0)
  1937. return;
  1938. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1939. // NSString *documents = [paths objectAtIndex:0];
  1940. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1941. // if([appDelegate.order_code isEqualToString:order_code])
  1942. // {
  1943. // //要删除的订单是当前打开的订单;
  1944. // appDelegate.order_code = nil;
  1945. // appDelegate.customerInfo = nil;
  1946. // RASingleton.sharedInstance.scan_cart = nil;
  1947. // appDelegate.contact_id = nil;
  1948. // // [appDelegate updateScanButton:false];
  1949. //
  1950. // [appDelegate update_count_mark];
  1951. // }
  1952. //删除订单目录
  1953. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1954. //维护订单列表
  1955. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1956. int count = [submitlist[@"count"] intValue];
  1957. if(count==0)
  1958. {
  1959. // order list 为空,不用维护
  1960. #ifdef RA_NOTIFICATION
  1961. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1962. #else
  1963. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1964. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1965. #endif
  1966. return;
  1967. }
  1968. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1969. //
  1970. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1971. //
  1972. //
  1973. //
  1974. // // [orderlist addObject:appDelegate.order_code];
  1975. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1976. //
  1977. // }
  1978. // else
  1979. // {
  1980. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1981. //
  1982. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1983. //
  1984. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1985. // }
  1986. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1987. int newcount = 0;
  1988. for(int i=0;i<count;i++)
  1989. {
  1990. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1991. if([item[@"order_code"] isEqualToString: order_code ])
  1992. continue;
  1993. else
  1994. {
  1995. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1996. newcount++;
  1997. }
  1998. }
  1999. newlist[@"count"] = @(newcount);
  2000. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2001. [self saveScanSubmitList:newlist];
  2002. #ifdef RA_NOTIFICATION
  2003. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2004. #else
  2005. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2006. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2007. #endif
  2008. }
  2009. +(void) updateScanOrder:(NSString*) order_code
  2010. {
  2011. NSString *userdir = [OLDataProvider getUserPath];
  2012. if(order_code.length==0)
  2013. return;
  2014. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2015. // NSString *documents = [paths objectAtIndex:0];
  2016. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2017. NSMutableDictionary* orderlist = [self getScanOrderList];
  2018. int count = [orderlist[@"count"] intValue];
  2019. if(count==0)
  2020. {
  2021. // order list 为空,不用维护
  2022. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2023. return;
  2024. }
  2025. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2026. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2027. //
  2028. //
  2029. //
  2030. // // [orderlist addObject:appDelegate.order_code];
  2031. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2032. //
  2033. // }
  2034. // else
  2035. // {
  2036. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2037. //
  2038. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2039. //
  2040. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2041. // }
  2042. //
  2043. //
  2044. // int count = [orderlist[@"count"] intValue];
  2045. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  2046. float p =0;
  2047. float s =0;
  2048. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  2049. {
  2050. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  2051. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  2052. int mcount = [cartitem[@"count"] intValue];
  2053. double msurcharge =0;
  2054. if(RASingleton.sharedInstance.price_type==1)
  2055. {
  2056. if([cartitem[@"special_price"] boolValue])
  2057. {
  2058. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price2"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2059. }
  2060. else
  2061. {
  2062. msurcharge=([cartitem[@"price3"] doubleValue]-[cartitem[@"price1"] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  2063. }
  2064. }
  2065. s+=msurcharge;
  2066. }
  2067. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  2068. int order_qty=0;
  2069. for(int c=0;c<[section[@"count"] intValue];c++)
  2070. {
  2071. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  2072. order_qty+=[item[@"count"] intValue];
  2073. }
  2074. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  2075. // int newcount = 0;
  2076. for(int i=0;i<count;i++)
  2077. {
  2078. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2079. if([item[@"order_code"] isEqualToString: order_code ])
  2080. {
  2081. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  2082. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  2083. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  2084. }
  2085. }
  2086. // newlist[@"count"] = @(newcount);
  2087. [self saveScanOrderList:orderlist];
  2088. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2089. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2090. }
  2091. +(void) deleteScanOrder:(NSString*) order_code
  2092. {
  2093. NSString *userdir = [OLDataProvider getUserPath];
  2094. if(order_code.length==0)
  2095. return;
  2096. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2097. // NSString *documents = [paths objectAtIndex:0];
  2098. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2099. if([appDelegate.order_code isEqualToString:order_code])
  2100. {
  2101. //要删除的订单是当前打开的订单;
  2102. // appDelegate.order_code = nil;
  2103. // appDelegate.customerInfo = nil;
  2104. // RASingleton.sharedInstance.scan_cart = nil;
  2105. // appDelegate.contact_id = nil;
  2106. [self scanCloseOrder];
  2107. // [appDelegate updateScanButton:false];
  2108. [appDelegate update_count_mark];
  2109. }
  2110. //删除订单目录
  2111. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  2112. //维护订单列表
  2113. NSMutableDictionary* orderlist = [self getScanOrderList];
  2114. int count = [orderlist[@"count"] intValue];
  2115. if(count==0)
  2116. {
  2117. // order list 为空,不用维护
  2118. #ifdef RA_NOTIFICATION
  2119. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2120. #else
  2121. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2122. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2123. #endif
  2124. return;
  2125. }
  2126. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2127. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2128. //
  2129. //
  2130. //
  2131. // // [orderlist addObject:appDelegate.order_code];
  2132. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2133. //
  2134. // }
  2135. // else
  2136. // {
  2137. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2138. //
  2139. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2140. //
  2141. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2142. // }
  2143. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2144. int newcount = 0;
  2145. for(int i=0;i<count;i++)
  2146. {
  2147. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2148. if([item[@"order_code"] isEqualToString: order_code ])
  2149. continue;
  2150. else
  2151. {
  2152. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2153. newcount++;
  2154. }
  2155. }
  2156. newlist[@"count"] = @(newcount);
  2157. [self saveScanOrderList:newlist];
  2158. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2159. #ifdef RA_NOTIFICATION
  2160. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2161. #else
  2162. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2163. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2164. #endif
  2165. }
  2166. +(void) scanRefreshCart
  2167. {
  2168. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2169. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2170. NSMutableDictionary * section = cart[@"section_0"];
  2171. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2172. int count = [section[@"count"] intValue];
  2173. int newcount = 0;
  2174. for(int i=0;i<count;i++)
  2175. {
  2176. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2177. NSString* model = item[@"model"];
  2178. NSDictionary* modeljson=appDelegate.scan_model[model];
  2179. if(modeljson==nil)
  2180. continue;
  2181. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2182. cartitem[@"note"]=item[@"note"];
  2183. cartitem[@"discount"]=item[@"discount"];
  2184. // [item removeObjectForKey:@"discount"];
  2185. // [item removeObjectForKey:@"note"];
  2186. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2187. newcount++;
  2188. }
  2189. newsection[@"count"]=@(newcount);
  2190. newsection[@"available"] = @(1);
  2191. newsection[@"title"] = @"Available List";
  2192. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2193. }
  2194. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2195. {
  2196. // _modelJson = modelJson;
  2197. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2198. // self.labelPrice0.text = appDelegate.price0_name;
  2199. // self.labelPrice1.text = appDelegate.price1_name;
  2200. // self.labelPrice2.text = appDelegate.price2_name;
  2201. // self.labelPrice3.text = appDelegate.price3_name;
  2202. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2203. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2204. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2205. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2206. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2207. NSLog( [RAConvertor dict2string:_modelJson]);
  2208. NSString * port=_modelJson[@"port"];
  2209. if(port.length==0)
  2210. port = @"N/A";
  2211. NSString * origin=_modelJson[@"origin"];
  2212. if(origin.length==0)
  2213. origin = @"N/A";
  2214. NSString * dimension=_modelJson[@"dimension"];
  2215. if(dimension.length==0)
  2216. dimension = @"N/A";
  2217. // _labelModel.text=_modelJson[@"model"];
  2218. // _labelDescription.text=_modelJson[@"description"];
  2219. // _labelDimension.text=dimension;
  2220. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2221. // _labelOrigin.text=origin;
  2222. // _labelPort.text=port;
  2223. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2224. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2225. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2226. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2227. if(![price0 isEqualToString: @"N/A"])
  2228. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2229. if(![price1 isEqualToString: @"N/A"])
  2230. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2231. if(![price2 isEqualToString: @"N/A"])
  2232. {
  2233. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2234. // if(RASingleton.sharedInstance.price_type==1)
  2235. _modelJson [@"special_price"] = @true;
  2236. }
  2237. if(![price3 isEqualToString: @"N/A"])
  2238. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2239. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2240. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2241. {
  2242. // if(RASingleton.sharedInstance.price_type==1)
  2243. _modelJson [@"net_price"] = @true;
  2244. }
  2245. //
  2246. // if(![price1 isEqualToString:@"N/A"])
  2247. // {
  2248. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2249. // _modelJson[@"price2"]= price2;
  2250. // }
  2251. // else
  2252. // {
  2253. // price2=@"N/A";
  2254. // }
  2255. //
  2256. // if([price0 isEqualToString:@"N/A"])
  2257. // _labelPriceCTNR.text= @"";//price0;
  2258. // else
  2259. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2260. //
  2261. // if([price3 isEqualToString:@"N/A"])
  2262. // _labelPriceNCA.text= price3;
  2263. // else
  2264. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2265. // _labelPrice25p.text=price2;
  2266. //
  2267. // if([_modelJson [@"special_price"] boolValue])
  2268. // {
  2269. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2270. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2271. // else
  2272. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2273. // }
  2274. // else
  2275. // {
  2276. // _labelPriceSpecial.text=@"";//@"N/A";
  2277. // }
  2278. //
  2279. // if([_modelJson [@"net_price"] boolValue])
  2280. // {
  2281. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2282. //// {
  2283. //// _labelPriceNet.text=_modelJson[@"price1"];
  2284. ////// _labelPriceNCA.text=@"N/A";
  2285. //// }
  2286. //// else
  2287. //// {
  2288. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2289. ////// _labelPriceNCA.text=@"N/A";
  2290. //// }
  2291. //
  2292. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2293. // {
  2294. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2295. //// _labelPriceNCA.text=@"N/A";
  2296. // }
  2297. // else
  2298. // {
  2299. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2300. //// _labelPriceNCA.text=@"N/A";
  2301. // }
  2302. //
  2303. // }
  2304. // else
  2305. // {
  2306. // _labelPriceNet.text=@"";//@"N/A";
  2307. // }
  2308. //
  2309. // {
  2310. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2311. // {
  2312. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2313. //// _labelPriceNCA.text=@"N/A";
  2314. // }
  2315. // else
  2316. // {
  2317. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2318. //// _labelPriceNet.text=@"N/A";
  2319. // }
  2320. // }
  2321. NSString * available=_modelJson[@"available"];
  2322. if(available.length==0)
  2323. available = @"N/A";
  2324. // _labelAvailable.text=available;
  2325. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2326. if(unit_cuft.length==0)
  2327. unit_cuft = @"N/A";
  2328. else
  2329. {
  2330. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2331. }
  2332. NSString * cell_price;
  2333. if(RASingleton.sharedInstance.price_type==1)
  2334. {
  2335. // if(_modelJson [@"net_price"])
  2336. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2337. // else if(_modelJson [@"special_price"])
  2338. // {
  2339. // unit_price= _modelJson[@"price2"];
  2340. // }
  2341. // else
  2342. // {
  2343. // unit_price= _modelJson[@"price1"];
  2344. // }
  2345. }
  2346. else
  2347. {
  2348. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2349. }
  2350. NSString * mpack=_modelJson[@"stockUom"];
  2351. if([mpack isEqualToString:@"N/A"])
  2352. mpack=@"1";
  2353. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2354. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2355. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2356. NSMutableDictionary* jitem = nil;
  2357. jitem = [_modelJson mutableCopy];
  2358. int stockUom =[jitem[@"stockUom"] intValue];
  2359. if(stockUom==0)
  2360. stockUom=1;
  2361. bool newitem = true;
  2362. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2363. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2364. // for(int i=0;i<count;i++)
  2365. // {
  2366. //
  2367. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2368. //
  2369. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2370. // {
  2371. //
  2372. //// int oldcount = [litem[@"stockUom"] intValue];
  2373. // newitem = false;
  2374. //
  2375. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2376. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2377. //
  2378. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2379. // break;
  2380. //
  2381. //
  2382. // }
  2383. //
  2384. //
  2385. //
  2386. // }
  2387. // if(true)
  2388. // {
  2389. jitem[@"count"]=@(stockUom);
  2390. jitem[@"check"]=@(true);
  2391. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2392. NSString * unit_price;
  2393. if(RASingleton.sharedInstance.price_type==0)
  2394. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2395. else if(_modelJson [@"special_price"])
  2396. {
  2397. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2398. }
  2399. else if(_modelJson [@"net_price"])
  2400. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2401. else
  2402. {
  2403. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2404. }
  2405. if([unit_price isEqualToString:@"N/A"])
  2406. unit_price = @"0";
  2407. else
  2408. {
  2409. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2410. }
  2411. jitem[@"unit_price"] = unit_price;
  2412. jitem[@"erp_unit_price"] = unit_price;
  2413. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2414. // section[@"count"]= @(count+1);
  2415. // count++;
  2416. // }
  2417. return jitem;
  2418. }
  2419. +(void) saveScanCart:(NSMutableDictionary*) cart
  2420. {
  2421. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2422. if(appDelegate.order_code.length<=0)
  2423. return;
  2424. if(cart==nil)
  2425. return;
  2426. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2427. // NSString *documents = [paths objectAtIndex:0];
  2428. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2429. if(RASingleton.sharedInstance.price_type==0)
  2430. {
  2431. NSMutableDictionary * section = cart[@"section_0"];
  2432. int count = [section[@"count"] intValue];
  2433. for(int i=0;i<count;i++)
  2434. {
  2435. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2436. [item removeObjectForKey:@"discount"];
  2437. [item removeObjectForKey:@"note"];
  2438. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2439. }
  2440. cart[@"section_0"]=section;
  2441. }
  2442. NSString *orderdir = [self getScanPath];
  2443. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2444. cart[@"price_group"] = appDelegate.price_group;
  2445. [RAUtils dicttofile:cartpath dict:cart];
  2446. }
  2447. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2448. {
  2449. assert(add_params[@"user"]!=nil);
  2450. // assert(add_params[@"contact_id"]!=nil);
  2451. // assert(add_params[@"password"]!=nil);
  2452. NSString* serial= [[NSUUID UUID] UUIDString];
  2453. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2454. NSString *cachefolder = [paths objectAtIndex:0];
  2455. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2456. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2457. NSFileManager* fileManager = [NSFileManager defaultManager];
  2458. BOOL bdir=YES;
  2459. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2460. {
  2461. NSError *error = nil;
  2462. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2463. if(!bsuccess)
  2464. DebugLog(@"Create UPLOAD folder failed");
  2465. }
  2466. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2467. ret[@"contact"]=[self prepareContact:serial];
  2468. ret[@"wishlist"]=[self prepareWishlist:serial];
  2469. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2470. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2471. ret[@"view_portfolio"] = [self preparePDF:serial];
  2472. NSString* str= [RAConvertor dict2string:ret];
  2473. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2474. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2475. NSError *error=nil;
  2476. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2477. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2478. ZipArchive* zip = [[ZipArchive alloc] init];
  2479. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2480. for(int i=0;i<arr_files.count;i++)
  2481. {
  2482. NSString* file=arr_files[i];
  2483. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2484. }
  2485. if( ![zip CloseZipFile2] )
  2486. {
  2487. zippath = @"";
  2488. }
  2489. ret[@"file"]=zippath;
  2490. return ret;
  2491. }
  2492. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2493. {
  2494. if(filename.length==0)
  2495. return false;
  2496. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2497. bool ret=false;
  2498. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2499. NSString *cachefolder = [paths objectAtIndex:0];
  2500. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2501. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2502. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2503. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2504. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2505. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2506. NSFileManager* fileManager = [NSFileManager defaultManager];
  2507. BOOL bdir=NO;
  2508. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2509. {
  2510. NSError *error = nil;
  2511. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2512. {
  2513. ret=false;
  2514. }
  2515. else
  2516. {
  2517. ret=true;
  2518. }
  2519. }
  2520. return ret;
  2521. }
  2522. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2523. {
  2524. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2525. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2526. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2527. NSString* where=@" 1=1";
  2528. if (ver!=nil) {
  2529. where=@"is_dirty=1";
  2530. }
  2531. sqlite3 *db = [iSalesDB get_db];
  2532. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2533. sqlite3_stmt * statement;
  2534. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2535. int count=0;
  2536. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2537. if ( dbresult== SQLITE_OK)
  2538. {
  2539. while (sqlite3_step(statement) == SQLITE_ROW)
  2540. {
  2541. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2542. int _id = sqlite3_column_int(statement, 0);
  2543. int product_id = sqlite3_column_int(statement, 1);
  2544. int item_id = sqlite3_column_int(statement, 2);
  2545. int qty = sqlite3_column_int(statement, 3);
  2546. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2547. NSString* nscreate_time=nil;
  2548. if(create_time!=nil)
  2549. {
  2550. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2551. }
  2552. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2553. NSString* nsmodify_time=nil;
  2554. if(modify_time!=nil)
  2555. {
  2556. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2557. }
  2558. int is_delete = sqlite3_column_int(statement, 6);
  2559. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2560. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2561. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2562. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2563. itemjson[@"create_time"]=nscreate_time;
  2564. itemjson[@"modify_time"]=nsmodify_time;
  2565. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2566. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2567. count++;
  2568. }
  2569. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2570. sqlite3_finalize(statement);
  2571. }
  2572. ret[@"count"]=[NSNumber numberWithInt:count ];
  2573. [iSalesDB close_db:db];
  2574. return ret;
  2575. }
  2576. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2577. {
  2578. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2579. // UIApplication * app = [UIApplication sharedApplication];
  2580. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2581. assert(add_params[@"user"]!=nil);
  2582. // assert(add_params[@"password"]!=nil);
  2583. 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 ];
  2584. // 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 ];
  2585. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2586. DebugLog(@"offline_login sql:%@",sqlQuery);
  2587. sqlite3_stmt * statement;
  2588. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2589. int count=0;
  2590. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2591. if ( dbresult== SQLITE_OK)
  2592. {
  2593. while (sqlite3_step(statement) == SQLITE_ROW)
  2594. {
  2595. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2596. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2597. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2598. int product_id = sqlite3_column_int(statement, 0);
  2599. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2600. int item_id = sqlite3_column_int(statement, 7);
  2601. NSString* Price=nil;
  2602. if(str_price==nil)
  2603. {
  2604. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2605. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2606. if(price==nil)
  2607. Price=@"No Price.";
  2608. else
  2609. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2610. }
  2611. else
  2612. {
  2613. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2614. }
  2615. double discount = sqlite3_column_double(statement, 2);
  2616. int item_count = sqlite3_column_int(statement, 3);
  2617. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2618. NSString *nsline_note=nil;
  2619. if(line_note!=nil)
  2620. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2621. char *name = (char*)sqlite3_column_text(statement, 5);
  2622. NSString *nsname=nil;
  2623. if(name!=nil)
  2624. nsname= [[NSString alloc]initWithUTF8String:name];
  2625. char *description = (char*)sqlite3_column_text(statement, 6);
  2626. NSString *nsdescription=nil;
  2627. if(description!=nil)
  2628. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2629. // int stockUom = sqlite3_column_int(statement, 8);
  2630. // int _id = sqlite3_column_int(statement, 9);
  2631. //
  2632. //
  2633. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2634. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2635. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2636. // int carton=[bsubtotaljson[@"carton"] intValue];
  2637. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2638. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2639. itemjson[@"model"]=nsname;
  2640. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2641. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2642. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2643. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2644. // itemjson[@"check"]=@"true";
  2645. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2646. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2647. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2648. itemjson[@"unit_price"]=Price;
  2649. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2650. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2651. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2652. itemjson[@"note"]=nsline_note;
  2653. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2654. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2655. count++;
  2656. }
  2657. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2658. sqlite3_finalize(statement);
  2659. }
  2660. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2661. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2662. return ret;
  2663. }
  2664. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2665. {
  2666. assert(add_params[@"user"]!=nil);
  2667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2668. sqlite3 *db = [iSalesDB get_db];
  2669. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2670. // for(int i=0;i<arr_soid.count;i++)
  2671. sqlite3_stmt * statement;
  2672. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2673. //int count=0;
  2674. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2675. if ( dbresult== SQLITE_OK)
  2676. {
  2677. int count=0;
  2678. while (sqlite3_step(statement) == SQLITE_ROW)
  2679. {
  2680. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2681. int _id = sqlite3_column_int(statement, 0);
  2682. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2683. NSString* nssync_data=nil;
  2684. if(sync_data!=nil)
  2685. {
  2686. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2687. }
  2688. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2689. NSString* nsimg_1=nil;
  2690. if(img_1!=nil)
  2691. {
  2692. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2693. [self copy_upImg:serial file:nsimg_1];
  2694. }
  2695. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2696. NSString* nsso_no=nil;
  2697. if(so_no!=nil)
  2698. {
  2699. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2700. }
  2701. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2702. itemjson[@"order_type"]=@"submit order";
  2703. else
  2704. itemjson[@"order_type"]=@"archive order";
  2705. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2706. itemjson[@"json_data"]= nssync_data;
  2707. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2708. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2709. count++;
  2710. }
  2711. ret[@"count"]=[NSNumber numberWithInt:count ];
  2712. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2713. }
  2714. sqlite3_finalize(statement);
  2715. [iSalesDB close_db:db];
  2716. return ret;
  2717. }
  2718. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2719. {
  2720. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2721. sqlite3 *db = [iSalesDB get_db];
  2722. // UIApplication * app = [UIApplication sharedApplication];
  2723. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2724. 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";
  2725. sqlite3_stmt * statement;
  2726. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2727. int count=0;
  2728. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2729. if ( dbresult== SQLITE_OK)
  2730. {
  2731. while (sqlite3_step(statement) == SQLITE_ROW)
  2732. {
  2733. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2734. int _id = sqlite3_column_int(statement, 0);
  2735. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2736. NSString* nsimg_0=nil;
  2737. if(img_0!=nil)
  2738. {
  2739. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2740. [self copy_upImg:serial file:nsimg_0];
  2741. }
  2742. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2743. NSString* nsimg_1=nil;
  2744. if(img_1!=nil)
  2745. {
  2746. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2747. [self copy_upImg:serial file:nsimg_1];
  2748. }
  2749. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2750. NSString* nsimg_2=nil;
  2751. if(img_2!=nil)
  2752. {
  2753. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2754. [self copy_upImg:serial file:nsimg_2];
  2755. }
  2756. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2757. NSString* nssync_data=nil;
  2758. if(sync_data!=nil)
  2759. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2760. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2761. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2762. itemjson[@"json_data"]= nssync_data;
  2763. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2764. count++;
  2765. }
  2766. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2767. sqlite3_finalize(statement);
  2768. }
  2769. ret[@"count"]=[NSNumber numberWithInt:count ];
  2770. [iSalesDB close_db:db];
  2771. return ret;
  2772. }
  2773. +(bool) check_offlinedata
  2774. {
  2775. // UIApplication * app = [UIApplication sharedApplication];
  2776. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2777. sqlite3 *db = [iSalesDB get_db];
  2778. NSString * where=@"1=1";
  2779. // if(appDelegate.user!=nil)
  2780. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2781. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2782. [iSalesDB close_db:db];
  2783. if(count==0)
  2784. {
  2785. return false;
  2786. }
  2787. return true;
  2788. //
  2789. //[iSalesDB close_db:db];
  2790. }
  2791. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2792. {
  2793. // UIApplication * app = [UIApplication sharedApplication];
  2794. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2795. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2796. sqlite3 *db = [iSalesDB get_db];
  2797. NSString* collectId=params[@"collectId"];
  2798. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2799. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2800. [iSalesDB execSql:sqlQuery db:db];
  2801. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2802. [iSalesDB close_db:db];
  2803. // appDelegate.wish_count =count;
  2804. //
  2805. // [appDelegate update_count_mark];
  2806. ret[@"result"]= [NSNumber numberWithInt:2];
  2807. ret[@"wish_count"]=@(count);
  2808. return ret;
  2809. }
  2810. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2811. //{
  2812. //
  2813. // UIApplication * app = [UIApplication sharedApplication];
  2814. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2815. //
  2816. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2817. // sqlite3 *db = [iSalesDB get_db];
  2818. // NSString* product_id=params[@"product_id"];
  2819. //
  2820. //
  2821. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2822. //
  2823. //
  2824. // for(int i=0;i<arr.count;i++)
  2825. // {
  2826. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2827. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2828. // if(count==0)
  2829. // {
  2830. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2831. // [iSalesDB execSql:sqlQuery db:db];
  2832. // }
  2833. // }
  2834. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2835. // [iSalesDB close_db:db];
  2836. //
  2837. // appDelegate.wish_count =count;
  2838. //
  2839. // [appDelegate update_count_mark];
  2840. // ret[@"result"]= [NSNumber numberWithInt:2];
  2841. // return ret;
  2842. // //
  2843. // //return ret;
  2844. //}
  2845. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2846. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2847. DebugLog(@"time interval: %lf",interval);
  2848. }
  2849. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2850. //{
  2851. // UIApplication * app = [UIApplication sharedApplication];
  2852. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2853. //
  2854. // int sort = [[params objectForKey:@"sort"] intValue];
  2855. // NSString *sort_str = @"";
  2856. // switch (sort) {
  2857. // case 0:{
  2858. // sort_str = @"order by w.modify_time desc";
  2859. // }
  2860. // break;
  2861. // case 1:{
  2862. // sort_str = @"order by w.modify_time asc";
  2863. // }
  2864. // break;
  2865. // case 2:{
  2866. // sort_str = @"order by m.name asc";
  2867. // }
  2868. // break;
  2869. // case 3:{
  2870. // sort_str = @"order by m.name desc";
  2871. // }
  2872. // break;
  2873. // case 4:{
  2874. // sort_str = @"order by m.description asc";
  2875. // }
  2876. // break;
  2877. //
  2878. // default:
  2879. // break;
  2880. //
  2881. // }
  2882. //
  2883. //
  2884. //// NSString* user = appDelegate.user;
  2885. //
  2886. // sqlite3 *db = [iSalesDB get_db];
  2887. //
  2888. // // order by w.create_time
  2889. // 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];
  2890. //
  2891. //// 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];
  2892. //
  2893. //
  2894. // sqlite3_stmt * statement;
  2895. //
  2896. // NSDate *date1 = [NSDate date];
  2897. //// NSDate *date2 = nil;
  2898. //
  2899. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2900. // int count=0;
  2901. //
  2902. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2903. // {
  2904. //
  2905. //
  2906. // while (sqlite3_step(statement) == SQLITE_ROW)
  2907. // {
  2908. //
  2909. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2910. //
  2911. // int product_id = sqlite3_column_double(statement, 0);
  2912. //
  2913. //
  2914. //
  2915. //
  2916. //
  2917. // char *description = (char*)sqlite3_column_text(statement, 1);
  2918. // if(description==nil)
  2919. // description= "";
  2920. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2921. //
  2922. // int item_id = sqlite3_column_double(statement, 2);
  2923. //
  2924. // NSDate *date_image = [NSDate date];
  2925. //
  2926. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2927. //
  2928. // printf("image : ");
  2929. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2930. //
  2931. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2932. // // if(url==nil)
  2933. // // url="";
  2934. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2935. //
  2936. // int qty = sqlite3_column_int(statement, 3);
  2937. //
  2938. //
  2939. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2940. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2941. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2942. // item[@"description"]= nsdescription;
  2943. // item[@"img"]= nsurl;
  2944. //
  2945. //
  2946. //
  2947. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2948. // count++;
  2949. //
  2950. // }
  2951. // printf("total time:");
  2952. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2953. //
  2954. // ret[@"count"]= [NSNumber numberWithInt:count];
  2955. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2956. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2957. // ret[@"result"]= [NSNumber numberWithInt:2];
  2958. //
  2959. //
  2960. // appDelegate.wish_count =count;
  2961. //
  2962. // [appDelegate update_count_mark];
  2963. // sqlite3_finalize(statement);
  2964. //
  2965. //
  2966. //
  2967. //
  2968. // }
  2969. //
  2970. // [iSalesDB close_db:db];
  2971. //
  2972. // return ret;
  2973. //}
  2974. +(NSDictionary*) offline_notimpl
  2975. {
  2976. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2977. ret[@"result"]=@"8";
  2978. ret[@"err_msg"]=@"offline mode does not support this function.";
  2979. return ret;
  2980. }
  2981. +(NSDictionary*) offline_home
  2982. {
  2983. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2984. NSString *cachefolder = [paths objectAtIndex:0];
  2985. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2986. NSData* json =nil;
  2987. json=[NSData dataWithContentsOfFile:img_cache];
  2988. if(json==nil)
  2989. return nil;
  2990. NSError *error=nil;
  2991. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2992. return menu;
  2993. }
  2994. +(NSDictionary*) offline_category_menu
  2995. {
  2996. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2997. NSString *cachefolder = [paths objectAtIndex:0];
  2998. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2999. NSData* json =nil;
  3000. json=[NSData dataWithContentsOfFile:img_cache];
  3001. if(json==nil)
  3002. return nil;
  3003. NSError *error=nil;
  3004. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3005. return menu;
  3006. }
  3007. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  3008. {
  3009. NSString* offline_command=params[@"offline_Command"];
  3010. NSDictionary* ret=nil;
  3011. if([offline_command isEqualToString:@"model_NIYMAL"])
  3012. {
  3013. NSString* category = params[@"category"];
  3014. ret = [self refresh_model_NIYMAL:category];
  3015. }
  3016. return ret;
  3017. }
  3018. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  3019. {
  3020. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3021. [ret setValue:@"2" forKey:@"result"];
  3022. [ret setValue:@"Regular Mode" forKey:@"mode"];
  3023. sqlite3* db= [iSalesDB get_db];
  3024. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3025. [iSalesDB close_db:db];
  3026. [ret setObject:detail1_section forKey:@"detail_1"];
  3027. return ret;
  3028. }
  3029. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  3030. {
  3031. //assert(user!=nil);
  3032. // UIApplication * app = [UIApplication sharedApplication];
  3033. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3034. // NSArray* arr1 = [self get_user_all_price_type];
  3035. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3036. // NSSet *set1 = [NSSet setWithArray:arr1];
  3037. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3038. // if(appDelegate.contact_id==nil)
  3039. // set2=[set1 mutableCopy];
  3040. // else
  3041. // [set2 intersectsSet:set1];
  3042. // NSArray *retarr = [set2 allObjects];
  3043. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3044. // sqlite3 *db = [iSalesDB get_db];
  3045. NSString* sqlQuery = nil;
  3046. if(contact_id==nil)
  3047. {
  3048. if(!blogin)
  3049. return nil;
  3050. 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];
  3051. }
  3052. else
  3053. 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];
  3054. sqlite3_stmt * statement;
  3055. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3056. int count=0;
  3057. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3058. {
  3059. while (sqlite3_step(statement) == SQLITE_ROW)
  3060. {
  3061. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  3062. char *name = (char*)sqlite3_column_text(statement, 0);
  3063. if(name==nil)
  3064. name="";
  3065. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3066. // double price = sqlite3_column_double(statement, 1);
  3067. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  3068. // if(isnull==nil)
  3069. // item[nsname]= @"No Price";
  3070. // else
  3071. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  3072. char *price = (char*)sqlite3_column_text(statement, 1);
  3073. if(price!=nil)
  3074. {
  3075. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3076. nsprice=[AESCrypt fastdecrypt:nsprice];
  3077. if(nsprice.length>0)
  3078. {
  3079. double dp= [nsprice doubleValue];
  3080. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  3081. }
  3082. }
  3083. else
  3084. {
  3085. item[nsname]= @"No Price";
  3086. }
  3087. // int type= sqlite3_column_int(statement, 2);
  3088. //item[@"type"]=@"price";
  3089. // item[nsname]= nsprice;
  3090. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3091. count++;
  3092. }
  3093. ret[@"count"]= [NSNumber numberWithInt:count];
  3094. sqlite3_finalize(statement);
  3095. }
  3096. // [iSalesDB close_db:db];
  3097. return ret;
  3098. }
  3099. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  3100. //{
  3101. // NSArray* arr1 = [self get_user_all_price_type:db];
  3102. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  3103. //
  3104. // // NSSet *set1 = [NSSet setWithArray:arr1];
  3105. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3106. // // [set2 intersectsSet:set1];
  3107. // //
  3108. // //
  3109. // // NSArray *retarr = [set2 allObjects];
  3110. //
  3111. // NSString* whereprice=nil;
  3112. // if(contact_id==nil)
  3113. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3114. // else
  3115. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3116. //
  3117. //
  3118. // // sqlite3 *db = [iSalesDB get_db];
  3119. //
  3120. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3121. // sqlite3_stmt * statement;
  3122. //
  3123. //
  3124. // NSNumber* ret = nil;
  3125. // double dprice=DBL_MAX;
  3126. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3127. // {
  3128. //
  3129. //
  3130. // while (sqlite3_step(statement) == SQLITE_ROW)
  3131. // {
  3132. //
  3133. // // double val = sqlite3_column_double(statement, 0);
  3134. // char *price = (char*)sqlite3_column_text(statement, 0);
  3135. // if(price!=nil)
  3136. // {
  3137. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3138. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3139. // if(nsprice.length>0)
  3140. // {
  3141. // double dp= [nsprice doubleValue];
  3142. // if(dp<dprice)
  3143. // dprice=dp;
  3144. // }
  3145. // }
  3146. // }
  3147. //
  3148. //
  3149. //
  3150. //
  3151. // sqlite3_finalize(statement);
  3152. //
  3153. //
  3154. //
  3155. //
  3156. // }
  3157. //
  3158. // // [iSalesDB close_db:db];
  3159. //
  3160. // if(dprice==DBL_MAX)
  3161. // ret= nil;
  3162. // else
  3163. // ret= [NSNumber numberWithDouble:dprice];
  3164. // return ret;
  3165. //}
  3166. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3167. {
  3168. assert(user!=nil);
  3169. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3170. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3171. NSString* whereprice=nil;
  3172. if(contact_id==nil)
  3173. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3174. else
  3175. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3176. // sqlite3 *db = [iSalesDB get_db];
  3177. NSString *productIdCondition = @"1 = 1";
  3178. if (product_id) {
  3179. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3180. }
  3181. NSString *itemIdCondition = @"";
  3182. if (item_id) {
  3183. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3184. }
  3185. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3186. sqlite3_stmt * statement;
  3187. NSNumber* ret = nil;
  3188. double dprice=DBL_MAX;
  3189. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3190. {
  3191. while (sqlite3_step(statement) == SQLITE_ROW)
  3192. {
  3193. // double val = sqlite3_column_double(statement, 0);
  3194. char *price = (char*)sqlite3_column_text(statement, 0);
  3195. if(price!=nil)
  3196. {
  3197. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3198. nsprice=[AESCrypt fastdecrypt:nsprice];
  3199. if(nsprice.length>0)
  3200. {
  3201. double dp= [nsprice doubleValue];
  3202. if(dp<dprice)
  3203. dprice=dp;
  3204. }
  3205. }
  3206. }
  3207. sqlite3_finalize(statement);
  3208. }
  3209. // [iSalesDB close_db:db];
  3210. if(dprice==DBL_MAX)
  3211. ret= nil;
  3212. else
  3213. ret= [NSNumber numberWithDouble:dprice];
  3214. return ret;
  3215. }
  3216. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3217. {
  3218. assert(user!=nil);
  3219. NSArray* ret=nil;
  3220. // sqlite3 *db = [iSalesDB get_db];
  3221. // no customer assigned , use login user contact_id
  3222. // UIApplication * app = [UIApplication sharedApplication];
  3223. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3224. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3225. sqlite3_stmt * statement;
  3226. // int count=0;
  3227. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3228. {
  3229. if (sqlite3_step(statement) == SQLITE_ROW)
  3230. {
  3231. char *val = (char*)sqlite3_column_text(statement, 0);
  3232. if(val==nil)
  3233. val="";
  3234. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3235. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3236. }
  3237. sqlite3_finalize(statement);
  3238. }
  3239. // [iSalesDB close_db:db];
  3240. return ret;
  3241. }
  3242. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3243. {
  3244. // sqlite3 *db = [iSalesDB get_db];
  3245. if(contact_id==nil)
  3246. {
  3247. // no customer assigned , use login user contact_id
  3248. // UIApplication * app = [UIApplication sharedApplication];
  3249. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3250. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3251. sqlite3_stmt * statement;
  3252. // int count=0;
  3253. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3254. {
  3255. if (sqlite3_step(statement) == SQLITE_ROW)
  3256. {
  3257. char *val = (char*)sqlite3_column_text(statement, 0);
  3258. if(val==nil)
  3259. val="";
  3260. contact_id = [[NSString alloc]initWithUTF8String:val];
  3261. }
  3262. sqlite3_finalize(statement);
  3263. }
  3264. if(contact_id.length<=0)
  3265. {
  3266. // [iSalesDB close_db:db];
  3267. return nil;
  3268. }
  3269. }
  3270. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3271. sqlite3_stmt * statement;
  3272. NSArray* ret=nil;
  3273. // int count=0;
  3274. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3275. {
  3276. if (sqlite3_step(statement) == SQLITE_ROW)
  3277. {
  3278. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3279. if(price_type==nil)
  3280. price_type="";
  3281. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3282. if(nsprice_type.length>0)
  3283. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3284. else
  3285. ret=nil;
  3286. }
  3287. sqlite3_finalize(statement);
  3288. }
  3289. // [iSalesDB close_db:db];
  3290. return ret;
  3291. }
  3292. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3293. {
  3294. NSString* ret= nil;
  3295. // sqlite3 *db = [iSalesDB get_db];
  3296. NSString *sqlQuery = nil;
  3297. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3298. // 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;
  3299. // 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
  3300. if(product_id==nil && model_name)
  3301. 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;
  3302. else if (product_id)
  3303. 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
  3304. sqlite3_stmt * statement;
  3305. // int count=0;
  3306. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3307. {
  3308. if (sqlite3_step(statement) == SQLITE_ROW)
  3309. {
  3310. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3311. if(imgurl==nil)
  3312. imgurl="";
  3313. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3314. ret=nsimgurl;
  3315. }
  3316. sqlite3_finalize(statement);
  3317. }
  3318. else
  3319. {
  3320. [ret setValue:@"8" forKey:@"result"];
  3321. }
  3322. // [iSalesDB close_db:db];
  3323. DebugLog(@"data string: %@",ret );
  3324. return ret;
  3325. }
  3326. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3327. {
  3328. NSString* ret= nil;
  3329. sqlite3 *db = [iSalesDB get_db];
  3330. NSString *sqlQuery = nil;
  3331. if(product_id==nil)
  3332. if(upc_code==nil)
  3333. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3334. else
  3335. 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='%@';
  3336. else
  3337. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3338. sqlite3_stmt * statement;
  3339. // int count=0;
  3340. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3341. {
  3342. if (sqlite3_step(statement) == SQLITE_ROW)
  3343. {
  3344. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3345. if(default_category==nil)
  3346. default_category="";
  3347. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3348. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3349. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3350. ret=nsdefault_category;
  3351. }
  3352. sqlite3_finalize(statement);
  3353. }
  3354. else
  3355. {
  3356. [ret setValue:@"8" forKey:@"result"];
  3357. }
  3358. [iSalesDB close_db:db];
  3359. DebugLog(@"data string: %@",ret );
  3360. return ret;
  3361. }
  3362. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3363. NSString *sql = nil;
  3364. if (product_id != nil) {
  3365. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3366. } else {
  3367. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3368. }
  3369. NSString *result_set = [iSalesDB jk_queryText:sql];
  3370. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3371. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3372. return result_set;
  3373. }
  3374. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3375. //{
  3376. //
  3377. //
  3378. // assert(params[@"user"]!=nil);
  3379. //
  3380. //
  3381. //
  3382. // NSString* model_name = [params valueForKey:@"product_name"];
  3383. //
  3384. // NSString* product_id = [params valueForKey:@"product_id"];
  3385. //
  3386. // NSString* category = [params valueForKey:@"category"];
  3387. //
  3388. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3389. // if(category==nil) {
  3390. // category = default_category_id;
  3391. // } else {
  3392. //
  3393. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3394. // // 参数重有多个category id
  3395. // if (arr_0.count > 1) {
  3396. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3397. //
  3398. // // 取交集
  3399. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3400. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3401. // [set_0 intersectSet:set_1];
  3402. //
  3403. // if (set_0.count == 1) {
  3404. // category = (NSString *)[set_0 anyObject];
  3405. // } else {
  3406. // if ([set_0 containsObject:default_category_id]) {
  3407. // category = default_category_id;
  3408. // } else {
  3409. // category = (NSString *)[set_0 anyObject];
  3410. // }
  3411. // }
  3412. // }
  3413. // }
  3414. //
  3415. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3416. //
  3417. //
  3418. //
  3419. //
  3420. //
  3421. //
  3422. // sqlite3 *db = [iSalesDB get_db];
  3423. //
  3424. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3425. //
  3426. //
  3427. // NSString *sqlQuery = nil;
  3428. //
  3429. // if(product_id==nil)
  3430. // 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='%@';
  3431. // else
  3432. //
  3433. // 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=%@;
  3434. //
  3435. //
  3436. // sqlite3_stmt * statement;
  3437. // [ret setValue:@"2" forKey:@"result"];
  3438. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3439. //
  3440. // // int count=0;
  3441. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3442. // {
  3443. //
  3444. //
  3445. // if (sqlite3_step(statement) == SQLITE_ROW)
  3446. // {
  3447. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3448. //
  3449. //
  3450. // char *name = (char*)sqlite3_column_text(statement, 0);
  3451. // if(name==nil)
  3452. // name="";
  3453. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3454. //
  3455. // char *description = (char*)sqlite3_column_text(statement, 1);
  3456. // if(description==nil)
  3457. // description="";
  3458. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3459. //
  3460. //
  3461. // int product_id = sqlite3_column_int(statement, 2);
  3462. //
  3463. //
  3464. // char *color = (char*)sqlite3_column_text(statement, 3);
  3465. // if(color==nil)
  3466. // color="";
  3467. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3468. // //
  3469. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3470. // // if(legcolor==nil)
  3471. // // legcolor="";
  3472. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3473. // //
  3474. // //
  3475. // int availability = sqlite3_column_int(statement, 5);
  3476. // //
  3477. // int incoming_stock = sqlite3_column_int(statement, 6);
  3478. //
  3479. //
  3480. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3481. // if(demension==nil)
  3482. // demension="";
  3483. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3484. //
  3485. //
  3486. //
  3487. // // ,,,,,,,,,
  3488. //
  3489. //
  3490. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3491. // if(seat_height==nil)
  3492. // seat_height="";
  3493. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3494. //
  3495. //
  3496. // char *material = (char*)sqlite3_column_text(statement, 9);
  3497. // if(material==nil)
  3498. // material="";
  3499. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3500. //
  3501. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3502. // if(box_dim==nil)
  3503. // box_dim="";
  3504. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3505. //
  3506. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3507. // if(volume==nil)
  3508. // volume="";
  3509. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3510. //
  3511. // double weight = sqlite3_column_double(statement, 12);
  3512. //
  3513. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3514. // if(model_set==nil)
  3515. // model_set="";
  3516. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3517. //
  3518. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3519. // if(load_ability==nil)
  3520. // load_ability="";
  3521. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3522. //
  3523. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3524. //// if(default_category==nil)
  3525. //// default_category="";
  3526. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3527. //
  3528. //
  3529. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3530. // if(fabric_content==nil)
  3531. // fabric_content="";
  3532. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3533. //
  3534. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3535. // if(assembling==nil)
  3536. // assembling="";
  3537. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3538. //
  3539. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3540. // if(made_in==nil)
  3541. // made_in="";
  3542. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3543. //
  3544. //
  3545. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3546. // if(special_remarks==nil)
  3547. // special_remarks="";
  3548. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3549. //
  3550. //
  3551. // int stockUcom = sqlite3_column_double(statement, 20);
  3552. //
  3553. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3554. // if(product_group==nil)
  3555. // product_group="";
  3556. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3557. //
  3558. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3559. // // if(fashion_selector==nil)
  3560. // // fashion_selector="";
  3561. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3562. //
  3563. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3564. // if(selector_field==nil)
  3565. // selector_field="";
  3566. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3567. //
  3568. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3569. // if(property_field==nil)
  3570. // property_field="";
  3571. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3572. //
  3573. //
  3574. //
  3575. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3576. // if(packaging==nil)
  3577. // packaging="";
  3578. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3579. //
  3580. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3581. //
  3582. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3583. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3584. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3585. //
  3586. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3587. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3588. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3589. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3590. //
  3591. //
  3592. //
  3593. //
  3594. //
  3595. // NSString* Availability=nil;
  3596. // if(availability>0)
  3597. // Availability=[NSString stringWithFormat:@"%d",availability];
  3598. // else
  3599. // Availability = @"Out of Stock";
  3600. //
  3601. // [img_section setValue:Availability forKey:@"Availability"];
  3602. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3603. //
  3604. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3605. //
  3606. //
  3607. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3608. // if(eta==nil)
  3609. // eta="";
  3610. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3611. // if ([nseta isEqualToString:@"null"]) {
  3612. // nseta = @"";
  3613. // }
  3614. // if (availability <= 0) {
  3615. // [img_section setValue:nseta forKey:@"ETA"];
  3616. // }
  3617. //
  3618. //
  3619. // int item_id = sqlite3_column_int(statement, 26);
  3620. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3621. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3622. //
  3623. // NSString* Price=nil;
  3624. // if(appDelegate.bLogin==false)
  3625. // Price=@"Must Sign in.";
  3626. // else
  3627. // {
  3628. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3629. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3630. // if(price==nil)
  3631. // Price=@"No Price.";
  3632. // else
  3633. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3634. // }
  3635. //
  3636. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3637. // [img_section setValue:Price forKey:@"price"];
  3638. // [img_section setValue:nsname forKey:@"model_name"];
  3639. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3640. //
  3641. // if (appDelegate.order_code) { // 离线order code即so#
  3642. //
  3643. // 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];
  3644. // __block int cartQTY = 0;
  3645. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3646. //
  3647. // cartQTY = sqlite3_column_int(stmt, 0);
  3648. //
  3649. // }];
  3650. //
  3651. // if (cartQTY > 0) {
  3652. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3653. // }
  3654. // }
  3655. //
  3656. // [ret setObject:img_section forKey:@"img_section"];
  3657. //
  3658. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3659. // int detail0_item_count=0;
  3660. //
  3661. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3662. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3663. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3664. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3665. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3666. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3667. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3668. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3669. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3670. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3671. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3672. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3673. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3674. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3675. //
  3676. //
  3677. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3678. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3679. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3680. // {
  3681. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3682. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3683. // }
  3684. //
  3685. //
  3686. //
  3687. //
  3688. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3689. // [detail0_section setValue:@"kv" forKey:@"type"];
  3690. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3691. //
  3692. // [ret setObject:detail0_section forKey:@"detail_0"];
  3693. //
  3694. //
  3695. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3696. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3697. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3698. // // [detail1_section setValue:@"content" forKey:@"type"];
  3699. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3700. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3701. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3702. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3703. // [ret setObject:detail1_section forKey:@"detail_1"];
  3704. //
  3705. //
  3706. //
  3707. //
  3708. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3709. // [detail2_section setValue:@"detail" forKey:@"target"];
  3710. // [detail2_section setValue:@"popup" forKey:@"action"];
  3711. // [detail2_section setValue:@"content" forKey:@"type"];
  3712. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3713. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3714. // [detail2_section setValue:@"local" forKey:@"data"];
  3715. // [ret setObject:detail2_section forKey:@"detail_2"];
  3716. // }
  3717. //
  3718. //
  3719. //
  3720. //
  3721. // sqlite3_finalize(statement);
  3722. // }
  3723. // else
  3724. // {
  3725. // [ret setValue:@"8" forKey:@"result"];
  3726. // }
  3727. //// DebugLog(@"count:%d",count);
  3728. //
  3729. //
  3730. // [iSalesDB close_db:db];
  3731. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3732. //
  3733. // return ret;
  3734. //}
  3735. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3736. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3737. NSString* keyword = [params valueForKey:@"keyword"];
  3738. keyword=keyword.lowercaseString;
  3739. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3740. int limit = [[params valueForKey:@"limit"] intValue];
  3741. int offset = [[params valueForKey:@"offset"] intValue];
  3742. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3743. NSString *limit_str = @"";
  3744. if (limited) {
  3745. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3746. }
  3747. sqlite3 *db = [iSalesDB get_db];
  3748. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3749. // UIApplication * app = [UIApplication sharedApplication];
  3750. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3751. NSString *sqlQuery = nil;
  3752. if(exactMatch )
  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'%@%%' 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 ;
  3754. else
  3755. 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
  3756. DebugLog(@"offline_search sql:%@",sqlQuery);
  3757. sqlite3_stmt * statement;
  3758. [ret setValue:@"2" forKey:@"result"];
  3759. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3760. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3761. // int count=0;
  3762. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3763. {
  3764. int i=0;
  3765. while (sqlite3_step(statement) == SQLITE_ROW)
  3766. {
  3767. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3768. // char *name = (char*)sqlite3_column_text(statement, 1);
  3769. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3770. char *name = (char*)sqlite3_column_text(statement, 0);
  3771. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3772. char *description = (char*)sqlite3_column_text(statement, 1);
  3773. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3774. int product_id = sqlite3_column_int(statement, 2);
  3775. // char *url = (char*)sqlite3_column_text(statement, 3);
  3776. // if(url==nil)
  3777. // url="";
  3778. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3779. int wid = sqlite3_column_int(statement, 3);
  3780. int closeout = sqlite3_column_int(statement, 4);
  3781. int cid = sqlite3_column_int(statement, 5);
  3782. int wisdelete = sqlite3_column_int(statement, 6);
  3783. int more_color = sqlite3_column_int(statement, 7);
  3784. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3785. if(wid !=0 && wisdelete != 1)
  3786. [item setValue:@"true" forKey:@"wish_exists"];
  3787. else
  3788. [item setValue:@"false" forKey:@"wish_exists"];
  3789. if(closeout==0)
  3790. [item setValue:@"false" forKey:@"is_closeout"];
  3791. else
  3792. [item setValue:@"true" forKey:@"is_closeout"];
  3793. if(cid==0)
  3794. [item setValue:@"false" forKey:@"cart_exists"];
  3795. else
  3796. [item setValue:@"true" forKey:@"cart_exists"];
  3797. if (more_color == 0) {
  3798. [item setObject:@(false) forKey:@"more_color"];
  3799. } else if (more_color == 1) {
  3800. [item setObject:@(true) forKey:@"more_color"];
  3801. }
  3802. [item addEntriesFromDictionary:imgjson];
  3803. // [item setValue:nsurl forKey:@"img"];
  3804. [item setValue:nsname forKey:@"fash_name"];
  3805. [item setValue:nsdescription forKey:@"description"];
  3806. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3807. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3808. i++;
  3809. }
  3810. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3811. [ret setObject:items forKey:@"items"];
  3812. sqlite3_finalize(statement);
  3813. }
  3814. DebugLog(@"count:%d",count);
  3815. [iSalesDB close_db:db];
  3816. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3817. return ret;
  3818. }
  3819. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3820. {
  3821. return [self search:params limited:YES];
  3822. }
  3823. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3824. //{
  3825. // UIApplication * app = [UIApplication sharedApplication];
  3826. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3827. //
  3828. //// [iSalesDB disable_trigger]
  3829. // [iSalesDB disable_trigger];
  3830. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3831. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3832. //
  3833. // [iSalesDB enable_trigger];
  3834. //// [iSalesDB enable_trigger]
  3835. // //
  3836. // // NSString* user = [params valueForKey:@"user"];
  3837. // //
  3838. // // NSString* password = [params valueForKey:@"password"];
  3839. //
  3840. //
  3841. //
  3842. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3843. //
  3844. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3845. //
  3846. //
  3847. //
  3848. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3849. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3850. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3851. //
  3852. // [appDelegate update_count_mark];
  3853. //
  3854. //
  3855. // appDelegate.can_show_price =false;
  3856. // appDelegate.can_see_price =false;
  3857. // appDelegate.can_create_portfolio =false;
  3858. // appDelegate.can_create_order =false;
  3859. //
  3860. //
  3861. // appDelegate.can_cancel_order =false;
  3862. // appDelegate.can_set_cart_price =false;
  3863. // appDelegate.can_delete_order =false;
  3864. // appDelegate.can_submit_order =false;
  3865. // appDelegate.can_set_tearsheet_price =false;
  3866. // appDelegate.can_update_contact_info = false;
  3867. //
  3868. // appDelegate.save_order_logout = false;
  3869. // appDelegate.submit_order_logout = false;
  3870. // appDelegate.alert_sold_in_quantities = false;
  3871. //
  3872. // appDelegate.ipad_perm =nil ;
  3873. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3874. // appDelegate.OrderFilter= nil;
  3875. // [appDelegate SetSo:nil];
  3876. // [appDelegate set_main_button_panel];
  3877. //
  3878. //
  3879. // // sqlite3 *db = [iSalesDB get_db];
  3880. // //
  3881. // //
  3882. // //
  3883. // //
  3884. // //
  3885. // // 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"]];
  3886. // //
  3887. // //
  3888. // //
  3889. // //
  3890. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3891. // // sqlite3_stmt * statement;
  3892. // //
  3893. // //
  3894. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3895. // //
  3896. // //
  3897. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3898. // // {
  3899. // //
  3900. // //
  3901. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3902. // // {
  3903. // //
  3904. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3905. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3906. // //
  3907. // //
  3908. // //
  3909. // // int can_show_price = sqlite3_column_int(statement, 0);
  3910. // // int can_see_price = sqlite3_column_int(statement, 1);
  3911. // //
  3912. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3913. // // if(contact_id==nil)
  3914. // // contact_id="";
  3915. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3916. // //
  3917. // // int user_type = sqlite3_column_int(statement, 3);
  3918. // //
  3919. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3920. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3921. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3922. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3923. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3924. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3925. // // int can_create_order = sqlite3_column_int(statement, 10);
  3926. // //
  3927. // //
  3928. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3929. // // if(mode==nil)
  3930. // // mode="";
  3931. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3932. // //
  3933. // //
  3934. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3935. // // if(username==nil)
  3936. // // username="";
  3937. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3938. // //
  3939. // //
  3940. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3941. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3942. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3943. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3944. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3945. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3946. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3947. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3948. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3949. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3950. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3951. // //
  3952. // // [header setValue:nsusername forKey:@"username"];
  3953. // //
  3954. // //
  3955. // // [ret setObject:header forKey:@"header"];
  3956. // // [ret setValue:nsmode forKey:@"mode"];
  3957. // //
  3958. // //
  3959. // // }
  3960. // //
  3961. // //
  3962. // //
  3963. // // sqlite3_finalize(statement);
  3964. // // }
  3965. // //
  3966. // //
  3967. // //
  3968. // // [iSalesDB close_db:db];
  3969. // //
  3970. // //
  3971. // //
  3972. // //
  3973. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3974. //
  3975. // return ret;
  3976. //}
  3977. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3978. //{
  3979. //
  3980. // UIApplication * app = [UIApplication sharedApplication];
  3981. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3982. //
  3983. // [iSalesDB disable_trigger];
  3984. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3985. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3986. // [iSalesDB enable_trigger];
  3987. //
  3988. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3989. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3990. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3991. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3992. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3993. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3994. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3995. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3996. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3997. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3998. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3999. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  4000. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  4001. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  4002. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  4003. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  4004. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4005. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  4006. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  4007. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  4008. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  4009. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  4010. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  4011. //
  4012. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4013. //
  4014. // [arr_name addObject:customer_first_name];
  4015. // [arr_name addObject:customer_last_name];
  4016. //
  4017. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4018. //
  4019. // // default ship from
  4020. // 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';";
  4021. //
  4022. // __block NSString *cid = @"";
  4023. // __block NSString *name = @"";
  4024. // __block NSString *ext = @"";
  4025. // __block NSString *contact = @"";
  4026. // __block NSString *email = @"";
  4027. // __block NSString *fax = @"";
  4028. // __block NSString *phone = @"";
  4029. //
  4030. // sqlite3 *db = [iSalesDB get_db];
  4031. //
  4032. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  4033. //
  4034. // cid = [self textAtColumn:0 statement:statment];
  4035. // name = [self textAtColumn:1 statement:statment];
  4036. // ext = [self textAtColumn:2 statement:statment];
  4037. // contact = [self textAtColumn:3 statement:statment];
  4038. // email = [self textAtColumn:4 statement:statment];
  4039. // fax = [self textAtColumn:5 statement:statment];
  4040. // phone = [self textAtColumn:6 statement:statment];
  4041. //
  4042. // }];
  4043. //
  4044. // NSString* so_id = [self get_offline_soid:db];
  4045. // if(so_id==nil)
  4046. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  4047. //
  4048. // 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];
  4049. //
  4050. //
  4051. //
  4052. // int result =[iSalesDB execSql:sql_neworder db:db];
  4053. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  4054. //
  4055. //
  4056. //
  4057. // //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'
  4058. // //soId
  4059. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  4060. //
  4061. //
  4062. //
  4063. //
  4064. //
  4065. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  4066. // sqlite3_stmt * statement;
  4067. //
  4068. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4069. // {
  4070. // if (sqlite3_step(statement) == SQLITE_ROW)
  4071. // {
  4072. // // char *name = (char*)sqlite3_column_text(statement, 1);
  4073. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4074. //
  4075. // //ret = sqlite3_column_int(statement, 0);
  4076. //
  4077. // char *soId = (char*)sqlite3_column_text(statement, 0);
  4078. // if(soId==nil)
  4079. // soId="";
  4080. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  4081. // [ret setValue:nssoId forKey:@"soId"];
  4082. // [ret setValue:nssoId forKey:@"orderCode"];
  4083. //
  4084. // }
  4085. // sqlite3_finalize(statement);
  4086. // }
  4087. //
  4088. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  4089. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  4090. //
  4091. //
  4092. // [iSalesDB close_db:db];
  4093. //
  4094. // return [RAConvertor dict2data:ret];
  4095. //
  4096. //}
  4097. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  4098. {
  4099. //assert(params[@"order_code"]);
  4100. // assert(params[@"order_code"]);
  4101. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4102. NSString* orderCode = [params valueForKey:@"orderCode"];
  4103. NSString* app_order_code= params[@"appDelegate.order_code"];
  4104. // UIApplication * app = [UIApplication sharedApplication];
  4105. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4106. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  4107. {
  4108. [iSalesDB disable_trigger];
  4109. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4110. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4111. [iSalesDB enable_trigger];
  4112. }
  4113. sqlite3 *db = [iSalesDB get_db];
  4114. int cart_count=[self query_ordercartcount:orderCode db:db];
  4115. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4116. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4117. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4118. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4119. [iSalesDB close_db:db];
  4120. return [RAConvertor dict2data:ret];
  4121. }
  4122. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4123. {
  4124. assert(params[@"can_create_backorder"]!=nil);
  4125. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4126. sqlite3 *db = [iSalesDB get_db];
  4127. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4128. int count =0;
  4129. if(params[@"count"]!=nil)
  4130. {
  4131. count = [params[@"count"] intValue];
  4132. }
  4133. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4134. NSString* product_id=params[@"product_id"];
  4135. NSString* orderCode=params[@"orderCode"];
  4136. NSString *qty = params[@"qty"];
  4137. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4138. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4139. __block int number_of_outOfStock = 0;
  4140. for(int i=0;i<arr_id.count;i++)
  4141. {
  4142. NSInteger item_qty= count;
  4143. if (qty) {
  4144. item_qty = [qty_arr[i] integerValue];
  4145. }
  4146. if(item_qty==0)
  4147. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4148. // 检查新加Model数量是否大于库存
  4149. if (![params[@"can_create_backorder"] boolValue]) {
  4150. __block BOOL needContinue = NO;
  4151. [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) {
  4152. int availability = sqlite3_column_int(stmt, 0);
  4153. // 库存小于购买量为缺货
  4154. if (availability < item_qty || availability <= 0) {
  4155. number_of_outOfStock++;
  4156. needContinue = YES;
  4157. }
  4158. }];
  4159. if (needContinue) {
  4160. continue;
  4161. }
  4162. }
  4163. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4164. NSString* sql=nil;
  4165. sqlite3_stmt *stmt = nil;
  4166. BOOL shouldStep = NO;
  4167. if(_id<0)
  4168. {
  4169. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4170. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4171. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4172. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4173. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4174. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4175. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4176. shouldStep = YES;
  4177. }
  4178. else
  4179. {
  4180. if (qty) { // wish list move to cart
  4181. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4182. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4183. sqlite3_bind_int(stmt, 1, _id);
  4184. shouldStep = YES;
  4185. } else {
  4186. 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];
  4187. __block BOOL update = YES;
  4188. if (![params[@"can_create_backorder"] boolValue]) {
  4189. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4190. int newQTY = sqlite3_column_int(stmt, 0);
  4191. int availability = sqlite3_column_int(stmt, 1);
  4192. if (newQTY > availability) { // 库存不够
  4193. update = NO;
  4194. number_of_outOfStock++;
  4195. }
  4196. }];
  4197. }
  4198. if (update) {
  4199. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4200. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4201. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4202. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4203. shouldStep = YES;
  4204. }
  4205. }
  4206. }
  4207. if (shouldStep) {
  4208. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4209. [iSalesDB execSql:@"ROLLBACK" db:db];
  4210. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4211. [iSalesDB close_db:db];
  4212. DebugLog(@"add to cart error");
  4213. return [RAConvertor dict2data:ret];
  4214. }
  4215. }
  4216. }
  4217. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4218. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4219. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4220. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4221. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4222. if (![params[@"can_create_backorder"] boolValue]) {
  4223. if (number_of_outOfStock > 0) {
  4224. ret[@"result"]=[NSNumber numberWithInt:8];
  4225. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4226. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4227. }
  4228. }
  4229. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4230. [iSalesDB close_db:db];
  4231. return [RAConvertor dict2data:ret];
  4232. }
  4233. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4234. {
  4235. // UIApplication * app = [UIApplication sharedApplication];
  4236. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4237. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4238. 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];
  4239. sqlite3_stmt * statement;
  4240. int count=0;
  4241. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4242. {
  4243. while (sqlite3_step(statement) == SQLITE_ROW)
  4244. {
  4245. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4246. int bitem_id = sqlite3_column_int(statement, 0);
  4247. int bitem_qty = sqlite3_column_int(statement, 1);
  4248. char *name = (char*)sqlite3_column_text(statement, 2);
  4249. if(name==nil)
  4250. name="";
  4251. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4252. char *description = (char*)sqlite3_column_text(statement, 3);
  4253. if(description==nil)
  4254. description="";
  4255. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4256. double unit_price = sqlite3_column_double(statement, 4);
  4257. int use_unitprice = sqlite3_column_int(statement, 5);
  4258. if(use_unitprice!=1)
  4259. {
  4260. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4261. }
  4262. itemjson[@"model"]=nsname;
  4263. itemjson[@"description"]=nsdescription;
  4264. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4265. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4266. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4267. if(compute)
  4268. {
  4269. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4270. }
  4271. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4272. count++;
  4273. }
  4274. sqlite3_finalize(statement);
  4275. }
  4276. ret[@"count"]=@(count);
  4277. if(count==0)
  4278. return nil;
  4279. else
  4280. return ret;
  4281. }
  4282. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4283. {
  4284. //compute: add part to subtotal;
  4285. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4286. dict_item[@(item_id)]=@"1";
  4287. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4288. double cuft=0;
  4289. double weight=0;
  4290. int carton=0;
  4291. 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];
  4292. sqlite3_stmt * statement;
  4293. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4294. {
  4295. if (sqlite3_step(statement) == SQLITE_ROW)
  4296. {
  4297. double ulength = sqlite3_column_double(statement, 0);
  4298. double uwidth = sqlite3_column_double(statement, 1);
  4299. double uheight = sqlite3_column_double(statement, 2);
  4300. double uweight = sqlite3_column_double(statement, 3);
  4301. double mlength = sqlite3_column_double(statement, 4);
  4302. double mwidth = sqlite3_column_double(statement, 5);
  4303. double mheight = sqlite3_column_double(statement, 6);
  4304. double mweight = sqlite3_column_double(statement, 7);
  4305. double ilength = sqlite3_column_double(statement, 8);
  4306. double iwidth = sqlite3_column_double(statement, 9);
  4307. double iheight = sqlite3_column_double(statement, 10);
  4308. double iweight = sqlite3_column_double(statement, 11);
  4309. // int pcs = sqlite3_column_int(statement,12);
  4310. int mpack = sqlite3_column_int(statement, 13);
  4311. int ipack = sqlite3_column_int(statement, 14);
  4312. double ucbf = sqlite3_column_double(statement, 15);
  4313. // double icbf = sqlite3_column_double(statement, 16);
  4314. // double mcbf = sqlite3_column_double(statement, 17);
  4315. if(ipack==0)
  4316. {
  4317. carton= count/mpack ;
  4318. weight = mweight*carton;
  4319. cuft= carton*(mlength*mwidth*mheight);
  4320. int remain=count%mpack;
  4321. if(remain==0)
  4322. {
  4323. //do nothing;
  4324. }
  4325. else
  4326. {
  4327. carton++;
  4328. weight += uweight*remain;
  4329. cuft += (ulength*uwidth*uheight)*remain;
  4330. }
  4331. }
  4332. else
  4333. {
  4334. carton = count/(mpack*ipack);
  4335. weight = mweight*carton;
  4336. cuft= carton*(mlength*mwidth*mheight);
  4337. int remain=count%(mpack*ipack);
  4338. if(remain==0)
  4339. {
  4340. // do nothing;
  4341. }
  4342. else
  4343. {
  4344. carton++;
  4345. int icarton =remain/ipack;
  4346. int iremain=remain%ipack;
  4347. weight += iweight*icarton;
  4348. cuft += (ilength*iwidth*iheight)*icarton;
  4349. if(iremain==0)
  4350. {
  4351. //do nothing;
  4352. }
  4353. else
  4354. {
  4355. weight += uweight*iremain;
  4356. cuft += (ulength*uwidth*uheight)*iremain;
  4357. }
  4358. }
  4359. }
  4360. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4361. cuft=ucbf*count;
  4362. weight= uweight*count;
  4363. #endif
  4364. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4365. cuft=ucbf*count;
  4366. weight= uweight*count;
  4367. #endif
  4368. }
  4369. sqlite3_finalize(statement);
  4370. }
  4371. if(compute)
  4372. {
  4373. NSArray * arr_count=nil;
  4374. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4375. for(int i=0;i<arr_bundle.count;i++)
  4376. {
  4377. dict_item[arr_bundle[i]]=@"1";
  4378. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4379. cuft+=[bundlejson[@"cuft"] doubleValue];
  4380. weight+=[bundlejson[@"weight"] doubleValue];
  4381. carton+=[bundlejson[@"carton"] intValue];
  4382. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4383. }
  4384. }
  4385. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4386. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4387. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4388. ret[@"items"]=dict_item;
  4389. return ret;
  4390. }
  4391. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4392. {
  4393. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4394. // 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 ];
  4395. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4396. DebugLog(@"offline_login sql:%@",sqlQuery);
  4397. sqlite3_stmt * statement;
  4398. int cart_count=0;
  4399. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4400. if ( dbresult== SQLITE_OK)
  4401. {
  4402. while (sqlite3_step(statement) == SQLITE_ROW)
  4403. {
  4404. int item_id = sqlite3_column_int(statement, 0);
  4405. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4406. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4407. }
  4408. sqlite3_finalize(statement);
  4409. }
  4410. return cart_count;
  4411. }
  4412. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4413. //{
  4414. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4415. // sqlite3 *db = [iSalesDB get_db];
  4416. // UIApplication * app = [UIApplication sharedApplication];
  4417. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4418. //
  4419. // NSString* orderCode=params[@"orderCode"];
  4420. //
  4421. // int sort = [[params objectForKey:@"sort"] intValue];
  4422. // NSString *sort_str = @"";
  4423. // switch (sort) {
  4424. // case 0:{
  4425. // sort_str = @"order by c.modify_time desc";
  4426. // }
  4427. // break;
  4428. // case 1:{
  4429. // sort_str = @"order by c.modify_time asc";
  4430. // }
  4431. // break;
  4432. // case 2:{
  4433. // sort_str = @"order by m.name asc";
  4434. // }
  4435. // break;
  4436. // case 3:{
  4437. // sort_str = @"order by m.name desc";
  4438. // }
  4439. // break;
  4440. // case 4:{
  4441. // sort_str = @"order by m.description asc";
  4442. // }
  4443. // break;
  4444. //
  4445. // default:
  4446. // break;
  4447. //
  4448. // }
  4449. //
  4450. //
  4451. //
  4452. // 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 ];
  4453. //
  4454. //
  4455. //// 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 ];
  4456. //
  4457. //
  4458. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4459. //
  4460. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4461. // sqlite3_stmt * statement;
  4462. //
  4463. //
  4464. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4465. //
  4466. // NSDate *date1 = [NSDate date];
  4467. //
  4468. // int count=0;
  4469. // int cart_count=0;
  4470. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4471. // if ( dbresult== SQLITE_OK)
  4472. // {
  4473. //
  4474. //
  4475. // while (sqlite3_step(statement) == SQLITE_ROW)
  4476. // {
  4477. //// NSDate *row_date = [NSDate date];
  4478. //
  4479. //
  4480. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4481. //
  4482. //
  4483. //
  4484. //
  4485. // int product_id = sqlite3_column_int(statement, 0);
  4486. //
  4487. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4488. //
  4489. // int item_id = sqlite3_column_int(statement, 7);
  4490. //
  4491. // NSString* Price=nil;
  4492. // if(str_price==nil)
  4493. // {
  4494. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4495. //// NSDate *price_date = [NSDate date];
  4496. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4497. //// DebugLog(@"price time interval");
  4498. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4499. //
  4500. // if(price==nil)
  4501. // Price=@"No Price.";
  4502. // else
  4503. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4504. // }
  4505. // else
  4506. // {
  4507. //
  4508. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4509. // }
  4510. //
  4511. //
  4512. // double discount = sqlite3_column_double(statement, 2);
  4513. // int item_count = sqlite3_column_int(statement, 3);
  4514. //
  4515. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4516. // NSString *nsline_note=nil;
  4517. // if(line_note!=nil)
  4518. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4519. //
  4520. //
  4521. // char *name = (char*)sqlite3_column_text(statement, 5);
  4522. // NSString *nsname=nil;
  4523. // if(name!=nil)
  4524. // nsname= [[NSString alloc]initWithUTF8String:name];
  4525. //
  4526. // char *description = (char*)sqlite3_column_text(statement, 6);
  4527. // NSString *nsdescription=nil;
  4528. // if(description!=nil)
  4529. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4530. //
  4531. //
  4532. //
  4533. // int stockUom = sqlite3_column_int(statement, 8);
  4534. // int _id = sqlite3_column_int(statement, 9);
  4535. // int availability = sqlite3_column_int(statement, 10);
  4536. //
  4537. //// NSDate *subtotal_date = [NSDate date];
  4538. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4539. //// DebugLog(@"subtotal_date time interval");
  4540. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4541. //
  4542. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4543. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4544. // int carton=[bsubtotaljson[@"carton"] intValue];
  4545. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4546. //
  4547. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4548. //// NSDate *img_date = [NSDate date];
  4549. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4550. //// DebugLog(@"img_date time interval");
  4551. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4552. //
  4553. // itemjson[@"model"]=nsname;
  4554. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4555. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4556. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4557. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4558. // itemjson[@"check"]=@"true";
  4559. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4560. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4561. // itemjson[@"unit_price"]=Price;
  4562. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4563. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4564. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4565. // itemjson[@"note"]=nsline_note;
  4566. // if (!appDelegate.can_create_backorder) {
  4567. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4568. // }
  4569. //// NSDate *date2 = [NSDate date];
  4570. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4571. //// DebugLog(@"model_bundle time interval");
  4572. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4573. //
  4574. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4575. // count++;
  4576. //
  4577. //// DebugLog(@"row time interval");
  4578. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4579. // }
  4580. //
  4581. //
  4582. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4583. // sqlite3_finalize(statement);
  4584. // }
  4585. //
  4586. //
  4587. // DebugLog(@"request cart total time interval");
  4588. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4589. //
  4590. //
  4591. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4592. //
  4593. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4594. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4595. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4596. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4597. //
  4598. // ret[@"mode"]=@"Regular Mode";
  4599. //
  4600. // [iSalesDB close_db:db];
  4601. //
  4602. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4603. //
  4604. // DebugLog(@"general notes :%@",general_note);
  4605. //
  4606. // ret[@"general_note"]= general_note;
  4607. //
  4608. // return [RAConvertor dict2data:ret];
  4609. //}
  4610. +(NSData*) offline_login :(NSMutableDictionary *) params
  4611. {
  4612. NSString* user = [params valueForKey:@"user"];
  4613. NSString* password = [params valueForKey:@"password"];
  4614. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4615. sqlite3 *db = [iSalesDB get_db];
  4616. 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"]];
  4617. DebugLog(@"offline_login sql:%@",sqlQuery);
  4618. sqlite3_stmt * statement;
  4619. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4620. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4621. {
  4622. if (sqlite3_step(statement) == SQLITE_ROW)
  4623. {
  4624. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4625. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4626. int can_show_price = sqlite3_column_int(statement, 0);
  4627. int can_see_price = sqlite3_column_int(statement, 1);
  4628. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4629. if(contact_id==nil)
  4630. contact_id="";
  4631. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4632. int user_type = sqlite3_column_int(statement, 3);
  4633. int can_cancel_order = sqlite3_column_int(statement, 4);
  4634. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4635. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4636. int can_delete_order = sqlite3_column_int(statement, 7);
  4637. int can_submit_order = sqlite3_column_int(statement, 8);
  4638. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4639. int can_create_order = sqlite3_column_int(statement, 10);
  4640. char *mode = (char*)sqlite3_column_text(statement, 11);
  4641. if(mode==nil)
  4642. mode="";
  4643. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4644. char *username = (char*)sqlite3_column_text(statement, 12);
  4645. if(username==nil)
  4646. username="";
  4647. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4648. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4649. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4650. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4651. [header setValue:nscontact_id forKey:@"contact_id"];
  4652. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4653. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4654. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4655. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4656. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4657. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4658. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4659. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4660. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4661. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4662. [header setValue:nsusername forKey:@"username"];
  4663. NSError* error=nil;
  4664. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4665. [header setValue:statusFilter forKey:@"statusFilter"];
  4666. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4667. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4668. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4669. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4670. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4671. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4672. [ret setObject:header forKey:@"header"];
  4673. [ret setValue:nsmode forKey:@"mode"];
  4674. }
  4675. sqlite3_finalize(statement);
  4676. }
  4677. [iSalesDB close_db:db];
  4678. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4679. return [RAConvertor dict2data:ret];
  4680. }
  4681. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4682. {
  4683. assert(params[@"mode"]!=nil);
  4684. NSString* contactId = [params valueForKey:@"contactId"];
  4685. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4686. sqlite3 *db = [iSalesDB get_db];
  4687. NSString *sqlQuery = nil;
  4688. {
  4689. 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];
  4690. }
  4691. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4692. sqlite3_stmt * statement;
  4693. [ret setValue:@"2" forKey:@"result"];
  4694. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4695. {
  4696. //int i = 0;
  4697. if (sqlite3_step(statement) == SQLITE_ROW)
  4698. {
  4699. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4700. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4701. // int editable = sqlite3_column_int(statement, 0);
  4702. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4703. NSString *nscompany_name =nil;
  4704. if(company_name==nil)
  4705. nscompany_name=@"";
  4706. else
  4707. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4708. char *country = (char*)sqlite3_column_text(statement, 2);
  4709. if(country==nil)
  4710. country="";
  4711. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4712. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4713. // if(addr==nil)
  4714. // addr="";
  4715. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4716. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4717. if(zipcode==nil)
  4718. zipcode="";
  4719. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4720. char *state = (char*)sqlite3_column_text(statement, 5);
  4721. if(state==nil)
  4722. state="";
  4723. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4724. char *city = (char*)sqlite3_column_text(statement, 6);
  4725. if(city==nil)
  4726. city="";
  4727. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4728. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4729. // NSString *nscontact_name = nil;
  4730. // if(contact_name==nil)
  4731. // nscontact_name=@"";
  4732. // else
  4733. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4734. char *phone = (char*)sqlite3_column_text(statement, 8);
  4735. NSString *nsphone = nil;
  4736. if(phone==nil)
  4737. nsphone=@"";
  4738. else
  4739. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4740. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4741. // if(contact_id==nil)
  4742. // contact_id="";
  4743. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4744. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4745. if(addr_1==nil)
  4746. addr_1="";
  4747. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4748. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4749. if(addr_2==nil)
  4750. addr_2="";
  4751. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4752. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4753. if(addr_3==nil)
  4754. addr_3="";
  4755. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4756. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4757. if(addr_4==nil)
  4758. addr_4="";
  4759. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4760. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4761. if(first_name==nil)
  4762. first_name="";
  4763. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4764. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4765. if(last_name==nil)
  4766. last_name="";
  4767. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4768. char *fax = (char*)sqlite3_column_text(statement, 16);
  4769. NSString *nsfax = nil;
  4770. if(fax==nil)
  4771. nsfax=@"";
  4772. else
  4773. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4774. char *email = (char*)sqlite3_column_text(statement, 17);
  4775. NSString *nsemail = nil;
  4776. if(email==nil)
  4777. nsemail=@"";
  4778. else
  4779. nsemail= [[NSString alloc]initWithUTF8String:email];
  4780. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4781. NSString *nsimg_0 = nil;
  4782. if(img_0==nil)
  4783. nsimg_0=@"";
  4784. else
  4785. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4786. [self copy_bcardImg:nsimg_0];
  4787. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4788. NSString *nsimg_1 = nil;
  4789. if(img_1==nil)
  4790. nsimg_1=@"";
  4791. else
  4792. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4793. [self copy_bcardImg:nsimg_1];
  4794. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4795. NSString *nsimg_2 = nil;
  4796. if(img_2==nil)
  4797. nsimg_2=@"";
  4798. else
  4799. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4800. [self copy_bcardImg:nsimg_2];
  4801. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4802. NSString *nsprice_type = nil;
  4803. if(price_type==nil)
  4804. nsprice_type=@"";
  4805. else
  4806. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4807. char *notes = (char*)sqlite3_column_text(statement, 22);
  4808. NSString *nsnotes = nil;
  4809. if(notes==nil)
  4810. nsnotes=@"";
  4811. else
  4812. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4813. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4814. NSString *nssalesrep = nil;
  4815. if(salesrep==nil)
  4816. nssalesrep=@"";
  4817. else
  4818. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4819. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4820. {
  4821. // decrypt
  4822. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4823. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4824. nsphone=[AESCrypt fastdecrypt:nsphone];
  4825. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4826. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4827. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4828. }
  4829. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4830. [arr_name addObject:nsfirst_name];
  4831. [arr_name addObject:nslast_name];
  4832. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4833. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4834. [arr_ext addObject:nsaddr_1];
  4835. [arr_ext addObject:nsaddr_2];
  4836. [arr_ext addObject:nsaddr_3];
  4837. [arr_ext addObject:nsaddr_4];
  4838. [arr_ext addObject:@"\r\n"];
  4839. [arr_ext addObject:nscity];
  4840. [arr_ext addObject:nsstate];
  4841. [arr_ext addObject:nszipcode];
  4842. [arr_ext addObject:nscountry];
  4843. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4844. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4845. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4846. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4847. [item setValue:nscountry forKey:@"customer_country"];
  4848. [item setValue:nsphone forKey:@"customer_phone"];
  4849. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4850. [item setValue:nscompany_name forKey:@"customer_name"];
  4851. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4852. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4853. [item setValue:nsext forKey:@"customer_contact_ext"];
  4854. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4855. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4856. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4857. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4858. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4859. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4860. [item setValue:nslast_name forKey:@"customer_last_name"];
  4861. [item setValue:nscity forKey:@"customer_city"];
  4862. [item setValue:nsstate forKey:@"customer_state"];
  4863. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4864. [item setValue:contactId forKey:@"customer_cid"];
  4865. [item setValue:nscontact_name forKey:@"customer_contact"];
  4866. [item setValue:nsfax forKey:@"customer_fax"];
  4867. [item setValue:nsemail forKey:@"customer_email"];
  4868. [item setValue:contact_type forKey:@"customer_contact_type"];
  4869. [ret setObject:item forKey:@"customerInfo"];
  4870. // i++;
  4871. }
  4872. // UIApplication * app = [UIApplication sharedApplication];
  4873. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4874. [ret setValue:params[@"mode"] forKey:@"mode"];
  4875. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4876. sqlite3_finalize(statement);
  4877. }
  4878. [iSalesDB close_db:db];
  4879. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4880. return ret;
  4881. }
  4882. + (bool) copy_bcardImg:(NSString*) filename
  4883. {
  4884. if(filename.length==0)
  4885. return false;
  4886. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4887. bool ret=false;
  4888. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4889. NSString *cachefolder = [paths objectAtIndex:0];
  4890. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4891. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4892. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4893. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4894. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4895. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4896. //
  4897. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4898. NSFileManager* fileManager = [NSFileManager defaultManager];
  4899. BOOL bdir=NO;
  4900. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4901. {
  4902. NSError *error = nil;
  4903. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4904. {
  4905. ret=false;
  4906. }
  4907. else
  4908. {
  4909. ret=true;
  4910. }
  4911. // NSError *error = nil;
  4912. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4913. //
  4914. // if(!bsuccess)
  4915. // {
  4916. // DebugLog(@"Create offline_createimg folder failed");
  4917. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4918. // return [RAConvertor dict2data:ret];
  4919. // }
  4920. // if(bsuccess)
  4921. // {
  4922. // sqlite3 *db = [self get_db];
  4923. //
  4924. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4925. // [iSalesDB close_db:db];
  4926. // }
  4927. }
  4928. return ret;
  4929. //
  4930. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4931. // if(bsuccess)
  4932. // {
  4933. // NSError *error = nil;
  4934. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4935. // {
  4936. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4937. // }
  4938. // else
  4939. // {
  4940. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4941. //
  4942. // ret[@"img_url_aname"]=filename;
  4943. // ret[@"img_url"]=savedImagePath;
  4944. // }
  4945. // }
  4946. }
  4947. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4948. {
  4949. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4950. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4951. NSString *cachefolder = [paths objectAtIndex:0];
  4952. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4953. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4954. NSFileManager* fileManager = [NSFileManager defaultManager];
  4955. BOOL bdir=YES;
  4956. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4957. {
  4958. NSError *error = nil;
  4959. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4960. if(!bsuccess)
  4961. {
  4962. DebugLog(@"Create offline_createimg folder failed");
  4963. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4964. return [RAConvertor dict2data:ret];
  4965. }
  4966. // if(bsuccess)
  4967. // {
  4968. // sqlite3 *db = [self get_db];
  4969. //
  4970. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4971. // [iSalesDB close_db:db];
  4972. // }
  4973. }
  4974. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4975. {
  4976. NSError *error = nil;
  4977. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4978. if(!bsuccess)
  4979. {
  4980. DebugLog(@"Create img_cache folder failed");
  4981. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4982. return [RAConvertor dict2data:ret];
  4983. }
  4984. // if(bsuccess)
  4985. // {
  4986. // sqlite3 *db = [self get_db];
  4987. //
  4988. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4989. // [iSalesDB close_db:db];
  4990. // }
  4991. }
  4992. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4993. //JEPG格式
  4994. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4995. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4996. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4997. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4998. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4999. if(bsuccess)
  5000. {
  5001. NSError *error = nil;
  5002. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5003. {
  5004. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5005. }
  5006. else
  5007. {
  5008. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5009. ret[@"img_url_aname"]=filename;
  5010. ret[@"img_url"]=filename;
  5011. }
  5012. }
  5013. else
  5014. {
  5015. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5016. }
  5017. return [RAConvertor dict2data:ret];
  5018. }
  5019. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  5020. {
  5021. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  5022. category = [category substringToIndex:3];
  5023. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5024. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  5025. params[@"category"]= category;
  5026. ret[@"params"]= params;
  5027. [ret setValue:@"detail" forKey:@"target"];
  5028. [ret setValue:@"popup" forKey:@"action"];
  5029. [ret setValue:@"content" forKey:@"type"];
  5030. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  5031. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  5032. [ret setValue:@"true" forKey:@"single_row"];
  5033. [ret setValue:@"true" forKey:@"partial_refresh"];
  5034. // sqlite3 *db = [iSalesDB get_db];
  5035. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  5036. NSString *sqlQuery =nil;
  5037. #ifdef BUILD_NPD
  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 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 ;
  5039. #else
  5040. 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];
  5041. #endif
  5042. sqlite3_stmt * statement;
  5043. int count = 0;
  5044. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  5045. // int count=0;
  5046. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5047. {
  5048. int i=0;
  5049. while (sqlite3_step(statement) == SQLITE_ROW)
  5050. {
  5051. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5052. // char *name = (char*)sqlite3_column_text(statement, 1);
  5053. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5054. char *name = (char*)sqlite3_column_text(statement, 0);
  5055. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5056. int product_id = sqlite3_column_int(statement, 1);
  5057. char *url = (char*)sqlite3_column_text(statement, 2);
  5058. if(url==nil)
  5059. url="";
  5060. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5061. [item setValue:nsurl forKey:@"picture_path"];
  5062. [item setValue:nsname forKey:@"fash_name"];
  5063. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5064. [item setValue:category forKey:@"category"];
  5065. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5066. i++;
  5067. }
  5068. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5069. sqlite3_finalize(statement);
  5070. }
  5071. DebugLog(@"count:%d",count);
  5072. // [iSalesDB close_db:db];
  5073. return ret;
  5074. }
  5075. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  5076. {
  5077. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5078. [ret setValue:key forKey:@"key"];
  5079. [ret setValue:value forKey:@"val"];
  5080. [ret setValue:@"price" forKey:@"type"];
  5081. return ret;
  5082. }
  5083. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  5084. {
  5085. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5086. [ret setValue:key forKey:@"key"];
  5087. [ret setValue:value forKey:@"val"];
  5088. return ret;
  5089. }
  5090. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  5091. {
  5092. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5093. [ret setValue:@"0" forKey:@"img_count"];
  5094. // sqlite3 *db = [iSalesDB get_db];
  5095. 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 ;
  5096. sqlite3_stmt * statement;
  5097. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5098. {
  5099. int i=0;
  5100. if (sqlite3_step(statement) == SQLITE_ROW)
  5101. {
  5102. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5103. // char *name = (char*)sqlite3_column_text(statement, 1);
  5104. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5105. char *value = (char*)sqlite3_column_text(statement, 0);
  5106. if(value==nil)
  5107. value="";
  5108. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5109. char *key = (char*)sqlite3_column_text(statement, 1);
  5110. if(key==nil)
  5111. key="";
  5112. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5113. [item setValue:nsvalue forKey:@"val"];
  5114. [item setValue:nskey forKey:@"key"];
  5115. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5116. [ret setValue:@"1" forKey:@"count"];
  5117. i++;
  5118. }
  5119. sqlite3_finalize(statement);
  5120. }
  5121. // [iSalesDB close_db:db];
  5122. return ret;
  5123. }
  5124. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5125. {
  5126. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5127. [ret setValue:@"0" forKey:@"count"];
  5128. // sqlite3 *db = [iSalesDB get_db];
  5129. 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;
  5130. sqlite3_stmt * statement;
  5131. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5132. {
  5133. int i=0;
  5134. while (sqlite3_step(statement) == SQLITE_ROW)
  5135. {
  5136. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5137. // char *name = (char*)sqlite3_column_text(statement, 1);
  5138. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5139. char *value = (char*)sqlite3_column_text(statement, 0);
  5140. if(value==nil)
  5141. value="";
  5142. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5143. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5144. if(selector_display==nil)
  5145. selector_display="";
  5146. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5147. int product_id = sqlite3_column_int(statement, 2);
  5148. char *category = (char*)sqlite3_column_text(statement, 3);
  5149. if(category==nil)
  5150. category="";
  5151. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5152. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5153. [item setValue:nsvalue forKey:@"title"];
  5154. [item setValue:url forKey:@"pic_url"];
  5155. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5156. [params setValue:@"2" forKey:@"count"];
  5157. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5158. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5159. [param0 setValue:@"product_id" forKey:@"name"];
  5160. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5161. [param1 setValue:nscategory forKey:@"val"];
  5162. [param1 setValue:@"category" forKey:@"name"];
  5163. [params setObject:param0 forKey:@"param_0"];
  5164. [params setObject:param1 forKey:@"param_1"];
  5165. [item setObject:params forKey:@"params"];
  5166. [ret setValue:nsselector_display forKey:@"name"];
  5167. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5168. i++;
  5169. }
  5170. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5171. [ret setValue:@"switch" forKey:@"action"];
  5172. sqlite3_finalize(statement);
  5173. }
  5174. // [iSalesDB close_db:db];
  5175. return ret;
  5176. }
  5177. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5178. {
  5179. // model 在 category search 显示的图片。
  5180. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5181. // sqlite3 *db = [iSalesDB get_db];
  5182. 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];
  5183. sqlite3_stmt * statement;
  5184. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5185. {
  5186. while (sqlite3_step(statement) == SQLITE_ROW)
  5187. {
  5188. char *url = (char*)sqlite3_column_text(statement, 0);
  5189. if(url==nil)
  5190. url="";
  5191. int type = sqlite3_column_int(statement, 1);
  5192. if(type==0)
  5193. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5194. else
  5195. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5196. }
  5197. sqlite3_finalize(statement);
  5198. }
  5199. // [iSalesDB close_db:db];
  5200. return ret;
  5201. }
  5202. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5203. {
  5204. int item_id=-1;
  5205. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5206. sqlite3_stmt * statement;
  5207. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5208. {
  5209. if (sqlite3_step(statement) == SQLITE_ROW)
  5210. {
  5211. item_id = sqlite3_column_int(statement, 0);
  5212. }
  5213. sqlite3_finalize(statement);
  5214. }
  5215. return item_id;
  5216. }
  5217. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5218. {
  5219. // NSString* ret = @"";
  5220. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5221. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5222. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5223. sqlite3_stmt * statement;
  5224. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5225. {
  5226. while (sqlite3_step(statement) == SQLITE_ROW)
  5227. {
  5228. int bitem_id = sqlite3_column_int(statement, 0);
  5229. int bitem_qty = sqlite3_column_int(statement, 1);
  5230. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5231. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5232. }
  5233. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5234. sqlite3_finalize(statement);
  5235. }
  5236. // if(ret==nil)
  5237. // ret=@"";
  5238. *count=arr_count;
  5239. return arr_bundle;
  5240. }
  5241. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5242. {
  5243. // get default sold qty, return -1 if model not found;
  5244. int ret = -1;
  5245. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5246. sqlite3_stmt * statement;
  5247. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5248. {
  5249. if (sqlite3_step(statement) == SQLITE_ROW)
  5250. {
  5251. ret = sqlite3_column_int(statement, 0);
  5252. }
  5253. sqlite3_finalize(statement);
  5254. }
  5255. return ret;
  5256. }
  5257. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5258. {
  5259. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5260. [ret setValue:@"0" forKey:@"img_count"];
  5261. // sqlite3 *db = [iSalesDB get_db];
  5262. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5263. sqlite3_stmt * statement;
  5264. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5265. {
  5266. int i=0;
  5267. while (sqlite3_step(statement) == SQLITE_ROW)
  5268. {
  5269. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5270. // char *name = (char*)sqlite3_column_text(statement, 1);
  5271. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5272. char *url = (char*)sqlite3_column_text(statement, 0);
  5273. if(url==nil)
  5274. url="";
  5275. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5276. [item setValue:nsurl forKey:@"s"];
  5277. [item setValue:nsurl forKey:@"l"];
  5278. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5279. i++;
  5280. }
  5281. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5282. sqlite3_finalize(statement);
  5283. }
  5284. // [iSalesDB close_db:db];
  5285. return ret;
  5286. }
  5287. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5288. // UIApplication * app = [UIApplication sharedApplication];
  5289. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5290. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5291. sqlite3 *db = [iSalesDB get_db];
  5292. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5293. NSString* product_id=params[@"product_id"];
  5294. NSString *item_count_str = params[@"item_count"];
  5295. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5296. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5297. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5298. // NSString *sql = @"";
  5299. for(int i=0;i<arr.count;i++)
  5300. {
  5301. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5302. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5303. __block int cart_count = 0;
  5304. if (!item_count_str) {
  5305. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5306. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5307. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5308. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5309. cart_count = [[model_set_components lastObject] intValue];
  5310. }];
  5311. }
  5312. if(count==0)
  5313. {
  5314. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5315. sqlite3_stmt *stmt;
  5316. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5317. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5318. if (item_count_arr) {
  5319. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5320. } else {
  5321. sqlite3_bind_int(stmt,2,cart_count);
  5322. }
  5323. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5324. [iSalesDB execSql:@"ROLLBACK" db:db];
  5325. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5326. [iSalesDB close_db:db];
  5327. DebugLog(@"add to wishlist error");
  5328. return ret;
  5329. }
  5330. } else {
  5331. int qty = 0;
  5332. if (item_count_arr) {
  5333. qty = [item_count_arr[i] intValue];
  5334. } else {
  5335. qty = cart_count;
  5336. }
  5337. 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]];
  5338. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5339. [iSalesDB execSql:@"ROLLBACK" db:db];
  5340. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5341. [iSalesDB close_db:db];
  5342. DebugLog(@"add to wishlist error");
  5343. return ret;
  5344. }
  5345. }
  5346. }
  5347. // [iSalesDB execSql:sql db:db];
  5348. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5349. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5350. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5351. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5352. [iSalesDB close_db:db];
  5353. ret[@"wish_count"]=@(count);
  5354. ret[@"result"]= [NSNumber numberWithInt:2];
  5355. return ret;
  5356. }
  5357. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5358. // 0 category
  5359. // 1 search
  5360. // 2 itemsearch
  5361. NSData *ret = nil;
  5362. NSDictionary *items = nil;
  5363. switch (from) {
  5364. case 0:{
  5365. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5366. }
  5367. break;
  5368. case 1:{
  5369. items = [[self search:params limited:NO] objectForKey:@"items"];
  5370. }
  5371. break;
  5372. case 2:{
  5373. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5374. }
  5375. break;
  5376. default:
  5377. break;
  5378. }
  5379. if (!items) {
  5380. return ret;
  5381. }
  5382. int count = [[items objectForKey:@"count"] intValue];
  5383. NSMutableString *product_id_str = [@"" mutableCopy];
  5384. for (int i = 0; i < count; i++) {
  5385. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5386. NSDictionary *item = [items objectForKey:key];
  5387. NSString *product_id = [item objectForKey:@"product_id"];
  5388. if (i == 0) {
  5389. [product_id_str appendString:product_id];
  5390. } else {
  5391. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5392. }
  5393. }
  5394. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5395. if ([add_to isEqualToString:@"cart"]) {
  5396. NSString *order_code = [params objectForKey:@"orderCode"];
  5397. if (order_code.length) {
  5398. NSDictionary *newParams = @{
  5399. @"product_id" : product_id_str,
  5400. @"orderCode" : order_code,
  5401. @"can_create_backorder":params[@"can_create_backorder"]
  5402. };
  5403. ret = [self offline_add2cart:newParams.mutableCopy];
  5404. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5405. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5406. ret = [RAConvertor dict2data:retDic];
  5407. }
  5408. } else if([add_to isEqualToString:@"wishlist"]) {
  5409. NSDictionary *newParams = @{
  5410. @"product_id" : product_id_str
  5411. };
  5412. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5413. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5414. ret = [RAConvertor dict2data:retDic];
  5415. } else if([add_to isEqualToString:@"portfolio"]) {
  5416. NSDictionary *newParams = @{
  5417. @"product_id" : product_id_str
  5418. };
  5419. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5420. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5421. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5422. ret = [RAConvertor dict2data:retDic];
  5423. }
  5424. return ret;
  5425. }
  5426. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5427. {
  5428. return [self addAll:params from:0];
  5429. }
  5430. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5431. {
  5432. return [self addAll:params from:1];
  5433. }
  5434. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5435. {
  5436. return [self addAll:params from:2];
  5437. }
  5438. #pragma mark - Jack
  5439. #warning 做SQL操作时转义!!
  5440. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5441. // "val_227" : {
  5442. // "check" : 1,
  5443. // "value" : "US United States",
  5444. // "value_id" : "228"
  5445. // },
  5446. if (!countryCode) {
  5447. countryCode = @"US";
  5448. }
  5449. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5450. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5451. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5452. int code_id = sqlite3_column_int(stmt, 3); // id
  5453. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5454. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5455. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5456. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5457. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5458. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5459. }
  5460. long n = *count;
  5461. *count = n + 1;
  5462. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5463. [container setValue:countryDic forKey:key];
  5464. }] mutableCopy];
  5465. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5466. return ret;
  5467. }
  5468. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5469. countryCode = [self translateSingleQuote:countryCode];
  5470. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5471. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5472. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5473. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5474. if (name == NULL) {
  5475. name = "";
  5476. }
  5477. if (code == NULL) {
  5478. code = "";
  5479. }
  5480. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5481. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5482. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5483. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5484. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5485. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5486. }
  5487. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5488. [container setValue:stateDic forKey:key];
  5489. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5490. DebugLog(@"query all state error: %@",err_msg);
  5491. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5492. // [stateDic setValue:@"Other" forKey:@"value"];
  5493. // [stateDic setValue:@"" forKey:@"value_id"];
  5494. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5495. //
  5496. // if (state_code && [@"" isEqualToString:state_code]) {
  5497. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5498. // }
  5499. //
  5500. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5501. // [container setValue:stateDic forKey:key];
  5502. }] mutableCopy];
  5503. [ret removeObjectForKey:@"result"];
  5504. // failure 可以不用了,一样的
  5505. if (ret.allKeys.count == 0) {
  5506. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5507. [stateDic setValue:@"Other" forKey:@"value"];
  5508. [stateDic setValue:@"" forKey:@"value_id"];
  5509. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5510. if (state_code && [@"" isEqualToString:state_code]) {
  5511. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5512. }
  5513. NSString *key = [NSString stringWithFormat:@"val_0"];
  5514. [ret setValue:stateDic forKey:key];
  5515. }
  5516. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5517. return ret;
  5518. }
  5519. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5520. codeId = [self translateSingleQuote:codeId];
  5521. 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];
  5522. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5523. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5524. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5525. if (name == NULL) {
  5526. name = "";
  5527. }
  5528. if (code == NULL) {
  5529. code = "";
  5530. }
  5531. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5532. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5533. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5534. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5535. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5536. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5537. }
  5538. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5539. [container setValue:stateDic forKey:key];
  5540. }] mutableCopy];
  5541. [ret removeObjectForKey:@"result"];
  5542. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5543. return ret;
  5544. }
  5545. + (NSDictionary *)offline_getPrice {
  5546. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5547. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5548. char *name = (char *) sqlite3_column_text(stmt, 1);
  5549. int type = sqlite3_column_int(stmt, 2);
  5550. int orderBy = sqlite3_column_int(stmt, 3);
  5551. if (name == NULL) {
  5552. name = "";
  5553. }
  5554. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5555. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5556. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5557. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5558. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5559. if (orderBy == 0) {
  5560. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5561. }
  5562. [container setValue:priceDic forKey:key];
  5563. }] mutableCopy];
  5564. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5565. return ret;
  5566. }
  5567. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5568. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5569. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5570. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5571. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5572. // int _id = sqlite3_column_int(stmt, 0);
  5573. NSString *name = [self textAtColumn:1 statement:stmt];
  5574. NSDictionary *typeDic = @{
  5575. @"value_id" : name,
  5576. @"value" : name,
  5577. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5578. };
  5579. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5580. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5581. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5582. }];
  5583. return ret;
  5584. }
  5585. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5586. // 首先从offline_login表中取出sales_code
  5587. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5588. // NSString *user = app.user;
  5589. user = [self translateSingleQuote:user];
  5590. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5591. __block NSString *user_code = @"";
  5592. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5593. char *code = (char *)sqlite3_column_text(stmt, 0);
  5594. if (code == NULL) {
  5595. code = "";
  5596. }
  5597. user_code = [NSString stringWithUTF8String:code];
  5598. }];
  5599. // 再取所有salesRep
  5600. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5601. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5602. // 1 name 2 code 3 salesrep_id
  5603. char *name = (char *)sqlite3_column_text(stmt, 1);
  5604. char *code = (char *)sqlite3_column_text(stmt, 2);
  5605. int salesrep_id = sqlite3_column_int(stmt, 3);
  5606. if (name == NULL) {
  5607. name = "";
  5608. }
  5609. if (code == NULL) {
  5610. code = "";
  5611. }
  5612. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5613. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5614. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5615. // 比较code 相等则check
  5616. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5617. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5618. }
  5619. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5620. }] mutableCopy];
  5621. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5622. return ret;
  5623. }
  5624. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5625. zipcode = [self translateSingleQuote:zipcode];
  5626. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5627. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5628. char *country = (char *)sqlite3_column_text(stmt, 0);
  5629. char *state = (char *)sqlite3_column_text(stmt, 1);
  5630. char *city = (char *)sqlite3_column_text(stmt, 2);
  5631. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5632. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5633. if (country == NULL) {
  5634. country = "";
  5635. }
  5636. if (state == NULL) {
  5637. state = "";
  5638. }
  5639. if (city == NULL) {
  5640. city = "";
  5641. }
  5642. if (country_code == NULL) {
  5643. country_code = "";
  5644. }
  5645. if (state_code == NULL) {
  5646. state_code = "";
  5647. }
  5648. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5649. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5650. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5651. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5652. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5653. }] mutableCopy];
  5654. return ret;
  5655. }
  5656. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5657. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5658. [item setValue:value forKey:valueKey];
  5659. [dic setValue:item forKey:itemKey];
  5660. }
  5661. + (NSString *)countryCodeByid:(NSString *)code_id {
  5662. NSString *ret = nil;
  5663. code_id = [self translateSingleQuote:code_id];
  5664. sqlite3 *db = [iSalesDB get_db];
  5665. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5666. sqlite3_stmt * statement;
  5667. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5668. while (sqlite3_step(statement) == SQLITE_ROW) {
  5669. char *code = (char *)sqlite3_column_text(statement, 0);
  5670. if (code == NULL) {
  5671. code = "";
  5672. }
  5673. ret = [NSString stringWithUTF8String:code];
  5674. }
  5675. sqlite3_finalize(statement);
  5676. }
  5677. [iSalesDB close_db:db];
  5678. return ret;
  5679. }
  5680. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5681. NSString *ret = nil;
  5682. code = [self translateSingleQuote:code];
  5683. sqlite3 *db = [iSalesDB get_db];
  5684. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5685. sqlite3_stmt * statement;
  5686. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5687. while (sqlite3_step(statement) == SQLITE_ROW) {
  5688. char *_id = (char *)sqlite3_column_text(statement, 0);
  5689. if (_id == NULL) {
  5690. _id = "";
  5691. }
  5692. ret = [NSString stringWithFormat:@"%s",_id];
  5693. }
  5694. sqlite3_finalize(statement);
  5695. }
  5696. [iSalesDB close_db:db];
  5697. return ret;
  5698. }
  5699. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5700. NSString *name = nil;
  5701. codeId = [self translateSingleQuote:codeId];
  5702. sqlite3 *db = [iSalesDB get_db];
  5703. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5704. sqlite3_stmt * statement;
  5705. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5706. while (sqlite3_step(statement) == SQLITE_ROW) {
  5707. char *value = (char *)sqlite3_column_text(statement, 0);
  5708. if (value == NULL) {
  5709. value = "";
  5710. }
  5711. name = [NSString stringWithUTF8String:value];
  5712. }
  5713. sqlite3_finalize(statement);
  5714. }
  5715. [iSalesDB close_db:db];
  5716. return name;
  5717. }
  5718. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5719. NSString *ret = nil;
  5720. sqlite3 *db = [iSalesDB get_db];
  5721. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5722. sqlite3_stmt * statement;
  5723. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5724. while (sqlite3_step(statement) == SQLITE_ROW) {
  5725. char *name = (char *)sqlite3_column_text(statement, 0);
  5726. if (name == NULL) {
  5727. name = "";
  5728. }
  5729. ret = [NSString stringWithUTF8String:name];
  5730. }
  5731. sqlite3_finalize(statement);
  5732. }
  5733. [iSalesDB close_db:db];
  5734. return ret;
  5735. }
  5736. + (NSString *)salesRepCodeById:(NSString *)_id {
  5737. NSString *ret = nil;
  5738. _id = [self translateSingleQuote:_id];
  5739. sqlite3 *db = [iSalesDB get_db];
  5740. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5741. sqlite3_stmt * statement;
  5742. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5743. while (sqlite3_step(statement) == SQLITE_ROW) {
  5744. char *rep = (char *)sqlite3_column_text(statement, 0);
  5745. if (rep == NULL) {
  5746. rep = "";
  5747. }
  5748. ret = [NSString stringWithUTF8String:rep];
  5749. }
  5750. sqlite3_finalize(statement);
  5751. }
  5752. [iSalesDB close_db:db];
  5753. return ret;
  5754. }
  5755. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5756. char *tx = (char *)sqlite3_column_text(stmt, col);
  5757. if (tx == NULL) {
  5758. tx = "";
  5759. }
  5760. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5761. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5762. if (!text) {
  5763. text = @"";
  5764. }
  5765. // 将字符全部为' '的字符串干掉
  5766. int spaceCount = 0;
  5767. for (int i = 0; i < text.length; i++) {
  5768. if ([text characterAtIndex:i] == ' ') {
  5769. spaceCount++;
  5770. }
  5771. }
  5772. if (spaceCount == text.length) {
  5773. text = @"";
  5774. }
  5775. return text;
  5776. }
  5777. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5778. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5779. NSData *data = [NSData dataWithContentsOfFile:path];
  5780. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5781. return ret;
  5782. }
  5783. + (NSString *)textFileName:(NSString *)name {
  5784. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5785. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5786. if (!text) {
  5787. text = @"";
  5788. }
  5789. return text;
  5790. }
  5791. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5792. return [[dic objectForKey:key] mutableCopy];
  5793. }
  5794. + (id)translateSingleQuote:(NSString *)string {
  5795. if ([string isKindOfClass:[NSString class]])
  5796. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5797. return string;
  5798. }
  5799. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5800. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5801. }
  5802. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5803. NSString* ret= nil;
  5804. NSString *sqlQuery = nil;
  5805. // 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
  5806. 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];
  5807. sqlite3_stmt * statement;
  5808. // int count=0;
  5809. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5810. {
  5811. if (sqlite3_step(statement) == SQLITE_ROW)
  5812. {
  5813. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5814. if(imgurl==nil)
  5815. imgurl="";
  5816. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5817. ret=nsimgurl;
  5818. }
  5819. sqlite3_finalize(statement);
  5820. }
  5821. else
  5822. {
  5823. [ret setValue:@"8" forKey:@"result"];
  5824. }
  5825. // [iSalesDB close_db:db];
  5826. // DebugLog(@"data string: %@",ret );
  5827. return ret;
  5828. }
  5829. #pragma mark contact Advanced search
  5830. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5831. {
  5832. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5833. return [RAConvertor dict2data:contactAdvanceDic];
  5834. }
  5835. #pragma mark create new contact
  5836. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5837. assert(params[@"user"]!=nil);
  5838. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5839. NSData *data = [NSData dataWithContentsOfFile:path];
  5840. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5841. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5842. NSString *countryCode = nil;
  5843. NSString *countryCode_id = nil;
  5844. NSString *stateCode = nil;
  5845. NSString *city = nil;
  5846. NSString *zipCode = nil;
  5847. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5848. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5849. NSString *code_id = params[@"country"];
  5850. countryCode_id = code_id;
  5851. countryCode = [self countryCodeByid:code_id];
  5852. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5853. NSString *zip_code = params[@"zipcode"];
  5854. // 剔除全部为空格
  5855. int spaceCount = 0;
  5856. for (int i = 0; i < zip_code.length; i++) {
  5857. if ([zip_code characterAtIndex:i] == ' ') {
  5858. spaceCount++;
  5859. }
  5860. }
  5861. if (spaceCount == zip_code.length) {
  5862. zip_code = @"";
  5863. }
  5864. zipCode = zip_code;
  5865. if (zipCode.length > 0) {
  5866. countryCode_id = params[@"country"];
  5867. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5868. countryCode = [dic valueForKey:@"country_code"];
  5869. if (!countryCode) {
  5870. // countryCode = @"US";
  5871. NSString *code_id = params[@"country"];
  5872. countryCode = [self countryCodeByid:code_id];
  5873. }
  5874. stateCode = [dic valueForKey:@"state_code"];
  5875. if (!stateCode.length) {
  5876. stateCode = params[@"state"];
  5877. }
  5878. city = [dic valueForKey:@"city"];
  5879. if (!city.length) {
  5880. city = params[@"city"];
  5881. }
  5882. // zip code
  5883. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5884. [zipDic setValue:zipCode forKey:@"value"];
  5885. [section_0 setValue:zipDic forKey:@"item_11"];
  5886. } else {
  5887. NSString *code_id = params[@"country"];
  5888. countryCode = [self countryCodeByid:code_id];
  5889. stateCode = params[@"state"];
  5890. city = params[@"city"];
  5891. }
  5892. }
  5893. } else {
  5894. // default: US United States
  5895. countryCode = @"US";
  5896. countryCode_id = @"228";
  5897. }
  5898. // country
  5899. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5900. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5901. // state
  5902. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5903. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5904. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5905. // city
  5906. if (city) {
  5907. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5908. [cityDic setValue:city forKey:@"value"];
  5909. [section_0 setValue:cityDic forKey:@"item_13"];
  5910. }
  5911. // price type
  5912. NSDictionary *priceDic = [self offline_getPrice];
  5913. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5914. // contact type
  5915. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5916. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5917. // Sales Rep
  5918. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5919. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5920. [ret setValue:section_0 forKey:@"section_0"];
  5921. return [RAConvertor dict2data:ret];
  5922. }
  5923. #pragma mark save
  5924. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5925. NSString *addr = nil;
  5926. NSString *contact_name = nil;
  5927. NSString *companyName = [params objectForKey:@"company"];
  5928. if (companyName) {
  5929. companyName = [AESCrypt fastencrypt:companyName];
  5930. } else {
  5931. companyName = @"";
  5932. }
  5933. DebugLog(@"company");
  5934. companyName = [self translateSingleQuote:companyName];
  5935. NSString *addr1 = [params objectForKey:@"address"];
  5936. NSString *addr2 = [params objectForKey:@"address2"];
  5937. NSString *addr3 = [params objectForKey:@"address_3"];
  5938. NSString *addr4 = [params objectForKey:@"address_4"];
  5939. if (!addr2) {
  5940. addr2 = @"";
  5941. }
  5942. if (!addr3) {
  5943. addr3 = @"";
  5944. }
  5945. if (!addr4) {
  5946. addr4 = @"";
  5947. }
  5948. if (!addr1) {
  5949. addr1 = @"";
  5950. }
  5951. DebugLog(@"addr");
  5952. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5953. addr = [AESCrypt fastencrypt:addr];
  5954. addr = [self translateSingleQuote:addr];
  5955. if (addr1 && ![addr1 isEqualToString:@""]) {
  5956. addr1 = [AESCrypt fastencrypt:addr1];
  5957. }
  5958. addr1 = [self translateSingleQuote:addr1];
  5959. addr2 = [self translateSingleQuote:addr2];
  5960. addr3 = [self translateSingleQuote:addr3];
  5961. addr4 = [self translateSingleQuote:addr4];
  5962. NSString *country = [params objectForKey:@"country"];
  5963. if (country) {
  5964. country = [self countryNameByCountryCodeId:country];
  5965. } else {
  5966. country = @"";
  5967. }
  5968. DebugLog(@"country");
  5969. country = [self translateSingleQuote:country];
  5970. NSString *state = [params objectForKey:@"state"];
  5971. if (!state) {
  5972. state = @"";
  5973. }
  5974. DebugLog(@"state");
  5975. state = [self translateSingleQuote:state];
  5976. NSString *city = [params objectForKey:@"city"];
  5977. if (!city) {
  5978. city = @"";
  5979. }
  5980. city = [self translateSingleQuote:city];
  5981. NSString *zipcode = [params objectForKey:@"zipcode"];
  5982. if (!zipcode) {
  5983. zipcode = @"";
  5984. }
  5985. DebugLog(@"zip");
  5986. zipcode = [self translateSingleQuote:zipcode];
  5987. NSString *fistName = [params objectForKey:@"firstname"];
  5988. if (!fistName) {
  5989. fistName = @"";
  5990. }
  5991. NSString *lastName = [params objectForKey:@"lastname"];
  5992. if (!lastName) {
  5993. lastName = @"";
  5994. }
  5995. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5996. DebugLog(@"contact_name");
  5997. contact_name = [self translateSingleQuote:contact_name];
  5998. fistName = [self translateSingleQuote:fistName];
  5999. lastName = [self translateSingleQuote:lastName];
  6000. NSString *phone = [params objectForKey:@"phone"];
  6001. if (phone) {
  6002. phone = [AESCrypt fastencrypt:phone];
  6003. } else {
  6004. phone = @"";
  6005. }
  6006. DebugLog(@"PHONE");
  6007. phone = [self translateSingleQuote:phone];
  6008. NSString *fax = [params objectForKey:@"fax"];
  6009. if (!fax) {
  6010. fax = @"";
  6011. }
  6012. DebugLog(@"FAX");
  6013. fax = [self translateSingleQuote:fax];
  6014. NSString *email = [params objectForKey:@"email"];
  6015. if (!email) {
  6016. email = @"";
  6017. }
  6018. DebugLog(@"EMAIL:%@",email);
  6019. email = [self translateSingleQuote:email];
  6020. NSString *notes = [params objectForKey:@"contact_notes"];
  6021. if (!notes) {
  6022. notes = @"";
  6023. }
  6024. DebugLog(@"NOTE:%@",notes);
  6025. notes = [self translateSingleQuote:notes];
  6026. NSString *price = [params objectForKey:@"price_name"];
  6027. if (price) {
  6028. price = [self priceNameByPriceId:price];
  6029. } else {
  6030. price = @"";
  6031. }
  6032. DebugLog(@"PRICE");
  6033. price = [self translateSingleQuote:price];
  6034. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6035. if (salesRep) {
  6036. salesRep = [self salesRepCodeById:salesRep];
  6037. } else {
  6038. salesRep = @"";
  6039. }
  6040. salesRep = [self translateSingleQuote:salesRep];
  6041. NSString *img = [params objectForKey:@"business_card"];
  6042. NSArray *array = [img componentsSeparatedByString:@","];
  6043. NSString *img_0 = array[0];
  6044. if (!img_0) {
  6045. img_0 = @"";
  6046. }
  6047. img_0 = [self translateSingleQuote:img_0];
  6048. NSString *img_1 = array[1];
  6049. if (!img_1) {
  6050. img_1 = @"";
  6051. }
  6052. img_1 = [self translateSingleQuote:img_1];
  6053. NSString *img_2 = array[2];
  6054. if (!img_2) {
  6055. img_2 = @"";
  6056. }
  6057. img_2 = [self translateSingleQuote:img_2];
  6058. NSString *contact_id = [NSUUID UUID].UUIDString;
  6059. NSString *contact_type = [params objectForKey:@"type_name"];
  6060. if (!contact_type) {
  6061. contact_type = @"";
  6062. }
  6063. contact_type = [self translateSingleQuote:contact_type];
  6064. // 判断更新时是否为customer
  6065. if (update) {
  6066. contact_id = [params objectForKey:@"contact_id"];
  6067. if (!contact_id) {
  6068. contact_id = @"";
  6069. }
  6070. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6071. __block int customer = 0;
  6072. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6073. customer = sqlite3_column_int(stmt, 0);
  6074. }];
  6075. isCustomer = customer ? YES : NO;
  6076. }
  6077. NSMutableDictionary *sync_dic = [params mutableCopy];
  6078. if (isCustomer) {
  6079. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6080. } else {
  6081. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6082. }
  6083. NSString *sync_data = nil;
  6084. NSString *sql = nil;
  6085. if (update){
  6086. contact_id = [params objectForKey:@"contact_id"];
  6087. if (!contact_id) {
  6088. contact_id = @"";
  6089. }
  6090. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6091. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6092. sync_data = [RAConvertor dict2string:sync_dic];
  6093. sync_data = [self translateSingleQuote:sync_data];
  6094. 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];
  6095. } else {
  6096. contact_id = [self translateSingleQuote:contact_id];
  6097. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6098. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6099. sync_data = [RAConvertor dict2string:sync_dic];
  6100. sync_data = [self translateSingleQuote:sync_data];
  6101. 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];
  6102. }
  6103. int result = [iSalesDB execSql:sql];
  6104. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6105. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6106. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6107. }
  6108. #pragma mark save new contact
  6109. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6110. {
  6111. return [self offline_saveContact:params update:NO isCustomer:YES];
  6112. }
  6113. #pragma mark edit contact
  6114. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6115. {
  6116. assert(params[@"user"]!=nil);
  6117. // {
  6118. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6119. // password = 123456;
  6120. // user = EvanK;
  6121. // }
  6122. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6123. NSData *data = [NSData dataWithContentsOfFile:path];
  6124. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6125. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6126. NSString *countryCode = nil;
  6127. NSString *countryCode_id = nil;
  6128. NSString *stateCode = nil;
  6129. /*------contact infor------*/
  6130. __block NSString *country = nil;
  6131. __block NSString *company_name = nil;
  6132. __block NSString *contact_id = params[@"contact_id"];
  6133. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6134. __block NSString *zipcode = nil;
  6135. __block NSString *state = nil; // state_code
  6136. __block NSString *city = nil; //
  6137. __block NSString *firt_name,*last_name;
  6138. __block NSString *phone,*fax,*email;
  6139. __block NSString *notes,*price_type,*sales_rep;
  6140. __block NSString *img_0,*img_1,*img_2;
  6141. __block NSString *contact_type;
  6142. contact_id = [self translateSingleQuote:contact_id];
  6143. 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];
  6144. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6145. country = [self textAtColumn:0 statement:stmt]; // country name
  6146. company_name = [self textAtColumn:1 statement:stmt];
  6147. addr_1 = [self textAtColumn:2 statement:stmt];
  6148. addr_2 = [self textAtColumn:3 statement:stmt];
  6149. addr_3 = [self textAtColumn:4 statement:stmt];
  6150. addr_4 = [self textAtColumn:5 statement:stmt];
  6151. zipcode = [self textAtColumn:6 statement:stmt];
  6152. state = [self textAtColumn:7 statement:stmt]; // state code
  6153. city = [self textAtColumn:8 statement:stmt];
  6154. firt_name = [self textAtColumn:9 statement:stmt];
  6155. last_name = [self textAtColumn:10 statement:stmt];
  6156. phone = [self textAtColumn:11 statement:stmt];
  6157. fax = [self textAtColumn:12 statement:stmt];
  6158. email = [self textAtColumn:13 statement:stmt];
  6159. notes = [self textAtColumn:14 statement:stmt];
  6160. price_type = [self textAtColumn:15 statement:stmt]; // name
  6161. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6162. img_0 = [self textAtColumn:17 statement:stmt];
  6163. img_1 = [self textAtColumn:18 statement:stmt];
  6164. img_2 = [self textAtColumn:19 statement:stmt];
  6165. contact_type = [self textAtColumn:20 statement:stmt];
  6166. }];
  6167. // decrypt
  6168. if (company_name) {
  6169. company_name = [AESCrypt fastdecrypt:company_name];
  6170. }
  6171. if (addr_1) {
  6172. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6173. }
  6174. if (phone) {
  6175. phone = [AESCrypt fastdecrypt:phone];
  6176. }
  6177. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6178. countryCode = [iSalesDB jk_queryText:countrySql];
  6179. stateCode = state;
  6180. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6181. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6182. NSString *code_id = params[@"country"];
  6183. countryCode_id = code_id;
  6184. countryCode = [self countryCodeByid:code_id];
  6185. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6186. NSString *zip_code = params[@"zipcode"];
  6187. // 剔除全部为空格
  6188. int spaceCount = 0;
  6189. for (int i = 0; i < zip_code.length; i++) {
  6190. if ([zip_code characterAtIndex:i] == ' ') {
  6191. spaceCount++;
  6192. }
  6193. }
  6194. if (spaceCount == zip_code.length) {
  6195. zip_code = @"";
  6196. }
  6197. if (zipcode.length > 0) {
  6198. zipcode = zip_code;
  6199. countryCode_id = params[@"country"];
  6200. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6201. countryCode = [dic valueForKey:@"country_code"];
  6202. if (!countryCode) {
  6203. NSString *code_id = params[@"country"];
  6204. countryCode = [self countryCodeByid:code_id];
  6205. }
  6206. stateCode = [dic valueForKey:@"state_code"];
  6207. if (!stateCode.length) {
  6208. stateCode = params[@"state"];
  6209. }
  6210. city = [dic valueForKey:@"city"];
  6211. if (!city.length) {
  6212. city = params[@"city"];
  6213. }
  6214. // zip code
  6215. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6216. [zipDic setValue:zipcode forKey:@"value"];
  6217. [section_0 setValue:zipDic forKey:@"item_8"];
  6218. } else {
  6219. NSString *code_id = params[@"country"];
  6220. countryCode = [self countryCodeByid:code_id];
  6221. stateCode = params[@"state"];
  6222. city = params[@"city"];
  6223. }
  6224. }
  6225. }
  6226. // 0 Country
  6227. // 1 Company Name
  6228. // 2 Contact ID
  6229. // 3 Picture
  6230. // 4 Address 1
  6231. // 5 Address 2
  6232. // 6 Address 3
  6233. // 7 Address 4
  6234. // 8 Zip Code
  6235. // 9 State/Province
  6236. // 10 City
  6237. // 11 Contact First Name
  6238. // 12 Contact Last Name
  6239. // 13 Phone
  6240. // 14 Fax
  6241. // 15 Email
  6242. // 16 Contact Notes
  6243. // 17 Price Type
  6244. // 18 Contact Type
  6245. // 19 Sales Rep
  6246. // country
  6247. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6248. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6249. // company
  6250. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6251. // contact_id
  6252. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6253. // picture
  6254. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6255. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6256. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6257. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6258. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6259. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6260. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6261. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6262. // addr 1 2 3 4
  6263. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6264. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6265. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6266. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6267. // zip code
  6268. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6269. // state
  6270. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6271. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6272. // city
  6273. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6274. // first last
  6275. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6276. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6277. // phone fax email
  6278. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6279. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6280. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6281. // notes
  6282. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6283. // price
  6284. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6285. for (NSString *key in priceDic.allKeys) {
  6286. if ([key containsString:@"val_"]) {
  6287. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6288. if ([dic[@"value"] isEqualToString:price_type]) {
  6289. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6290. [priceDic setValue:dic forKey:key];
  6291. }
  6292. }
  6293. }
  6294. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6295. // Contact Rep
  6296. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6297. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6298. // Sales Rep
  6299. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6300. for (NSString *key in repDic.allKeys) {
  6301. if ([key containsString:@"val_"]) {
  6302. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6303. NSString *value = dic[@"value"];
  6304. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6305. if (code && [code isEqualToString:sales_rep]) {
  6306. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6307. [repDic setValue:dic forKey:key];
  6308. }
  6309. }
  6310. }
  6311. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6312. [ret setValue:section_0 forKey:@"section_0"];
  6313. return [RAConvertor dict2data:ret];
  6314. }
  6315. #pragma mark save contact
  6316. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6317. {
  6318. return [self offline_saveContact:params update:YES isCustomer:YES];
  6319. }
  6320. #pragma mark category
  6321. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6322. if (ck) {
  6323. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6324. for (NSString *key in res.allKeys) {
  6325. if (![key isEqualToString:@"count"]) {
  6326. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6327. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6328. if ([val[@"value"] isEqualToString:ck]) {
  6329. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6330. }
  6331. [res setValue:val forKey:key];
  6332. }
  6333. }
  6334. [dic setValue:res forKey:valueKey];
  6335. }
  6336. }
  6337. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6338. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6339. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6340. NSString* category = [params valueForKey:@"category"];
  6341. if (!category || [category isEqualToString:@""]) {
  6342. category = @"%";
  6343. }
  6344. category = [self translateSingleQuote:category];
  6345. int limit = [[params valueForKey:@"limit"] intValue];
  6346. int offset = [[params valueForKey:@"offset"] intValue];
  6347. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6348. NSString *limit_str = @"";
  6349. if (limited) {
  6350. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6351. }
  6352. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6353. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6354. sqlite3 *db = [iSalesDB get_db];
  6355. // [iSalesDB AddExFunction:db];
  6356. int count;
  6357. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6358. 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];
  6359. double price_min = 0;
  6360. double price_max = 0;
  6361. if ([params.allKeys containsObject:@"alert"]) {
  6362. // alert
  6363. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6364. NSString *alert = params[@"alert"];
  6365. if ([alert isEqualToString:@"Display All"]) {
  6366. alert = [NSString stringWithFormat:@""];
  6367. } else {
  6368. alert = [self translateSingleQuote:alert];
  6369. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6370. }
  6371. // available
  6372. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6373. NSString *available = params[@"available"];
  6374. NSString *available_condition;
  6375. if ([available isEqualToString:@"Display All"]) {
  6376. available_condition = @"";
  6377. } else if ([available isEqualToString:@"Available Now"]) {
  6378. available_condition = @"and availability > 0";
  6379. } else {
  6380. available_condition = @"and availability == 0";
  6381. }
  6382. // best seller
  6383. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6384. NSString *best_seller = @"";
  6385. NSString *order_best_seller = @"m.name asc";
  6386. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6387. best_seller = @"and best_seller > 0";
  6388. order_best_seller = @"m.best_seller desc,m.name asc";
  6389. }
  6390. // price
  6391. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6392. NSString *price = params[@"price"];
  6393. price_min = 0;
  6394. price_max = MAXFLOAT;
  6395. if (params[@"user"] && price != nil) {
  6396. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6397. NSMutableString *priceName = [NSMutableString string];
  6398. for (int i = 0; i < priceTypeArray.count; i++) {
  6399. NSString *pricetype = priceTypeArray[i];
  6400. pricetype = [self translateSingleQuote:pricetype];
  6401. if (i == 0) {
  6402. [priceName appendFormat:@"'%@'",pricetype];
  6403. } else {
  6404. [priceName appendFormat:@",'%@'",pricetype];
  6405. }
  6406. }
  6407. if ([price isEqualToString:@"Display All"]) {
  6408. price = [NSString stringWithFormat:@""];
  6409. } else if([price containsString:@"+"]){
  6410. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6411. price_min = [price doubleValue];
  6412. 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];
  6413. } else {
  6414. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6415. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6416. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6417. 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];
  6418. }
  6419. } else {
  6420. price = @"";
  6421. }
  6422. // sold_by_qty : Sold in quantities of %@
  6423. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6424. NSString *qty = params[@"sold_by_qty"];
  6425. if ([qty isEqualToString:@"Display All"]) {
  6426. qty = @"";
  6427. } else {
  6428. qty = [self translateSingleQuote:qty];
  6429. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6430. }
  6431. // model name;
  6432. NSString* modelname =params[@"modelName"];
  6433. if(modelname.length==0)
  6434. {
  6435. modelname=@"";
  6436. }
  6437. else
  6438. {
  6439. modelname = modelname.lowercaseString;
  6440. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6441. }
  6442. //modelDescription
  6443. NSString* modelDescription =params[@"modelDescription"];
  6444. if(modelDescription.length==0)
  6445. {
  6446. modelDescription=@"";
  6447. }
  6448. else
  6449. {
  6450. modelDescription = modelDescription.lowercaseString;
  6451. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6452. }
  6453. // cate
  6454. // category = [self translateSingleQuote:category];
  6455. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6456. // cate mutiple selection
  6457. NSString *category_id = params[@"category"];
  6458. NSMutableArray *cate_id_array = nil;
  6459. NSMutableString *cateWhere = [NSMutableString string];
  6460. if ([category_id isEqualToString:@""] || !category_id) {
  6461. [cateWhere appendString:@"1 = 1"];
  6462. } else {
  6463. if ([category_id containsString:@","]) {
  6464. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6465. } else {
  6466. cate_id_array = [@[category_id] mutableCopy];
  6467. }
  6468. [cateWhere appendString:@"("];
  6469. for (int i = 0; i < cate_id_array.count; i++) {
  6470. if (i == 0) {
  6471. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6472. } else {
  6473. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6474. }
  6475. }
  6476. [cateWhere appendString:@")"];
  6477. }
  6478. // where bestseller > 0 order by bestseller desc
  6479. // sql query: alert availability(int) best_seller(int) price qty
  6480. 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];
  6481. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6482. }
  6483. DebugLog(@"offline category where: %@",where);
  6484. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6485. if (!params[@"user"]) {
  6486. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6487. }
  6488. [ret setValue:filter forKey:@"filter"];
  6489. DebugLog(@"offline_category sql:%@",sqlQuery);
  6490. sqlite3_stmt * statement;
  6491. [ret setValue:@"2" forKey:@"result"];
  6492. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6493. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6494. // int count=0;
  6495. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6496. {
  6497. int i=0;
  6498. while (sqlite3_step(statement) == SQLITE_ROW)
  6499. {
  6500. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6501. char *name = (char*)sqlite3_column_text(statement, 0);
  6502. if(name==nil)
  6503. name="";
  6504. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6505. char *description = (char*)sqlite3_column_text(statement, 1);
  6506. if(description==nil)
  6507. description="";
  6508. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6509. int product_id = sqlite3_column_int(statement, 2);
  6510. int wid = sqlite3_column_int(statement, 3);
  6511. int closeout = sqlite3_column_int(statement, 4);
  6512. int cid = sqlite3_column_int(statement, 5);
  6513. int wisdelete = sqlite3_column_int(statement, 6);
  6514. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6515. int more_color = sqlite3_column_int(statement, 8);
  6516. // Defaul Category ID
  6517. __block NSString *categoryID = nil;
  6518. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6519. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6520. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6521. if(default_category==nil)
  6522. default_category="";
  6523. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6524. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6525. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6526. categoryID = nsdefault_category;
  6527. }];
  6528. if (!categoryID.length) {
  6529. NSString *cateIDs = params[@"category"];
  6530. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6531. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6532. for (NSString *cateID in requestCategoryArr) {
  6533. BOOL needBreak = NO;
  6534. for (NSString *itemCateIDBox in itemCategoryArr) {
  6535. if (itemCateIDBox.length > 4) {
  6536. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6537. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6538. if ([itemCategoryID isEqualToString:cateID]) {
  6539. needBreak = YES;
  6540. categoryID = itemCategoryID;
  6541. break;
  6542. }
  6543. }
  6544. }
  6545. if (needBreak) {
  6546. break;
  6547. }
  6548. }
  6549. }
  6550. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6551. if(wid !=0 && wisdelete != 1)
  6552. [item setValue:@"true" forKey:@"wish_exists"];
  6553. else
  6554. [item setValue:@"false" forKey:@"wish_exists"];
  6555. if(closeout==0)
  6556. [item setValue:@"false" forKey:@"is_closeout"];
  6557. else
  6558. [item setValue:@"true" forKey:@"is_closeout"];
  6559. if(cid==0)
  6560. [item setValue:@"false" forKey:@"cart_exists"];
  6561. else
  6562. [item setValue:@"true" forKey:@"cart_exists"];
  6563. if (more_color == 0) {
  6564. [item setObject:@(false) forKey:@"more_color"];
  6565. } else if (more_color == 1) {
  6566. [item setObject:@(true) forKey:@"more_color"];
  6567. }
  6568. [item addEntriesFromDictionary:imgjson];
  6569. // [item setValue:nsurl forKey:@"img"];
  6570. [item setValue:nsname forKey:@"name"];
  6571. [item setValue:nsdescription forKey:@"description"];
  6572. if (categoryID) {
  6573. [item setValue:categoryID forKey:@"item_category_id"];
  6574. }
  6575. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6576. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6577. i++;
  6578. }
  6579. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6580. [ret setObject:items forKey:@"items"];
  6581. sqlite3_finalize(statement);
  6582. } else {
  6583. DebugLog(@"nothing...");
  6584. }
  6585. DebugLog(@"count:%d",count);
  6586. [iSalesDB close_db:db];
  6587. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6588. return ret;
  6589. }
  6590. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6591. {
  6592. return [self categoryList:params limited:YES];
  6593. }
  6594. # pragma mark item search
  6595. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6596. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6597. // assert(params[@"order_code"]);
  6598. // params[@"user"]
  6599. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6600. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6601. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6602. // category
  6603. NSDictionary *category_menu = [self offline_category_menu];
  6604. [filter setValue:category_menu forKey:@"category"];
  6605. NSString* where= nil;
  6606. NSString* orderby= @"m.name";
  6607. if (!filterSearch) {
  6608. int covertype = [[params valueForKey:@"covertype"] intValue];
  6609. switch (covertype) {
  6610. case 0:
  6611. {
  6612. where=@"m.category like'%%#005#%%'";
  6613. break;
  6614. }
  6615. case 1:
  6616. {
  6617. where=@"m.alert like '%QS%'";
  6618. break;
  6619. }
  6620. case 2:
  6621. {
  6622. where=@"m.availability>0";
  6623. break;
  6624. }
  6625. case 3:
  6626. {
  6627. where=@"m.best_seller>0";
  6628. orderby=@"m.best_seller desc,m.name asc";
  6629. break;
  6630. }
  6631. default:
  6632. where=@"1=1";
  6633. break;
  6634. }
  6635. }
  6636. int limit = [[params valueForKey:@"limit"] intValue];
  6637. int offset = [[params valueForKey:@"offset"] intValue];
  6638. NSString *limit_str = @"";
  6639. if (limited) {
  6640. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6641. }
  6642. sqlite3 *db = [iSalesDB get_db];
  6643. // [iSalesDB AddExFunction:db];
  6644. int count;
  6645. NSString *sqlQuery = nil;
  6646. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6647. 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];
  6648. double price_min = 0;
  6649. double price_max = 0;
  6650. if (filterSearch) {
  6651. // alert
  6652. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6653. NSString *alert = params[@"alert"];
  6654. if ([alert isEqualToString:@"Display All"]) {
  6655. alert = [NSString stringWithFormat:@""];
  6656. } else {
  6657. alert = [self translateSingleQuote:alert];
  6658. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6659. }
  6660. // available
  6661. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6662. NSString *available = params[@"available"];
  6663. NSString *available_condition;
  6664. if ([available isEqualToString:@"Display All"]) {
  6665. available_condition = @"";
  6666. } else if ([available isEqualToString:@"Available Now"]) {
  6667. available_condition = @"and availability > 0";
  6668. } else {
  6669. available_condition = @"and availability == 0";
  6670. }
  6671. // best seller
  6672. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6673. NSString *best_seller = @"";
  6674. NSString *order_best_seller = @"m.name asc";
  6675. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6676. best_seller = @"and best_seller > 0";
  6677. order_best_seller = @"m.best_seller desc,m.name asc";
  6678. }
  6679. // price
  6680. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6681. NSString *price = params[@"price"];
  6682. price_min = 0;
  6683. price_max = MAXFLOAT;
  6684. if (params[@"user"]) {
  6685. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6686. NSMutableString *priceName = [NSMutableString string];
  6687. for (int i = 0; i < priceTypeArray.count; i++) {
  6688. NSString *pricetype = priceTypeArray[i];
  6689. pricetype = [self translateSingleQuote:pricetype];
  6690. if (i == 0) {
  6691. [priceName appendFormat:@"'%@'",pricetype];
  6692. } else {
  6693. [priceName appendFormat:@",'%@'",pricetype];
  6694. }
  6695. }
  6696. if ([price isEqualToString:@"Display All"]) {
  6697. price = [NSString stringWithFormat:@""];
  6698. } else if([price containsString:@"+"]){
  6699. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6700. price_min = [price doubleValue];
  6701. 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];
  6702. } else {
  6703. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6704. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6705. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6706. 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];
  6707. }
  6708. } else {
  6709. price = @"";
  6710. }
  6711. // sold_by_qty : Sold in quantities of %@
  6712. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6713. NSString *qty = params[@"sold_by_qty"];
  6714. if ([qty isEqualToString:@"Display All"]) {
  6715. qty = @"";
  6716. } else {
  6717. qty = [self translateSingleQuote:qty];
  6718. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6719. }
  6720. // model name;
  6721. NSString* modelname =params[@"modelName"];
  6722. if(modelname.length==0)
  6723. {
  6724. modelname=@"";
  6725. }
  6726. else
  6727. {
  6728. modelname = modelname.lowercaseString;
  6729. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6730. }
  6731. //modelDescription
  6732. NSString* modelDescription =params[@"modelDescription"];
  6733. if(modelDescription.length==0)
  6734. {
  6735. modelDescription=@"";
  6736. }
  6737. else
  6738. {
  6739. modelDescription = modelDescription.lowercaseString;
  6740. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6741. }
  6742. //lower(description) like'%%%@%%'
  6743. // category
  6744. NSString *category_id = params[@"ctgId"];
  6745. NSMutableArray *cate_id_array = nil;
  6746. NSMutableString *cateWhere = [NSMutableString string];
  6747. if ([category_id isEqualToString:@""] || !category_id) {
  6748. [cateWhere appendString:@"1 = 1"];
  6749. } else {
  6750. if ([category_id containsString:@","]) {
  6751. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6752. } else {
  6753. cate_id_array = [@[category_id] mutableCopy];
  6754. }
  6755. /*-----------*/
  6756. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6757. [cateWhere appendString:@"("];
  6758. for (int i = 0; i < cate_id_array.count; i++) {
  6759. for (NSString *key0 in cateDic.allKeys) {
  6760. if ([key0 containsString:@"category_"]) {
  6761. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6762. for (NSString *key1 in category0.allKeys) {
  6763. if ([key1 containsString:@"category_"]) {
  6764. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6765. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6766. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6767. cate_id_array[i] = [category1 objectForKey:@"id"];
  6768. if (i == 0) {
  6769. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6770. } else {
  6771. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6772. }
  6773. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6774. [category0 setValue:category1 forKey:key1];
  6775. [cateDic setValue:category0 forKey:key0];
  6776. }
  6777. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6778. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6779. cate_id_array[i] = [category0 objectForKey:@"id"];
  6780. if (i == 0) {
  6781. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6782. } else {
  6783. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6784. }
  6785. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6786. [cateDic setValue:category0 forKey:key0];
  6787. }
  6788. }
  6789. }
  6790. }
  6791. }
  6792. }
  6793. [cateWhere appendString:@")"];
  6794. [filter setValue:cateDic forKey:@"category"];
  6795. }
  6796. // where bestseller > 0 order by bestseller desc
  6797. // sql query: alert availability(int) best_seller(int) price qty
  6798. 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];
  6799. // count
  6800. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6801. }
  6802. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6803. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6804. if (!params[@"user"]) {
  6805. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6806. }
  6807. [ret setValue:filter forKey:@"filter"];
  6808. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6809. sqlite3_stmt * statement;
  6810. [ret setValue:@"2" forKey:@"result"];
  6811. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6812. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6813. // int count=0;
  6814. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6815. {
  6816. int i=0;
  6817. while (sqlite3_step(statement) == SQLITE_ROW)
  6818. {
  6819. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6820. char *name = (char*)sqlite3_column_text(statement, 0);
  6821. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6822. char *description = (char*)sqlite3_column_text(statement, 1);
  6823. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6824. int product_id = sqlite3_column_int(statement, 2);
  6825. int wid = sqlite3_column_int(statement, 3);
  6826. int closeout = sqlite3_column_int(statement, 4);
  6827. int cid = sqlite3_column_int(statement, 5);
  6828. int wisdelete = sqlite3_column_int(statement, 6);
  6829. int more_color = sqlite3_column_int(statement, 7);
  6830. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6831. if(wid !=0 && wisdelete != 1)
  6832. [item setValue:@"true" forKey:@"wish_exists"];
  6833. else
  6834. [item setValue:@"false" forKey:@"wish_exists"];
  6835. if(closeout==0)
  6836. [item setValue:@"false" forKey:@"is_closeout"];
  6837. else
  6838. [item setValue:@"true" forKey:@"is_closeout"];
  6839. if(cid==0)
  6840. [item setValue:@"false" forKey:@"cart_exists"];
  6841. else
  6842. [item setValue:@"true" forKey:@"cart_exists"];
  6843. if (more_color == 0) {
  6844. [item setObject:@(false) forKey:@"more_color"];
  6845. } else if (more_color == 1) {
  6846. [item setObject:@(true) forKey:@"more_color"];
  6847. }
  6848. [item addEntriesFromDictionary:imgjson];
  6849. // [item setValue:nsurl forKey:@"img"];
  6850. [item setValue:nsname forKey:@"fash_name"];
  6851. [item setValue:nsdescription forKey:@"description"];
  6852. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6853. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6854. i++;
  6855. }
  6856. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6857. [ret setObject:items forKey:@"items"];
  6858. sqlite3_finalize(statement);
  6859. }
  6860. [iSalesDB close_db:db];
  6861. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6862. return ret;
  6863. }
  6864. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6865. {
  6866. return [self itemsearch:params limited:YES];
  6867. }
  6868. #pragma mark order detail
  6869. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6870. if (str1.length == 0) {
  6871. str1 = @"&nbsp";
  6872. }
  6873. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6874. return str;
  6875. }
  6876. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6877. {
  6878. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6879. [fromformatter setDateFormat:from];
  6880. NSDate *date = [fromformatter dateFromString:datetime];
  6881. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6882. [toformatter setDateFormat:to];
  6883. NSString * ret = [toformatter stringFromDate:date];
  6884. return ret;
  6885. }
  6886. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6887. // 把毫秒去掉
  6888. if ([dateTime containsString:@"."]) {
  6889. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6890. }
  6891. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6892. formatter.dateFormat = formate;
  6893. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6894. NSDate *date = [formatter dateFromString:dateTime];
  6895. formatter.dateFormat = newFormate;
  6896. NSString *result = [formatter stringFromDate:date];
  6897. return result ? result : @"";
  6898. }
  6899. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6900. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6901. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6902. }
  6903. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6904. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6905. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6906. }
  6907. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6908. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6909. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6910. }
  6911. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6912. {
  6913. assert(params[@"mode"]!=nil);
  6914. assert(params[@"user"]!=nil);
  6915. DebugLog(@"offline oderdetail params: %@",params);
  6916. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6917. int orderId = [params[@"orderId"] intValue];
  6918. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6919. // decrypt card number and card security code
  6920. // 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 ];
  6921. 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];
  6922. sqlite3 *db = [iSalesDB get_db];
  6923. sqlite3_stmt * statement;
  6924. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6925. NSString *nssoid = @"";
  6926. NSString* orderinfo = @"";
  6927. NSString *moreInfo = @"";
  6928. double handlingFee = 0;
  6929. double payments_and_credist = 0;
  6930. double totalPrice = 0;
  6931. double shippingFee = 0;
  6932. double lift_gate = 0;
  6933. NSString *customer_contact = @"";
  6934. NSString *customer_email = @"";
  6935. NSString *customer_fax = @"";
  6936. NSString *customer_phone = @"";
  6937. NSString *customer_city = @"";
  6938. NSString *customer_state = @"";
  6939. NSString *customer_zipcode = @"";
  6940. NSString *customer_country = @"";
  6941. BOOL must_call = NO;
  6942. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6943. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6944. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6945. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6946. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6947. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6948. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6949. {
  6950. if (sqlite3_step(statement) == SQLITE_ROW)
  6951. {
  6952. // int order_id = sqlite3_column_int(statement, 0);
  6953. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6954. // ret[@"sign_url"] = sign_url;
  6955. section_1 = @{
  6956. @"data":sign_url,
  6957. @"title":@"Signature",
  6958. @"type":@"sign_url"
  6959. }.mutableCopy;
  6960. [ret setObject:section_1 forKey:@"section_1"];
  6961. customer_contact = [self textAtColumn:52 statement:statement];
  6962. customer_email = [self textAtColumn:53 statement:statement];
  6963. customer_phone = [self textAtColumn:54 statement:statement];
  6964. customer_fax = [self textAtColumn:55 statement:statement];
  6965. customer_city = [self textAtColumn:60 statement:statement];
  6966. customer_state = [self textAtColumn:61 statement:statement];
  6967. customer_zipcode = [self textAtColumn:62 statement:statement];
  6968. customer_country = [self textAtColumn:63 statement:statement];
  6969. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6970. int offline_edit=sqlite3_column_int(statement, 56);
  6971. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6972. char *soid = (char*)sqlite3_column_text(statement, 1);
  6973. if(soid==nil)
  6974. soid= "";
  6975. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6976. // so#
  6977. ret[@"so#"] = nssoid;
  6978. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6979. if(poNumber==nil)
  6980. poNumber= "";
  6981. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6982. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6983. if(create_time==nil)
  6984. create_time= "";
  6985. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6986. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6987. int status = sqlite3_column_int(statement, 4);
  6988. int erpStatus = sqlite3_column_int(statement, 49);
  6989. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6990. // status
  6991. if (status > 1 && status != 3) {
  6992. status = erpStatus;
  6993. } else if (status == 3) {
  6994. status = 15;
  6995. }
  6996. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6997. ret[@"order_status"] = nsstatus;
  6998. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6999. if(company_name==nil)
  7000. company_name= "";
  7001. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7002. // company name
  7003. ret[@"company_name"] = nscompany_name;
  7004. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7005. if(customer_contact==nil)
  7006. customer_contact= "";
  7007. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7008. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7009. if(addr_1==nil)
  7010. addr_1="";
  7011. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7012. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7013. if(addr_2==nil)
  7014. addr_2="";
  7015. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7016. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7017. if(addr_3==nil)
  7018. addr_3="";
  7019. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7020. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7021. if(addr_4==nil)
  7022. addr_4="";
  7023. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7024. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7025. [arr_addr addObject:nsaddr_1];
  7026. [arr_addr addObject:nsaddr_2];
  7027. [arr_addr addObject:nsaddr_3];
  7028. [arr_addr addObject:nsaddr_4];
  7029. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7030. [arr_addr addObject:customer_state];
  7031. [arr_addr addObject:customer_zipcode];
  7032. [arr_addr addObject:customer_country];
  7033. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7034. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7035. char *logist = (char*)sqlite3_column_text(statement, 11);
  7036. if(logist==nil)
  7037. logist= "";
  7038. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7039. if (/*status == -11 || */status == 10 || status == 11) {
  7040. nslogist = [self textAtColumn:59 statement:statement];
  7041. };
  7042. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7043. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7044. shipping = @"Shipping To Be Quoted";
  7045. } else {
  7046. shippingFee = sqlite3_column_double(statement, 12);
  7047. }
  7048. // Shipping
  7049. // ret[@"Shipping"] = shipping;
  7050. NSDictionary *shipping_item = @{
  7051. @"title":@"Shipping",
  7052. @"value":shipping
  7053. };
  7054. [price_data setObject:shipping_item forKey:@"item_1"];
  7055. int have_lift_gate = sqlite3_column_int(statement, 17);
  7056. lift_gate = sqlite3_column_double(statement, 13);
  7057. // Liftgate Fee(No loading dock)
  7058. if (!have_lift_gate) {
  7059. lift_gate = 0;
  7060. }
  7061. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7062. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7063. if (sqlite3_column_int(statement, 57)) {
  7064. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7065. liftgate_value = @"Shipping To Be Quoted";
  7066. }
  7067. NSDictionary *liftgate_item = @{
  7068. @"title":@"Liftgate Fee(No loading dock)",
  7069. @"value":liftgate_value
  7070. };
  7071. [price_data setObject:liftgate_item forKey:@"item_2"];
  7072. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7073. // [ret removeObjectForKey:@"Shipping"];
  7074. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7075. // }
  7076. //
  7077. // if (have_lift_gate) {
  7078. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7079. // }
  7080. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7081. if(general_notes==nil)
  7082. general_notes= "";
  7083. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7084. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7085. if(internal_notes==nil)
  7086. internal_notes= "";
  7087. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7088. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7089. if(payment_type==nil)
  7090. payment_type= "";
  7091. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7092. // order info
  7093. orderinfo = [self textFileName:@"order_info.html"];
  7094. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7095. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7096. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7097. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7098. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7099. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7100. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7101. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7102. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7103. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7104. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7105. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7106. NSString *payment = nil;
  7107. // id -> show
  7108. __block NSString *show_pay_type = nspayment_type;
  7109. [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) {
  7110. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7111. if (show_typ_ch != NULL) {
  7112. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7113. }
  7114. }];
  7115. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7116. {
  7117. payment = [self textFileName:@"creditcardpayment.html"];
  7118. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7119. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7120. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7121. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7122. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7123. NSString *card_type = [self textAtColumn:42 statement:statement];
  7124. if (card_type.length > 0) { // 显示星号
  7125. card_type = @"****";
  7126. }
  7127. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7128. if (card_number.length > 0 && card_number.length > 4) {
  7129. for (int i = 0; i < card_number.length - 4; i++) {
  7130. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7131. }
  7132. } else {
  7133. card_number = @"";
  7134. }
  7135. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7136. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7137. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7138. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7139. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7140. card_expiration = @"****";
  7141. }
  7142. NSString *card_city = [self textAtColumn:46 statement:statement];
  7143. NSString *card_state = [self textAtColumn:47 statement:statement];
  7144. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7145. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7146. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7147. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7148. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7149. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7150. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7151. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7152. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7153. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7154. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7155. }
  7156. else
  7157. {
  7158. payment=[self textFileName:@"normalpayment.html"];
  7159. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7160. }
  7161. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7162. ret[@"result"]= [NSNumber numberWithInt:2];
  7163. // more info
  7164. moreInfo = [self textFileName:@"more_info.html"];
  7165. /*****ship to******/
  7166. // ShipToCompany_or_&nbsp
  7167. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7168. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7169. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7170. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7171. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7172. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7173. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7174. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7175. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7176. /*****ship from******/
  7177. // ShipFromCompany_or_&nbsp
  7178. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7179. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7180. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7181. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7182. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7183. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7184. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7185. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7186. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7187. /*****freight to******/
  7188. // FreightBillToCompany_or_&nbsp
  7189. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7190. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7191. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7192. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7193. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7194. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7195. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7196. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7197. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7198. /*****merchandise to******/
  7199. // MerchandiseBillToCompany_or_&nbsp
  7200. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7201. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7202. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7203. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7204. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7205. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7206. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7207. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7208. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7209. /*****return to******/
  7210. // ReturnToCompany_or_&nbsp
  7211. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7212. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7213. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7214. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7215. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7216. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7217. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7218. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7219. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7220. //
  7221. // DebugLog(@"more info : %@",moreInfo);
  7222. // handling fee
  7223. handlingFee = sqlite3_column_double(statement, 33);
  7224. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7225. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7226. if (sqlite3_column_int(statement, 58)) {
  7227. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7228. handling_fee_value = @"Shipping To Be Quoted";
  7229. }
  7230. NSDictionary *handling_fee_item = @{
  7231. @"title":@"Handling Fee",
  7232. @"value":handling_fee_value
  7233. };
  7234. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7235. //
  7236. // customer info
  7237. customerID = [self textAtColumn:36 statement:statement];
  7238. // mode
  7239. ret[@"mode"] = params[@"mode"];
  7240. // model_count
  7241. ret[@"model_count"] = @(0);
  7242. }
  7243. sqlite3_finalize(statement);
  7244. }
  7245. [iSalesDB close_db:db];
  7246. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7247. // "customer_email" = "Shui Hu";
  7248. // "customer_fax" = "";
  7249. // "customer_first_name" = F;
  7250. // "customer_last_name" = L;
  7251. // "customer_name" = ",da He Xiang Dong Liu A";
  7252. // "customer_phone" = "Hey Xuan Feng";
  7253. contactInfo[@"customer_phone"] = customer_phone;
  7254. contactInfo[@"customer_fax"] = customer_fax;
  7255. contactInfo[@"customer_email"] = customer_email;
  7256. NSString *first_name = @"";
  7257. NSString *last_name = @"";
  7258. if ([customer_contact isEqualToString:@""]) {
  7259. } else if ([customer_contact containsString:@" "]) {
  7260. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7261. first_name = [customer_contact substringToIndex:first_space_index];
  7262. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7263. }
  7264. contactInfo[@"customer_first_name"] = first_name;
  7265. contactInfo[@"customer_last_name"] = last_name;
  7266. ret[@"customerInfo"] = contactInfo;
  7267. // models
  7268. if (nssoid) {
  7269. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7270. __block double TotalCuft = 0;
  7271. __block double TotalWeight = 0;
  7272. __block int TotalCarton = 0;
  7273. __block double allItemPrice = 0;
  7274. 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];
  7275. sqlite3 *db1 = [iSalesDB get_db];
  7276. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7277. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7278. int product_id = sqlite3_column_int(stmt, 0);
  7279. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7280. int item_id = sqlite3_column_int(stmt, 7);
  7281. NSString* Price=nil;
  7282. if(str_price==nil)
  7283. {
  7284. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7285. if(price==nil)
  7286. Price=@"No Price.";
  7287. else
  7288. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7289. }
  7290. else
  7291. {
  7292. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7293. }
  7294. double discount = sqlite3_column_double(stmt, 2);
  7295. int item_count = sqlite3_column_int(stmt, 3);
  7296. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7297. NSString *nsline_note=nil;
  7298. if(line_note!=nil)
  7299. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7300. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7301. // NSString *nsname = nil;
  7302. // if(name!=nil)
  7303. // nsname= [[NSString alloc]initWithUTF8String:name];
  7304. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7305. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7306. // NSString *nsdescription=nil;
  7307. // if(description!=nil)
  7308. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7309. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7310. // int stockUom = sqlite3_column_int(stmt, 8);
  7311. // int _id = sqlite3_column_int(stmt, 9);
  7312. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7313. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7314. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7315. int carton=[bsubtotaljson[@"carton"] intValue];
  7316. TotalCuft += cuft;
  7317. TotalWeight += weight;
  7318. TotalCarton += carton;
  7319. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7320. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7321. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7322. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7323. itemjson[@"note"]=nsline_note;
  7324. itemjson[@"origin_price"] = Price;
  7325. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7326. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7327. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7328. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7329. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7330. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7331. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7332. if(itemjson[@"combine"] != nil)
  7333. {
  7334. // int citem=0;
  7335. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7336. for(int bc=0;bc<bcount;bc++)
  7337. {
  7338. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7339. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7340. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7341. subTotal += uprice * modulus * item_count;
  7342. }
  7343. }
  7344. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7345. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7346. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7347. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7348. itemjson[@"type"] = @"order_item";
  7349. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7350. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7351. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7352. allItemPrice += subTotal;
  7353. }];
  7354. section_3[@"data"] = model_data;
  7355. section_3[@"type"] = @"sub_order";
  7356. section_3[@"title"] = @"Models";
  7357. section_3[@"switch"] = @(false);
  7358. ret[@"section_3"] = section_3;
  7359. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7360. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7361. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7362. // payments/Credits
  7363. // payments_and_credist = sqlite3_column_double(statement, 34);
  7364. payments_and_credist = allItemPrice;
  7365. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7366. NSDictionary *sub_total_item = @{
  7367. @"title":@"Sub-Total",
  7368. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7369. };
  7370. [price_data setObject:sub_total_item forKey:@"item_0"];
  7371. // // total
  7372. // totalPrice = sqlite3_column_double(statement, 35);
  7373. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7374. } else {
  7375. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7376. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7377. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7378. // payments/Credits
  7379. payments_and_credist = 0;
  7380. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7381. NSDictionary *sub_total_item = @{
  7382. @"title":@"Sub-Total",
  7383. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7384. };
  7385. [price_data setObject:sub_total_item forKey:@"item_0"];
  7386. }
  7387. // total
  7388. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7389. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7390. NSDictionary *total_item = @{
  7391. @"title":@"Total",
  7392. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7393. };
  7394. [price_data setObject:total_item forKey:@"item_4"];
  7395. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7396. section_4 = @{
  7397. @"data":price_data,
  7398. @"title":@"Price Info",
  7399. @"type":@"price_info"
  7400. }.mutableCopy;
  7401. ret[@"section_4"] = section_4;
  7402. // ret[@"order_info"]= orderinfo;
  7403. section_0 = @{
  7404. @"data":orderinfo,
  7405. @"title":@"Order Info",
  7406. @"type":@"order_info"
  7407. }.mutableCopy;
  7408. ret[@"section_0"] = section_0;
  7409. // ret[@"more_order_info"] = moreInfo;
  7410. section_2 = @{
  7411. @"data":moreInfo,
  7412. @"title":@"More Info",
  7413. @"type":@"more_order_info"
  7414. }.mutableCopy;
  7415. ret[@"section_2"] = section_2;
  7416. ret[@"count"] = @(5);
  7417. return [RAConvertor dict2data:ret];
  7418. }
  7419. #pragma mark order list
  7420. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7421. NSString *nsstatus = @"";
  7422. switch (status) {
  7423. case 0:
  7424. {
  7425. nsstatus=@"Temp Order";
  7426. break;
  7427. }
  7428. case 1:
  7429. {
  7430. nsstatus=@"Saved Order";
  7431. break;
  7432. }
  7433. case 2: {
  7434. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7435. break;
  7436. }
  7437. case 3:
  7438. case 15:
  7439. {
  7440. nsstatus=@"Cancelled";
  7441. break;
  7442. }
  7443. case 10:
  7444. {
  7445. nsstatus=@"Quote Submitted";
  7446. break;
  7447. }
  7448. case 11:
  7449. {
  7450. nsstatus=@"Sales Order Submitted";
  7451. break;
  7452. }
  7453. case 12:
  7454. {
  7455. nsstatus=@"Processing";
  7456. break;
  7457. }
  7458. case 13:
  7459. {
  7460. nsstatus=@"Shipped";
  7461. break;
  7462. }
  7463. case 14:
  7464. {
  7465. nsstatus=@"Closed";
  7466. break;
  7467. }
  7468. case -11:
  7469. {
  7470. nsstatus = @"Ready For Submit";
  7471. break;
  7472. }
  7473. default:
  7474. break;
  7475. }
  7476. return nsstatus;
  7477. }
  7478. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7479. {
  7480. double total = 0;
  7481. __block double payments_and_credist = 0;
  7482. __block double allItemPrice = 0;
  7483. // sqlite3 *db1 = [iSalesDB get_db];
  7484. if (so_id) {
  7485. // 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];
  7486. 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];
  7487. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7488. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7489. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7490. int product_id = sqlite3_column_int(stmt, 0);
  7491. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7492. int discount = sqlite3_column_int(stmt, 2);
  7493. int item_count = sqlite3_column_int(stmt, 3);
  7494. // int item_id = sqlite3_column_int(stmt, 7);
  7495. int item_id = sqlite3_column_int(stmt, 4);
  7496. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7497. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7498. NSString* Price=nil;
  7499. if(str_price==nil)
  7500. {
  7501. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7502. if(price==nil)
  7503. Price=@"No Price.";
  7504. else
  7505. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7506. }
  7507. else
  7508. {
  7509. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7510. }
  7511. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7512. itemjson[@"The unit price"]=Price;
  7513. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7514. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7515. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7516. if(itemjson[@"combine"] != nil)
  7517. {
  7518. // int citem=0;
  7519. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7520. for(int bc=0;bc<bcount;bc++)
  7521. {
  7522. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7523. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7524. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7525. subTotal += uprice * modulus * item_count;
  7526. }
  7527. }
  7528. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7529. allItemPrice += subTotal;
  7530. }];
  7531. payments_and_credist = allItemPrice;
  7532. } else {
  7533. // payments/Credits
  7534. payments_and_credist = 0;
  7535. }
  7536. // lift_gate handlingFee shippingFee
  7537. __block double lift_gate = 0;
  7538. __block double handlingFee = 0;
  7539. __block double shippingFee = 0;
  7540. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7541. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7542. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7543. if (have_lift_gate) {
  7544. lift_gate = sqlite3_column_double(stmt, 1);
  7545. }
  7546. handlingFee = sqlite3_column_double(stmt, 2);
  7547. shippingFee = sqlite3_column_double(stmt, 3);
  7548. }];
  7549. // total
  7550. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7551. if (close) {
  7552. [iSalesDB close_db:db1];
  7553. }
  7554. return total;
  7555. }
  7556. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7557. {
  7558. assert(params[@"user"]!=nil);
  7559. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7560. int limit = [[params valueForKey:@"limit"] intValue];
  7561. int offset = [[params valueForKey:@"offset"] intValue];
  7562. NSString* keyword = [params valueForKey:@"keyWord"];
  7563. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7564. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7565. NSString* where=@"1 = 1";
  7566. if(keyword.length>0)
  7567. 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]];
  7568. if (orderStatus.length > 0) {
  7569. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7570. if (order_status_array.count == 1) {
  7571. int status_value = [[order_status_array firstObject] intValue];
  7572. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7573. if (status_value == 15 || status_value == 3) {
  7574. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7575. } else {
  7576. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7577. }
  7578. } else {
  7579. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7580. }
  7581. } else if (order_status_array.count > 1) {
  7582. for (int i = 0; i < order_status_array.count;i++) {
  7583. NSString *status = order_status_array[i];
  7584. NSString *condition = @" or";
  7585. if (i == 0) {
  7586. condition = @" and (";
  7587. }
  7588. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7589. int status_value = [status intValue];
  7590. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7591. if (status_value == 15 || status_value == 3) {
  7592. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7593. } else {
  7594. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7595. }
  7596. } else {
  7597. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7598. }
  7599. }
  7600. where = [where stringByAppendingString:@" )"];
  7601. }
  7602. }
  7603. 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];
  7604. // DebugLog(@"order list sql: %@",sqlQuery);
  7605. sqlite3 *db = [iSalesDB get_db];
  7606. sqlite3_stmt * statement;
  7607. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7608. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7609. {
  7610. int count=0;
  7611. while (sqlite3_step(statement) == SQLITE_ROW)
  7612. {
  7613. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7614. int order_id = sqlite3_column_double(statement, 0);
  7615. char *soid = (char*)sqlite3_column_text(statement, 1);
  7616. if(soid==nil)
  7617. soid= "";
  7618. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7619. int status = sqlite3_column_double(statement, 2);
  7620. int erpStatus = sqlite3_column_double(statement, 9);
  7621. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7622. if(sales_rep==nil)
  7623. sales_rep= "";
  7624. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7625. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7626. if(create_by==nil)
  7627. create_by= "";
  7628. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7629. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7630. if(company_name==nil)
  7631. company_name= "";
  7632. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7633. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7634. if(create_time==nil)
  7635. create_time= "";
  7636. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7637. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7638. // double total_price = sqlite3_column_double(statement, 7);
  7639. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7640. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7641. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7642. int offline_edit = sqlite3_column_int(statement, 10);
  7643. // ": "JH",
  7644. // "": "$8307.00",
  7645. // "": "MOB1608050001",
  7646. // "": "ArpithaT",
  7647. // "": "1st Stage Property Transformations",
  7648. // "": "JANICE SUTTON",
  7649. // "": 2255,
  7650. // "": "08/02/2016 09:49:18",
  7651. // "": 1,
  7652. // "": "Saved Order"
  7653. // "": "1470384050483",
  7654. // "model_count": "6 / 28"
  7655. item[@"sales_rep"]= nssales_rep;
  7656. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7657. item[@"so#"]= nssoid;
  7658. item[@"create_by"]= nscreate_by;
  7659. item[@"customer_name"]= nscompany_name;
  7660. item[@"customer_contact"] = customer_contact;
  7661. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7662. item[@"purchase_time"]= nscreate_time;
  7663. int statusCode = status;
  7664. if (statusCode == 2) {
  7665. statusCode = erpStatus;
  7666. } else if (statusCode == 3) {
  7667. statusCode = 15;
  7668. }
  7669. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7670. item[@"order_status"]= nsstatus;
  7671. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7672. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7673. // item[@"model_count"]
  7674. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7675. count++;
  7676. }
  7677. ret[@"count"]= [NSNumber numberWithInt:count];
  7678. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7679. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7680. ret[@"result"]= [NSNumber numberWithInt:2];
  7681. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7682. ret[@"time_zone"] = @"PST";
  7683. sqlite3_finalize(statement);
  7684. }
  7685. 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];
  7686. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7687. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7688. [iSalesDB close_db:db];
  7689. return [RAConvertor dict2data:ret];
  7690. }
  7691. #pragma mark update gnotes
  7692. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7693. {
  7694. DebugLog(@"params: %@",params);
  7695. // comments = Meyoyoyoyoyoyoy;
  7696. // orderCode = MOB1608110001;
  7697. // password = 123456;
  7698. // user = EvanK;
  7699. 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]];
  7700. int ret = [iSalesDB execSql:sql];
  7701. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7702. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7703. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7704. // [dic setValue:@"160409" forKey:@"min_ver"];
  7705. return [RAConvertor dict2data:dic];
  7706. }
  7707. #pragma mark move to wishlist
  7708. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7709. {
  7710. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7711. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7712. _id = [NSString stringWithFormat:@"(%@)",_id];
  7713. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7714. sqlite3 *db = [iSalesDB get_db];
  7715. __block NSString *product_id = @"";
  7716. __block NSString *item_count_str = @"";
  7717. // __block NSString *item_id = nil;
  7718. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7719. // product_id = [self textAtColumn:0 statement:stmt];
  7720. int item_count = sqlite3_column_int(stmt, 1);
  7721. // item_id = [self textAtColumn:2 statement:stmt];
  7722. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7723. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7724. if (p_id.length) {
  7725. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7726. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7727. }
  7728. }];
  7729. [iSalesDB close_db:db];
  7730. // 去除第一个,
  7731. if (product_id.length > 1) {
  7732. product_id = [product_id substringFromIndex:1];
  7733. }
  7734. if (item_count_str.length > 1) {
  7735. item_count_str = [item_count_str substringFromIndex:1];
  7736. }
  7737. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7738. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7739. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7740. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7741. sqlite3 *db1 = [iSalesDB get_db];
  7742. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7743. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7744. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7745. // 删除
  7746. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7747. int ret = [iSalesDB execSql:deleteSql db:db1];
  7748. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7749. [iSalesDB close_db:db1];
  7750. return [RAConvertor dict2data:dic];
  7751. }
  7752. #pragma mark cart delete
  7753. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7754. {
  7755. // cartItemId = 548;
  7756. // orderCode = MOB1608110001;
  7757. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7758. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7759. _id = [NSString stringWithFormat:@"(%@)",_id];
  7760. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7761. int ret = [iSalesDB execSql:sql];
  7762. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7763. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7764. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7765. // [dic setValue:@"160409" forKey:@"min_ver"];
  7766. return [RAConvertor dict2data:dic];
  7767. }
  7768. #pragma mark set price
  7769. +(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7770. {
  7771. DebugLog(@"cart set price params: %@",params);
  7772. // "cartitem_id" = 1;
  7773. // discount = "0.000000";
  7774. // "item_note" = "";
  7775. // price = "269.000000";
  7776. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7777. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7778. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7779. // NSString *price = [self valueInParams:params key:@"price"];
  7780. // bool badd_price_changed=false;
  7781. // sqlite3* db=[iSalesDB get_db];
  7782. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7783. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7784. // NSRange range;
  7785. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7786. //
  7787. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7788. // badd_price_changed=true;
  7789. // [iSalesDB close_db:db];
  7790. //
  7791. // if(badd_price_changed)
  7792. // {
  7793. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7794. // }
  7795. //
  7796. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7797. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7798. int ret = [iSalesDB execSql:sql];
  7799. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7800. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7801. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7802. // [dic setValue:@"160409" forKey:@"min_ver"];
  7803. // return [RAConvertor dict2data:dic];
  7804. return dic;
  7805. }
  7806. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7807. {
  7808. DebugLog(@"cart set price params: %@",params);
  7809. // "cartitem_id" = 1;
  7810. // discount = "0.000000";
  7811. // "item_note" = "";
  7812. // price = "269.000000";
  7813. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7814. NSString *notes = [self valueInParams:params key:@"item_note"];
  7815. NSString *discount = [self valueInParams:params key:@"discount"];
  7816. NSString *price = [self valueInParams:params key:@"price"];
  7817. // bool badd_price_changed=false;
  7818. // sqlite3* db=[iSalesDB get_db];
  7819. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7820. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7821. // NSRange range;
  7822. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7823. //
  7824. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7825. // badd_price_changed=true;
  7826. // [iSalesDB close_db:db];
  7827. //
  7828. // if(badd_price_changed)
  7829. // {
  7830. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7831. // }
  7832. //
  7833. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7834. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7835. int ret = [iSalesDB execSql:sql];
  7836. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7837. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7838. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7839. // [dic setValue:@"160409" forKey:@"min_ver"];
  7840. return [RAConvertor dict2data:dic];
  7841. }
  7842. #pragma mark set line notes
  7843. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7844. {
  7845. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7846. NSString *notes = [self valueInParams:params key:@"notes"];
  7847. notes = [self translateSingleQuote:notes];
  7848. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7849. int ret = [iSalesDB execSql:sql];
  7850. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7851. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7852. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7853. // [dic setValue:@"160409" forKey:@"min_ver"];
  7854. return [RAConvertor dict2data:dic];
  7855. }
  7856. #pragma mark set qty
  7857. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7858. {
  7859. assert(params[@"can_create_backorder"]!=nil);
  7860. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7861. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7862. int item_count = [params[@"inputInt"] intValue];
  7863. // 购买检查数量大于库存
  7864. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7865. if (!params[@"can_create_backorder"]) {
  7866. 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];
  7867. __block BOOL out_of_stock = NO;
  7868. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7869. int availability = sqlite3_column_int(stmt, 0);
  7870. if (availability < item_count) {
  7871. out_of_stock = YES;
  7872. }
  7873. }];
  7874. if (out_of_stock) { // 缺货
  7875. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7876. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7877. return [RAConvertor dict2data:dic];
  7878. }
  7879. }
  7880. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7881. int ret = [iSalesDB execSql:sql];
  7882. __block int item_id = 0;
  7883. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7884. item_id = sqlite3_column_int(stmt, 0);
  7885. }];
  7886. sqlite3 *db = [iSalesDB get_db];
  7887. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7888. [iSalesDB close_db:db];
  7889. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7890. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7891. // [dic setValue:@"160409" forKey:@"min_ver"];
  7892. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7893. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7894. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7895. return [RAConvertor dict2data:dic];
  7896. }
  7897. #pragma mark place order
  7898. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7899. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7900. if (aname) {
  7901. [dic setValue:aname forKey:@"aname"];
  7902. }
  7903. if (control) {
  7904. [dic setValue:control forKey:@"control"];
  7905. }
  7906. if (keyboard) {
  7907. [dic setValue:keyboard forKey:@"keyboard"];
  7908. }
  7909. if (name) {
  7910. [dic setValue:name forKey:@"name"];
  7911. }
  7912. if (value) {
  7913. [dic setValue:value forKey:@"value"];
  7914. }
  7915. return dic;
  7916. }
  7917. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7918. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7919. orderCode = [self translateSingleQuote:orderCode];
  7920. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7921. 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];
  7922. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7923. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7924. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7925. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7926. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7927. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7928. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7929. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7930. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7931. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7932. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7933. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7934. NSString *email = [self textAtColumn:11 statement:stmt];
  7935. NSString *phone = [self textAtColumn:12 statement:stmt];
  7936. NSString *fax = [self textAtColumn:13 statement:stmt];
  7937. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7938. NSString *city = [self textAtColumn:15 statement:stmt];
  7939. NSString *state = [self textAtColumn:16 statement:stmt];
  7940. NSString *country = [self textAtColumn:17 statement:stmt];
  7941. NSString *name = [self textAtColumn:18 statement:stmt];
  7942. // contact id
  7943. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7944. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7945. [contact_id_dic setValue:@"text" forKey:@"control"];
  7946. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7947. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7948. [contact_id_dic setValue:@"true" forKey:@"required"];
  7949. [contact_id_dic setValue:contact_id forKey:@"value"];
  7950. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7951. // business card
  7952. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7953. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7954. [business_card_dic setValue:@"img" forKey:@"control"];
  7955. [business_card_dic setValue:@"1" forKey:@"disable"];
  7956. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7957. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7958. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7959. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7960. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7961. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7962. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7963. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7964. // fax
  7965. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7966. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7967. // zipcode
  7968. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7969. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7970. // city
  7971. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7972. [customer_dic setValue:city_dic forKey:@"item_12"];
  7973. // state
  7974. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7975. [customer_dic setValue:state_dic forKey:@"item_13"];
  7976. // country
  7977. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7978. [customer_dic setValue:country_dic forKey:@"item_14"];
  7979. // company name
  7980. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7981. [customer_dic setValue:company_dic forKey:@"item_2"];
  7982. // addr_1
  7983. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7984. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7985. // addr_2
  7986. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7987. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7988. // addr_3
  7989. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7990. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7991. // addr_4
  7992. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7993. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7994. // Contact
  7995. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7996. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7997. // email
  7998. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7999. [customer_dic setValue:email_dic forKey:@"item_8"];
  8000. // phone
  8001. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8002. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8003. // title
  8004. [customer_dic setValue:@"Customer" forKey:@"title"];
  8005. // count
  8006. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8007. }];
  8008. // setting
  8009. NSDictionary *setting = params[@"setting"];
  8010. NSNumber *hide = setting[@"CustomerHide"];
  8011. [customer_dic setValue:hide forKey:@"hide"];
  8012. return customer_dic;
  8013. }
  8014. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8015. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8016. // setting
  8017. NSDictionary *setting = params[@"setting"];
  8018. NSNumber *hide = setting[@"ShipToHide"];
  8019. [dic setValue:hide forKey:@"hide"];
  8020. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8021. orderCode = [self translateSingleQuote:orderCode];
  8022. 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];
  8023. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8024. NSString *cid = [self textAtColumn:0 statement:stmt];
  8025. NSString *name = [self textAtColumn:1 statement:stmt];
  8026. NSString *ext = [self textAtColumn:2 statement:stmt];
  8027. NSString *contact = [self textAtColumn:3 statement:stmt];
  8028. NSString *email = [self textAtColumn:4 statement:stmt];
  8029. NSString *fax = [self textAtColumn:5 statement:stmt];
  8030. NSString *phone = [self textAtColumn:6 statement:stmt];
  8031. // count
  8032. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8033. // title
  8034. [dic setValue:@"Ship To" forKey:@"title"];
  8035. // choose
  8036. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8037. [choose_dic setValue:@"choose" forKey:@"aname"];
  8038. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8039. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8040. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8041. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8042. @"receive_contact" : @"customer_contact",
  8043. @"receive_email" : @"customer_email",
  8044. @"receive_ext" : @"customer_contact_ext",
  8045. @"receive_fax" : @"customer_fax",
  8046. @"receive_name" : @"customer_name",
  8047. @"receive_phone" : @"customer_phone"},
  8048. @"refresh" : [NSNumber numberWithInteger:1],
  8049. @"type" : @"pull"};
  8050. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8051. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8052. @"name" : @"Add new address",
  8053. @"refresh" : [NSNumber numberWithInteger:1],
  8054. @"value" : @"new_addr"
  8055. };
  8056. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8057. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8058. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8059. @"receive_contact" : @"customer_contact",
  8060. @"receive_email" : @"customer_email",
  8061. @"receive_ext" : @"customer_contact_ext",
  8062. @"receive_fax" : @"customer_fax",
  8063. @"receive_name" : @"customer_name",
  8064. @"receive_phone" : @"customer_phone"},
  8065. @"name" : @"select_ship_to",
  8066. @"refresh" : [NSNumber numberWithInteger:1],
  8067. @"value" : @"Sales_Order_Ship_To"};
  8068. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8069. [dic setValue:choose_dic forKey:@"item_0"];
  8070. // contact id
  8071. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8072. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8073. [contact_id_dic setValue:@"true" forKey:@"required"];
  8074. [dic setValue:contact_id_dic forKey:@"item_1"];
  8075. // company name
  8076. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8077. [dic setValue:company_name_dic forKey:@"item_2"];
  8078. // address
  8079. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8080. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8081. if ([required integerValue]) {
  8082. [ext_dic setValue:@"true" forKey:@"required"];
  8083. }
  8084. [dic setValue:ext_dic forKey:@"item_3"];
  8085. // contact
  8086. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8087. [dic setValue:contact_dic forKey:@"item_4"];
  8088. // phone
  8089. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8090. [dic setValue:phone_dic forKey:@"item_5"];
  8091. // fax
  8092. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8093. [dic setValue:fax_dic forKey:@"item_6"];
  8094. // email
  8095. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8096. [dic setValue:email_dic forKey:@"item_7"];
  8097. }];
  8098. return dic;
  8099. }
  8100. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8101. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8102. // setting
  8103. NSDictionary *setting = params[@"setting"];
  8104. NSNumber *hide = setting[@"ShipFromHide"];
  8105. [dic setValue:hide forKey:@"hide"];
  8106. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8107. orderCode = [self translateSingleQuote:orderCode];
  8108. 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];
  8109. 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';";
  8110. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8111. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8112. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8113. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8114. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8115. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8116. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8117. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8118. if (!cid.length) {
  8119. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8120. cid = [self textAtColumn:0 statement:statment];
  8121. name = [self textAtColumn:1 statement:statment];
  8122. ext = [self textAtColumn:2 statement:statment];
  8123. contact = [self textAtColumn:3 statement:statment];
  8124. email = [self textAtColumn:4 statement:statment];
  8125. fax = [self textAtColumn:5 statement:statment];
  8126. phone = [self textAtColumn:6 statement:statment];
  8127. }];
  8128. }
  8129. // count
  8130. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8131. // title
  8132. [dic setValue:@"Ship From" forKey:@"title"];
  8133. // hide
  8134. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8135. // choose
  8136. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8137. [choose_dic setValue:@"choose" forKey:@"aname"];
  8138. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8139. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8140. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8141. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8142. @"sender_contact" : @"customer_contact",
  8143. @"sender_email" : @"customer_email",
  8144. @"sender_ext" : @"customer_contact_ext",
  8145. @"sender_fax" : @"customer_fax",
  8146. @"sender_name" : @"customer_name",
  8147. @"sender_phone" : @"customer_phone"},
  8148. @"name" : @"select_cid",
  8149. @"refresh" : [NSNumber numberWithInteger:0],
  8150. @"value" : @"Sales_Order_Ship_From"};
  8151. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8152. [dic setValue:choose_dic forKey:@"item_0"];
  8153. // contact id
  8154. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8155. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8156. [contact_id_dic setValue:@"true" forKey:@"required"];
  8157. [dic setValue:contact_id_dic forKey:@"item_1"];
  8158. // company name
  8159. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8160. [dic setValue:company_name_dic forKey:@"item_2"];
  8161. // address
  8162. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8163. [dic setValue:ext_dic forKey:@"item_3"];
  8164. // contact
  8165. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8166. [dic setValue:contact_dic forKey:@"item_4"];
  8167. // phone
  8168. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8169. [dic setValue:phone_dic forKey:@"item_5"];
  8170. // fax
  8171. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8172. [dic setValue:fax_dic forKey:@"item_6"];
  8173. // email
  8174. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8175. [dic setValue:email_dic forKey:@"item_7"];
  8176. }];
  8177. return dic;
  8178. }
  8179. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8180. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8181. // setting
  8182. NSDictionary *setting = params[@"setting"];
  8183. NSNumber *hide = setting[@"FreightBillToHide"];
  8184. [dic setValue:hide forKey:@"hide"];
  8185. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8186. orderCode = [self translateSingleQuote:orderCode];
  8187. 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];
  8188. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8189. NSString *cid = [self textAtColumn:0 statement:stmt];
  8190. NSString *name = [self textAtColumn:1 statement:stmt];
  8191. NSString *ext = [self textAtColumn:2 statement:stmt];
  8192. NSString *contact = [self textAtColumn:3 statement:stmt];
  8193. NSString *email = [self textAtColumn:4 statement:stmt];
  8194. NSString *fax = [self textAtColumn:5 statement:stmt];
  8195. NSString *phone = [self textAtColumn:6 statement:stmt];
  8196. // count
  8197. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8198. // title
  8199. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8200. // hide
  8201. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8202. // choose
  8203. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8204. [choose_dic setValue:@"choose" forKey:@"aname"];
  8205. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8206. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8207. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8208. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8209. @"shipping_billto_contact" : @"receive_contact",
  8210. @"shipping_billto_email" : @"receive_email",
  8211. @"shipping_billto_ext" : @"receive_ext",
  8212. @"shipping_billto_fax" : @"receive_fax",
  8213. @"shipping_billto_name" : @"receive_name",
  8214. @"shipping_billto_phone" : @"receive_phone"},
  8215. @"type" : @"pull"};
  8216. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8217. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8218. @"type" : @"pull",
  8219. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8220. @"shipping_billto_contact" : @"customer_contact",
  8221. @"shipping_billto_email" : @"customer_email",
  8222. @"shipping_billto_ext" : @"customer_contact_ext",
  8223. @"shipping_billto_fax" : @"customer_fax",
  8224. @"shipping_billto_name" : @"customer_name",
  8225. @"shipping_billto_phone" : @"customer_phone"}
  8226. };
  8227. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8228. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8229. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8230. @"shipping_billto_contact" : @"sender_contact",
  8231. @"shipping_billto_email" : @"sender_email",
  8232. @"shipping_billto_ext" : @"sender_ext",
  8233. @"shipping_billto_fax" : @"sender_fax",
  8234. @"shipping_billto_name" : @"sender_name",
  8235. @"shipping_billto_phone" : @"sender_phone"},
  8236. @"type" : @"pull"
  8237. };
  8238. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8239. NSDictionary *select_freight_bill_to_dic = @{
  8240. @"aname" : @"Select freight bill to",
  8241. @"name" : @"select_cid",
  8242. @"refresh" : [NSNumber numberWithInteger:0],
  8243. @"value" : @"Sales_Order_Freight_Bill_To",
  8244. @"key_map" : @{
  8245. @"shipping_billto_cid" : @"customer_cid",
  8246. @"shipping_billto_contact" : @"customer_contact",
  8247. @"shipping_billto_email" : @"customer_email",
  8248. @"shipping_billto_ext" : @"customer_contact_ext",
  8249. @"shipping_billto_fax" : @"customer_fax",
  8250. @"shipping_billto_name" : @"customer_name",
  8251. @"shipping_billto_phone" : @"customer_phone"
  8252. }
  8253. };
  8254. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8255. [dic setValue:choose_dic forKey:@"item_0"];
  8256. // contact id
  8257. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8258. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8259. [contact_id_dic setValue:@"true" forKey:@"required"];
  8260. [dic setValue:contact_id_dic forKey:@"item_1"];
  8261. // company name
  8262. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8263. [dic setValue:company_name_dic forKey:@"item_2"];
  8264. // address
  8265. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8266. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8267. if ([ext_required integerValue]) {
  8268. [ext_dic setValue:@"true" forKey:@"required"];
  8269. }
  8270. [dic setValue:ext_dic forKey:@"item_3"];
  8271. // contact
  8272. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8273. [dic setValue:contact_dic forKey:@"item_4"];
  8274. // phone
  8275. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8276. [dic setValue:phone_dic forKey:@"item_5"];
  8277. // fax
  8278. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8279. [dic setValue:fax_dic forKey:@"item_6"];
  8280. // email
  8281. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8282. [dic setValue:email_dic forKey:@"item_7"];
  8283. }];
  8284. return dic;
  8285. }
  8286. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8287. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8288. // setting
  8289. NSDictionary *setting = params[@"setting"];
  8290. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8291. [dic setValue:hide forKey:@"hide"];
  8292. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8293. orderCode = [self translateSingleQuote:orderCode];
  8294. 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];
  8295. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8296. NSString *cid = [self textAtColumn:0 statement:stmt];
  8297. NSString *name = [self textAtColumn:1 statement:stmt];
  8298. NSString *ext = [self textAtColumn:2 statement:stmt];
  8299. NSString *contact = [self textAtColumn:3 statement:stmt];
  8300. NSString *email = [self textAtColumn:4 statement:stmt];
  8301. NSString *fax = [self textAtColumn:5 statement:stmt];
  8302. NSString *phone = [self textAtColumn:6 statement:stmt];
  8303. // count
  8304. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8305. // title
  8306. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8307. // hide
  8308. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8309. // choose
  8310. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8311. [choose_dic setValue:@"choose" forKey:@"aname"];
  8312. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8313. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8314. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8315. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8316. @"billing_contact" : @"receive_contact",
  8317. @"billing_email" : @"receive_email",
  8318. @"billing_ext" : @"receive_ext",
  8319. @"billing_fax" : @"receive_fax",
  8320. @"billing_name" : @"receive_name",
  8321. @"billing_phone" : @"receive_phone"},
  8322. @"type" : @"pull"};
  8323. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8324. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8325. @"type" : @"pull",
  8326. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8327. @"billing_contact" : @"customer_contact",
  8328. @"billing_email" : @"customer_email",
  8329. @"billing_ext" : @"customer_contact_ext",
  8330. @"billing_fax" : @"customer_fax",
  8331. @"billing_name" : @"customer_name",
  8332. @"billing_phone" : @"customer_phone"}
  8333. };
  8334. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8335. NSDictionary *select_bill_to_dic = @{
  8336. @"aname" : @"Select bill to",
  8337. @"name" : @"select_cid",
  8338. @"refresh" : [NSNumber numberWithInteger:0],
  8339. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8340. @"key_map" : @{
  8341. @"billing_cid" : @"customer_cid",
  8342. @"billing_contact" : @"customer_contact",
  8343. @"billing_email" : @"customer_email",
  8344. @"billing_ext" : @"customer_contact_ext",
  8345. @"billing_fax" : @"customer_fax",
  8346. @"billing_name" : @"customer_name",
  8347. @"billing_phone" : @"customer_phone"
  8348. }
  8349. };
  8350. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8351. [dic setValue:choose_dic forKey:@"item_0"];
  8352. // contact id
  8353. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8354. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8355. [contact_id_dic setValue:@"true" forKey:@"required"];
  8356. [dic setValue:contact_id_dic forKey:@"item_1"];
  8357. // company name
  8358. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8359. [dic setValue:company_name_dic forKey:@"item_2"];
  8360. // address
  8361. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8362. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8363. if ([ext_required integerValue]) {
  8364. [ext_dic setValue:@"true" forKey:@"required"];
  8365. }
  8366. [dic setValue:ext_dic forKey:@"item_3"];
  8367. // contact
  8368. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8369. [dic setValue:contact_dic forKey:@"item_4"];
  8370. // phone
  8371. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8372. [dic setValue:phone_dic forKey:@"item_5"];
  8373. // fax
  8374. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8375. [dic setValue:fax_dic forKey:@"item_6"];
  8376. // email
  8377. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8378. [dic setValue:email_dic forKey:@"item_7"];
  8379. }];
  8380. return dic;
  8381. }
  8382. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8383. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8384. // setting
  8385. NSDictionary *setting = params[@"setting"];
  8386. NSNumber *hide = setting[@"ReturnToHide"];
  8387. [dic setValue:hide forKey:@"hide"];
  8388. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8389. orderCode = [self translateSingleQuote:orderCode];
  8390. 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];
  8391. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8392. NSString *cid = [self textAtColumn:0 statement:stmt];
  8393. NSString *name = [self textAtColumn:1 statement:stmt];
  8394. NSString *ext = [self textAtColumn:2 statement:stmt];
  8395. NSString *contact = [self textAtColumn:3 statement:stmt];
  8396. NSString *email = [self textAtColumn:4 statement:stmt];
  8397. NSString *fax = [self textAtColumn:5 statement:stmt];
  8398. NSString *phone = [self textAtColumn:6 statement:stmt];
  8399. // count
  8400. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8401. // title
  8402. [dic setValue:@"Return To" forKey:@"title"];
  8403. // hide
  8404. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8405. // choose
  8406. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8407. [choose_dic setValue:@"choose" forKey:@"aname"];
  8408. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8409. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8410. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8411. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8412. @"returnto_contact" : @"sender_contact",
  8413. @"returnto_email" : @"sender_email",
  8414. @"returnto_ext" : @"sender_ext",
  8415. @"returnto_fax" : @"sender_fax",
  8416. @"returnto_name" : @"sender_name",
  8417. @"returnto_phone" : @"sender_phone"},
  8418. @"type" : @"pull"};
  8419. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8420. NSDictionary *select_return_to_dic = @{
  8421. @"aname" : @"Select return to",
  8422. @"name" : @"select_cid",
  8423. @"refresh" : [NSNumber numberWithInteger:0],
  8424. @"value" : @"Contact_Return_To",
  8425. @"key_map" : @{
  8426. @"returnto_cid" : @"customer_cid",
  8427. @"returnto_contact" : @"customer_contact",
  8428. @"returnto_email" : @"customer_email",
  8429. @"returnto_ext" : @"customer_contact_ext",
  8430. @"returnto_fax" : @"customer_fax",
  8431. @"returnto_name" : @"customer_name",
  8432. @"returnto_phone" : @"customer_phone"
  8433. }
  8434. };
  8435. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8436. [dic setValue:choose_dic forKey:@"item_0"];
  8437. // contact id
  8438. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8439. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8440. [contact_id_dic setValue:@"true" forKey:@"required"];
  8441. [dic setValue:contact_id_dic forKey:@"item_1"];
  8442. // company name
  8443. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8444. [dic setValue:company_name_dic forKey:@"item_2"];
  8445. // address
  8446. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8447. [dic setValue:ext_dic forKey:@"item_3"];
  8448. // contact
  8449. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8450. [dic setValue:contact_dic forKey:@"item_4"];
  8451. // phone
  8452. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8453. [dic setValue:phone_dic forKey:@"item_5"];
  8454. // fax
  8455. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8456. [dic setValue:fax_dic forKey:@"item_6"];
  8457. // email
  8458. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8459. [dic setValue:email_dic forKey:@"item_7"];
  8460. }];
  8461. return dic;
  8462. }
  8463. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8464. {
  8465. assert(params[@"user"]!=nil);
  8466. assert(params[@"contact_id"]!=nil);
  8467. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8468. orderCode = [self translateSingleQuote:orderCode];
  8469. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8470. __block double TotalCuft = 0;
  8471. __block double TotalWeight = 0;
  8472. __block int TotalCarton = 0;
  8473. __block double payments = 0;
  8474. // setting
  8475. NSDictionary *setting = params[@"setting"];
  8476. NSNumber *hide = setting[@"ModelInformationHide"];
  8477. [dic setValue:hide forKey:@"hide"];
  8478. 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];
  8479. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8480. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8481. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8482. // item id
  8483. int item_id = sqlite3_column_int(stmt, 0);
  8484. // count
  8485. int item_count = sqlite3_column_int(stmt, 1);
  8486. // stockUom
  8487. int stockUom = sqlite3_column_int(stmt, 2);
  8488. // unit price
  8489. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8490. NSString* Price=nil;
  8491. if([str_price isEqualToString:@""])
  8492. {
  8493. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8494. if(price==nil)
  8495. Price=@"No Price.";
  8496. else
  8497. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8498. }
  8499. else
  8500. {
  8501. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8502. }
  8503. // discount
  8504. double discount = sqlite3_column_double(stmt, 4);
  8505. // name
  8506. NSString *name = [self textAtColumn:5 statement:stmt];
  8507. // description
  8508. NSString *description = [self textAtColumn:6 statement:stmt];
  8509. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8510. // line note
  8511. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8512. int avaulability = sqlite3_column_int(stmt, 8);
  8513. // img
  8514. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8515. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8516. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8517. if(combine != nil)
  8518. {
  8519. // int citem=0;
  8520. int bcount=[[combine valueForKey:@"count"] intValue];
  8521. for(int bc=0;bc<bcount;bc++)
  8522. {
  8523. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8524. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8525. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8526. subTotal += uprice * modulus * item_count;
  8527. }
  8528. }
  8529. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8530. [model_dic setValue:@"model" forKey:@"control"];
  8531. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8532. [model_dic setValue:description forKey:@"description"];
  8533. [model_dic setValue:line_note forKey:@"note"];
  8534. [model_dic setValue:img forKey:@"img_url"];
  8535. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8536. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8537. [model_dic setValue:Price forKey:@"unit_price"];
  8538. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8539. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8540. if (combine) {
  8541. [model_dic setValue:combine forKey:@"combine"];
  8542. }
  8543. // well,what under the row is the info for total
  8544. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8545. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8546. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8547. int carton=[bsubtotaljson[@"carton"] intValue];
  8548. TotalCuft += cuft;
  8549. TotalWeight += weight;
  8550. TotalCarton += carton;
  8551. payments += subTotal;
  8552. //---------------------------
  8553. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8554. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8555. }];
  8556. [dic setValue:@"Model Information" forKey:@"title"];
  8557. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8558. return dic;
  8559. }
  8560. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8561. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8562. [dic setValue:@"Remarks Content" forKey:@"title"];
  8563. // setting
  8564. NSDictionary *setting = params[@"setting"];
  8565. NSNumber *hide = setting[@"RemarksContentHide"];
  8566. [dic setValue:hide forKey:@"hide"];
  8567. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8568. orderCode = [self translateSingleQuote:orderCode];
  8569. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8570. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8571. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8572. int mustCall = sqlite3_column_int(stmt, 1);
  8573. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8574. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8575. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8576. NSDictionary *po_dic = @{
  8577. @"aname" : @"PO#",
  8578. @"control" : @"edit",
  8579. @"keyboard" : @"text",
  8580. @"name" : @"poNumber",
  8581. @"value" : poNumber
  8582. };
  8583. #ifdef BUILD_CONTRAST
  8584. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8585. NSDictionary *schedule_dict = @{
  8586. @"aname" : @"Schdule Date",
  8587. @"control" : @"datepicker",
  8588. @"type": @"date",
  8589. @"required": @"true",
  8590. @"name" : @"schedule_date_str",
  8591. @"value" : nsdate
  8592. };
  8593. #endif
  8594. NSDictionary *must_call_dic = @{
  8595. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8596. @"control" : @"switch",
  8597. @"name" : @"must_call",
  8598. @"value" : mustCall ? @"true" : @"false"
  8599. };
  8600. NSDictionary *general_notes_dic = @{
  8601. @"aname" : @"General notes",
  8602. @"control" : @"text_view",
  8603. @"keyboard" : @"text",
  8604. @"name" : @"comments",
  8605. @"value" : generalNotes
  8606. };
  8607. // NSDictionary *internal_notes_dic = @{
  8608. // @"aname" : @"Internal notes",
  8609. // @"control" : @"text_view",
  8610. // @"keyboard" : @"text",
  8611. // @"name" : @"internal_notes",
  8612. // @"value" : internalNotes
  8613. // };
  8614. #ifdef BUILD_CONTRAST
  8615. [dic setValue:po_dic forKey:@"item_0"];
  8616. [dic setValue:schedule_dict forKey:@"item_1"];
  8617. [dic setValue:must_call_dic forKey:@"item_2"];
  8618. [dic setValue:general_notes_dic forKey:@"item_3"];
  8619. #else
  8620. [dic setValue:po_dic forKey:@"item_0"];
  8621. [dic setValue:must_call_dic forKey:@"item_1"];
  8622. [dic setValue:general_notes_dic forKey:@"item_2"];
  8623. #endif
  8624. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8625. }];
  8626. #ifdef BUILD_CONTRAST
  8627. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8628. #else
  8629. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8630. #endif
  8631. return dic;
  8632. }
  8633. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8634. // params
  8635. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8636. orderCode = [self translateSingleQuote:orderCode];
  8637. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8638. // setting
  8639. NSDictionary *setting = params[@"setting"];
  8640. NSNumber *hide = setting[@"OrderTotalHide"];
  8641. [dic setValue:hide forKey:@"hide"];
  8642. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8643. __block double lift_gate_value = 0;
  8644. __block double handling_fee = 0;
  8645. __block double shipping = 0;
  8646. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8647. int lift_gate = sqlite3_column_int(stmt, 0);
  8648. lift_gate_value = sqlite3_column_double(stmt, 1);
  8649. shipping = sqlite3_column_double(stmt, 2);
  8650. handling_fee = sqlite3_column_double(stmt, 3);
  8651. if (!lift_gate) {
  8652. lift_gate_value = 0;
  8653. }
  8654. }];
  8655. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8656. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8657. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8658. double payments = [[total valueForKey:@"payments"] doubleValue];
  8659. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8660. double totalPrice = payments;
  8661. [dic setValue:@"Order Total" forKey:@"title"];
  8662. NSDictionary *payments_dic = @{
  8663. @"align" : @"right",
  8664. @"aname" : @"Payments/Credits",
  8665. @"control" : @"text",
  8666. @"name" : @"paymentsAndCredits",
  8667. @"type" : @"price",
  8668. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8669. };
  8670. [dic setValue:payments_dic forKey:@"item_0"];
  8671. // version 1.71 remove
  8672. // NSDictionary *handling_fee_dic = @{
  8673. // @"align" : @"right",
  8674. // @"aname" : @"Handling Fee",
  8675. // @"control" : @"text",
  8676. // @"name" : @"handling_fee_value",
  8677. // @"required" : @"true",
  8678. // @"type" : @"price",
  8679. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8680. // };
  8681. NSDictionary *shipping_dic = @{
  8682. @"align" : @"right",
  8683. @"aname" : @"Shipping*",
  8684. @"control" : @"text",
  8685. @"name" : @"shipping",
  8686. @"required" : @"true",
  8687. @"type" : @"price",
  8688. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8689. };
  8690. NSDictionary *lift_gate_dic = @{
  8691. @"align" : @"right",
  8692. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8693. @"control" : @"text",
  8694. @"name" : @"lift_gate_value",
  8695. @"required" : @"true",
  8696. @"type" : @"price",
  8697. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8698. };
  8699. int item_count = 1;
  8700. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8701. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8702. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8703. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8704. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8705. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8706. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8707. } else {
  8708. }
  8709. }
  8710. // version 1.71 remove
  8711. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8712. NSDictionary *total_price_dic = @{
  8713. @"align" : @"right",
  8714. @"aname" : @"Total",
  8715. @"control" : @"text",
  8716. @"name" : @"totalPrice",
  8717. @"type" : @"price",
  8718. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8719. };
  8720. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8721. NSDictionary *total_cuft_dic = @{
  8722. @"align" : @"right",
  8723. @"aname" : @"Total Cuft",
  8724. @"control" : @"text",
  8725. @"name" : @"",
  8726. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8727. };
  8728. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8729. NSDictionary *total_weight_dic = @{
  8730. @"align" : @"right",
  8731. @"aname" : @"Total Weight",
  8732. @"control" : @"text",
  8733. @"name" : @"",
  8734. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8735. };
  8736. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8737. NSDictionary *total_carton_dic = @{
  8738. @"align" : @"right",
  8739. @"aname" : @"Total Carton",
  8740. @"control" : @"text",
  8741. @"name" : @"",
  8742. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8743. };
  8744. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8745. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8746. return dic;
  8747. }
  8748. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8749. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8750. orderCode = [self translateSingleQuote:orderCode];
  8751. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8752. // setting
  8753. NSDictionary *setting = params[@"setting"];
  8754. NSNumber *hide = setting[@"SignatureHide"];
  8755. [dic setValue:hide forKey:@"hide"];
  8756. [dic setValue:@"Signature" forKey:@"title"];
  8757. __block NSString *pic_path = @"";
  8758. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8759. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8760. pic_path = [self textAtColumn:0 statement:stmt];
  8761. }];
  8762. NSDictionary *pic_dic = @{
  8763. @"aname" : @"Signature",
  8764. @"avalue" :pic_path,
  8765. @"control" : @"signature",
  8766. @"name" : @"sign_picpath",
  8767. @"value" : pic_path
  8768. };
  8769. [dic setValue:pic_dic forKey:@"item_0"];
  8770. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8771. return dic;
  8772. }
  8773. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8774. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8775. // setting
  8776. NSDictionary *setting = params[@"setting"];
  8777. NSNumber *hide = setting[@"ShippingMethodHide"];
  8778. [dic setValue:hide forKey:@"hide"];
  8779. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8780. orderCode = [self translateSingleQuote:orderCode];
  8781. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8782. __block NSString *logist = @"";
  8783. __block NSString *lift_gate = @"";
  8784. __block int lift_gate_integer = 0;
  8785. __block NSString *logistic_note = @"";
  8786. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8787. logist = [self textAtColumn:0 statement:stmt];
  8788. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8789. logistic_note = [self textAtColumn:2 statement:stmt];
  8790. }];
  8791. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8792. [dic setValue:@"Shipping Method" forKey:@"title"];
  8793. // val_0
  8794. int PERSONAL_PICK_UP_check = 0;
  8795. int USE_MY_CARRIER_check = 0;
  8796. NSString *logistic_note_text = @"";
  8797. int will_call_check = 0;
  8798. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8799. will_call_check = 1;
  8800. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8801. PERSONAL_PICK_UP_check = 1;
  8802. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8803. USE_MY_CARRIER_check = 1;
  8804. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8805. logistic_note = [logistic_note_array firstObject];
  8806. if (logistic_note_array.count > 1) {
  8807. logistic_note_text = [logistic_note_array lastObject];
  8808. }
  8809. }
  8810. }
  8811. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8812. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8813. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8814. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8815. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8816. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8817. NSDictionary *val0_subItem_item0 = @{
  8818. @"aname" : @"Option",
  8819. @"cadedate" : @{
  8820. @"count" : [NSNumber numberWithInteger:2],
  8821. @"val_0" : @{
  8822. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8823. @"refresh" : [NSNumber numberWithInteger:0],
  8824. @"value" : @"PERSONAL PICK UP",
  8825. @"value_id" : @"PERSONAL PICK UP"
  8826. },
  8827. @"val_1" : @{
  8828. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8829. @"refresh" : [NSNumber numberWithInteger:0],
  8830. @"sub_item" : @{
  8831. @"count" : [NSNumber numberWithInteger:1],
  8832. @"item_0" : @{
  8833. @"aname" : @"BOL",
  8834. @"control" : @"edit",
  8835. @"keyboard" : @"text",
  8836. @"name" : @"logist_note_text",
  8837. @"refresh" : [NSNumber numberWithInteger:0],
  8838. @"required" : @"false",
  8839. @"value" : logistic_note_text
  8840. }
  8841. },
  8842. @"value" : @"USE MY CARRIER",
  8843. @"value_id" : @"USE MY CARRIER"
  8844. }
  8845. },
  8846. @"control" : @"enum",
  8847. @"name" : @"logistic_note",
  8848. @"required" : @"true",
  8849. @"single_select" : @"true"
  8850. };
  8851. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8852. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8853. // val_1
  8854. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8855. if([lift_gate isEqualToString:@""]) {
  8856. if (lift_gate_integer == 1) {
  8857. lift_gate = @"true";
  8858. } else {
  8859. lift_gate = @"false";
  8860. }
  8861. }
  8862. int common_carrier_check = 0;
  8863. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8864. common_carrier_check = 1;
  8865. [params setValue:lift_gate forKey:@"lift_gate"];
  8866. }
  8867. NSDictionary *val_1 = @{
  8868. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8869. @"sub_item" : @{
  8870. @"count" : [NSNumber numberWithInteger:1],
  8871. @"item_0" : @{
  8872. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8873. @"control" : @"switch",
  8874. @"name" : @"lift_gate",
  8875. @"refresh" : [NSNumber numberWithInteger:1],
  8876. @"required" : @"true",
  8877. @"value" : lift_gate
  8878. }
  8879. },
  8880. @"value" : @"COMMON CARRIER",
  8881. @"value_id" : @"COMMON CARRIER"
  8882. };
  8883. // cadedate
  8884. NSDictionary *cadedate = @{
  8885. @"count" : [NSNumber numberWithInteger:2],
  8886. @"val_0" : val_0,
  8887. @"val_1" : val_1
  8888. };
  8889. // item_0
  8890. NSMutableDictionary *item_0 = @{
  8891. @"aname" : @"Shipping",
  8892. @"cadedate" : cadedate,
  8893. @"control" : @"enum",
  8894. @"name" : @"logist",
  8895. @"refresh" : [NSNumber numberWithInteger:1],
  8896. @"single_select" : @"true",
  8897. }.mutableCopy;
  8898. NSNumber *required = setting[@"ShippingMethodRequire"];
  8899. if ([required integerValue]) {
  8900. [item_0 setValue:@"true" forKey:@"required"];
  8901. }
  8902. [dic setValue:item_0 forKey:@"item_0"];
  8903. if ([logist isEqualToString:@"WILL CALL"]) {
  8904. [dic removeObjectForKey:@"lift_gate"];
  8905. }
  8906. return dic;
  8907. }
  8908. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8909. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8910. orderCode = [self translateSingleQuote:orderCode];
  8911. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8912. __block int submit_as_integer = 0;
  8913. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8914. submit_as_integer = sqlite3_column_int(stmt, 0);
  8915. }];
  8916. int check11 = 0;
  8917. int check10 = 0;
  8918. if (submit_as_integer == 11) {
  8919. check11 = 1;
  8920. }
  8921. if (submit_as_integer == 10) {
  8922. check10 = 1;
  8923. }
  8924. // section_0
  8925. NSMutableDictionary *dic = @{
  8926. @"count" : @(1),
  8927. @"item_0" : @{
  8928. @"aname" : @"Submit Order As",
  8929. @"cadedate" : @{
  8930. @"count" : @(2),
  8931. @"val_0" : @{
  8932. @"check" : [NSNumber numberWithInteger:check11],
  8933. @"value" : @"Sales Order",
  8934. @"value_id" : @(11)
  8935. },
  8936. @"val_1" : @{
  8937. @"check" : [NSNumber numberWithInteger:check10],
  8938. @"value" : @"Quote",
  8939. @"value_id" : @(10)
  8940. }
  8941. },
  8942. @"control" : @"enum",
  8943. @"name" : @"erpOrderStatus",
  8944. @"required" : @"true",
  8945. @"single_select" : @"true"
  8946. },
  8947. @"title" : @"Order Type"
  8948. }.mutableCopy;
  8949. // setting
  8950. NSDictionary *setting = params[@"setting"];
  8951. NSNumber *hide = setting[@"OrderTypeHide"];
  8952. [dic setValue:hide forKey:@"hide"];
  8953. return dic;
  8954. }
  8955. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8956. countryCode = [self translateSingleQuote:countryCode];
  8957. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8958. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8959. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8960. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8961. if (name == NULL) {
  8962. name = "";
  8963. }
  8964. if (code == NULL) {
  8965. code = "";
  8966. }
  8967. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8968. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8969. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8970. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8971. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8972. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8973. }
  8974. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8975. [container setValue:stateDic forKey:key];
  8976. }] mutableCopy];
  8977. [ret removeObjectForKey:@"result"];
  8978. // failure 可以不用了,一样的
  8979. if (ret.allKeys.count == 0) {
  8980. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8981. [stateDic setValue:@"Other" forKey:@"value"];
  8982. [stateDic setValue:@"" forKey:@"value_id"];
  8983. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8984. if (state_code && [@"" isEqualToString:state_code]) {
  8985. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8986. }
  8987. NSString *key = [NSString stringWithFormat:@"val_0"];
  8988. [ret setValue:stateDic forKey:key];
  8989. }
  8990. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8991. return ret;
  8992. }
  8993. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8994. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8995. orderCode = [self translateSingleQuote:orderCode];
  8996. 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];
  8997. __block NSString *payType = @"";
  8998. __block NSString *firstName = @"";
  8999. __block NSString *lastName = @"";
  9000. __block NSString *addr1 = @"";
  9001. __block NSString *addr2 = @"";
  9002. __block NSString *zipcode = @"";
  9003. __block NSString *cardType = @"";
  9004. __block NSString *cardNumber = @"";
  9005. __block NSString *securityCode = @"";
  9006. __block NSString *month = @"";
  9007. __block NSString *year = @"";
  9008. __block NSString *city = @"";
  9009. __block NSString *state = @"";
  9010. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9011. payType = [self textAtColumn:0 statement:stmt];
  9012. firstName = [self textAtColumn:1 statement:stmt];
  9013. lastName = [self textAtColumn:2 statement:stmt];
  9014. addr1 = [self textAtColumn:3 statement:stmt];
  9015. addr2 = [self textAtColumn:4 statement:stmt];
  9016. zipcode = [self textAtColumn:5 statement:stmt];
  9017. cardType = [self textAtColumn:6 statement:stmt];
  9018. cardNumber = [self textAtColumn:7 statement:stmt];
  9019. securityCode = [self textAtColumn:8 statement:stmt];
  9020. month = [self textAtColumn:9 statement:stmt];
  9021. year = [self textAtColumn:10 statement:stmt];
  9022. city = [self textAtColumn:11 statement:stmt];
  9023. state = [self textAtColumn:12 statement:stmt];
  9024. }];
  9025. NSString *required = @"true";
  9026. // setting
  9027. NSDictionary *setting = params[@"setting"];
  9028. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9029. if ([requiredNumber integerValue]) {
  9030. required = @"true";
  9031. } else {
  9032. required = @"false";
  9033. }
  9034. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9035. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9036. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9037. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9038. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9039. NSString *type = [self textAtColumn:1 statement:stmt];
  9040. NSMutableDictionary *val = @{
  9041. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9042. @"value" : type,
  9043. @"value_id" : type_id
  9044. }.mutableCopy;
  9045. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9046. NSDictionary *sub_item =nil;
  9047. #ifdef BUILD_CONTRAST
  9048. sub_item = @{
  9049. @"count" : @(3),
  9050. @"item_0" : @{
  9051. @"aname" : @"Number",
  9052. @"control" : @"edit",
  9053. @"keyboard" : @"int",
  9054. @"length" : @"16",
  9055. @"name" : @"credit_card_number",
  9056. @"required" : @"true",
  9057. @"value" : cardNumber
  9058. },
  9059. @"item_1" : @{
  9060. @"aname" : @"Expiration Date",
  9061. @"control" : @"monthpicker",
  9062. @"name" : @"credit_card_expiration",
  9063. @"required" : @"true",
  9064. @"type" : @"date",
  9065. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9066. },
  9067. @"item_2" : @{
  9068. @"aname" : @"Security Code",
  9069. @"control" : @"edit",
  9070. @"keyboard" : @"int",
  9071. @"length" : @"4",
  9072. @"name" : @"credit_card_security_code",
  9073. @"required" : @"true",
  9074. @"value" : securityCode
  9075. },
  9076. };
  9077. #else
  9078. sub_item= @{
  9079. @"count" : @(3),
  9080. @"item_0" : @{
  9081. @"aname" : @"choose",
  9082. @"control" : @"multi_action",
  9083. @"count" : @(1),
  9084. @"item_0" : @{
  9085. @"aname" : @"Same as customer",
  9086. @"key_map" : @{
  9087. @"credit_card_address1" : @"customer_address1",
  9088. @"credit_card_address2" : @"customer_address2",
  9089. @"credit_card_city" : @"customer_city",
  9090. @"credit_card_first_name" : @"customer_first_name",
  9091. @"credit_card_last_name" : @"customer_last_name",
  9092. @"credit_card_state" : @"customer_state",
  9093. @"credit_card_zipcode" : @"customer_zipcode"
  9094. },
  9095. @"type" : @"pull"
  9096. }
  9097. },
  9098. @"item_1" : @{
  9099. @"aname" : @"",
  9100. @"color" : @"red",
  9101. @"control" : @"text",
  9102. @"name" : @"",
  9103. @"value" : @"USA Credit cards only"
  9104. },
  9105. @"item_2" : @{
  9106. @"aname" : @"Fill",
  9107. @"cadedate" : @{
  9108. @"count" : @(2),
  9109. @"val_0" : @{
  9110. @"check" : @(1),
  9111. @"sub_item" : @{
  9112. @"count" : @(11),
  9113. @"item_0" : @{
  9114. @"aname" : @"Type",
  9115. @"cadedate" : @{
  9116. @"count" : @(2),
  9117. @"val_0" : @{
  9118. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9119. @"value" : @"VISA",
  9120. @"value_id" : @"VISA"/*@(0)*/
  9121. },
  9122. @"val_1" : @{
  9123. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9124. @"value" : @"MASTER CARD",
  9125. @"value_id" : @"MASTER CARD"/*@(1)*/
  9126. }
  9127. },
  9128. @"control" : @"enum",
  9129. @"name" : @"credit_card_type",
  9130. @"required" : @"true",
  9131. @"single_select" : @"true"
  9132. },
  9133. @"item_1" : @{
  9134. @"aname" : @"Number",
  9135. @"control" : @"edit",
  9136. @"keyboard" : @"int",
  9137. @"length" : @"16",
  9138. @"name" : @"credit_card_number",
  9139. @"required" : @"true",
  9140. @"value" : cardNumber
  9141. },
  9142. @"item_10" : @{
  9143. @"aname" : @"State",
  9144. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9145. @"control" : @"enum",
  9146. @"enum" : @"true",
  9147. @"name" : @"credit_card_state",
  9148. @"required" : @"true",
  9149. @"single_select" : @"true"
  9150. },
  9151. @"item_2" : @{
  9152. @"aname" : @"Expiration Date",
  9153. @"control" : @"monthpicker",
  9154. @"name" : @"credit_card_expiration",
  9155. @"required" : @"true",
  9156. @"type" : @"date",
  9157. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9158. },
  9159. @"item_3" : @{
  9160. @"aname" : @"Security Code",
  9161. @"control" : @"edit",
  9162. @"keyboard" : @"int",
  9163. @"length" : @"3",
  9164. @"name" : @"credit_card_security_code",
  9165. @"required" : @"true",
  9166. @"value" : securityCode
  9167. },
  9168. @"item_4" : @{
  9169. @"aname" : @"First Name",
  9170. @"control" : @"edit",
  9171. @"keyboard" : @"text",
  9172. @"name" : @"credit_card_first_name",
  9173. @"required" : @"true",
  9174. @"value" : firstName
  9175. },
  9176. @"item_5" : @{
  9177. @"aname" : @"Last Name",
  9178. @"control" : @"edit",
  9179. @"keyboard" : @"text",
  9180. @"name" : @"credit_card_last_name",
  9181. @"required" : @"true",
  9182. @"value" : lastName
  9183. },
  9184. @"item_6" : @{
  9185. @"aname" : @"Address 1",
  9186. @"control" : @"edit",
  9187. @"keyboard" : @"text",
  9188. @"name" : @"credit_card_address1",
  9189. @"required" : @"true",
  9190. @"value" : addr1
  9191. },
  9192. @"item_7" : @{
  9193. @"aname" : @"Address 2",
  9194. @"control" : @"edit",
  9195. @"keyboard" : @"text",
  9196. @"name" : @"credit_card_address2",
  9197. @"value" : addr2
  9198. },
  9199. @"item_8" : @{
  9200. @"aname" : @"zip code",
  9201. @"control" : @"edit",
  9202. @"keyboard" : @"text",
  9203. @"name" : @"credit_card_zipcode",
  9204. @"required" : @"true",
  9205. @"value" : zipcode
  9206. },
  9207. @"item_9" : @{
  9208. @"aname" : @"City",
  9209. @"control" : @"edit",
  9210. @"keyboard" : @"text",
  9211. @"name" : @"credit_card_city",
  9212. @"required" : @"true",
  9213. @"value" : city
  9214. }
  9215. },
  9216. @"value" : @"Fill Now",
  9217. @"value_id" : @""
  9218. },
  9219. @"val_1" : @{
  9220. @"check" : @(0),
  9221. @"value" : @"Fill Later",
  9222. @"value_id" : @""
  9223. }
  9224. },
  9225. @"control" : @"enum",
  9226. @"name" : @"",
  9227. @"single_select" : @"true"
  9228. }
  9229. };
  9230. #endif;
  9231. [val setObject:sub_item forKey:@"sub_item"];
  9232. }
  9233. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9234. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9235. }];
  9236. // "section_2"
  9237. NSMutableDictionary *dic = @{
  9238. @"count" : @(1),
  9239. @"item_0" : @{
  9240. @"aname" : @"Payment",
  9241. @"required" : required,
  9242. @"cadedate" : cadedate,
  9243. // @{
  9244. // @"count" : @(6),
  9245. // @"val_3" : @{
  9246. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9247. // @"value" : @"Check",
  9248. // @"value_id" : @"Check"
  9249. // },
  9250. // @"val_2" : @{
  9251. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9252. // @"value" : @"Cash",
  9253. // @"value_id" : @"Cash"
  9254. // },
  9255. // @"val_1" : @{
  9256. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9257. // @"value" : @"NET 60",
  9258. // @"value_id" : @"NET 30"
  9259. // },
  9260. // @"val_4" : @{
  9261. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9262. // @"value" : @"Wire Transfer",
  9263. // @"value_id" : @"Wire Transfer"
  9264. // },
  9265. // @"val_0" : @{
  9266. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9267. // @"sub_item" : @{
  9268. // @"count" : @(3),
  9269. // @"item_0" : @{
  9270. // @"aname" : @"choose",
  9271. // @"control" : @"multi_action",
  9272. // @"count" : @(1),
  9273. // @"item_0" : @{
  9274. // @"aname" : @"Same as customer",
  9275. // @"key_map" : @{
  9276. // @"credit_card_address1" : @"customer_address1",
  9277. // @"credit_card_address2" : @"customer_address2",
  9278. // @"credit_card_city" : @"customer_city",
  9279. // @"credit_card_first_name" : @"customer_first_name",
  9280. // @"credit_card_last_name" : @"customer_last_name",
  9281. // @"credit_card_state" : @"customer_state",
  9282. // @"credit_card_zipcode" : @"customer_zipcode"
  9283. // },
  9284. // @"type" : @"pull"
  9285. // }
  9286. // },
  9287. // @"item_1" : @{
  9288. // @"aname" : @"",
  9289. // @"color" : @"red",
  9290. // @"control" : @"text",
  9291. // @"name" : @"",
  9292. // @"value" : @"USA Credit cards only"
  9293. // },
  9294. // @"item_2" : @{
  9295. // @"aname" : @"Fill",
  9296. // @"cadedate" : @{
  9297. // @"count" : @(2),
  9298. // @"val_0" : @{
  9299. // @"check" : @(1),
  9300. // @"sub_item" : @{
  9301. // @"count" : @(11),
  9302. // @"item_0" : @{
  9303. // @"aname" : @"Type",
  9304. // @"cadedate" : @{
  9305. // @"count" : @(2),
  9306. // @"val_0" : @{
  9307. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9308. // @"value" : @"VISA",
  9309. // @"value_id" : @(0)
  9310. // },
  9311. // @"val_1" : @{
  9312. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9313. // @"value" : @"MASTER CARD",
  9314. // @"value_id" : @(1)
  9315. // }
  9316. // },
  9317. // @"control" : @"enum",
  9318. // @"name" : @"credit_card_type",
  9319. // @"required" : @"true",
  9320. // @"single_select" : @"true"
  9321. // },
  9322. // @"item_1" : @{
  9323. // @"aname" : @"Number",
  9324. // @"control" : @"edit",
  9325. // @"keyboard" : @"int",
  9326. // @"length" : @"16",
  9327. // @"name" : @"credit_card_number",
  9328. // @"required" : @"true",
  9329. // @"value" : cardNumber
  9330. // },
  9331. // @"item_10" : @{
  9332. // @"aname" : @"State",
  9333. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9334. // @"control" : @"enum",
  9335. // @"enum" : @"true",
  9336. // @"name" : @"credit_card_state",
  9337. // @"required" : @"true",
  9338. // @"single_select" : @"true"
  9339. // },
  9340. // @"item_2" : @{
  9341. // @"aname" : @"Expiration Date",
  9342. // @"control" : @"monthpicker",
  9343. // @"name" : @"credit_card_expiration",
  9344. // @"required" : @"true",
  9345. // @"type" : @"date",
  9346. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9347. // },
  9348. // @"item_3" : @{
  9349. // @"aname" : @"Security Code",
  9350. // @"control" : @"edit",
  9351. // @"keyboard" : @"int",
  9352. // @"length" : @"3",
  9353. // @"name" : @"credit_card_security_code",
  9354. // @"required" : @"true",
  9355. // @"value" : securityCode
  9356. // },
  9357. // @"item_4" : @{
  9358. // @"aname" : @"First Name",
  9359. // @"control" : @"edit",
  9360. // @"keyboard" : @"text",
  9361. // @"name" : @"credit_card_first_name",
  9362. // @"required" : @"true",
  9363. // @"value" : firstName
  9364. // },
  9365. // @"item_5" : @{
  9366. // @"aname" : @"Last Name",
  9367. // @"control" : @"edit",
  9368. // @"keyboard" : @"text",
  9369. // @"name" : @"credit_card_last_name",
  9370. // @"required" : @"true",
  9371. // @"value" : lastName
  9372. // },
  9373. // @"item_6" : @{
  9374. // @"aname" : @"Address 1",
  9375. // @"control" : @"edit",
  9376. // @"keyboard" : @"text",
  9377. // @"name" : @"credit_card_address1",
  9378. // @"required" : @"true",
  9379. // @"value" : addr1
  9380. // },
  9381. // @"item_7" : @{
  9382. // @"aname" : @"Address 2",
  9383. // @"control" : @"edit",
  9384. // @"keyboard" : @"text",
  9385. // @"name" : @"credit_card_address2",
  9386. // @"value" : addr2
  9387. // },
  9388. // @"item_8" : @{
  9389. // @"aname" : @"zip code",
  9390. // @"control" : @"edit",
  9391. // @"keyboard" : @"text",
  9392. // @"name" : @"credit_card_zipcode",
  9393. // @"required" : @"true",
  9394. // @"value" : zipcode
  9395. // },
  9396. // @"item_9" : @{
  9397. // @"aname" : @"City",
  9398. // @"control" : @"edit",
  9399. // @"keyboard" : @"text",
  9400. // @"name" : @"credit_card_city",
  9401. // @"required" : @"true",
  9402. // @"value" : city
  9403. // }
  9404. // },
  9405. // @"value" : @"Fill Now",
  9406. // @"value_id" : @""
  9407. // },
  9408. // @"val_1" : @{
  9409. // @"check" : @(0),
  9410. // @"value" : @"Fill Later",
  9411. // @"value_id" : @""
  9412. // }
  9413. // },
  9414. // @"control" : @"enum",
  9415. // @"name" : @"",
  9416. // @"single_select" : @"true"
  9417. // }
  9418. // },
  9419. // @"value" : @"Visa/Master",
  9420. // @"value_id" : @"Credit Card"
  9421. // },
  9422. // @"val_5" : @{
  9423. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9424. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9425. // @"value_id" : @"FOLLOW EXISTING"
  9426. // }
  9427. // },
  9428. @"control" : @"enum",
  9429. @"name" : @"paymentType",
  9430. @"single_select" : @"true"
  9431. },
  9432. @"title" : @"Payment Information"
  9433. }.mutableCopy;
  9434. NSNumber *hide = setting[@"PaymentInformationHide"];
  9435. [dic setValue:hide forKey:@"hide"];
  9436. return dic;
  9437. }
  9438. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9439. // params
  9440. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9441. orderCode = [self translateSingleQuote:orderCode];
  9442. // 缺货检查
  9443. 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];
  9444. __block BOOL outOfStock = NO;
  9445. sqlite3 *database = db;
  9446. if (!db) {
  9447. database = [iSalesDB get_db];
  9448. }
  9449. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9450. int availability = sqlite3_column_int(stmt, 0);
  9451. int item_qty = sqlite3_column_int(stmt, 1);
  9452. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9453. }];
  9454. if (!db) {
  9455. [iSalesDB close_db:database];
  9456. }
  9457. return outOfStock;
  9458. }
  9459. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9460. {
  9461. assert(params[@"user"]!=nil);
  9462. assert(params[@"contact_id"]!=nil);
  9463. assert(params[@"can_create_backorder"]!=nil);
  9464. sqlite3 *db = [iSalesDB get_db];
  9465. // params
  9466. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9467. orderCode = [self translateSingleQuote:orderCode];
  9468. // 缺货检查
  9469. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9470. if (![params[@"can_create_backorder"] boolValue]) {
  9471. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9472. if (out_of_stock) {
  9473. [iSalesDB close_db:db];
  9474. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9475. [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"];
  9476. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9477. return [RAConvertor dict2data:resultDic];
  9478. }
  9479. }
  9480. // UISetting
  9481. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9482. NSString *cachefolder = [paths objectAtIndex:0];
  9483. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9484. NSData* json =nil;
  9485. json=[NSData dataWithContentsOfFile:img_cache];
  9486. NSError *error=nil;
  9487. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9488. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9489. [params setObject:setting forKey:@"setting"];
  9490. int section_count = 0;
  9491. // 0 Order Type 1 Shipping Method 2 Payment Information
  9492. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9493. // 0 Order Type
  9494. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9495. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9496. [ret setValue:order_type_dic forKey:key0];
  9497. // 1 Shipping Method
  9498. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9499. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9500. [ret setValue:shipping_method_dic forKey:key1];
  9501. // 2 Payment Information
  9502. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9503. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9504. [ret setValue:payment_info_dic forKey:key2];
  9505. // 3 Customer
  9506. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9507. NSDictionary *customer_dic = [self customerDic:params db:db];
  9508. [ret setValue:customer_dic forKey:key3];
  9509. // 4 Ship To
  9510. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9511. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9512. [ret setValue:ship_to_dic forKey:key4];
  9513. // 5 Ship From
  9514. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9515. if (![shipFromDisable integerValue]) {
  9516. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9517. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9518. [ret setValue:ship_from_dic forKey:key5];
  9519. }
  9520. // 6 Freight Bill To
  9521. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9522. if (![freightBillToDisable integerValue]) {
  9523. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9524. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9525. [ret setValue:freight_bill_to forKey:key6];
  9526. }
  9527. // 7 Merchandise Bill To
  9528. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9529. if (![merchandiseBillToDisable integerValue]) {
  9530. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9531. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9532. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9533. }
  9534. // 8 Return To
  9535. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9536. if (![returnToDisable integerValue]) {
  9537. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9538. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9539. [ret setValue:return_to_dic forKey:key8];
  9540. }
  9541. // 9 Model Information
  9542. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9543. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9544. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9545. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9546. [ret setValue:model_info_dic forKey:key9];
  9547. // 10 Remarks Content
  9548. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9549. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9550. [ret setValue:remarks_content_dic forKey:key10];
  9551. // 11 Order Total
  9552. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9553. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9554. [ret setValue:order_total_dic forKey:key11];
  9555. // 12 Signature
  9556. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9557. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9558. [ret setValue:sign_dic forKey:key12];
  9559. [ret setValue:@(section_count) forKey:@"section_count"];
  9560. [iSalesDB close_db:db];
  9561. return [RAConvertor dict2data:ret];
  9562. // return nil;
  9563. }
  9564. #pragma mark addr editor
  9565. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9566. NSMutableDictionary *new_item = [item mutableCopy];
  9567. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9568. return new_item;
  9569. }
  9570. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9571. {
  9572. // "is_subaction" = true;
  9573. // orderCode = MOB1608240002;
  9574. // password = 123456;
  9575. // "subaction_tag" = 1;
  9576. // user = EvanK;
  9577. // {
  9578. // "is_subaction" = true;
  9579. // orderCode = MOB1608240002;
  9580. // password = 123456;
  9581. // "refresh_trigger" = zipcode;
  9582. // "subaction_tag" = 1;
  9583. // user = EvanK;
  9584. // }
  9585. NSString *country_code = nil;
  9586. NSString *zipCode = nil;
  9587. NSString *stateCode = nil;
  9588. NSString *city = nil;
  9589. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9590. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9591. NSString *code_id = params[@"country"];
  9592. country_code = [self countryCodeByid:code_id];
  9593. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9594. NSString *zip_code = params[@"zipcode"];
  9595. // 剔除全部为空格
  9596. int spaceCount = 0;
  9597. for (int i = 0; i < zip_code.length; i++) {
  9598. if ([zip_code characterAtIndex:i] == ' ') {
  9599. spaceCount++;
  9600. }
  9601. }
  9602. if (spaceCount == zip_code.length) {
  9603. zip_code = @"";
  9604. }
  9605. zipCode = zip_code;
  9606. if (zipCode.length > 0) {
  9607. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9608. country_code = [dic valueForKey:@"country_code"];
  9609. if (!country_code) {
  9610. // country_code = @"US";
  9611. NSString *code_id = params[@"country"];
  9612. country_code = [self countryCodeByid:code_id];
  9613. }
  9614. stateCode = [dic valueForKey:@"state_code"];
  9615. if(!stateCode.length) {
  9616. stateCode = params[@"state"];
  9617. }
  9618. city = [dic valueForKey:@"city"];
  9619. if (!city.length) {
  9620. city = params[@"city"];
  9621. }
  9622. // zip code
  9623. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9624. // [zipDic setValue:zipCode forKey:@"value"];
  9625. // [section_0 setValue:zipDic forKey:@"item_11"];
  9626. } else {
  9627. NSString *code_id = params[@"country"];
  9628. country_code = [self countryCodeByid:code_id];
  9629. stateCode = params[@"state"];
  9630. city = params[@"city"];
  9631. }
  9632. }
  9633. }
  9634. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9635. // [ret removeObjectForKey:@"up_params"];
  9636. [ret setObject:@"New Address" forKey:@"title"];
  9637. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9638. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9639. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9640. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9641. // [country_dic removeObjectForKey:@"refresh"];
  9642. // [country_dic removeObjectForKey:@"restore"];
  9643. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9644. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9645. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9646. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9647. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9648. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9649. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9650. // [zip_code_dic removeObjectForKey:@"refresh"];
  9651. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9652. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9653. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9654. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9655. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9656. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9657. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9658. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9659. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9660. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9661. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9662. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9663. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9664. // country
  9665. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9666. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9667. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9668. // state
  9669. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9670. // NSDictionary *tmpDic = @{
  9671. // @"value" : @"Other",
  9672. // @"value_id" : @"",
  9673. // @"check" : [NSNumber numberWithInteger:0]
  9674. // };
  9675. //
  9676. // for (int i = 0; i < allState.allKeys.count; i++) {
  9677. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9678. //
  9679. // NSDictionary *tmp = allState[key];
  9680. // [allState setValue:tmpDic forKey:key];
  9681. // tmpDic = tmp;
  9682. // }
  9683. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9684. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9685. [ret setValue:new_section_0 forKey:@"section_0"];
  9686. return [RAConvertor dict2data:ret];
  9687. }
  9688. #pragma mark save addr
  9689. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9690. {
  9691. // NSString *companyName = [self valueInParams:params key:@"company"];
  9692. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9693. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9694. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9695. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9696. // NSString *countryId = [self valueInParams:params key:@"country"];
  9697. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9698. // NSString *city = [self valueInParams:params key:@"city"];
  9699. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9700. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9701. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9702. // NSString *phone = [self valueInParams:params key:@"phone"];
  9703. // NSString *fax = [self valueInParams:params key:@"fax"];
  9704. // NSString *email = [self valueInParams:params key:@"email"];
  9705. return [self offline_saveContact:params update:NO isCustomer:NO];
  9706. }
  9707. #pragma mark cancel order
  9708. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9709. {
  9710. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9711. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9712. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9713. if (order_id.length) {
  9714. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9715. }
  9716. int ret = [iSalesDB execSql:sql];
  9717. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9718. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9719. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9720. // [dic setValue:@"160409" forKey:@"min_ver"];
  9721. return [RAConvertor dict2data:dic];
  9722. }
  9723. #pragma mark sign order
  9724. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9725. {
  9726. //参考 offline_saveBusinesscard
  9727. DebugLog(@"sign order params: %@",params);
  9728. // orderCode = MOB1608240002;
  9729. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9730. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9731. orderCode = [self translateSingleQuote:orderCode];
  9732. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9733. picPath = [self translateSingleQuote:picPath];
  9734. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9735. int ret = [iSalesDB execSql:sql];
  9736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9737. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9738. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9739. // [dic setValue:@"160409" forKey:@"min_ver"];
  9740. return [RAConvertor dict2data:dic];
  9741. }
  9742. #pragma mark save order
  9743. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9744. NSString *ret = [self valueInParams:params key:key];
  9745. if (translate) {
  9746. ret = [self translateSingleQuote:ret];
  9747. }
  9748. return ret;
  9749. }
  9750. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9751. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9752. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9753. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9754. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9755. if (schedule_date.length==0 ) {
  9756. schedule_date = @"";
  9757. } else {
  9758. schedule_date = [self rchangeDateFormate:schedule_date];
  9759. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9760. }
  9761. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9762. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9763. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9764. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9765. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9766. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9767. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9768. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9769. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9770. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9771. if (![total_price isEqualToString:@""]) {
  9772. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9773. } else {
  9774. total_price = @"";
  9775. }
  9776. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9777. if ([paymentsAndCredits isEqualToString:@""]) {
  9778. paymentsAndCredits = @"";
  9779. } else {
  9780. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9781. }
  9782. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9783. if ([handling_fee_value isEqualToString:@""]) {
  9784. handling_fee_value = @"";
  9785. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9786. handling_fee_value = @"";
  9787. } else {
  9788. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9789. }
  9790. NSString *handling_fee_placeholder = handling_fee_value;
  9791. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9792. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9793. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9794. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9795. NSString *lift_gate_placeholder = @"";
  9796. if ([lift_gate_value isEqualToString:@""]) {
  9797. lift_gate_placeholder = @"";
  9798. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9799. lift_gate_placeholder = @"";
  9800. } else {
  9801. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9802. }
  9803. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9804. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9805. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9806. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9807. 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];
  9808. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9809. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9810. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9811. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9812. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9813. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9814. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9815. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9816. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9817. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9818. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9819. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9820. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9821. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9822. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9823. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9824. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9825. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9826. if (!number_nil) {
  9827. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9828. }
  9829. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9830. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9831. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9832. if (!security_code_nil) {
  9833. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9834. }
  9835. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9836. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9837. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9838. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9839. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9840. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9841. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9842. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9843. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9844. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9845. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9846. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9847. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9848. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9849. NSString *contact_id = customer_cid;
  9850. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9851. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9852. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9853. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9854. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9855. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9856. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9857. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9858. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9859. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9860. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9861. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9862. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9863. //
  9864. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9865. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9866. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9867. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9868. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9869. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9870. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9871. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9872. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9873. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9874. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9875. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9876. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9877. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9878. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9879. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9880. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9881. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9882. 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];
  9883. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9884. if (receive_cid.length) {
  9885. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9886. }
  9887. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9888. if (receive_name.length) {
  9889. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9890. }
  9891. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9892. if (receive_ext.length) {
  9893. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9894. }
  9895. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9896. if (receive_contact.length) {
  9897. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9898. }
  9899. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9900. if (receive_phone.length) {
  9901. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9902. }
  9903. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9904. if (receive_email.length) {
  9905. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9906. }
  9907. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9908. if (receive_fax.length) {
  9909. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9910. }
  9911. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9912. if (sender_cid.length) {
  9913. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9914. }
  9915. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9916. if (sender_name.length) {
  9917. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9918. }
  9919. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9920. if (sender_ext.length) {
  9921. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9922. }
  9923. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9924. if(sender_contact.length) {
  9925. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9926. }
  9927. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9928. if (sender_phone.length) {
  9929. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9930. }
  9931. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9932. if (sender_fax.length) {
  9933. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9934. }
  9935. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9936. if (sender_email.length) {
  9937. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9938. }
  9939. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9940. if (shipping_billto_cid.length) {
  9941. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9942. }
  9943. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9944. if (shipping_billto_name.length) {
  9945. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9946. }
  9947. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9948. if (shipping_billto_ext.length) {
  9949. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9950. }
  9951. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9952. if (shipping_billto_contact.length) {
  9953. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9954. }
  9955. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9956. if (shipping_billto_phone.length) {
  9957. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9958. }
  9959. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9960. if (shipping_billto_fax.length) {
  9961. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9962. }
  9963. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9964. if (shipping_billto_email.length) {
  9965. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9966. }
  9967. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9968. if (billing_cid.length) {
  9969. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9970. }
  9971. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9972. if (billing_name.length) {
  9973. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9974. }
  9975. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9976. if (billing_ext.length) {
  9977. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9978. }
  9979. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9980. if (billing_contact.length) {
  9981. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9982. }
  9983. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9984. if (billing_phone.length) {
  9985. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9986. }
  9987. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9988. if (billing_fax.length) {
  9989. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9990. }
  9991. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9992. if (billing_email.length) {
  9993. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9994. }
  9995. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9996. if (returnto_cid.length) {
  9997. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9998. }
  9999. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  10000. if (returnto_name.length) {
  10001. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10002. }
  10003. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10004. if (returnto_ext.length) {
  10005. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10006. }
  10007. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10008. if (returnto_contact.length) {
  10009. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10010. }
  10011. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10012. if (returnto_phone.length) {
  10013. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10014. }
  10015. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10016. if (returnto_fax.length) {
  10017. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10018. }
  10019. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10020. if (returnto_email.length) {
  10021. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10022. }
  10023. NSString *order_status = @"status = 1,";
  10024. if (submit) {
  10025. order_status = @"status = -11,";
  10026. }
  10027. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10028. NSString *sync_sql = @"";
  10029. if (submit) {
  10030. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10031. [param setValue:sales_rep forKey:@"sales_rep"];
  10032. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10033. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10034. }
  10035. 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];
  10036. DebugLog(@"save order contactSql: %@",contactSql);
  10037. DebugLog(@"save order orderSql: %@",orderSql);
  10038. // int contact_ret = [iSalesDB execSql:contactSql];
  10039. int order_ret = [iSalesDB execSql:orderSql];
  10040. int ret = order_ret;
  10041. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10042. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10043. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10044. // [dic setValue:@"160409" forKey:@"min_ver"];
  10045. [dic setObject:so_id forKey:@"so#"];
  10046. return [RAConvertor dict2data:dic];
  10047. }
  10048. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10049. {
  10050. // id foo = nil;
  10051. // NSMutableArray *a = @[].mutableCopy;
  10052. // [a addObject:foo];
  10053. return [self saveorder:param submit:NO];
  10054. }
  10055. #pragma mark add to cart by name
  10056. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10057. {
  10058. NSString *orderCode = [params objectForKey:@"orderCode"];
  10059. NSDictionary *newParams = @{
  10060. @"product_id" : params[@"product_id_string"],
  10061. @"orderCode" : orderCode,
  10062. @"can_create_backorder":params[@"can_create_backorder"]
  10063. };
  10064. return [self offline_add2cart:[newParams mutableCopy]];
  10065. }
  10066. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10067. {
  10068. NSString *orderCode = [params objectForKey:@"orderCode"];
  10069. NSString *upccode = [params objectForKey:@"upc_code"];
  10070. // product_name = [self translateSingleQuote:product_name];
  10071. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10072. // bool search_upc = [params[@"search_upc"] boolValue];
  10073. sqlite3 *db = [iSalesDB get_db];
  10074. __block NSMutableString *product_id_string = [NSMutableString string];
  10075. // NSString *name = [product_name_array objectAtIndex:i];
  10076. NSString *sql =nil;
  10077. 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];
  10078. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10079. int product_id = sqlite3_column_int(stmt, 0);
  10080. [product_id_string appendFormat:@"%d",product_id];
  10081. }];
  10082. if (!orderCode) {
  10083. orderCode = @"";
  10084. }
  10085. NSDictionary *newParams = @{
  10086. @"product_id" : product_id_string,
  10087. @"orderCode" : orderCode,
  10088. @"can_create_backorder":params[@"can_create_backorder"]
  10089. };
  10090. [iSalesDB close_db:db];
  10091. return [self offline_add2cart:[newParams mutableCopy]];
  10092. }
  10093. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10094. {
  10095. NSString *orderCode = [params objectForKey:@"orderCode"];
  10096. NSString *product_name = [params objectForKey:@"product_name"];
  10097. product_name = [self translateSingleQuote:product_name];
  10098. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10099. bool search_upc = [params[@"search_upc"] boolValue];
  10100. sqlite3 *db = [iSalesDB get_db];
  10101. __block NSMutableString *product_id_string = [NSMutableString string];
  10102. for (int i = 0; i < product_name_array.count; i++) {
  10103. NSString *name = [product_name_array objectAtIndex:i];
  10104. NSString *sql =nil;
  10105. if(search_upc)
  10106. 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];
  10107. else
  10108. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10109. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10110. int product_id = sqlite3_column_int(stmt, 0);
  10111. if (i == product_name_array.count - 1) {
  10112. [product_id_string appendFormat:@"%d",product_id];
  10113. } else {
  10114. [product_id_string appendFormat:@"%d,",product_id];
  10115. }
  10116. }];
  10117. }
  10118. if (!orderCode) {
  10119. orderCode = @"";
  10120. }
  10121. NSDictionary *newParams = @{
  10122. @"product_id" : product_id_string,
  10123. @"orderCode" : orderCode,
  10124. @"can_create_backorder":params[@"can_create_backorder"]
  10125. };
  10126. [iSalesDB close_db:db];
  10127. return [self offline_add2cart:[newParams mutableCopy]];
  10128. }
  10129. #pragma mark reset order
  10130. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10131. {
  10132. // UIApplication * app = [UIApplication sharedApplication];
  10133. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10134. [iSalesDB disable_trigger];
  10135. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10136. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10137. [iSalesDB enable_trigger];
  10138. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10139. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10140. return [RAConvertor dict2data:dic];
  10141. }
  10142. #pragma mark submit order
  10143. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10144. {
  10145. return [self saveorder:params submit:YES];
  10146. }
  10147. #pragma mark copy order
  10148. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10149. {
  10150. NSMutableDictionary *ret = @{}.mutableCopy;
  10151. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10152. sqlite3 *db = [iSalesDB get_db];
  10153. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10154. // 首先查看联系人是否active
  10155. 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];
  10156. __block int customer_is_active = 1;
  10157. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10158. customer_is_active = sqlite3_column_int(stmt, 0);
  10159. }];
  10160. if (!customer_is_active) {
  10161. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10162. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10163. [iSalesDB close_db:db];
  10164. return [RAConvertor dict2data:ret];
  10165. }
  10166. // new order
  10167. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10168. NSString *new_order_code = [self get_offline_soid:db];
  10169. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10170. user = [self translateSingleQuote:user];
  10171. 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
  10172. __block int result = 1;
  10173. result = [iSalesDB execSql:insert_order_sql db:db];
  10174. if (!result) {
  10175. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10176. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10177. [iSalesDB close_db:db];
  10178. return [RAConvertor dict2data:ret];
  10179. }
  10180. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10181. // __block NSString *product_id = @"";
  10182. __weak typeof(self) weakSelf = self;
  10183. 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];
  10184. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10185. int is_active = sqlite3_column_int(stmt, 1);
  10186. if (is_active) {
  10187. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10188. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10189. 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];
  10190. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10191. }
  10192. }];
  10193. // product_id = [product_id substringFromIndex:1];
  10194. //
  10195. // [self offline_add2cart:@{}.mutableCopy];
  10196. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10197. [iSalesDB close_db:db];
  10198. if (result) {
  10199. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10200. } else {
  10201. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10202. }
  10203. [ret setObject:new_order_code forKey:@"so_id"];
  10204. return [RAConvertor dict2data:ret];
  10205. }
  10206. #pragma mark move wish list to cart
  10207. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10208. assert(params[@"can_create_backorder"]!=nil);
  10209. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10210. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10211. NSString *collectId = params[@"collectId"];
  10212. 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];
  10213. __block NSString *product_id = @"";
  10214. __block NSString *qty = @"";
  10215. __block int number_of_outOfStock = 0;
  10216. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10217. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10218. int productId = sqlite3_column_int(stmt, 0);
  10219. int item_qty = sqlite3_column_int(stmt, 1);
  10220. int availability = sqlite3_column_int(stmt, 2);
  10221. int _id = sqlite3_column_int(stmt, 3);
  10222. if (![params[@"can_create_backorder"] boolValue]) {
  10223. // 库存小于购买量为缺货
  10224. if (availability >= item_qty) {
  10225. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10226. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10227. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10228. } else {
  10229. number_of_outOfStock++;
  10230. }
  10231. } else {
  10232. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10233. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10234. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10235. }
  10236. }];
  10237. NSMutableDictionary *retDic = nil;
  10238. if (![params[@"can_create_backorder"] boolValue]) {
  10239. if (delete_collectId.count == 0) {
  10240. retDic = [NSMutableDictionary dictionary];
  10241. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10242. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10243. return [RAConvertor dict2data:retDic];
  10244. }
  10245. }
  10246. if (product_id.length > 1) {
  10247. product_id = [product_id substringFromIndex:1];
  10248. }
  10249. if (qty.length > 1) {
  10250. qty = [qty substringFromIndex:1];
  10251. }
  10252. NSString *orderCode = params[@"orderCode"];
  10253. if (!orderCode) {
  10254. orderCode = @"";
  10255. }
  10256. NSDictionary *newParams = @{
  10257. @"product_id" : product_id,
  10258. @"orderCode" : orderCode,
  10259. @"qty" : qty,
  10260. @"can_create_backorder":params[@"can_create_backorder"]
  10261. };
  10262. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10263. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10264. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10265. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10266. NSInteger ret = [wish_return[@"result"] intValue];
  10267. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10268. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10269. }
  10270. if (![params[@"can_create_backorder"] boolValue]) {
  10271. if (number_of_outOfStock > 0) {
  10272. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10273. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10274. }
  10275. }
  10276. return [RAConvertor dict2data:retDic];
  10277. }
  10278. #pragma mark - portfolio
  10279. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10280. // assert(params[@"contact_id"]!=nil);
  10281. assert(params[@"user"]!=nil);
  10282. assert(params[@"can_see_price"]!=nil);
  10283. int sort = [[params valueForKey:@"sort"] intValue];
  10284. int offset = [[params valueForKey:@"offset"] intValue];
  10285. int limit = [[params valueForKey:@"limit"] intValue];
  10286. NSString *orderBy = @"";
  10287. switch (sort) {
  10288. case 0:{
  10289. orderBy = @"p.modify_time desc";
  10290. }
  10291. break;
  10292. case 1:{
  10293. orderBy = @"modify_time asc";
  10294. }
  10295. break;
  10296. case 2:{
  10297. orderBy = @"p.name asc";
  10298. }
  10299. break;
  10300. case 3:{
  10301. orderBy = @"p.name desc";
  10302. }
  10303. break;
  10304. case 4:{
  10305. orderBy = @"p.description asc";
  10306. }
  10307. break;
  10308. case 5: {
  10309. orderBy = @"m.default_category asc";
  10310. }
  10311. default:
  10312. break;
  10313. }
  10314. // 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];
  10315. 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];
  10316. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10317. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10318. sqlite3 *db = [iSalesDB get_db];
  10319. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10320. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10321. int product_id = sqlite3_column_int(stmt, 0);
  10322. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10323. NSString *name = [self textAtColumn:1 statement:stmt];
  10324. NSString *description = [self textAtColumn:2 statement:stmt];
  10325. double price = sqlite3_column_double(stmt, 3);
  10326. double discount = sqlite3_column_double(stmt,4);
  10327. int qty = sqlite3_column_int(stmt, 5);
  10328. int percentage = sqlite3_column_int(stmt, 6);
  10329. int item_id = sqlite3_column_int(stmt, 7);
  10330. // int fashion_id = sqlite3_column_int(stmt, 8);
  10331. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10332. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10333. double percent = sqlite3_column_double(stmt, 11);
  10334. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10335. if ([price_null isEqualToString:@"null"]) {
  10336. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10337. }
  10338. NSMutableDictionary *item = @{
  10339. @"linenotes": line_note,
  10340. @"check": @(1),
  10341. @"product_id": product_id_string,
  10342. @"available_qty": @(qty),
  10343. @"available_percent" : @(percent),
  10344. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10345. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10346. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10347. @"img": img_path,
  10348. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10349. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10350. }.mutableCopy;
  10351. if (percentage) {
  10352. [item removeObjectForKey:@"available_qty"];
  10353. } else {
  10354. [item removeObjectForKey:@"available_percent"];
  10355. }
  10356. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10357. if ([qty_null isEqualToString:@"null"]) {
  10358. [item removeObjectForKey:@"available_qty"];
  10359. }
  10360. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10361. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10362. }];
  10363. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10364. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10365. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10366. }
  10367. [iSalesDB close_db:db];
  10368. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10369. [dic setValue:@"" forKey:@"email_content"];
  10370. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10371. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10372. return [RAConvertor dict2data:dic];
  10373. }
  10374. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10375. __block int qty = 0;
  10376. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10377. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10378. qty = sqlite3_column_int(stmt, 0);
  10379. }];
  10380. return qty;
  10381. }
  10382. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10383. {
  10384. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10385. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10386. // NSMutableDictionary * values = params[@"replaceValue"];
  10387. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10388. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10389. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10390. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10391. NSString *pdf_path = @"";
  10392. if (direct) {
  10393. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10394. } else {
  10395. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10396. }
  10397. NSString *create_user = [self valueInParams:params key:@"user"];
  10398. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10399. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10400. // model info
  10401. // 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];
  10402. // V1.90 more color
  10403. 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];
  10404. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10405. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10406. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10407. sqlite3 *db = [iSalesDB get_db];
  10408. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10409. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10410. int product_id = sqlite3_column_int(stmt, 0);
  10411. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10412. double price = sqlite3_column_double(stmt, 1);
  10413. double discount = sqlite3_column_double(stmt,2);
  10414. int qty = sqlite3_column_int(stmt, 3);
  10415. int percentage = sqlite3_column_int(stmt, 4);
  10416. int item_id = sqlite3_column_int(stmt, 5);
  10417. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10418. double percent = sqlite3_column_double(stmt, 7);
  10419. int more_color = sqlite3_column_int(stmt, 8);
  10420. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10421. /* if ([price_null isEqualToString:@"null"]) {
  10422. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10423. }
  10424. */
  10425. [product_ids_string appendFormat:@"%@,",product_id_string];
  10426. // Regular Price
  10427. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10428. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10429. // QTY
  10430. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10431. if ([qty_null isEqualToString:@"null"]) {
  10432. // 查available
  10433. qty = [self model_QTY:product_id_string db:db];
  10434. }
  10435. if (percentage) {
  10436. qty = qty * percent / 100;
  10437. }
  10438. // Special Price
  10439. if ([price_null isEqualToString:@"null"]) {
  10440. // price = regular price
  10441. price = [regular_price_str doubleValue];
  10442. }
  10443. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10444. if (![discount_null isEqualToString:@"null"]) {
  10445. price = price * (1 - discount / 100.0);
  10446. }
  10447. NSMutableDictionary *item = @{
  10448. @"line_note": line_note,
  10449. @"product_id": product_id_string,
  10450. @"available_qty": @(qty),
  10451. @"more_color":@(more_color),
  10452. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10453. @"regular_price" : regular_price_str,
  10454. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10455. }.mutableCopy;
  10456. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10457. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10458. }];
  10459. [iSalesDB close_db:db];
  10460. if (product_ids_string.length > 0) {
  10461. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10462. }
  10463. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10464. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10465. return [RAConvertor dict2data:resultDictionary];
  10466. }
  10467. NSString *model_info = [RAConvertor dict2string:dic];
  10468. // 创建PDF
  10469. // 在preview情况下保存,则不需要新建了
  10470. if (direct) {
  10471. NSMutableDictionary *tear_sheet_params = params;
  10472. // if (tear_sheet_id) {
  10473. // tear_sheet_params = values;
  10474. // }
  10475. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10476. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10477. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10478. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10479. resultDictionary = pdfInfo.mutableCopy;
  10480. } else { // 创建PDF失败
  10481. return pdfData;
  10482. }
  10483. } else {
  10484. // pdf_path 就是本地路径
  10485. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10486. }
  10487. // 将文件移动到PDF Cache文件夹
  10488. NSString *newPath = [pdf_path lastPathComponent];
  10489. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10490. NSString *cachefolder = [paths objectAtIndex:0];
  10491. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10492. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10493. NSFileManager *fileManager = [NSFileManager defaultManager];
  10494. NSError *error = nil;
  10495. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10496. if (error) { // 移动文件失败
  10497. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10498. return [RAConvertor dict2data:resultDictionary];
  10499. }
  10500. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10501. pdf_path = [newPath lastPathComponent];
  10502. // 保存信息
  10503. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10504. NSString *off_params = [RAConvertor dict2string:params];
  10505. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10506. pdf_path = [self translateSingleQuote:pdf_path];
  10507. create_user = [self translateSingleQuote:create_user];
  10508. tear_note = [self translateSingleQuote:tear_note];
  10509. tear_name = [self translateSingleQuote:tear_name];
  10510. model_info = [self translateSingleQuote:model_info];
  10511. configureParams = [self translateSingleQuote:configureParams];
  10512. off_params = [self translateSingleQuote:off_params];
  10513. 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];
  10514. if (tear_sheet_id) {
  10515. int _id = [tear_sheet_id intValue];
  10516. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10517. }
  10518. int result = [iSalesDB execSql:save_pdf_sql];
  10519. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10520. //
  10521. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10522. if (remove_Item) {
  10523. // portfolioId
  10524. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10525. }
  10526. return [RAConvertor dict2data:resultDictionary];
  10527. }
  10528. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10529. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10530. }
  10531. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10532. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10533. }
  10534. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10535. int offset = [[params valueForKey:@"offset"] intValue];
  10536. int limit = [[params valueForKey:@"limit"] intValue];
  10537. NSString *keyword = [params valueForKey:@"keyWord"];
  10538. NSString *where = @"where is_delete is null or is_delete = 0";
  10539. if (keyword.length) {
  10540. keyword = [self translateSingleQuote:keyword];
  10541. 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];
  10542. }
  10543. 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
  10544. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10545. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10546. NSString *cachefolder = [paths objectAtIndex:0];
  10547. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10548. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10549. NSString *name = [self textAtColumn:0 statement:stmt];
  10550. NSString *note = [self textAtColumn:1 statement:stmt];
  10551. NSString *time = [self textAtColumn:2 statement:stmt];
  10552. NSString *user = [self textAtColumn:3 statement:stmt];
  10553. NSString *path = [self textAtColumn:4 statement:stmt];
  10554. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10555. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10556. path = [pdfFolder stringByAppendingPathComponent:path];
  10557. BOOL bdir=NO;
  10558. NSFileManager* fileManager = [NSFileManager defaultManager];
  10559. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10560. {
  10561. //pdf文件不存在
  10562. path=nil;
  10563. }
  10564. time = [self changeDateTimeFormate:time];
  10565. time = [time stringByAppendingString:@" PST"];
  10566. int sheet_id = sqlite3_column_int(stmt, 5);
  10567. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10568. item[@"tearsheetsId"]=@(sheet_id);
  10569. item[@"pdf_path"]=path;
  10570. item[@"create_time"]=time;
  10571. item[@"create_user"]=user;
  10572. item[@"tear_note"]=note;
  10573. item[@"tear_name"]=name;
  10574. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10575. item[@"model_info"]=model_info;
  10576. item[@"off_params"]=off_params;
  10577. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10578. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10579. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10580. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10581. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10582. }];
  10583. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10584. return [RAConvertor dict2data:dic];
  10585. }
  10586. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10587. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10588. // NSString *user = [params objectForKey:@"user"];
  10589. // 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];
  10590. 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];
  10591. int result = [iSalesDB execSql:sql];
  10592. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10593. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10594. if (result == RESULT_TRUE) {
  10595. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10596. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10597. sqlite3 *db = [iSalesDB get_db];
  10598. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10599. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10600. [iSalesDB close_db:db];
  10601. }
  10602. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10603. return [RAConvertor dict2data:dic];
  10604. }
  10605. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10606. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10607. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10608. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10609. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10610. NSDictionary *company_item = @{
  10611. @"control": @"text",
  10612. @"keyboard": @"text",
  10613. @"name": @"company_name",
  10614. @"value": COMPANY_FULL_NAME,
  10615. @"aname": @"Company Name"
  10616. };
  10617. [section_0 setObject:company_item forKey:@"item_0"];
  10618. [dic setObject:section_0 forKey:@"section_0"];
  10619. // Regurlar Price
  10620. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10621. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10622. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10623. __block long val_count = 0;
  10624. NSString *sql = @"select name,type,order_by from price order by order_by";
  10625. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10626. NSString *name = [self textAtColumn:0 statement:stmt];
  10627. int type = sqlite3_column_int(stmt, 1);
  10628. int order_by = sqlite3_column_int(stmt, 2);
  10629. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10630. NSDictionary *price_dic = @{
  10631. @"value" : name,
  10632. @"value_id" : [NSNumber numberWithInteger:type],
  10633. @"check" : order_by == 0 ? @(1) : @(0)
  10634. };
  10635. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10636. val_count = ++(*count);
  10637. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10638. val_count = 0;
  10639. }];
  10640. [cadedate setObject:@{@"value" : @"None",
  10641. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10642. val_count++;
  10643. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10644. [price setObject:cadedate forKey:@"cadedate"];
  10645. [section1 setObject:price forKey:@"item_2"];
  10646. [dic setObject:section1 forKey:@"section_1"];
  10647. return [RAConvertor dict2data:dic];
  10648. }
  10649. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10650. NSString *product_id = [params objectForKey:@"fashionId"];
  10651. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10652. __block int result = RESULT_TRUE;
  10653. __block int qty = 0;
  10654. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10655. qty = sqlite3_column_int(stmt, 0);
  10656. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10657. result = RESULT_FALSE;
  10658. }];
  10659. NSMutableDictionary *dic = @{
  10660. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10661. @"mode" : @"Regular Mode",
  10662. @"quantity_available" : @(qty),
  10663. @"result" : @(result),
  10664. }.mutableCopy;
  10665. return [RAConvertor dict2data:dic];
  10666. }
  10667. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10668. NSString *item_ids = [params objectForKey:@"item_id"];
  10669. NSString *line_notes = [params objectForKey:@"notes"];
  10670. NSString *price_str = [params objectForKey:@"price"];
  10671. NSString *discount_str = [params objectForKey:@"discount"];
  10672. NSString *percent = [params objectForKey:@"available_percent"];
  10673. NSString *qty = [params objectForKey:@"available_qty"];
  10674. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10675. int dot = 0;
  10676. if (line_notes) {
  10677. line_notes = [self translateSingleQuote:line_notes];
  10678. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10679. sql = [sql stringByAppendingString:line_notes];
  10680. dot = 1;
  10681. } else {
  10682. line_notes = @"";
  10683. }
  10684. if (price_str) {
  10685. if (dot) {
  10686. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10687. } else {
  10688. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10689. dot = 1;
  10690. }
  10691. sql = [sql stringByAppendingString:price_str];
  10692. } else {
  10693. price_str = @"";
  10694. }
  10695. if (discount_str) {
  10696. if (dot) {
  10697. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10698. } else {
  10699. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10700. dot = 1;
  10701. }
  10702. sql = [sql stringByAppendingString:discount_str];
  10703. } else {
  10704. discount_str = @"";
  10705. }
  10706. if (percent) {
  10707. if (dot) {
  10708. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10709. } else {
  10710. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10711. dot = 1;
  10712. }
  10713. sql = [sql stringByAppendingString:percent];
  10714. } else {
  10715. percent = @"";
  10716. }
  10717. if (qty) {
  10718. if (dot) {
  10719. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10720. } else {
  10721. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10722. dot = 1;
  10723. }
  10724. sql = [sql stringByAppendingString:qty];
  10725. } else {
  10726. qty = @"";
  10727. }
  10728. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10729. sql = [sql stringByAppendingString:where];
  10730. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10731. int result = [iSalesDB execSql:sql];
  10732. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10733. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10734. return [RAConvertor dict2data:dic];
  10735. }
  10736. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10737. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10738. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10739. sqlite3 *db = [iSalesDB get_db];
  10740. 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];
  10741. int result = [iSalesDB execSql:sql db:db];
  10742. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10743. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10744. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10745. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10746. [iSalesDB close_db:db];
  10747. return [RAConvertor dict2data:dic];
  10748. }
  10749. + (void)offline_removePDFWithName:(NSString *)name {
  10750. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10751. NSString *cachefolder = [paths objectAtIndex:0];
  10752. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10753. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10754. NSFileManager *fileManager = [NSFileManager defaultManager];
  10755. [fileManager removeItemAtPath:path error:nil];
  10756. }
  10757. + (void)offline_clear_PDFCache {
  10758. NSFileManager *fileManager = [NSFileManager defaultManager];
  10759. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10760. NSString *cachefolder = [paths objectAtIndex:0];
  10761. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10762. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10763. for (NSString *path in pdf_files) {
  10764. [self offline_removePDFWithName:[path lastPathComponent]];
  10765. }
  10766. }
  10767. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10768. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10769. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10770. NSString *user = [params objectForKey:@"user"];
  10771. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10772. if (![create_user isEqualToString:user]) {
  10773. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10774. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10775. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10776. return [RAConvertor dict2data:dic];
  10777. }
  10778. 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]];
  10779. __block int is_local = 0;
  10780. __block NSString *path = @"";
  10781. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10782. is_local = sqlite3_column_int(stmt, 0);
  10783. path = [self textAtColumn:1 statement:stmt];
  10784. }];
  10785. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10786. if (is_local == 1) {
  10787. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10788. // 删除文件
  10789. [self offline_removePDFWithName:path];
  10790. }
  10791. int result = [iSalesDB execSql:sql];
  10792. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10793. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10794. return [RAConvertor dict2data:dic];
  10795. }
  10796. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10797. {
  10798. // assert(add_params[@"contact_id"]!=nil);
  10799. assert(add_params[@"user"]!=nil);
  10800. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10801. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10802. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10803. NSString* where=@"1=1";
  10804. if (ver!=nil) {
  10805. where=@"is_dirty=1";
  10806. }
  10807. 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];
  10808. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10809. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10810. sqlite3 *db = [iSalesDB get_db];
  10811. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10812. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10813. NSInteger _id = sqlite3_column_int(stmt, 0);
  10814. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10815. NSString *name = [self textAtColumn:2 statement:stmt];
  10816. NSString *desc = [self textAtColumn:3 statement:stmt];
  10817. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10818. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10819. NSInteger qty = sqlite3_column_int(stmt, 6);
  10820. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10821. double percent = sqlite3_column_double(stmt, 8);
  10822. double price = sqlite3_column_double(stmt, 9);
  10823. double discount = sqlite3_column_double(stmt, 10);
  10824. NSString *img = [self textAtColumn:11 statement:stmt];
  10825. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10826. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10827. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10828. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10829. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10830. if ([price_null isEqualToString:@"null"]) {
  10831. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10832. }
  10833. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10834. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10835. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10836. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10837. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10838. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10839. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10840. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10841. if ([qty_null isEqualToString:@"null"]) {
  10842. // [item setValue:@"null" forKey:@"available_qty"];
  10843. } else {
  10844. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10845. }
  10846. if ([is_percent_null isEqualToString:@"null"]) {
  10847. // [item setValue:@"null" forKey:@"percentage"];
  10848. } else {
  10849. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10850. }
  10851. if ([percent_null isEqualToString:@"null"]) {
  10852. // [item setValue:@"null" forKey:@"percent"];
  10853. } else {
  10854. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10855. }
  10856. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10857. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10858. [item setValue:name forKey:@"name"];
  10859. [item setValue:desc forKey:@"description"];
  10860. [item setValue:img forKey:@"img"];
  10861. [item setValue:line_note forKey:@"line_note"];
  10862. [item setValue:create_time forKey:@"createtime"];
  10863. [item setValue:modify_time forKey:@"modifytime"];
  10864. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10865. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10866. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10867. }];
  10868. [iSalesDB close_db:db];
  10869. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10870. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10871. }
  10872. return ret;
  10873. }
  10874. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10875. {
  10876. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10877. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10878. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10879. NSString* where=@"1=1";
  10880. if (ver!=nil) {
  10881. where=@"is_dirty=1";
  10882. }
  10883. 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];
  10884. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10885. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10886. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10887. NSInteger _id = sqlite3_column_int(stmt, 0);
  10888. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10889. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10890. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10891. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10892. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10893. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10894. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10895. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10896. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10897. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10898. NSString *uuid = [NSUUID UUID].UUIDString;
  10899. int is_delete = sqlite3_column_int(stmt, 11);
  10900. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10901. int is_local = sqlite3_column_int(stmt, 12);
  10902. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10903. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10904. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10905. }
  10906. [item setObject:pdf_path forKey:@"pdf_path"];
  10907. [item setObject:create_user forKey:@"create_user"];
  10908. [item setObject:tear_note forKey:@"tear_note"];
  10909. [item setObject:tear_name forKey:@"tear_name"];
  10910. [item setObject:model_info forKey:@"model_info"];
  10911. [item setObject:createtime forKey:@"createtime"];
  10912. [item setObject:modifytime forKey:@"modifytime"];
  10913. [item setObject:urlParams forKey:@"urlParams"];
  10914. [item setObject:off_params forKey:@"off_params"];
  10915. [item setObject:uuid forKey:@"pdf_token"];
  10916. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10917. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10918. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10919. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10920. } else {
  10921. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10922. }
  10923. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10924. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10925. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10926. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10927. }];
  10928. return ret;
  10929. }
  10930. #pragma mark 2020
  10931. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10932. {
  10933. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10934. sqlite3 *db = [iSalesDB get_db];
  10935. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10936. if (contactType) {
  10937. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10938. } else {
  10939. contactType = @"1 = 1";
  10940. }
  10941. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10942. DebugLog(@"offline contact list keyword: %@",keyword);
  10943. // advanced search
  10944. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10945. if (contact_name) {
  10946. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10947. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10948. } else {
  10949. contact_name = @"";
  10950. }
  10951. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10952. if (customer_phone) {
  10953. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10954. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10955. } else {
  10956. customer_phone = @"";
  10957. }
  10958. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10959. if (customer_fax) {
  10960. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10961. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10962. } else {
  10963. customer_fax = @"";
  10964. }
  10965. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10966. if (customer_zipcode) {
  10967. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10968. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10969. } else {
  10970. customer_zipcode = @"";
  10971. }
  10972. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10973. if (customer_sales_rep) {
  10974. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10975. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10976. } else {
  10977. customer_sales_rep = @"";
  10978. }
  10979. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10980. if (customer_state) {
  10981. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10982. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10983. } else {
  10984. customer_state = @"";
  10985. }
  10986. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10987. if (customer_name) {
  10988. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10989. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10990. } else {
  10991. customer_name = @"";
  10992. }
  10993. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10994. if (customer_country) {
  10995. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10996. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10997. } else {
  10998. customer_country = @"";
  10999. }
  11000. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11001. if (customer_cid) {
  11002. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11003. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11004. } else {
  11005. customer_cid = @"";
  11006. }
  11007. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11008. if (customer_city) {
  11009. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11010. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11011. } else {
  11012. customer_city = @"";
  11013. }
  11014. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11015. if (customer_address) {
  11016. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11017. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11018. } else {
  11019. customer_address = @"";
  11020. }
  11021. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11022. if (customer_email) {
  11023. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11024. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11025. } else {
  11026. customer_email = @"";
  11027. }
  11028. NSString *price_name = [params valueForKey:@"price_name"];
  11029. if (price_name) {
  11030. if ([price_name containsString:@","]) {
  11031. // 首先从 price表中查处name
  11032. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11033. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11034. for (int i = 1;i < pArray.count;i++) {
  11035. NSString *p = pArray[i];
  11036. [mutablePStr appendFormat:@" or type = %@ ",p];
  11037. }
  11038. [mutablePStr appendString:@";"];
  11039. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11040. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11041. char *name = (char *)sqlite3_column_text(stmt, 0);
  11042. if (!name)
  11043. name = "";
  11044. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11045. }];
  11046. // 再根据name 拼sql
  11047. NSMutableString *mutable_price_name = [NSMutableString string];
  11048. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11049. for (int i = 1; i < price_name_array.count; i++) {
  11050. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11051. }
  11052. [mutable_price_name appendString:@")"];
  11053. price_name = mutable_price_name;
  11054. } else {
  11055. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11056. if ([price_name isEqualToString:@""]) {
  11057. price_name = @"";
  11058. } else {
  11059. __block NSString *price;
  11060. price_name = [self translateSingleQuote:price_name];
  11061. [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) {
  11062. char *p = (char *)sqlite3_column_text(stmt, 0);
  11063. if (p == NULL) {
  11064. p = "";
  11065. }
  11066. price = [NSString stringWithUTF8String:p];
  11067. }];
  11068. if ([price isEqualToString:@""]) {
  11069. price_name = @"";
  11070. } else {
  11071. price = [self translateSingleQuote:price];
  11072. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11073. }
  11074. }
  11075. }
  11076. } else {
  11077. price_name = @"";
  11078. }
  11079. int limit = [[params valueForKey:@"limit"] intValue];
  11080. int offset = [[params valueForKey:@"offset"] intValue];
  11081. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11082. 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];
  11083. 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];
  11084. // int result= [iSalesDB AddExFunction:db];
  11085. int count =0;
  11086. NSString *sqlQuery = nil;
  11087. if(keyword.length==0)
  11088. {
  11089. // 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];
  11090. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11091. sqlQuery = sql;
  11092. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11093. }
  11094. else
  11095. {
  11096. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11097. keyword = keyword.lowercaseString;
  11098. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11099. 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];
  11100. 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]];
  11101. }
  11102. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11103. sqlite3_stmt * statement;
  11104. [ret setValue:@"2" forKey:@"result"];
  11105. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11106. // 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";
  11107. int i = 0;
  11108. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11109. {
  11110. while (sqlite3_step(statement) == SQLITE_ROW)
  11111. {
  11112. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11113. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11114. int editable = sqlite3_column_int(statement, 0);
  11115. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11116. NSString *nscompany_name =nil;
  11117. if(company_name==nil)
  11118. nscompany_name=@"";
  11119. else
  11120. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11121. char *country = (char*)sqlite3_column_text(statement, 2);
  11122. if(country==nil)
  11123. country="";
  11124. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11125. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11126. // if(addr==nil)
  11127. // addr="";
  11128. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11129. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11130. if(zipcode==nil)
  11131. zipcode="";
  11132. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11133. char *state = (char*)sqlite3_column_text(statement, 5);
  11134. if(state==nil)
  11135. state="";
  11136. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11137. char *city = (char*)sqlite3_column_text(statement, 6);
  11138. if(city==nil)
  11139. city="";
  11140. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11141. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11142. // NSString *nscontact_name = nil;
  11143. // if(contact_name==nil)
  11144. // nscontact_name=@"";
  11145. // else
  11146. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11147. char *phone = (char*)sqlite3_column_text(statement, 8);
  11148. NSString *nsphone = nil;
  11149. if(phone==nil)
  11150. nsphone=@"";
  11151. else
  11152. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11153. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11154. if(contact_id==nil)
  11155. contact_id="";
  11156. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11157. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11158. if(addr_1==nil)
  11159. addr_1="";
  11160. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11161. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11162. if(addr_2==nil)
  11163. addr_2="";
  11164. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11165. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11166. if(addr_3==nil)
  11167. addr_3="";
  11168. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11169. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11170. if(addr_4==nil)
  11171. addr_4="";
  11172. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11173. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11174. if(first_name==nil)
  11175. first_name="";
  11176. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11177. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11178. if(last_name==nil)
  11179. last_name="";
  11180. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11181. char *fax = (char*)sqlite3_column_text(statement, 16);
  11182. NSString *nsfax = nil;
  11183. if(fax==nil)
  11184. nsfax=@"";
  11185. else
  11186. {
  11187. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11188. if(nsfax.length>0)
  11189. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11190. }
  11191. char *email = (char*)sqlite3_column_text(statement, 17);
  11192. NSString *nsemail = nil;
  11193. if(email==nil)
  11194. nsemail=@"";
  11195. else
  11196. {
  11197. nsemail= [[NSString alloc]initWithUTF8String:email];
  11198. if(nsemail.length>0)
  11199. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11200. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11201. }
  11202. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11203. [arr_name addObject:nsfirst_name];
  11204. [arr_name addObject:nslast_name];
  11205. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11206. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11207. {
  11208. // decrypt
  11209. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11210. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11211. nsphone=[AESCrypt fastdecrypt:nsphone];
  11212. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11213. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11214. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11215. }
  11216. [arr_addr addObject:nscompany_name];
  11217. [arr_addr addObject:nscontact_name];
  11218. [arr_addr addObject:@"<br>"];
  11219. [arr_addr addObject:nsaddr_1];
  11220. [arr_addr addObject:nsaddr_2];
  11221. [arr_addr addObject:nsaddr_3];
  11222. [arr_addr addObject:nsaddr_4];
  11223. //[arr_addr addObject:nsaddr];
  11224. [arr_addr addObject:nszipcode];
  11225. [arr_addr addObject:nscity];
  11226. [arr_addr addObject:nsstate];
  11227. [arr_addr addObject:nscountry];
  11228. [arr_addr addObject:@"<br>"];
  11229. [arr_addr addObject:nsphone];
  11230. [arr_addr addObject:nsfax];
  11231. [arr_addr addObject:nsemail];
  11232. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11233. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11234. [item setValue:name forKey:@"name"];
  11235. [item setValue:nscontact_id forKey:@"contact_id"];
  11236. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11237. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11238. i++;
  11239. }
  11240. sqlite3_finalize(statement);
  11241. }
  11242. [iSalesDB close_db:db];
  11243. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11244. dispatch_async(dispatch_get_main_queue(), ^{
  11245. UIApplication * app = [UIApplication sharedApplication];
  11246. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11247. [ret setValue:appDelegate.mode forKey:@"mode"];
  11248. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11249. result(ret);
  11250. });
  11251. return ;
  11252. });
  11253. }
  11254. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11255. //{
  11256. //
  11257. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11258. //
  11259. // NSString* contactId = [params valueForKey:@"contactId"];
  11260. //
  11261. //
  11262. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11263. //
  11264. //
  11265. //
  11266. //
  11267. //
  11268. //
  11269. // sqlite3 *db = [iSalesDB get_db];
  11270. //
  11271. //
  11272. //
  11273. //
  11274. //
  11275. // NSString *sqlQuery = nil;
  11276. //
  11277. //
  11278. // {
  11279. // 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];
  11280. //
  11281. // }
  11282. //
  11283. //
  11284. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11285. // sqlite3_stmt * statement;
  11286. //
  11287. //
  11288. // [ret setValue:@"2" forKey:@"result"];
  11289. //
  11290. //
  11291. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11292. // {
  11293. //
  11294. // //int i = 0;
  11295. // if (sqlite3_step(statement) == SQLITE_ROW)
  11296. // {
  11297. //
  11298. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11299. //
  11300. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11301. //
  11302. //
  11303. // // int editable = sqlite3_column_int(statement, 0);
  11304. //
  11305. //
  11306. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11307. // NSString *nscompany_name =nil;
  11308. // if(company_name==nil)
  11309. // nscompany_name=@"";
  11310. // else
  11311. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11312. //
  11313. //
  11314. // char *country = (char*)sqlite3_column_text(statement, 2);
  11315. // if(country==nil)
  11316. // country="";
  11317. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11318. //
  11319. //
  11320. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11321. // // if(addr==nil)
  11322. // // addr="";
  11323. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11324. //
  11325. //
  11326. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11327. // if(zipcode==nil)
  11328. // zipcode="";
  11329. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11330. //
  11331. //
  11332. // char *state = (char*)sqlite3_column_text(statement, 5);
  11333. // if(state==nil)
  11334. // state="";
  11335. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11336. //
  11337. // char *city = (char*)sqlite3_column_text(statement, 6);
  11338. // if(city==nil)
  11339. // city="";
  11340. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11341. //
  11342. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11343. // // NSString *nscontact_name = nil;
  11344. // // if(contact_name==nil)
  11345. // // nscontact_name=@"";
  11346. // // else
  11347. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11348. //
  11349. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11350. // NSString *nsphone = nil;
  11351. // if(phone==nil)
  11352. // nsphone=@"";
  11353. // else
  11354. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11355. //
  11356. //
  11357. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11358. // // if(contact_id==nil)
  11359. // // contact_id="";
  11360. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11361. //
  11362. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11363. // if(addr_1==nil)
  11364. // addr_1="";
  11365. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11366. //
  11367. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11368. // if(addr_2==nil)
  11369. // addr_2="";
  11370. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11371. //
  11372. //
  11373. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11374. // if(addr_3==nil)
  11375. // addr_3="";
  11376. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11377. //
  11378. //
  11379. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11380. // if(addr_4==nil)
  11381. // addr_4="";
  11382. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11383. //
  11384. //
  11385. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11386. // if(first_name==nil)
  11387. // first_name="";
  11388. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11389. //
  11390. //
  11391. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11392. // if(last_name==nil)
  11393. // last_name="";
  11394. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11395. //
  11396. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11397. // NSString *nsfax = nil;
  11398. // if(fax==nil)
  11399. // nsfax=@"";
  11400. // else
  11401. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11402. //
  11403. // char *email = (char*)sqlite3_column_text(statement, 17);
  11404. // NSString *nsemail = nil;
  11405. // if(email==nil)
  11406. // nsemail=@"";
  11407. // else
  11408. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11409. //
  11410. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11411. // NSString *nsimg_0 = nil;
  11412. // if(img_0==nil)
  11413. // nsimg_0=@"";
  11414. // else
  11415. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11416. //
  11417. // [self copy_bcardImg:nsimg_0];
  11418. //
  11419. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11420. // NSString *nsimg_1 = nil;
  11421. // if(img_1==nil)
  11422. // nsimg_1=@"";
  11423. // else
  11424. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11425. // [self copy_bcardImg:nsimg_1];
  11426. //
  11427. //
  11428. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11429. // NSString *nsimg_2 = nil;
  11430. // if(img_2==nil)
  11431. // nsimg_2=@"";
  11432. // else
  11433. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11434. // [self copy_bcardImg:nsimg_2];
  11435. //
  11436. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11437. // NSString *nsprice_type = nil;
  11438. // if(price_type==nil)
  11439. // nsprice_type=@"";
  11440. // else
  11441. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11442. //
  11443. //
  11444. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11445. // NSString *nsnotes = nil;
  11446. // if(notes==nil)
  11447. // nsnotes=@"";
  11448. // else
  11449. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11450. //
  11451. //
  11452. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11453. // NSString *nssalesrep = nil;
  11454. // if(salesrep==nil)
  11455. // nssalesrep=@"";
  11456. // else
  11457. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11458. //
  11459. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11460. //
  11461. //
  11462. // {
  11463. // // decrypt
  11464. //
  11465. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11466. //
  11467. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11468. //
  11469. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11470. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11471. //
  11472. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11473. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11474. //
  11475. // }
  11476. //
  11477. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11478. //
  11479. // [arr_name addObject:nsfirst_name];
  11480. // [arr_name addObject:nslast_name];
  11481. //
  11482. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11483. //
  11484. //
  11485. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11486. //
  11487. // [arr_ext addObject:nsaddr_1];
  11488. // [arr_ext addObject:nsaddr_2];
  11489. // [arr_ext addObject:nsaddr_3];
  11490. // [arr_ext addObject:nsaddr_4];
  11491. // [arr_ext addObject:@"\r\n"];
  11492. //
  11493. // [arr_ext addObject:nscity];
  11494. // [arr_ext addObject:nsstate];
  11495. // [arr_ext addObject:nszipcode];
  11496. // [arr_ext addObject:nscountry];
  11497. //
  11498. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11499. //
  11500. //
  11501. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11502. //
  11503. //
  11504. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11505. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11506. // [item setValue:nscountry forKey:@"customer_country"];
  11507. // [item setValue:nsphone forKey:@"customer_phone"];
  11508. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11509. // [item setValue:nscompany_name forKey:@"customer_name"];
  11510. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11511. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11512. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11513. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11514. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11515. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11516. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11517. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11518. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11519. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11520. // [item setValue:nscity forKey:@"customer_city"];
  11521. // [item setValue:nsstate forKey:@"customer_state"];
  11522. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11523. // [item setValue:contactId forKey:@"customer_cid"];
  11524. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11525. // [item setValue:nsfax forKey:@"customer_fax"];
  11526. // [item setValue:nsemail forKey:@"customer_email"];
  11527. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11528. //
  11529. //
  11530. // [ret setObject:item forKey:@"customerInfo"];
  11531. // // i++;
  11532. //
  11533. //
  11534. //
  11535. // }
  11536. //
  11537. //
  11538. //
  11539. //
  11540. // sqlite3_finalize(statement);
  11541. // }
  11542. //
  11543. //
  11544. //
  11545. // [iSalesDB close_db:db];
  11546. //
  11547. //
  11548. //
  11549. //
  11550. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11551. // dispatch_async(dispatch_get_main_queue(), ^{
  11552. // UIApplication * app = [UIApplication sharedApplication];
  11553. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11554. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11555. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11556. // result(ret);
  11557. // });
  11558. //
  11559. // return ;
  11560. // });
  11561. //}
  11562. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11563. {
  11564. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11565. NSString* order_code= appDelegate.order_code;
  11566. NSString* user = appDelegate.user;
  11567. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11568. NSString* category = [params valueForKey:@"category"];
  11569. if (!category || [category isEqualToString:@""]) {
  11570. category = @"%";
  11571. }
  11572. category = [self translateSingleQuote:category];
  11573. int limit = [[params valueForKey:@"limit"] intValue];
  11574. int offset = [[params valueForKey:@"offset"] intValue];
  11575. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11576. NSString *limit_str = @"";
  11577. if (limited) {
  11578. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11579. }
  11580. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11581. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11582. sqlite3 *db = [iSalesDB get_db];
  11583. // [iSalesDB AddExFunction:db];
  11584. int count;
  11585. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11586. 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];
  11587. double price_min = 0;
  11588. double price_max = 0;
  11589. if ([params.allKeys containsObject:@"alert"]) {
  11590. // alert
  11591. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11592. NSString *alert = params[@"alert"];
  11593. if ([alert isEqualToString:@"Display All"]) {
  11594. alert = [NSString stringWithFormat:@""];
  11595. } else {
  11596. alert = [self translateSingleQuote:alert];
  11597. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11598. }
  11599. // available
  11600. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11601. NSString *available = params[@"available"];
  11602. NSString *available_condition;
  11603. if ([available isEqualToString:@"Display All"]) {
  11604. available_condition = @"";
  11605. } else if ([available isEqualToString:@"Available Now"]) {
  11606. available_condition = @"and availability > 0";
  11607. } else {
  11608. available_condition = @"and availability == 0";
  11609. }
  11610. // best seller
  11611. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11612. NSString *best_seller = @"";
  11613. NSString *order_best_seller = @"m.name asc";
  11614. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11615. best_seller = @"and best_seller > 0";
  11616. order_best_seller = @"m.best_seller desc,m.name asc";
  11617. }
  11618. // price
  11619. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11620. NSString *price = params[@"price"];
  11621. price_min = 0;
  11622. price_max = MAXFLOAT;
  11623. if (user && price != nil) {
  11624. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11625. NSMutableString *priceName = [NSMutableString string];
  11626. for (int i = 0; i < priceTypeArray.count; i++) {
  11627. NSString *pricetype = priceTypeArray[i];
  11628. pricetype = [self translateSingleQuote:pricetype];
  11629. if (i == 0) {
  11630. [priceName appendFormat:@"'%@'",pricetype];
  11631. } else {
  11632. [priceName appendFormat:@",'%@'",pricetype];
  11633. }
  11634. }
  11635. if ([price isEqualToString:@"Display All"]) {
  11636. price = [NSString stringWithFormat:@""];
  11637. } else if([price containsString:@"+"]){
  11638. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11639. price_min = [price doubleValue];
  11640. 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];
  11641. } else {
  11642. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11643. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11644. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11645. 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];
  11646. }
  11647. } else {
  11648. price = @"";
  11649. }
  11650. // sold_by_qty : Sold in quantities of %@
  11651. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11652. NSString *qty = params[@"sold_by_qty"];
  11653. if ([qty isEqualToString:@"Display All"]) {
  11654. qty = @"";
  11655. } else {
  11656. qty = [self translateSingleQuote:qty];
  11657. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11658. }
  11659. // cate
  11660. // category = [self translateSingleQuote:category];
  11661. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11662. // cate mutiple selection
  11663. NSString *category_id = params[@"category"];
  11664. NSMutableArray *cate_id_array = nil;
  11665. NSMutableString *cateWhere = [NSMutableString string];
  11666. if ([category_id isEqualToString:@""] || !category_id) {
  11667. [cateWhere appendString:@"1 = 1"];
  11668. } else {
  11669. if ([category_id containsString:@","]) {
  11670. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11671. } else {
  11672. cate_id_array = [@[category_id] mutableCopy];
  11673. }
  11674. [cateWhere appendString:@"("];
  11675. for (int i = 0; i < cate_id_array.count; i++) {
  11676. if (i == 0) {
  11677. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11678. } else {
  11679. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11680. }
  11681. }
  11682. [cateWhere appendString:@")"];
  11683. }
  11684. // where bestseller > 0 order by bestseller desc
  11685. // sql query: alert availability(int) best_seller(int) price qty
  11686. 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];
  11687. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11688. }
  11689. DebugLog(@"offline category where: %@",where);
  11690. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11691. if (!user) {
  11692. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11693. }
  11694. [ret setValue:filter forKey:@"filter"];
  11695. DebugLog(@"offline_category sql:%@",sqlQuery);
  11696. sqlite3_stmt * statement;
  11697. [ret setValue:@"2" forKey:@"result"];
  11698. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11699. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11700. // int count=0;
  11701. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11702. {
  11703. int i=0;
  11704. while (sqlite3_step(statement) == SQLITE_ROW)
  11705. {
  11706. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11707. char *name = (char*)sqlite3_column_text(statement, 0);
  11708. if(name==nil)
  11709. name="";
  11710. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11711. char *description = (char*)sqlite3_column_text(statement, 1);
  11712. if(description==nil)
  11713. description="";
  11714. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11715. int product_id = sqlite3_column_int(statement, 2);
  11716. int wid = sqlite3_column_int(statement, 3);
  11717. int closeout = sqlite3_column_int(statement, 4);
  11718. int cid = sqlite3_column_int(statement, 5);
  11719. int wisdelete = sqlite3_column_int(statement, 6);
  11720. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11721. int more_color = sqlite3_column_int(statement, 8);
  11722. // Defaul Category ID
  11723. __block NSString *categoryID = nil;
  11724. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11725. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11726. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11727. if(default_category==nil)
  11728. default_category="";
  11729. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11730. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11731. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11732. categoryID = nsdefault_category;
  11733. }];
  11734. if (!categoryID.length) {
  11735. NSString *cateIDs = params[@"category"];
  11736. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11737. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11738. for (NSString *cateID in requestCategoryArr) {
  11739. BOOL needBreak = NO;
  11740. for (NSString *itemCateIDBox in itemCategoryArr) {
  11741. if (itemCateIDBox.length > 4) {
  11742. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11743. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11744. if ([itemCategoryID isEqualToString:cateID]) {
  11745. needBreak = YES;
  11746. categoryID = itemCategoryID;
  11747. break;
  11748. }
  11749. }
  11750. }
  11751. if (needBreak) {
  11752. break;
  11753. }
  11754. }
  11755. }
  11756. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11757. if(wid !=0 && wisdelete != 1)
  11758. [item setValue:@"true" forKey:@"wish_exists"];
  11759. else
  11760. [item setValue:@"false" forKey:@"wish_exists"];
  11761. if(closeout==0)
  11762. [item setValue:@"false" forKey:@"is_closeout"];
  11763. else
  11764. [item setValue:@"true" forKey:@"is_closeout"];
  11765. if(cid==0)
  11766. [item setValue:@"false" forKey:@"cart_exists"];
  11767. else
  11768. [item setValue:@"true" forKey:@"cart_exists"];
  11769. if (more_color == 0) {
  11770. [item setObject:@(false) forKey:@"more_color"];
  11771. } else if (more_color == 1) {
  11772. [item setObject:@(true) forKey:@"more_color"];
  11773. }
  11774. [item addEntriesFromDictionary:imgjson];
  11775. // [item setValue:nsurl forKey:@"img"];
  11776. [item setValue:nsname forKey:@"name"];
  11777. [item setValue:nsdescription forKey:@"description"];
  11778. if (categoryID) {
  11779. [item setValue:categoryID forKey:@"item_category_id"];
  11780. }
  11781. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11782. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11783. i++;
  11784. }
  11785. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11786. [ret setObject:items forKey:@"items"];
  11787. sqlite3_finalize(statement);
  11788. } else {
  11789. DebugLog(@"nothing...");
  11790. }
  11791. DebugLog(@"count:%d",count);
  11792. [iSalesDB close_db:db];
  11793. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11794. dispatch_async(dispatch_get_main_queue(), ^{
  11795. // UIApplication * app = [UIApplication sharedApplication];
  11796. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11797. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11798. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11799. result(ret);
  11800. });
  11801. });
  11802. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11803. }
  11804. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11805. {
  11806. [self categoryList:params limited:YES completionHandler:result];
  11807. }
  11808. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11809. {
  11810. UIApplication * app = [UIApplication sharedApplication];
  11811. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11812. NSString* order_code = appDelegate.order_code;
  11813. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11814. NSString* keyword = [params valueForKey:@"keyword"];
  11815. keyword=keyword.lowercaseString;
  11816. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11817. int limit = [[params valueForKey:@"limit"] intValue];
  11818. int offset = [[params valueForKey:@"offset"] intValue];
  11819. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11820. NSString *limit_str = @"";
  11821. if (limited) {
  11822. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11823. }
  11824. sqlite3 *db = [iSalesDB get_db];
  11825. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11826. NSString *sqlQuery = nil;
  11827. if(exactMatch )
  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'%@%%' 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 ;
  11829. else
  11830. 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
  11831. DebugLog(@"offline_search sql:%@",sqlQuery);
  11832. sqlite3_stmt * statement;
  11833. [ret setValue:@"2" forKey:@"result"];
  11834. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11835. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11836. // int count=0;
  11837. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11838. {
  11839. int i=0;
  11840. while (sqlite3_step(statement) == SQLITE_ROW)
  11841. {
  11842. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11843. // char *name = (char*)sqlite3_column_text(statement, 1);
  11844. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11845. char *name = (char*)sqlite3_column_text(statement, 0);
  11846. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11847. char *description = (char*)sqlite3_column_text(statement, 1);
  11848. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11849. int product_id = sqlite3_column_int(statement, 2);
  11850. // char *url = (char*)sqlite3_column_text(statement, 3);
  11851. // if(url==nil)
  11852. // url="";
  11853. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11854. int wid = sqlite3_column_int(statement, 3);
  11855. int closeout = sqlite3_column_int(statement, 4);
  11856. int cid = sqlite3_column_int(statement, 5);
  11857. int wisdelete = sqlite3_column_int(statement, 6);
  11858. int more_color = sqlite3_column_int(statement, 7);
  11859. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11860. if(wid !=0 && wisdelete != 1)
  11861. [item setValue:@"true" forKey:@"wish_exists"];
  11862. else
  11863. [item setValue:@"false" forKey:@"wish_exists"];
  11864. if(closeout==0)
  11865. [item setValue:@"false" forKey:@"is_closeout"];
  11866. else
  11867. [item setValue:@"true" forKey:@"is_closeout"];
  11868. if(cid==0)
  11869. [item setValue:@"false" forKey:@"cart_exists"];
  11870. else
  11871. [item setValue:@"true" forKey:@"cart_exists"];
  11872. if (more_color == 0) {
  11873. [item setObject:@(false) forKey:@"more_color"];
  11874. } else if (more_color == 1) {
  11875. [item setObject:@(true) forKey:@"more_color"];
  11876. }
  11877. [item addEntriesFromDictionary:imgjson];
  11878. // [item setValue:nsurl forKey:@"img"];
  11879. [item setValue:nsname forKey:@"fash_name"];
  11880. [item setValue:nsdescription forKey:@"description"];
  11881. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11882. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11883. i++;
  11884. }
  11885. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11886. [ret setObject:items forKey:@"items"];
  11887. sqlite3_finalize(statement);
  11888. }
  11889. DebugLog(@"count:%d",count);
  11890. [iSalesDB close_db:db];
  11891. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11892. dispatch_async(dispatch_get_main_queue(), ^{
  11893. result(ret);
  11894. });
  11895. });
  11896. }
  11897. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11898. {
  11899. [self search:params limited:YES completionHandler:result];
  11900. }
  11901. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11902. //{
  11903. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11904. // NSString* order_code = appDelegate.order_code;
  11905. // NSString* user = appDelegate.user;
  11906. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11907. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11908. //
  11909. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11910. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11911. //
  11912. // // category
  11913. // NSDictionary *category_menu = [self offline_category_menu];
  11914. // [filter setValue:category_menu forKey:@"category"];
  11915. //
  11916. // NSString* where= nil;
  11917. // NSString* orderby= @"m.name";
  11918. // if (!filterSearch) {
  11919. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11920. //
  11921. // switch (covertype) {
  11922. // case 0:
  11923. // {
  11924. // where=@"m.category like'%%#005#%%'";
  11925. // break;
  11926. // }
  11927. // case 1:
  11928. // {
  11929. // where=@"m.alert like '%QS%'";
  11930. // break;
  11931. // }
  11932. // case 2:
  11933. // {
  11934. // where=@"m.availability>0";
  11935. // break;
  11936. // }
  11937. // case 3:
  11938. // {
  11939. // where=@"m.best_seller>0";
  11940. // orderby=@"m.best_seller desc,m.name asc";
  11941. // break;
  11942. // }
  11943. // default:
  11944. // where=@"1=1";
  11945. // break;
  11946. // }
  11947. //
  11948. // }
  11949. //
  11950. //
  11951. // int limit = [[params valueForKey:@"limit"] intValue];
  11952. // int offset = [[params valueForKey:@"offset"] intValue];
  11953. //
  11954. // NSString *limit_str = @"";
  11955. // if (limited) {
  11956. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11957. // }
  11958. //
  11959. //
  11960. // sqlite3 *db = [iSalesDB get_db];
  11961. // // [iSalesDB AddExFunction:db];
  11962. //
  11963. // int count;
  11964. //
  11965. // NSString *sqlQuery = nil;
  11966. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11967. // 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];
  11968. //
  11969. //
  11970. // double price_min = 0;
  11971. // double price_max = 0;
  11972. // if (filterSearch) {
  11973. // // alert
  11974. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11975. // NSString *alert = params[@"alert"];
  11976. // if ([alert isEqualToString:@"Display All"]) {
  11977. // alert = [NSString stringWithFormat:@""];
  11978. // } else {
  11979. // alert = [self translateSingleQuote:alert];
  11980. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11981. // }
  11982. //
  11983. // // available
  11984. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11985. // NSString *available = params[@"available"];
  11986. // NSString *available_condition;
  11987. // if ([available isEqualToString:@"Display All"]) {
  11988. // available_condition = @"";
  11989. // } else if ([available isEqualToString:@"Available Now"]) {
  11990. // available_condition = @"and availability > 0";
  11991. // } else {
  11992. // available_condition = @"and availability == 0";
  11993. // }
  11994. //
  11995. // // best seller
  11996. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11997. // NSString *best_seller = @"";
  11998. // NSString *order_best_seller = @"m.name asc";
  11999. //
  12000. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12001. // best_seller = @"and best_seller > 0";
  12002. // order_best_seller = @"m.best_seller desc,m.name asc";
  12003. // }
  12004. //
  12005. // // price
  12006. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12007. // NSString *price = params[@"price"];
  12008. // price_min = 0;
  12009. // price_max = MAXFLOAT;
  12010. // if (user) {
  12011. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12012. // NSMutableString *priceName = [NSMutableString string];
  12013. // for (int i = 0; i < priceTypeArray.count; i++) {
  12014. // NSString *pricetype = priceTypeArray[i];
  12015. // pricetype = [self translateSingleQuote:pricetype];
  12016. // if (i == 0) {
  12017. // [priceName appendFormat:@"'%@'",pricetype];
  12018. // } else {
  12019. // [priceName appendFormat:@",'%@'",pricetype];
  12020. // }
  12021. // }
  12022. //
  12023. // if ([price isEqualToString:@"Display All"]) {
  12024. // price = [NSString stringWithFormat:@""];
  12025. // } else if([price containsString:@"+"]){
  12026. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12027. // price_min = [price doubleValue];
  12028. // 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];
  12029. // } else {
  12030. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12031. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12032. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12033. // 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];
  12034. // }
  12035. // } else {
  12036. // price = @"";
  12037. // }
  12038. //
  12039. // // sold_by_qty : Sold in quantities of %@
  12040. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12041. // NSString *qty = params[@"sold_by_qty"];
  12042. // if ([qty isEqualToString:@"Display All"]) {
  12043. // qty = @"";
  12044. // } else {
  12045. // qty = [self translateSingleQuote:qty];
  12046. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12047. // }
  12048. //
  12049. // // category
  12050. // NSString *category_id = params[@"ctgId"];
  12051. // NSMutableArray *cate_id_array = nil;
  12052. // NSMutableString *cateWhere = [NSMutableString string];
  12053. //
  12054. // if ([category_id isEqualToString:@""] || !category_id) {
  12055. // [cateWhere appendString:@"1 = 1"];
  12056. // } else {
  12057. //
  12058. // if ([category_id containsString:@","]) {
  12059. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12060. // } else {
  12061. // cate_id_array = [@[category_id] mutableCopy];
  12062. // }
  12063. // /*-----------*/
  12064. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12065. //
  12066. // [cateWhere appendString:@"("];
  12067. // for (int i = 0; i < cate_id_array.count; i++) {
  12068. //
  12069. // for (NSString *key0 in cateDic.allKeys) {
  12070. //
  12071. // if ([key0 containsString:@"category_"]) {
  12072. //
  12073. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12074. //
  12075. // for (NSString *key1 in category0.allKeys) {
  12076. //
  12077. // if ([key1 containsString:@"category_"]) {
  12078. //
  12079. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12080. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12081. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12082. //
  12083. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12084. // if (i == 0) {
  12085. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12086. // } else {
  12087. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12088. // }
  12089. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12090. // [category0 setValue:category1 forKey:key1];
  12091. // [cateDic setValue:category0 forKey:key0];
  12092. //
  12093. // }
  12094. //
  12095. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12096. //
  12097. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12098. //
  12099. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12100. // if (i == 0) {
  12101. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12102. // } else {
  12103. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12104. // }
  12105. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12106. // [cateDic setValue:category0 forKey:key0];
  12107. //
  12108. // }
  12109. //
  12110. // }
  12111. //
  12112. // }
  12113. //
  12114. // }
  12115. //
  12116. // }
  12117. //
  12118. // }
  12119. // [cateWhere appendString:@")"];
  12120. //
  12121. // [filter setValue:cateDic forKey:@"category"];
  12122. // }
  12123. //
  12124. // // where bestseller > 0 order by bestseller desc
  12125. // // sql query: alert availability(int) best_seller(int) price qty
  12126. //
  12127. // 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];
  12128. //
  12129. //
  12130. // // count
  12131. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12132. //
  12133. // }
  12134. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12135. //
  12136. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12137. //
  12138. //
  12139. //
  12140. //
  12141. // if (!user) {
  12142. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12143. // }
  12144. //
  12145. // [ret setValue:filter forKey:@"filter"];
  12146. //
  12147. //
  12148. //
  12149. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12150. //
  12151. // sqlite3_stmt * statement;
  12152. // [ret setValue:@"2" forKey:@"result"];
  12153. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12154. //
  12155. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12156. // // int count=0;
  12157. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12158. // {
  12159. //
  12160. // int i=0;
  12161. // while (sqlite3_step(statement) == SQLITE_ROW)
  12162. // {
  12163. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12164. //
  12165. //
  12166. // char *name = (char*)sqlite3_column_text(statement, 0);
  12167. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12168. //
  12169. // char *description = (char*)sqlite3_column_text(statement, 1);
  12170. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12171. //
  12172. //
  12173. //
  12174. // int product_id = sqlite3_column_int(statement, 2);
  12175. //
  12176. //
  12177. // int wid = sqlite3_column_int(statement, 3);
  12178. // int closeout = sqlite3_column_int(statement, 4);
  12179. // int cid = sqlite3_column_int(statement, 5);
  12180. // int wisdelete = sqlite3_column_int(statement, 6);
  12181. // int more_color = sqlite3_column_int(statement, 7);
  12182. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12183. //
  12184. // if(wid !=0 && wisdelete != 1)
  12185. // [item setValue:@"true" forKey:@"wish_exists"];
  12186. // else
  12187. // [item setValue:@"false" forKey:@"wish_exists"];
  12188. //
  12189. // if(closeout==0)
  12190. // [item setValue:@"false" forKey:@"is_closeout"];
  12191. // else
  12192. // [item setValue:@"true" forKey:@"is_closeout"];
  12193. //
  12194. // if(cid==0)
  12195. // [item setValue:@"false" forKey:@"cart_exists"];
  12196. // else
  12197. // [item setValue:@"true" forKey:@"cart_exists"];
  12198. //
  12199. // if (more_color == 0) {
  12200. // [item setObject:@(false) forKey:@"more_color"];
  12201. // } else if (more_color == 1) {
  12202. // [item setObject:@(true) forKey:@"more_color"];
  12203. // }
  12204. //
  12205. // [item addEntriesFromDictionary:imgjson];
  12206. //
  12207. //
  12208. // // [item setValue:nsurl forKey:@"img"];
  12209. // [item setValue:nsname forKey:@"fash_name"];
  12210. // [item setValue:nsdescription forKey:@"description"];
  12211. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12212. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12213. // i++;
  12214. // }
  12215. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12216. // [ret setObject:items forKey:@"items"];
  12217. // sqlite3_finalize(statement);
  12218. // }
  12219. //
  12220. // [iSalesDB close_db:db];
  12221. //
  12222. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12223. //
  12224. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12225. // dispatch_async(dispatch_get_main_queue(), ^{
  12226. //
  12227. // result(ret);
  12228. // });
  12229. //
  12230. // });
  12231. //
  12232. //
  12233. //
  12234. //}
  12235. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12236. //{
  12237. // [self itemsearch:params limited:YES completionHandler:result];
  12238. //}
  12239. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12240. {
  12241. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12242. NSString* offline_command=params[@"offline_Command"];
  12243. NSMutableDictionary* ret=nil;
  12244. if([offline_command isEqualToString:@"model_NIYMAL"])
  12245. {
  12246. NSString* category = params[@"category"];
  12247. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12248. }
  12249. dispatch_async(dispatch_get_main_queue(), ^{
  12250. result(ret);
  12251. });
  12252. });
  12253. }
  12254. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12255. {
  12256. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12257. int sort = [[params objectForKey:@"sort"] intValue];
  12258. NSString *sort_str = @"";
  12259. switch (sort) {
  12260. case 0:{
  12261. sort_str = @"order by w.modify_time desc";
  12262. }
  12263. break;
  12264. case 1:{
  12265. sort_str = @"order by w.modify_time asc";
  12266. }
  12267. break;
  12268. case 2:{
  12269. sort_str = @"order by m.name asc";
  12270. }
  12271. break;
  12272. case 3:{
  12273. sort_str = @"order by m.name desc";
  12274. }
  12275. break;
  12276. case 4:{
  12277. sort_str = @"order by m.description asc";
  12278. }
  12279. break;
  12280. default:
  12281. break;
  12282. }
  12283. // NSString* user = appDelegate.user;
  12284. sqlite3 *db = [iSalesDB get_db];
  12285. // order by w.create_time
  12286. 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];
  12287. // 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];
  12288. sqlite3_stmt * statement;
  12289. NSDate *date1 = [NSDate date];
  12290. // NSDate *date2 = nil;
  12291. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12292. int count=0;
  12293. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12294. {
  12295. while (sqlite3_step(statement) == SQLITE_ROW)
  12296. {
  12297. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12298. int product_id = sqlite3_column_double(statement, 0);
  12299. char *description = (char*)sqlite3_column_text(statement, 1);
  12300. if(description==nil)
  12301. description= "";
  12302. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12303. int item_id = sqlite3_column_double(statement, 2);
  12304. NSDate *date_image = [NSDate date];
  12305. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12306. printf("image : ");
  12307. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12308. // char *url = (char*)sqlite3_column_text(statement, 3);
  12309. // if(url==nil)
  12310. // url="";
  12311. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12312. int qty = sqlite3_column_int(statement, 3);
  12313. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12314. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12315. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12316. item[@"description"]= nsdescription;
  12317. item[@"img"]= nsurl;
  12318. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12319. count++;
  12320. }
  12321. printf("total time:");
  12322. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12323. ret[@"count"]= [NSNumber numberWithInt:count];
  12324. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12325. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12326. ret[@"result"]= [NSNumber numberWithInt:2];
  12327. sqlite3_finalize(statement);
  12328. }
  12329. [iSalesDB close_db:db];
  12330. dispatch_async(dispatch_get_main_queue(), ^{
  12331. UIApplication * app = [UIApplication sharedApplication];
  12332. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12333. appDelegate.wish_count =count;
  12334. [appDelegate update_count_mark];
  12335. result(ret);
  12336. });
  12337. });
  12338. }
  12339. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12340. {
  12341. // UIApplication * app = [UIApplication sharedApplication];
  12342. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12343. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12344. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12345. sqlite3 *db = [iSalesDB get_db];
  12346. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12347. NSString* product_id=params[@"product_id"];
  12348. NSString *item_count_str = params[@"item_count"];
  12349. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12350. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12351. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12352. // NSString *sql = @"";
  12353. for(int i=0;i<arr.count;i++)
  12354. {
  12355. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12356. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12357. __block int cart_count = 0;
  12358. if (!item_count_str) {
  12359. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12360. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12361. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12362. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12363. cart_count = [[model_set_components lastObject] intValue];
  12364. }];
  12365. }
  12366. if(count==0)
  12367. {
  12368. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12369. sqlite3_stmt *stmt;
  12370. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12371. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12372. if (item_count_arr) {
  12373. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12374. } else {
  12375. sqlite3_bind_int(stmt,2,cart_count);
  12376. }
  12377. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12378. [iSalesDB execSql:@"ROLLBACK" db:db];
  12379. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12380. [iSalesDB close_db:db];
  12381. DebugLog(@"add to wishlist error");
  12382. dispatch_async(dispatch_get_main_queue(), ^{
  12383. result(ret);
  12384. });
  12385. }
  12386. } else {
  12387. int qty = 0;
  12388. if (item_count_arr) {
  12389. qty = [item_count_arr[i] intValue];
  12390. } else {
  12391. qty = cart_count;
  12392. }
  12393. 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]];
  12394. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12395. [iSalesDB execSql:@"ROLLBACK" db:db];
  12396. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12397. [iSalesDB close_db:db];
  12398. DebugLog(@"add to wishlist error");
  12399. dispatch_async(dispatch_get_main_queue(), ^{
  12400. result(ret);
  12401. });
  12402. }
  12403. }
  12404. }
  12405. // [iSalesDB execSql:sql db:db];
  12406. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12407. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12408. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12409. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12410. [iSalesDB close_db:db];
  12411. ret[@"result"]= [NSNumber numberWithInt:2];
  12412. dispatch_async(dispatch_get_main_queue(), ^{
  12413. UIApplication * app = [UIApplication sharedApplication];
  12414. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12415. appDelegate.wish_count =count;
  12416. [appDelegate update_count_mark];
  12417. result(ret);
  12418. });
  12419. });
  12420. }
  12421. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12422. {
  12423. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12424. bool bLogin = appDelegate.bLogin;
  12425. __block NSString* contact_id = appDelegate.contact_id;
  12426. __block NSString* user = appDelegate.user;
  12427. __block NSString* order_code = appDelegate.order_code;
  12428. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12429. NSString* model_name = [params valueForKey:@"product_name"];
  12430. NSString* product_id = [params valueForKey:@"product_id"];
  12431. NSString* upc_code = [params valueForKey:@"upc_code"];
  12432. NSString* category = [params valueForKey:@"category"];
  12433. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12434. if(category==nil) {
  12435. category = default_category_id;
  12436. } else {
  12437. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12438. // 参数重有多个category id
  12439. if (arr_0.count > 1) {
  12440. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12441. // 取交集
  12442. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12443. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12444. [set_0 intersectSet:set_1];
  12445. if (set_0.count == 1) {
  12446. category = (NSString *)[set_0 anyObject];
  12447. } else {
  12448. if ([set_0 containsObject:default_category_id]) {
  12449. category = default_category_id;
  12450. } else {
  12451. category = (NSString *)[set_0 anyObject];
  12452. }
  12453. }
  12454. }
  12455. }
  12456. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12457. sqlite3 *db = [iSalesDB get_db];
  12458. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12459. NSString *sqlQuery = nil;
  12460. if(product_id==nil)
  12461. if(model_name==nil)
  12462. {
  12463. 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='%@';
  12464. }
  12465. else
  12466. {
  12467. 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='%@';
  12468. }
  12469. else
  12470. 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=%@;
  12471. sqlite3_stmt * statement;
  12472. [ret setValue:@"2" forKey:@"result"];
  12473. [ret setValue:@"3" forKey:@"detail_section_count"];
  12474. // int count=0;
  12475. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12476. {
  12477. if (sqlite3_step(statement) == SQLITE_ROW)
  12478. {
  12479. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12480. char *name = (char*)sqlite3_column_text(statement, 0);
  12481. if(name==nil)
  12482. name="";
  12483. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12484. char *description = (char*)sqlite3_column_text(statement, 1);
  12485. if(description==nil)
  12486. description="";
  12487. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12488. int product_id = sqlite3_column_int(statement, 2);
  12489. char *color = (char*)sqlite3_column_text(statement, 3);
  12490. if(color==nil)
  12491. color="";
  12492. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12493. //
  12494. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12495. // if(legcolor==nil)
  12496. // legcolor="";
  12497. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12498. //
  12499. //
  12500. int availability = sqlite3_column_int(statement, 5);
  12501. //
  12502. int incoming_stock = sqlite3_column_int(statement, 6);
  12503. char *demension = (char*)sqlite3_column_text(statement, 7);
  12504. if(demension==nil)
  12505. demension="";
  12506. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12507. // ,,,,,,,,,
  12508. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12509. if(seat_height==nil)
  12510. seat_height="";
  12511. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12512. char *material = (char*)sqlite3_column_text(statement, 9);
  12513. if(material==nil)
  12514. material="";
  12515. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12516. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12517. if(box_dim==nil)
  12518. box_dim="";
  12519. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12520. char *volume = (char*)sqlite3_column_text(statement, 11);
  12521. if(volume==nil)
  12522. volume="";
  12523. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12524. double weight = sqlite3_column_double(statement, 12);
  12525. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12526. if(model_set==nil)
  12527. model_set="";
  12528. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12529. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12530. if(load_ability==nil)
  12531. load_ability="";
  12532. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12533. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12534. // if(default_category==nil)
  12535. // default_category="";
  12536. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12537. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12538. if(fabric_content==nil)
  12539. fabric_content="";
  12540. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12541. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12542. if(assembling==nil)
  12543. assembling="";
  12544. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12545. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12546. if(made_in==nil)
  12547. made_in="";
  12548. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12549. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12550. if(special_remarks==nil)
  12551. special_remarks="";
  12552. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12553. int stockUcom = sqlite3_column_double(statement, 20);
  12554. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12555. if(product_group==nil)
  12556. product_group="";
  12557. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12558. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12559. // if(fashion_selector==nil)
  12560. // fashion_selector="";
  12561. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12562. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12563. if(selector_field==nil)
  12564. selector_field="";
  12565. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12566. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12567. if(property_field==nil)
  12568. property_field="";
  12569. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12570. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12571. if(packaging==nil)
  12572. packaging="";
  12573. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12574. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12575. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12576. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12577. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12578. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12579. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12580. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12581. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12582. NSString* Availability=nil;
  12583. if(availability>0)
  12584. Availability=[NSString stringWithFormat:@"%d",availability];
  12585. else
  12586. Availability = @"Out of Stock";
  12587. [img_section setValue:Availability forKey:@"Availability"];
  12588. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12589. char *eta = (char*)sqlite3_column_text(statement, 25);
  12590. if(eta==nil)
  12591. eta="";
  12592. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12593. if ([nseta isEqualToString:@"null"]) {
  12594. nseta = @"";
  12595. }
  12596. if (availability <= 0) {
  12597. [img_section setValue:nseta forKey:@"ETA"];
  12598. }
  12599. int item_id = sqlite3_column_int(statement, 26);
  12600. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12601. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12602. NSString* Price=nil;
  12603. if(bLogin==false)
  12604. Price=@"Must Sign in.";
  12605. else
  12606. {
  12607. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12608. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12609. if(price==nil)
  12610. Price=@"No Price.";
  12611. else
  12612. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12613. }
  12614. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12615. [img_section setValue:Price forKey:@"price"];
  12616. [img_section setValue:nsname forKey:@"model_name"];
  12617. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12618. if (order_code) { // 离线order code即so#
  12619. 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];
  12620. __block int cartQTY = 0;
  12621. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12622. cartQTY = sqlite3_column_int(stmt, 0);
  12623. }];
  12624. if (cartQTY > 0) {
  12625. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12626. }
  12627. }
  12628. [ret setObject:img_section forKey:@"img_section"];
  12629. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12630. int detail0_item_count=0;
  12631. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12632. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12633. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12634. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12635. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12636. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12637. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12638. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12639. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12640. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12641. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12642. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12643. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12644. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12645. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12646. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12647. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12648. {
  12649. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12650. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12651. }
  12652. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12653. [detail0_section setValue:@"kv" forKey:@"type"];
  12654. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12655. [ret setObject:detail0_section forKey:@"detail_0"];
  12656. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12657. // [detail1_section setValue:@"detail" forKey:@"target"];
  12658. // [detail1_section setValue:@"popup" forKey:@"action"];
  12659. // [detail1_section setValue:@"content" forKey:@"type"];
  12660. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12661. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12662. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12663. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12664. [ret setObject:detail1_section forKey:@"detail_1"];
  12665. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12666. [detail2_section setValue:@"detail" forKey:@"target"];
  12667. [detail2_section setValue:@"popup" forKey:@"action"];
  12668. [detail2_section setValue:@"content" forKey:@"type"];
  12669. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12670. [detail2_section setValue:@"true" forKey:@"single_row"];
  12671. [detail2_section setValue:@"local" forKey:@"data"];
  12672. [ret setObject:detail2_section forKey:@"detail_2"];
  12673. }
  12674. sqlite3_finalize(statement);
  12675. }
  12676. else
  12677. {
  12678. [ret setValue:@"8" forKey:@"result"];
  12679. }
  12680. // DebugLog(@"count:%d",count);
  12681. [iSalesDB close_db:db];
  12682. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12683. dispatch_async(dispatch_get_main_queue(), ^{
  12684. result(ret);
  12685. });
  12686. });
  12687. }
  12688. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12689. //{
  12690. //
  12691. // UIApplication * app = [UIApplication sharedApplication];
  12692. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12693. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12694. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12695. // sqlite3 *db = [iSalesDB get_db];
  12696. // NSString* collectId=params[@"collectId"];
  12697. //
  12698. //
  12699. //
  12700. //
  12701. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12702. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12703. // [iSalesDB execSql:sqlQuery db:db];
  12704. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12705. // [iSalesDB close_db:db];
  12706. //
  12707. //
  12708. // ret[@"result"]= [NSNumber numberWithInt:2];
  12709. // dispatch_async(dispatch_get_main_queue(), ^{
  12710. //
  12711. // appDelegate.wish_count =count;
  12712. //
  12713. // [appDelegate update_count_mark];
  12714. // result(ret);
  12715. // });
  12716. //
  12717. // });
  12718. //}
  12719. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12720. {
  12721. UIApplication * app = [UIApplication sharedApplication];
  12722. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12723. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12724. [iSalesDB disable_trigger];
  12725. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12726. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12727. [iSalesDB enable_trigger];
  12728. dispatch_async(dispatch_get_main_queue(), ^{
  12729. //
  12730. // NSString* user = [params valueForKey:@"user"];
  12731. //
  12732. // NSString* password = [params valueForKey:@"password"];
  12733. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12734. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12735. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12736. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12737. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12738. [appDelegate update_count_mark];
  12739. appDelegate.can_show_price =false;
  12740. appDelegate.can_see_price =false;
  12741. appDelegate.can_create_portfolio =false;
  12742. appDelegate.can_create_order =false;
  12743. appDelegate.can_cancel_order =false;
  12744. appDelegate.can_set_cart_price =false;
  12745. appDelegate.can_delete_order =false;
  12746. appDelegate.can_submit_order =false;
  12747. appDelegate.can_set_tearsheet_price =false;
  12748. appDelegate.can_update_contact_info = false;
  12749. appDelegate.save_order_logout = false;
  12750. appDelegate.submit_order_logout = false;
  12751. appDelegate.alert_sold_in_quantities = false;
  12752. appDelegate.ipad_perm =nil ;
  12753. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12754. appDelegate.OrderFilter= nil;
  12755. [appDelegate SetSo:nil];
  12756. [appDelegate set_main_button_panel];
  12757. result(ret);
  12758. });
  12759. });
  12760. }
  12761. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12762. {
  12763. // iSalesDB.
  12764. // UIApplication * app = [UIApplication sharedApplication];
  12765. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12766. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12767. [iSalesDB disable_trigger];
  12768. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12769. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12770. [iSalesDB enable_trigger];
  12771. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12772. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12773. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12774. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12775. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12776. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12777. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12778. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12779. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12780. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12781. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12782. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12783. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12784. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12785. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12786. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12787. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12788. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12789. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12790. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12791. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12792. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12793. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12794. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12795. [arr_name addObject:customer_first_name];
  12796. [arr_name addObject:customer_last_name];
  12797. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12798. // default ship from
  12799. 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';";
  12800. __block NSString *cid = @"";
  12801. __block NSString *name = @"";
  12802. __block NSString *ext = @"";
  12803. __block NSString *contact = @"";
  12804. __block NSString *email = @"";
  12805. __block NSString *fax = @"";
  12806. __block NSString *phone = @"";
  12807. sqlite3 *db = [iSalesDB get_db];
  12808. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12809. cid = [self textAtColumn:0 statement:statment];
  12810. name = [self textAtColumn:1 statement:statment];
  12811. ext = [self textAtColumn:2 statement:statment];
  12812. contact = [self textAtColumn:3 statement:statment];
  12813. email = [self textAtColumn:4 statement:statment];
  12814. fax = [self textAtColumn:5 statement:statment];
  12815. phone = [self textAtColumn:6 statement:statment];
  12816. }];
  12817. NSString* so_id = [self get_offline_soid:db];
  12818. if(so_id==nil)
  12819. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12820. 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];
  12821. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12822. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12823. //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'
  12824. //soId
  12825. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12826. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12827. sqlite3_stmt * statement;
  12828. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12829. {
  12830. if (sqlite3_step(statement) == SQLITE_ROW)
  12831. {
  12832. // char *name = (char*)sqlite3_column_text(statement, 1);
  12833. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12834. //ret = sqlite3_column_int(statement, 0);
  12835. char *soId = (char*)sqlite3_column_text(statement, 0);
  12836. if(soId==nil)
  12837. soId="";
  12838. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12839. [ret setValue:nssoId forKey:@"soId"];
  12840. [ret setValue:nssoId forKey:@"orderCode"];
  12841. }
  12842. sqlite3_finalize(statement);
  12843. }
  12844. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12845. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12846. [iSalesDB close_db:db];
  12847. dispatch_async(dispatch_get_main_queue(), ^{
  12848. result(ret);
  12849. });
  12850. });
  12851. }
  12852. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12853. {
  12854. UIApplication * app = [UIApplication sharedApplication];
  12855. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12856. bool can_create_backorder= appDelegate.can_create_backorder;
  12857. NSString* user = appDelegate.user;
  12858. NSString* contact_id = appDelegate.contact_id;
  12859. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12860. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12861. sqlite3 *db = [iSalesDB get_db];
  12862. NSString* orderCode=params[@"orderCode"];
  12863. int sort = [[params objectForKey:@"sort"] intValue];
  12864. NSString *sort_str = @"";
  12865. switch (sort) {
  12866. case 0:{
  12867. sort_str = @"order by c.modify_time desc";
  12868. }
  12869. break;
  12870. case 1:{
  12871. sort_str = @"order by c.modify_time asc";
  12872. }
  12873. break;
  12874. case 2:{
  12875. sort_str = @"order by m.name asc";
  12876. }
  12877. break;
  12878. case 3:{
  12879. sort_str = @"order by m.name desc";
  12880. }
  12881. break;
  12882. case 4:{
  12883. sort_str = @"order by m.description asc";
  12884. }
  12885. break;
  12886. default:
  12887. break;
  12888. }
  12889. 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 ];
  12890. // 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 ];
  12891. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12892. DebugLog(@"offline_login sql:%@",sqlQuery);
  12893. sqlite3_stmt * statement;
  12894. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12895. NSDate *date1 = [NSDate date];
  12896. int count=0;
  12897. int cart_count=0;
  12898. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12899. if ( dbresult== SQLITE_OK)
  12900. {
  12901. while (sqlite3_step(statement) == SQLITE_ROW)
  12902. {
  12903. // NSDate *row_date = [NSDate date];
  12904. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12905. int product_id = sqlite3_column_int(statement, 0);
  12906. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12907. int item_id = sqlite3_column_int(statement, 7);
  12908. NSString* Price=nil;
  12909. if(str_price==nil)
  12910. {
  12911. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12912. // NSDate *price_date = [NSDate date];
  12913. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12914. // DebugLog(@"price time interval");
  12915. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12916. if(price==nil)
  12917. Price=@"No Price.";
  12918. else
  12919. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12920. }
  12921. else
  12922. {
  12923. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12924. }
  12925. double discount = sqlite3_column_double(statement, 2);
  12926. int item_count = sqlite3_column_int(statement, 3);
  12927. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12928. NSString *nsline_note=nil;
  12929. if(line_note!=nil)
  12930. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12931. char *name = (char*)sqlite3_column_text(statement, 5);
  12932. NSString *nsname=nil;
  12933. if(name!=nil)
  12934. nsname= [[NSString alloc]initWithUTF8String:name];
  12935. char *description = (char*)sqlite3_column_text(statement, 6);
  12936. NSString *nsdescription=nil;
  12937. if(description!=nil)
  12938. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12939. int stockUom = sqlite3_column_int(statement, 8);
  12940. int _id = sqlite3_column_int(statement, 9);
  12941. int availability = sqlite3_column_int(statement, 10);
  12942. // NSDate *subtotal_date = [NSDate date];
  12943. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12944. // DebugLog(@"subtotal_date time interval");
  12945. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12946. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12947. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12948. int carton=[bsubtotaljson[@"carton"] intValue];
  12949. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12950. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12951. // NSDate *img_date = [NSDate date];
  12952. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12953. // DebugLog(@"img_date time interval");
  12954. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12955. itemjson[@"model"]=nsname;
  12956. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12957. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12958. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12959. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12960. itemjson[@"check"]=@"true";
  12961. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12962. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12963. itemjson[@"unit_price"]=Price;
  12964. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12965. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12966. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12967. itemjson[@"note"]=nsline_note;
  12968. if (!can_create_backorder) {
  12969. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12970. }
  12971. // NSDate *date2 = [NSDate date];
  12972. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12973. // DebugLog(@"model_bundle time interval");
  12974. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12975. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12976. count++;
  12977. // DebugLog(@"row time interval");
  12978. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12979. }
  12980. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12981. sqlite3_finalize(statement);
  12982. }
  12983. DebugLog(@"request cart total time interval");
  12984. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12985. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12986. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12987. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12988. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12989. ret[@"count"]=[NSNumber numberWithInt:count ];
  12990. ret[@"mode"]=@"Regular Mode";
  12991. [iSalesDB close_db:db];
  12992. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12993. DebugLog(@"general notes :%@",general_note);
  12994. ret[@"general_note"]= general_note;
  12995. dispatch_async(dispatch_get_main_queue(), ^{
  12996. result(ret);
  12997. });
  12998. });
  12999. }
  13000. @end