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. [RAUtils dicttofile:cartpath dict:cart];
  2445. }
  2446. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2447. {
  2448. assert(add_params[@"user"]!=nil);
  2449. // assert(add_params[@"contact_id"]!=nil);
  2450. // assert(add_params[@"password"]!=nil);
  2451. NSString* serial= [[NSUUID UUID] UUIDString];
  2452. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2453. NSString *cachefolder = [paths objectAtIndex:0];
  2454. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2455. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2456. NSFileManager* fileManager = [NSFileManager defaultManager];
  2457. BOOL bdir=YES;
  2458. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2459. {
  2460. NSError *error = nil;
  2461. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2462. if(!bsuccess)
  2463. DebugLog(@"Create UPLOAD folder failed");
  2464. }
  2465. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2466. ret[@"contact"]=[self prepareContact:serial];
  2467. ret[@"wishlist"]=[self prepareWishlist:serial];
  2468. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2469. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2470. ret[@"view_portfolio"] = [self preparePDF:serial];
  2471. NSString* str= [RAConvertor dict2string:ret];
  2472. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2473. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2474. NSError *error=nil;
  2475. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2476. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2477. ZipArchive* zip = [[ZipArchive alloc] init];
  2478. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2479. for(int i=0;i<arr_files.count;i++)
  2480. {
  2481. NSString* file=arr_files[i];
  2482. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2483. }
  2484. if( ![zip CloseZipFile2] )
  2485. {
  2486. zippath = @"";
  2487. }
  2488. ret[@"file"]=zippath;
  2489. return ret;
  2490. }
  2491. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2492. {
  2493. if(filename.length==0)
  2494. return false;
  2495. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2496. bool ret=false;
  2497. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2498. NSString *cachefolder = [paths objectAtIndex:0];
  2499. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2500. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2501. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2502. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2503. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2504. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2505. NSFileManager* fileManager = [NSFileManager defaultManager];
  2506. BOOL bdir=NO;
  2507. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2508. {
  2509. NSError *error = nil;
  2510. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2511. {
  2512. ret=false;
  2513. }
  2514. else
  2515. {
  2516. ret=true;
  2517. }
  2518. }
  2519. return ret;
  2520. }
  2521. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2522. {
  2523. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2524. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2525. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2526. NSString* where=@" 1=1";
  2527. if (ver!=nil) {
  2528. where=@"is_dirty=1";
  2529. }
  2530. sqlite3 *db = [iSalesDB get_db];
  2531. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2532. sqlite3_stmt * statement;
  2533. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2534. int count=0;
  2535. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2536. if ( dbresult== SQLITE_OK)
  2537. {
  2538. while (sqlite3_step(statement) == SQLITE_ROW)
  2539. {
  2540. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2541. int _id = sqlite3_column_int(statement, 0);
  2542. int product_id = sqlite3_column_int(statement, 1);
  2543. int item_id = sqlite3_column_int(statement, 2);
  2544. int qty = sqlite3_column_int(statement, 3);
  2545. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2546. NSString* nscreate_time=nil;
  2547. if(create_time!=nil)
  2548. {
  2549. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2550. }
  2551. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2552. NSString* nsmodify_time=nil;
  2553. if(modify_time!=nil)
  2554. {
  2555. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2556. }
  2557. int is_delete = sqlite3_column_int(statement, 6);
  2558. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2559. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2560. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2561. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2562. itemjson[@"create_time"]=nscreate_time;
  2563. itemjson[@"modify_time"]=nsmodify_time;
  2564. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2565. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2566. count++;
  2567. }
  2568. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2569. sqlite3_finalize(statement);
  2570. }
  2571. ret[@"count"]=[NSNumber numberWithInt:count ];
  2572. [iSalesDB close_db:db];
  2573. return ret;
  2574. }
  2575. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2576. {
  2577. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2578. // UIApplication * app = [UIApplication sharedApplication];
  2579. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2580. assert(add_params[@"user"]!=nil);
  2581. // assert(add_params[@"password"]!=nil);
  2582. 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 ];
  2583. // 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 ];
  2584. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2585. DebugLog(@"offline_login sql:%@",sqlQuery);
  2586. sqlite3_stmt * statement;
  2587. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2588. int count=0;
  2589. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2590. if ( dbresult== SQLITE_OK)
  2591. {
  2592. while (sqlite3_step(statement) == SQLITE_ROW)
  2593. {
  2594. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2595. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2596. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2597. int product_id = sqlite3_column_int(statement, 0);
  2598. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2599. int item_id = sqlite3_column_int(statement, 7);
  2600. NSString* Price=nil;
  2601. if(str_price==nil)
  2602. {
  2603. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2604. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2605. if(price==nil)
  2606. Price=@"No Price.";
  2607. else
  2608. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2609. }
  2610. else
  2611. {
  2612. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2613. }
  2614. double discount = sqlite3_column_double(statement, 2);
  2615. int item_count = sqlite3_column_int(statement, 3);
  2616. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2617. NSString *nsline_note=nil;
  2618. if(line_note!=nil)
  2619. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2620. char *name = (char*)sqlite3_column_text(statement, 5);
  2621. NSString *nsname=nil;
  2622. if(name!=nil)
  2623. nsname= [[NSString alloc]initWithUTF8String:name];
  2624. char *description = (char*)sqlite3_column_text(statement, 6);
  2625. NSString *nsdescription=nil;
  2626. if(description!=nil)
  2627. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2628. // int stockUom = sqlite3_column_int(statement, 8);
  2629. // int _id = sqlite3_column_int(statement, 9);
  2630. //
  2631. //
  2632. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2633. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2634. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2635. // int carton=[bsubtotaljson[@"carton"] intValue];
  2636. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2637. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2638. itemjson[@"model"]=nsname;
  2639. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2640. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2641. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2642. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2643. // itemjson[@"check"]=@"true";
  2644. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2645. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2646. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2647. itemjson[@"unit_price"]=Price;
  2648. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2649. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2650. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2651. itemjson[@"note"]=nsline_note;
  2652. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2653. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2654. count++;
  2655. }
  2656. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2657. sqlite3_finalize(statement);
  2658. }
  2659. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2660. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2661. return ret;
  2662. }
  2663. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2664. {
  2665. assert(add_params[@"user"]!=nil);
  2666. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2667. sqlite3 *db = [iSalesDB get_db];
  2668. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2669. // for(int i=0;i<arr_soid.count;i++)
  2670. sqlite3_stmt * statement;
  2671. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2672. //int count=0;
  2673. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2674. if ( dbresult== SQLITE_OK)
  2675. {
  2676. int count=0;
  2677. while (sqlite3_step(statement) == SQLITE_ROW)
  2678. {
  2679. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2680. int _id = sqlite3_column_int(statement, 0);
  2681. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2682. NSString* nssync_data=nil;
  2683. if(sync_data!=nil)
  2684. {
  2685. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2686. }
  2687. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2688. NSString* nsimg_1=nil;
  2689. if(img_1!=nil)
  2690. {
  2691. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2692. [self copy_upImg:serial file:nsimg_1];
  2693. }
  2694. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2695. NSString* nsso_no=nil;
  2696. if(so_no!=nil)
  2697. {
  2698. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2699. }
  2700. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2701. itemjson[@"order_type"]=@"submit order";
  2702. else
  2703. itemjson[@"order_type"]=@"archive order";
  2704. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2705. itemjson[@"json_data"]= nssync_data;
  2706. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2707. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2708. count++;
  2709. }
  2710. ret[@"count"]=[NSNumber numberWithInt:count ];
  2711. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2712. }
  2713. sqlite3_finalize(statement);
  2714. [iSalesDB close_db:db];
  2715. return ret;
  2716. }
  2717. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2718. {
  2719. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2720. sqlite3 *db = [iSalesDB get_db];
  2721. // UIApplication * app = [UIApplication sharedApplication];
  2722. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2723. 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";
  2724. sqlite3_stmt * statement;
  2725. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2726. int count=0;
  2727. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2728. if ( dbresult== SQLITE_OK)
  2729. {
  2730. while (sqlite3_step(statement) == SQLITE_ROW)
  2731. {
  2732. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2733. int _id = sqlite3_column_int(statement, 0);
  2734. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2735. NSString* nsimg_0=nil;
  2736. if(img_0!=nil)
  2737. {
  2738. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2739. [self copy_upImg:serial file:nsimg_0];
  2740. }
  2741. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2742. NSString* nsimg_1=nil;
  2743. if(img_1!=nil)
  2744. {
  2745. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2746. [self copy_upImg:serial file:nsimg_1];
  2747. }
  2748. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2749. NSString* nsimg_2=nil;
  2750. if(img_2!=nil)
  2751. {
  2752. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2753. [self copy_upImg:serial file:nsimg_2];
  2754. }
  2755. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2756. NSString* nssync_data=nil;
  2757. if(sync_data!=nil)
  2758. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2759. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2760. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2761. itemjson[@"json_data"]= nssync_data;
  2762. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2763. count++;
  2764. }
  2765. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2766. sqlite3_finalize(statement);
  2767. }
  2768. ret[@"count"]=[NSNumber numberWithInt:count ];
  2769. [iSalesDB close_db:db];
  2770. return ret;
  2771. }
  2772. +(bool) check_offlinedata
  2773. {
  2774. // UIApplication * app = [UIApplication sharedApplication];
  2775. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2776. sqlite3 *db = [iSalesDB get_db];
  2777. NSString * where=@"1=1";
  2778. // if(appDelegate.user!=nil)
  2779. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2780. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2781. [iSalesDB close_db:db];
  2782. if(count==0)
  2783. {
  2784. return false;
  2785. }
  2786. return true;
  2787. //
  2788. //[iSalesDB close_db:db];
  2789. }
  2790. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2791. {
  2792. // UIApplication * app = [UIApplication sharedApplication];
  2793. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2794. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2795. sqlite3 *db = [iSalesDB get_db];
  2796. NSString* collectId=params[@"collectId"];
  2797. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2798. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2799. [iSalesDB execSql:sqlQuery db:db];
  2800. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2801. [iSalesDB close_db:db];
  2802. // appDelegate.wish_count =count;
  2803. //
  2804. // [appDelegate update_count_mark];
  2805. ret[@"result"]= [NSNumber numberWithInt:2];
  2806. ret[@"wish_count"]=@(count);
  2807. return ret;
  2808. }
  2809. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2810. //{
  2811. //
  2812. // UIApplication * app = [UIApplication sharedApplication];
  2813. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2814. //
  2815. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2816. // sqlite3 *db = [iSalesDB get_db];
  2817. // NSString* product_id=params[@"product_id"];
  2818. //
  2819. //
  2820. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2821. //
  2822. //
  2823. // for(int i=0;i<arr.count;i++)
  2824. // {
  2825. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2826. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2827. // if(count==0)
  2828. // {
  2829. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2830. // [iSalesDB execSql:sqlQuery db:db];
  2831. // }
  2832. // }
  2833. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2834. // [iSalesDB close_db:db];
  2835. //
  2836. // appDelegate.wish_count =count;
  2837. //
  2838. // [appDelegate update_count_mark];
  2839. // ret[@"result"]= [NSNumber numberWithInt:2];
  2840. // return ret;
  2841. // //
  2842. // //return ret;
  2843. //}
  2844. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2845. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2846. DebugLog(@"time interval: %lf",interval);
  2847. }
  2848. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2849. //{
  2850. // UIApplication * app = [UIApplication sharedApplication];
  2851. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2852. //
  2853. // int sort = [[params objectForKey:@"sort"] intValue];
  2854. // NSString *sort_str = @"";
  2855. // switch (sort) {
  2856. // case 0:{
  2857. // sort_str = @"order by w.modify_time desc";
  2858. // }
  2859. // break;
  2860. // case 1:{
  2861. // sort_str = @"order by w.modify_time asc";
  2862. // }
  2863. // break;
  2864. // case 2:{
  2865. // sort_str = @"order by m.name asc";
  2866. // }
  2867. // break;
  2868. // case 3:{
  2869. // sort_str = @"order by m.name desc";
  2870. // }
  2871. // break;
  2872. // case 4:{
  2873. // sort_str = @"order by m.description asc";
  2874. // }
  2875. // break;
  2876. //
  2877. // default:
  2878. // break;
  2879. //
  2880. // }
  2881. //
  2882. //
  2883. //// NSString* user = appDelegate.user;
  2884. //
  2885. // sqlite3 *db = [iSalesDB get_db];
  2886. //
  2887. // // order by w.create_time
  2888. // 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];
  2889. //
  2890. //// 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];
  2891. //
  2892. //
  2893. // sqlite3_stmt * statement;
  2894. //
  2895. // NSDate *date1 = [NSDate date];
  2896. //// NSDate *date2 = nil;
  2897. //
  2898. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2899. // int count=0;
  2900. //
  2901. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2902. // {
  2903. //
  2904. //
  2905. // while (sqlite3_step(statement) == SQLITE_ROW)
  2906. // {
  2907. //
  2908. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2909. //
  2910. // int product_id = sqlite3_column_double(statement, 0);
  2911. //
  2912. //
  2913. //
  2914. //
  2915. //
  2916. // char *description = (char*)sqlite3_column_text(statement, 1);
  2917. // if(description==nil)
  2918. // description= "";
  2919. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2920. //
  2921. // int item_id = sqlite3_column_double(statement, 2);
  2922. //
  2923. // NSDate *date_image = [NSDate date];
  2924. //
  2925. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2926. //
  2927. // printf("image : ");
  2928. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2929. //
  2930. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2931. // // if(url==nil)
  2932. // // url="";
  2933. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2934. //
  2935. // int qty = sqlite3_column_int(statement, 3);
  2936. //
  2937. //
  2938. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2939. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2940. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2941. // item[@"description"]= nsdescription;
  2942. // item[@"img"]= nsurl;
  2943. //
  2944. //
  2945. //
  2946. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2947. // count++;
  2948. //
  2949. // }
  2950. // printf("total time:");
  2951. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2952. //
  2953. // ret[@"count"]= [NSNumber numberWithInt:count];
  2954. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2955. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2956. // ret[@"result"]= [NSNumber numberWithInt:2];
  2957. //
  2958. //
  2959. // appDelegate.wish_count =count;
  2960. //
  2961. // [appDelegate update_count_mark];
  2962. // sqlite3_finalize(statement);
  2963. //
  2964. //
  2965. //
  2966. //
  2967. // }
  2968. //
  2969. // [iSalesDB close_db:db];
  2970. //
  2971. // return ret;
  2972. //}
  2973. +(NSDictionary*) offline_notimpl
  2974. {
  2975. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2976. ret[@"result"]=@"8";
  2977. ret[@"err_msg"]=@"offline mode does not support this function.";
  2978. return ret;
  2979. }
  2980. +(NSDictionary*) offline_home
  2981. {
  2982. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2983. NSString *cachefolder = [paths objectAtIndex:0];
  2984. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2985. NSData* json =nil;
  2986. json=[NSData dataWithContentsOfFile:img_cache];
  2987. if(json==nil)
  2988. return nil;
  2989. NSError *error=nil;
  2990. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2991. return menu;
  2992. }
  2993. +(NSDictionary*) offline_category_menu
  2994. {
  2995. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2996. NSString *cachefolder = [paths objectAtIndex:0];
  2997. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2998. NSData* json =nil;
  2999. json=[NSData dataWithContentsOfFile:img_cache];
  3000. if(json==nil)
  3001. return nil;
  3002. NSError *error=nil;
  3003. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  3004. return menu;
  3005. }
  3006. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  3007. {
  3008. NSString* offline_command=params[@"offline_Command"];
  3009. NSDictionary* ret=nil;
  3010. if([offline_command isEqualToString:@"model_NIYMAL"])
  3011. {
  3012. NSString* category = params[@"category"];
  3013. ret = [self refresh_model_NIYMAL:category];
  3014. }
  3015. return ret;
  3016. }
  3017. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  3018. {
  3019. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3020. [ret setValue:@"2" forKey:@"result"];
  3021. [ret setValue:@"Regular Mode" forKey:@"mode"];
  3022. sqlite3* db= [iSalesDB get_db];
  3023. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3024. [iSalesDB close_db:db];
  3025. [ret setObject:detail1_section forKey:@"detail_1"];
  3026. return ret;
  3027. }
  3028. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  3029. {
  3030. //assert(user!=nil);
  3031. // UIApplication * app = [UIApplication sharedApplication];
  3032. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3033. // NSArray* arr1 = [self get_user_all_price_type];
  3034. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3035. // NSSet *set1 = [NSSet setWithArray:arr1];
  3036. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3037. // if(appDelegate.contact_id==nil)
  3038. // set2=[set1 mutableCopy];
  3039. // else
  3040. // [set2 intersectsSet:set1];
  3041. // NSArray *retarr = [set2 allObjects];
  3042. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3043. // sqlite3 *db = [iSalesDB get_db];
  3044. NSString* sqlQuery = nil;
  3045. if(contact_id==nil)
  3046. {
  3047. if(!blogin)
  3048. return nil;
  3049. 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];
  3050. }
  3051. else
  3052. 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];
  3053. sqlite3_stmt * statement;
  3054. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3055. int count=0;
  3056. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3057. {
  3058. while (sqlite3_step(statement) == SQLITE_ROW)
  3059. {
  3060. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  3061. char *name = (char*)sqlite3_column_text(statement, 0);
  3062. if(name==nil)
  3063. name="";
  3064. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3065. // double price = sqlite3_column_double(statement, 1);
  3066. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  3067. // if(isnull==nil)
  3068. // item[nsname]= @"No Price";
  3069. // else
  3070. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  3071. char *price = (char*)sqlite3_column_text(statement, 1);
  3072. if(price!=nil)
  3073. {
  3074. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3075. nsprice=[AESCrypt fastdecrypt:nsprice];
  3076. if(nsprice.length>0)
  3077. {
  3078. double dp= [nsprice doubleValue];
  3079. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  3080. }
  3081. }
  3082. else
  3083. {
  3084. item[nsname]= @"No Price";
  3085. }
  3086. // int type= sqlite3_column_int(statement, 2);
  3087. //item[@"type"]=@"price";
  3088. // item[nsname]= nsprice;
  3089. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  3090. count++;
  3091. }
  3092. ret[@"count"]= [NSNumber numberWithInt:count];
  3093. sqlite3_finalize(statement);
  3094. }
  3095. // [iSalesDB close_db:db];
  3096. return ret;
  3097. }
  3098. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  3099. //{
  3100. // NSArray* arr1 = [self get_user_all_price_type:db];
  3101. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  3102. //
  3103. // // NSSet *set1 = [NSSet setWithArray:arr1];
  3104. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  3105. // // [set2 intersectsSet:set1];
  3106. // //
  3107. // //
  3108. // // NSArray *retarr = [set2 allObjects];
  3109. //
  3110. // NSString* whereprice=nil;
  3111. // if(contact_id==nil)
  3112. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3113. // else
  3114. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3115. //
  3116. //
  3117. // // sqlite3 *db = [iSalesDB get_db];
  3118. //
  3119. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3120. // sqlite3_stmt * statement;
  3121. //
  3122. //
  3123. // NSNumber* ret = nil;
  3124. // double dprice=DBL_MAX;
  3125. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3126. // {
  3127. //
  3128. //
  3129. // while (sqlite3_step(statement) == SQLITE_ROW)
  3130. // {
  3131. //
  3132. // // double val = sqlite3_column_double(statement, 0);
  3133. // char *price = (char*)sqlite3_column_text(statement, 0);
  3134. // if(price!=nil)
  3135. // {
  3136. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3137. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3138. // if(nsprice.length>0)
  3139. // {
  3140. // double dp= [nsprice doubleValue];
  3141. // if(dp<dprice)
  3142. // dprice=dp;
  3143. // }
  3144. // }
  3145. // }
  3146. //
  3147. //
  3148. //
  3149. //
  3150. // sqlite3_finalize(statement);
  3151. //
  3152. //
  3153. //
  3154. //
  3155. // }
  3156. //
  3157. // // [iSalesDB close_db:db];
  3158. //
  3159. // if(dprice==DBL_MAX)
  3160. // ret= nil;
  3161. // else
  3162. // ret= [NSNumber numberWithDouble:dprice];
  3163. // return ret;
  3164. //}
  3165. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3166. {
  3167. assert(user!=nil);
  3168. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3169. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3170. NSString* whereprice=nil;
  3171. if(contact_id==nil)
  3172. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3173. else
  3174. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3175. // sqlite3 *db = [iSalesDB get_db];
  3176. NSString *productIdCondition = @"1 = 1";
  3177. if (product_id) {
  3178. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3179. }
  3180. NSString *itemIdCondition = @"";
  3181. if (item_id) {
  3182. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3183. }
  3184. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3185. sqlite3_stmt * statement;
  3186. NSNumber* ret = nil;
  3187. double dprice=DBL_MAX;
  3188. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3189. {
  3190. while (sqlite3_step(statement) == SQLITE_ROW)
  3191. {
  3192. // double val = sqlite3_column_double(statement, 0);
  3193. char *price = (char*)sqlite3_column_text(statement, 0);
  3194. if(price!=nil)
  3195. {
  3196. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3197. nsprice=[AESCrypt fastdecrypt:nsprice];
  3198. if(nsprice.length>0)
  3199. {
  3200. double dp= [nsprice doubleValue];
  3201. if(dp<dprice)
  3202. dprice=dp;
  3203. }
  3204. }
  3205. }
  3206. sqlite3_finalize(statement);
  3207. }
  3208. // [iSalesDB close_db:db];
  3209. if(dprice==DBL_MAX)
  3210. ret= nil;
  3211. else
  3212. ret= [NSNumber numberWithDouble:dprice];
  3213. return ret;
  3214. }
  3215. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3216. {
  3217. assert(user!=nil);
  3218. NSArray* ret=nil;
  3219. // sqlite3 *db = [iSalesDB get_db];
  3220. // no customer assigned , use login user contact_id
  3221. // UIApplication * app = [UIApplication sharedApplication];
  3222. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3223. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3224. sqlite3_stmt * statement;
  3225. // int count=0;
  3226. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3227. {
  3228. if (sqlite3_step(statement) == SQLITE_ROW)
  3229. {
  3230. char *val = (char*)sqlite3_column_text(statement, 0);
  3231. if(val==nil)
  3232. val="";
  3233. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3234. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3235. }
  3236. sqlite3_finalize(statement);
  3237. }
  3238. // [iSalesDB close_db:db];
  3239. return ret;
  3240. }
  3241. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3242. {
  3243. // sqlite3 *db = [iSalesDB get_db];
  3244. if(contact_id==nil)
  3245. {
  3246. // no customer assigned , use login user contact_id
  3247. // UIApplication * app = [UIApplication sharedApplication];
  3248. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3249. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3250. sqlite3_stmt * statement;
  3251. // int count=0;
  3252. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3253. {
  3254. if (sqlite3_step(statement) == SQLITE_ROW)
  3255. {
  3256. char *val = (char*)sqlite3_column_text(statement, 0);
  3257. if(val==nil)
  3258. val="";
  3259. contact_id = [[NSString alloc]initWithUTF8String:val];
  3260. }
  3261. sqlite3_finalize(statement);
  3262. }
  3263. if(contact_id.length<=0)
  3264. {
  3265. // [iSalesDB close_db:db];
  3266. return nil;
  3267. }
  3268. }
  3269. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3270. sqlite3_stmt * statement;
  3271. NSArray* ret=nil;
  3272. // int count=0;
  3273. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3274. {
  3275. if (sqlite3_step(statement) == SQLITE_ROW)
  3276. {
  3277. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3278. if(price_type==nil)
  3279. price_type="";
  3280. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3281. if(nsprice_type.length>0)
  3282. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3283. else
  3284. ret=nil;
  3285. }
  3286. sqlite3_finalize(statement);
  3287. }
  3288. // [iSalesDB close_db:db];
  3289. return ret;
  3290. }
  3291. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3292. {
  3293. NSString* ret= nil;
  3294. // sqlite3 *db = [iSalesDB get_db];
  3295. NSString *sqlQuery = nil;
  3296. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3297. // 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;
  3298. // 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
  3299. if(product_id==nil && model_name)
  3300. 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;
  3301. else if (product_id)
  3302. 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
  3303. sqlite3_stmt * statement;
  3304. // int count=0;
  3305. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3306. {
  3307. if (sqlite3_step(statement) == SQLITE_ROW)
  3308. {
  3309. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3310. if(imgurl==nil)
  3311. imgurl="";
  3312. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3313. ret=nsimgurl;
  3314. }
  3315. sqlite3_finalize(statement);
  3316. }
  3317. else
  3318. {
  3319. [ret setValue:@"8" forKey:@"result"];
  3320. }
  3321. // [iSalesDB close_db:db];
  3322. DebugLog(@"data string: %@",ret );
  3323. return ret;
  3324. }
  3325. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3326. {
  3327. NSString* ret= nil;
  3328. sqlite3 *db = [iSalesDB get_db];
  3329. NSString *sqlQuery = nil;
  3330. if(product_id==nil)
  3331. if(upc_code==nil)
  3332. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3333. else
  3334. 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='%@';
  3335. else
  3336. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3337. sqlite3_stmt * statement;
  3338. // int count=0;
  3339. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3340. {
  3341. if (sqlite3_step(statement) == SQLITE_ROW)
  3342. {
  3343. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3344. if(default_category==nil)
  3345. default_category="";
  3346. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3347. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3348. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3349. ret=nsdefault_category;
  3350. }
  3351. sqlite3_finalize(statement);
  3352. }
  3353. else
  3354. {
  3355. [ret setValue:@"8" forKey:@"result"];
  3356. }
  3357. [iSalesDB close_db:db];
  3358. DebugLog(@"data string: %@",ret );
  3359. return ret;
  3360. }
  3361. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3362. NSString *sql = nil;
  3363. if (product_id != nil) {
  3364. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3365. } else {
  3366. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3367. }
  3368. NSString *result_set = [iSalesDB jk_queryText:sql];
  3369. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3370. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3371. return result_set;
  3372. }
  3373. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3374. //{
  3375. //
  3376. //
  3377. // assert(params[@"user"]!=nil);
  3378. //
  3379. //
  3380. //
  3381. // NSString* model_name = [params valueForKey:@"product_name"];
  3382. //
  3383. // NSString* product_id = [params valueForKey:@"product_id"];
  3384. //
  3385. // NSString* category = [params valueForKey:@"category"];
  3386. //
  3387. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3388. // if(category==nil) {
  3389. // category = default_category_id;
  3390. // } else {
  3391. //
  3392. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3393. // // 参数重有多个category id
  3394. // if (arr_0.count > 1) {
  3395. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3396. //
  3397. // // 取交集
  3398. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3399. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3400. // [set_0 intersectSet:set_1];
  3401. //
  3402. // if (set_0.count == 1) {
  3403. // category = (NSString *)[set_0 anyObject];
  3404. // } else {
  3405. // if ([set_0 containsObject:default_category_id]) {
  3406. // category = default_category_id;
  3407. // } else {
  3408. // category = (NSString *)[set_0 anyObject];
  3409. // }
  3410. // }
  3411. // }
  3412. // }
  3413. //
  3414. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3415. //
  3416. //
  3417. //
  3418. //
  3419. //
  3420. //
  3421. // sqlite3 *db = [iSalesDB get_db];
  3422. //
  3423. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3424. //
  3425. //
  3426. // NSString *sqlQuery = nil;
  3427. //
  3428. // if(product_id==nil)
  3429. // 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='%@';
  3430. // else
  3431. //
  3432. // 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=%@;
  3433. //
  3434. //
  3435. // sqlite3_stmt * statement;
  3436. // [ret setValue:@"2" forKey:@"result"];
  3437. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3438. //
  3439. // // int count=0;
  3440. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3441. // {
  3442. //
  3443. //
  3444. // if (sqlite3_step(statement) == SQLITE_ROW)
  3445. // {
  3446. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3447. //
  3448. //
  3449. // char *name = (char*)sqlite3_column_text(statement, 0);
  3450. // if(name==nil)
  3451. // name="";
  3452. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3453. //
  3454. // char *description = (char*)sqlite3_column_text(statement, 1);
  3455. // if(description==nil)
  3456. // description="";
  3457. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3458. //
  3459. //
  3460. // int product_id = sqlite3_column_int(statement, 2);
  3461. //
  3462. //
  3463. // char *color = (char*)sqlite3_column_text(statement, 3);
  3464. // if(color==nil)
  3465. // color="";
  3466. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3467. // //
  3468. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3469. // // if(legcolor==nil)
  3470. // // legcolor="";
  3471. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3472. // //
  3473. // //
  3474. // int availability = sqlite3_column_int(statement, 5);
  3475. // //
  3476. // int incoming_stock = sqlite3_column_int(statement, 6);
  3477. //
  3478. //
  3479. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3480. // if(demension==nil)
  3481. // demension="";
  3482. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3483. //
  3484. //
  3485. //
  3486. // // ,,,,,,,,,
  3487. //
  3488. //
  3489. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3490. // if(seat_height==nil)
  3491. // seat_height="";
  3492. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3493. //
  3494. //
  3495. // char *material = (char*)sqlite3_column_text(statement, 9);
  3496. // if(material==nil)
  3497. // material="";
  3498. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3499. //
  3500. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3501. // if(box_dim==nil)
  3502. // box_dim="";
  3503. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3504. //
  3505. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3506. // if(volume==nil)
  3507. // volume="";
  3508. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3509. //
  3510. // double weight = sqlite3_column_double(statement, 12);
  3511. //
  3512. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3513. // if(model_set==nil)
  3514. // model_set="";
  3515. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3516. //
  3517. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3518. // if(load_ability==nil)
  3519. // load_ability="";
  3520. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3521. //
  3522. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3523. //// if(default_category==nil)
  3524. //// default_category="";
  3525. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3526. //
  3527. //
  3528. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3529. // if(fabric_content==nil)
  3530. // fabric_content="";
  3531. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3532. //
  3533. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3534. // if(assembling==nil)
  3535. // assembling="";
  3536. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3537. //
  3538. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3539. // if(made_in==nil)
  3540. // made_in="";
  3541. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3542. //
  3543. //
  3544. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3545. // if(special_remarks==nil)
  3546. // special_remarks="";
  3547. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3548. //
  3549. //
  3550. // int stockUcom = sqlite3_column_double(statement, 20);
  3551. //
  3552. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3553. // if(product_group==nil)
  3554. // product_group="";
  3555. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3556. //
  3557. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3558. // // if(fashion_selector==nil)
  3559. // // fashion_selector="";
  3560. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3561. //
  3562. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3563. // if(selector_field==nil)
  3564. // selector_field="";
  3565. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3566. //
  3567. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3568. // if(property_field==nil)
  3569. // property_field="";
  3570. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3571. //
  3572. //
  3573. //
  3574. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3575. // if(packaging==nil)
  3576. // packaging="";
  3577. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3578. //
  3579. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3580. //
  3581. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3582. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3583. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3584. //
  3585. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3586. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3587. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3588. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3589. //
  3590. //
  3591. //
  3592. //
  3593. //
  3594. // NSString* Availability=nil;
  3595. // if(availability>0)
  3596. // Availability=[NSString stringWithFormat:@"%d",availability];
  3597. // else
  3598. // Availability = @"Out of Stock";
  3599. //
  3600. // [img_section setValue:Availability forKey:@"Availability"];
  3601. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3602. //
  3603. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3604. //
  3605. //
  3606. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3607. // if(eta==nil)
  3608. // eta="";
  3609. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3610. // if ([nseta isEqualToString:@"null"]) {
  3611. // nseta = @"";
  3612. // }
  3613. // if (availability <= 0) {
  3614. // [img_section setValue:nseta forKey:@"ETA"];
  3615. // }
  3616. //
  3617. //
  3618. // int item_id = sqlite3_column_int(statement, 26);
  3619. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3620. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3621. //
  3622. // NSString* Price=nil;
  3623. // if(appDelegate.bLogin==false)
  3624. // Price=@"Must Sign in.";
  3625. // else
  3626. // {
  3627. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3628. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3629. // if(price==nil)
  3630. // Price=@"No Price.";
  3631. // else
  3632. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3633. // }
  3634. //
  3635. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3636. // [img_section setValue:Price forKey:@"price"];
  3637. // [img_section setValue:nsname forKey:@"model_name"];
  3638. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3639. //
  3640. // if (appDelegate.order_code) { // 离线order code即so#
  3641. //
  3642. // 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];
  3643. // __block int cartQTY = 0;
  3644. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3645. //
  3646. // cartQTY = sqlite3_column_int(stmt, 0);
  3647. //
  3648. // }];
  3649. //
  3650. // if (cartQTY > 0) {
  3651. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3652. // }
  3653. // }
  3654. //
  3655. // [ret setObject:img_section forKey:@"img_section"];
  3656. //
  3657. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3658. // int detail0_item_count=0;
  3659. //
  3660. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3661. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3662. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3663. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3664. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3665. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3666. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3667. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3668. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3669. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3670. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3671. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3672. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3673. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3674. //
  3675. //
  3676. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3677. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3678. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3679. // {
  3680. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3681. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3682. // }
  3683. //
  3684. //
  3685. //
  3686. //
  3687. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3688. // [detail0_section setValue:@"kv" forKey:@"type"];
  3689. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3690. //
  3691. // [ret setObject:detail0_section forKey:@"detail_0"];
  3692. //
  3693. //
  3694. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3695. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3696. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3697. // // [detail1_section setValue:@"content" forKey:@"type"];
  3698. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3699. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3700. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3701. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3702. // [ret setObject:detail1_section forKey:@"detail_1"];
  3703. //
  3704. //
  3705. //
  3706. //
  3707. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3708. // [detail2_section setValue:@"detail" forKey:@"target"];
  3709. // [detail2_section setValue:@"popup" forKey:@"action"];
  3710. // [detail2_section setValue:@"content" forKey:@"type"];
  3711. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3712. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3713. // [detail2_section setValue:@"local" forKey:@"data"];
  3714. // [ret setObject:detail2_section forKey:@"detail_2"];
  3715. // }
  3716. //
  3717. //
  3718. //
  3719. //
  3720. // sqlite3_finalize(statement);
  3721. // }
  3722. // else
  3723. // {
  3724. // [ret setValue:@"8" forKey:@"result"];
  3725. // }
  3726. //// DebugLog(@"count:%d",count);
  3727. //
  3728. //
  3729. // [iSalesDB close_db:db];
  3730. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3731. //
  3732. // return ret;
  3733. //}
  3734. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3735. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3736. NSString* keyword = [params valueForKey:@"keyword"];
  3737. keyword=keyword.lowercaseString;
  3738. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3739. int limit = [[params valueForKey:@"limit"] intValue];
  3740. int offset = [[params valueForKey:@"offset"] intValue];
  3741. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3742. NSString *limit_str = @"";
  3743. if (limited) {
  3744. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3745. }
  3746. sqlite3 *db = [iSalesDB get_db];
  3747. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3748. // UIApplication * app = [UIApplication sharedApplication];
  3749. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3750. NSString *sqlQuery = nil;
  3751. if(exactMatch )
  3752. 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 ;
  3753. else
  3754. 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
  3755. DebugLog(@"offline_search sql:%@",sqlQuery);
  3756. sqlite3_stmt * statement;
  3757. [ret setValue:@"2" forKey:@"result"];
  3758. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3759. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3760. // int count=0;
  3761. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3762. {
  3763. int i=0;
  3764. while (sqlite3_step(statement) == SQLITE_ROW)
  3765. {
  3766. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3767. // char *name = (char*)sqlite3_column_text(statement, 1);
  3768. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3769. char *name = (char*)sqlite3_column_text(statement, 0);
  3770. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3771. char *description = (char*)sqlite3_column_text(statement, 1);
  3772. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3773. int product_id = sqlite3_column_int(statement, 2);
  3774. // char *url = (char*)sqlite3_column_text(statement, 3);
  3775. // if(url==nil)
  3776. // url="";
  3777. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3778. int wid = sqlite3_column_int(statement, 3);
  3779. int closeout = sqlite3_column_int(statement, 4);
  3780. int cid = sqlite3_column_int(statement, 5);
  3781. int wisdelete = sqlite3_column_int(statement, 6);
  3782. int more_color = sqlite3_column_int(statement, 7);
  3783. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3784. if(wid !=0 && wisdelete != 1)
  3785. [item setValue:@"true" forKey:@"wish_exists"];
  3786. else
  3787. [item setValue:@"false" forKey:@"wish_exists"];
  3788. if(closeout==0)
  3789. [item setValue:@"false" forKey:@"is_closeout"];
  3790. else
  3791. [item setValue:@"true" forKey:@"is_closeout"];
  3792. if(cid==0)
  3793. [item setValue:@"false" forKey:@"cart_exists"];
  3794. else
  3795. [item setValue:@"true" forKey:@"cart_exists"];
  3796. if (more_color == 0) {
  3797. [item setObject:@(false) forKey:@"more_color"];
  3798. } else if (more_color == 1) {
  3799. [item setObject:@(true) forKey:@"more_color"];
  3800. }
  3801. [item addEntriesFromDictionary:imgjson];
  3802. // [item setValue:nsurl forKey:@"img"];
  3803. [item setValue:nsname forKey:@"fash_name"];
  3804. [item setValue:nsdescription forKey:@"description"];
  3805. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3806. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3807. i++;
  3808. }
  3809. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3810. [ret setObject:items forKey:@"items"];
  3811. sqlite3_finalize(statement);
  3812. }
  3813. DebugLog(@"count:%d",count);
  3814. [iSalesDB close_db:db];
  3815. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3816. return ret;
  3817. }
  3818. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3819. {
  3820. return [self search:params limited:YES];
  3821. }
  3822. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3823. //{
  3824. // UIApplication * app = [UIApplication sharedApplication];
  3825. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3826. //
  3827. //// [iSalesDB disable_trigger]
  3828. // [iSalesDB disable_trigger];
  3829. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3830. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3831. //
  3832. // [iSalesDB enable_trigger];
  3833. //// [iSalesDB enable_trigger]
  3834. // //
  3835. // // NSString* user = [params valueForKey:@"user"];
  3836. // //
  3837. // // NSString* password = [params valueForKey:@"password"];
  3838. //
  3839. //
  3840. //
  3841. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3842. //
  3843. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3844. //
  3845. //
  3846. //
  3847. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3848. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3849. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3850. //
  3851. // [appDelegate update_count_mark];
  3852. //
  3853. //
  3854. // appDelegate.can_show_price =false;
  3855. // appDelegate.can_see_price =false;
  3856. // appDelegate.can_create_portfolio =false;
  3857. // appDelegate.can_create_order =false;
  3858. //
  3859. //
  3860. // appDelegate.can_cancel_order =false;
  3861. // appDelegate.can_set_cart_price =false;
  3862. // appDelegate.can_delete_order =false;
  3863. // appDelegate.can_submit_order =false;
  3864. // appDelegate.can_set_tearsheet_price =false;
  3865. // appDelegate.can_update_contact_info = false;
  3866. //
  3867. // appDelegate.save_order_logout = false;
  3868. // appDelegate.submit_order_logout = false;
  3869. // appDelegate.alert_sold_in_quantities = false;
  3870. //
  3871. // appDelegate.ipad_perm =nil ;
  3872. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3873. // appDelegate.OrderFilter= nil;
  3874. // [appDelegate SetSo:nil];
  3875. // [appDelegate set_main_button_panel];
  3876. //
  3877. //
  3878. // // sqlite3 *db = [iSalesDB get_db];
  3879. // //
  3880. // //
  3881. // //
  3882. // //
  3883. // //
  3884. // // 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"]];
  3885. // //
  3886. // //
  3887. // //
  3888. // //
  3889. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3890. // // sqlite3_stmt * statement;
  3891. // //
  3892. // //
  3893. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3894. // //
  3895. // //
  3896. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3897. // // {
  3898. // //
  3899. // //
  3900. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3901. // // {
  3902. // //
  3903. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3904. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3905. // //
  3906. // //
  3907. // //
  3908. // // int can_show_price = sqlite3_column_int(statement, 0);
  3909. // // int can_see_price = sqlite3_column_int(statement, 1);
  3910. // //
  3911. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3912. // // if(contact_id==nil)
  3913. // // contact_id="";
  3914. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3915. // //
  3916. // // int user_type = sqlite3_column_int(statement, 3);
  3917. // //
  3918. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3919. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3920. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3921. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3922. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3923. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3924. // // int can_create_order = sqlite3_column_int(statement, 10);
  3925. // //
  3926. // //
  3927. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3928. // // if(mode==nil)
  3929. // // mode="";
  3930. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3931. // //
  3932. // //
  3933. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3934. // // if(username==nil)
  3935. // // username="";
  3936. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3937. // //
  3938. // //
  3939. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3940. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3941. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3942. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3943. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3944. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3945. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3946. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3947. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3948. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3949. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3950. // //
  3951. // // [header setValue:nsusername forKey:@"username"];
  3952. // //
  3953. // //
  3954. // // [ret setObject:header forKey:@"header"];
  3955. // // [ret setValue:nsmode forKey:@"mode"];
  3956. // //
  3957. // //
  3958. // // }
  3959. // //
  3960. // //
  3961. // //
  3962. // // sqlite3_finalize(statement);
  3963. // // }
  3964. // //
  3965. // //
  3966. // //
  3967. // // [iSalesDB close_db:db];
  3968. // //
  3969. // //
  3970. // //
  3971. // //
  3972. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3973. //
  3974. // return ret;
  3975. //}
  3976. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3977. //{
  3978. //
  3979. // UIApplication * app = [UIApplication sharedApplication];
  3980. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3981. //
  3982. // [iSalesDB disable_trigger];
  3983. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3984. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3985. // [iSalesDB enable_trigger];
  3986. //
  3987. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3988. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3989. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3990. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3991. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3992. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3993. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3994. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3995. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3996. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3997. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3998. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3999. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  4000. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  4001. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  4002. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  4003. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  4004. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  4005. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  4006. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  4007. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  4008. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  4009. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  4010. //
  4011. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4012. //
  4013. // [arr_name addObject:customer_first_name];
  4014. // [arr_name addObject:customer_last_name];
  4015. //
  4016. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4017. //
  4018. // // default ship from
  4019. // 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';";
  4020. //
  4021. // __block NSString *cid = @"";
  4022. // __block NSString *name = @"";
  4023. // __block NSString *ext = @"";
  4024. // __block NSString *contact = @"";
  4025. // __block NSString *email = @"";
  4026. // __block NSString *fax = @"";
  4027. // __block NSString *phone = @"";
  4028. //
  4029. // sqlite3 *db = [iSalesDB get_db];
  4030. //
  4031. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  4032. //
  4033. // cid = [self textAtColumn:0 statement:statment];
  4034. // name = [self textAtColumn:1 statement:statment];
  4035. // ext = [self textAtColumn:2 statement:statment];
  4036. // contact = [self textAtColumn:3 statement:statment];
  4037. // email = [self textAtColumn:4 statement:statment];
  4038. // fax = [self textAtColumn:5 statement:statment];
  4039. // phone = [self textAtColumn:6 statement:statment];
  4040. //
  4041. // }];
  4042. //
  4043. // NSString* so_id = [self get_offline_soid:db];
  4044. // if(so_id==nil)
  4045. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  4046. //
  4047. // 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];
  4048. //
  4049. //
  4050. //
  4051. // int result =[iSalesDB execSql:sql_neworder db:db];
  4052. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  4053. //
  4054. //
  4055. //
  4056. // //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'
  4057. // //soId
  4058. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  4059. //
  4060. //
  4061. //
  4062. //
  4063. //
  4064. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  4065. // sqlite3_stmt * statement;
  4066. //
  4067. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4068. // {
  4069. // if (sqlite3_step(statement) == SQLITE_ROW)
  4070. // {
  4071. // // char *name = (char*)sqlite3_column_text(statement, 1);
  4072. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4073. //
  4074. // //ret = sqlite3_column_int(statement, 0);
  4075. //
  4076. // char *soId = (char*)sqlite3_column_text(statement, 0);
  4077. // if(soId==nil)
  4078. // soId="";
  4079. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  4080. // [ret setValue:nssoId forKey:@"soId"];
  4081. // [ret setValue:nssoId forKey:@"orderCode"];
  4082. //
  4083. // }
  4084. // sqlite3_finalize(statement);
  4085. // }
  4086. //
  4087. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  4088. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  4089. //
  4090. //
  4091. // [iSalesDB close_db:db];
  4092. //
  4093. // return [RAConvertor dict2data:ret];
  4094. //
  4095. //}
  4096. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  4097. {
  4098. //assert(params[@"order_code"]);
  4099. // assert(params[@"order_code"]);
  4100. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4101. NSString* orderCode = [params valueForKey:@"orderCode"];
  4102. NSString* app_order_code= params[@"appDelegate.order_code"];
  4103. // UIApplication * app = [UIApplication sharedApplication];
  4104. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4105. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  4106. {
  4107. [iSalesDB disable_trigger];
  4108. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  4109. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4110. [iSalesDB enable_trigger];
  4111. }
  4112. sqlite3 *db = [iSalesDB get_db];
  4113. int cart_count=[self query_ordercartcount:orderCode db:db];
  4114. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4115. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4116. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4117. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4118. [iSalesDB close_db:db];
  4119. return [RAConvertor dict2data:ret];
  4120. }
  4121. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4122. {
  4123. assert(params[@"can_create_backorder"]!=nil);
  4124. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4125. sqlite3 *db = [iSalesDB get_db];
  4126. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4127. int count =0;
  4128. if(params[@"count"]!=nil)
  4129. {
  4130. count = [params[@"count"] intValue];
  4131. }
  4132. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4133. NSString* product_id=params[@"product_id"];
  4134. NSString* orderCode=params[@"orderCode"];
  4135. NSString *qty = params[@"qty"];
  4136. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4137. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4138. __block int number_of_outOfStock = 0;
  4139. for(int i=0;i<arr_id.count;i++)
  4140. {
  4141. NSInteger item_qty= count;
  4142. if (qty) {
  4143. item_qty = [qty_arr[i] integerValue];
  4144. }
  4145. if(item_qty==0)
  4146. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4147. // 检查新加Model数量是否大于库存
  4148. if (![params[@"can_create_backorder"] boolValue]) {
  4149. __block BOOL needContinue = NO;
  4150. [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) {
  4151. int availability = sqlite3_column_int(stmt, 0);
  4152. // 库存小于购买量为缺货
  4153. if (availability < item_qty || availability <= 0) {
  4154. number_of_outOfStock++;
  4155. needContinue = YES;
  4156. }
  4157. }];
  4158. if (needContinue) {
  4159. continue;
  4160. }
  4161. }
  4162. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4163. NSString* sql=nil;
  4164. sqlite3_stmt *stmt = nil;
  4165. BOOL shouldStep = NO;
  4166. if(_id<0)
  4167. {
  4168. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4169. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4170. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4171. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4172. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4173. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4174. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4175. shouldStep = YES;
  4176. }
  4177. else
  4178. {
  4179. if (qty) { // wish list move to cart
  4180. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4181. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4182. sqlite3_bind_int(stmt, 1, _id);
  4183. shouldStep = YES;
  4184. } else {
  4185. 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];
  4186. __block BOOL update = YES;
  4187. if (![params[@"can_create_backorder"] boolValue]) {
  4188. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4189. int newQTY = sqlite3_column_int(stmt, 0);
  4190. int availability = sqlite3_column_int(stmt, 1);
  4191. if (newQTY > availability) { // 库存不够
  4192. update = NO;
  4193. number_of_outOfStock++;
  4194. }
  4195. }];
  4196. }
  4197. if (update) {
  4198. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4199. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4200. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4201. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4202. shouldStep = YES;
  4203. }
  4204. }
  4205. }
  4206. if (shouldStep) {
  4207. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4208. [iSalesDB execSql:@"ROLLBACK" db:db];
  4209. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4210. [iSalesDB close_db:db];
  4211. DebugLog(@"add to cart error");
  4212. return [RAConvertor dict2data:ret];
  4213. }
  4214. }
  4215. }
  4216. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4217. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4218. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4219. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4220. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4221. if (![params[@"can_create_backorder"] boolValue]) {
  4222. if (number_of_outOfStock > 0) {
  4223. ret[@"result"]=[NSNumber numberWithInt:8];
  4224. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4225. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4226. }
  4227. }
  4228. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4229. [iSalesDB close_db:db];
  4230. return [RAConvertor dict2data:ret];
  4231. }
  4232. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4233. {
  4234. // UIApplication * app = [UIApplication sharedApplication];
  4235. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4236. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4237. 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];
  4238. sqlite3_stmt * statement;
  4239. int count=0;
  4240. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4241. {
  4242. while (sqlite3_step(statement) == SQLITE_ROW)
  4243. {
  4244. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4245. int bitem_id = sqlite3_column_int(statement, 0);
  4246. int bitem_qty = sqlite3_column_int(statement, 1);
  4247. char *name = (char*)sqlite3_column_text(statement, 2);
  4248. if(name==nil)
  4249. name="";
  4250. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4251. char *description = (char*)sqlite3_column_text(statement, 3);
  4252. if(description==nil)
  4253. description="";
  4254. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4255. double unit_price = sqlite3_column_double(statement, 4);
  4256. int use_unitprice = sqlite3_column_int(statement, 5);
  4257. if(use_unitprice!=1)
  4258. {
  4259. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4260. }
  4261. itemjson[@"model"]=nsname;
  4262. itemjson[@"description"]=nsdescription;
  4263. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4264. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4265. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4266. if(compute)
  4267. {
  4268. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4269. }
  4270. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4271. count++;
  4272. }
  4273. sqlite3_finalize(statement);
  4274. }
  4275. ret[@"count"]=@(count);
  4276. if(count==0)
  4277. return nil;
  4278. else
  4279. return ret;
  4280. }
  4281. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4282. {
  4283. //compute: add part to subtotal;
  4284. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4285. dict_item[@(item_id)]=@"1";
  4286. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4287. double cuft=0;
  4288. double weight=0;
  4289. int carton=0;
  4290. 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];
  4291. sqlite3_stmt * statement;
  4292. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4293. {
  4294. if (sqlite3_step(statement) == SQLITE_ROW)
  4295. {
  4296. double ulength = sqlite3_column_double(statement, 0);
  4297. double uwidth = sqlite3_column_double(statement, 1);
  4298. double uheight = sqlite3_column_double(statement, 2);
  4299. double uweight = sqlite3_column_double(statement, 3);
  4300. double mlength = sqlite3_column_double(statement, 4);
  4301. double mwidth = sqlite3_column_double(statement, 5);
  4302. double mheight = sqlite3_column_double(statement, 6);
  4303. double mweight = sqlite3_column_double(statement, 7);
  4304. double ilength = sqlite3_column_double(statement, 8);
  4305. double iwidth = sqlite3_column_double(statement, 9);
  4306. double iheight = sqlite3_column_double(statement, 10);
  4307. double iweight = sqlite3_column_double(statement, 11);
  4308. // int pcs = sqlite3_column_int(statement,12);
  4309. int mpack = sqlite3_column_int(statement, 13);
  4310. int ipack = sqlite3_column_int(statement, 14);
  4311. double ucbf = sqlite3_column_double(statement, 15);
  4312. // double icbf = sqlite3_column_double(statement, 16);
  4313. // double mcbf = sqlite3_column_double(statement, 17);
  4314. if(ipack==0)
  4315. {
  4316. carton= count/mpack ;
  4317. weight = mweight*carton;
  4318. cuft= carton*(mlength*mwidth*mheight);
  4319. int remain=count%mpack;
  4320. if(remain==0)
  4321. {
  4322. //do nothing;
  4323. }
  4324. else
  4325. {
  4326. carton++;
  4327. weight += uweight*remain;
  4328. cuft += (ulength*uwidth*uheight)*remain;
  4329. }
  4330. }
  4331. else
  4332. {
  4333. carton = count/(mpack*ipack);
  4334. weight = mweight*carton;
  4335. cuft= carton*(mlength*mwidth*mheight);
  4336. int remain=count%(mpack*ipack);
  4337. if(remain==0)
  4338. {
  4339. // do nothing;
  4340. }
  4341. else
  4342. {
  4343. carton++;
  4344. int icarton =remain/ipack;
  4345. int iremain=remain%ipack;
  4346. weight += iweight*icarton;
  4347. cuft += (ilength*iwidth*iheight)*icarton;
  4348. if(iremain==0)
  4349. {
  4350. //do nothing;
  4351. }
  4352. else
  4353. {
  4354. weight += uweight*iremain;
  4355. cuft += (ulength*uwidth*uheight)*iremain;
  4356. }
  4357. }
  4358. }
  4359. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4360. cuft=ucbf*count;
  4361. weight= uweight*count;
  4362. #endif
  4363. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4364. cuft=ucbf*count;
  4365. weight= uweight*count;
  4366. #endif
  4367. }
  4368. sqlite3_finalize(statement);
  4369. }
  4370. if(compute)
  4371. {
  4372. NSArray * arr_count=nil;
  4373. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4374. for(int i=0;i<arr_bundle.count;i++)
  4375. {
  4376. dict_item[arr_bundle[i]]=@"1";
  4377. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4378. cuft+=[bundlejson[@"cuft"] doubleValue];
  4379. weight+=[bundlejson[@"weight"] doubleValue];
  4380. carton+=[bundlejson[@"carton"] intValue];
  4381. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4382. }
  4383. }
  4384. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4385. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4386. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4387. ret[@"items"]=dict_item;
  4388. return ret;
  4389. }
  4390. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4391. {
  4392. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4393. // 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 ];
  4394. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4395. DebugLog(@"offline_login sql:%@",sqlQuery);
  4396. sqlite3_stmt * statement;
  4397. int cart_count=0;
  4398. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4399. if ( dbresult== SQLITE_OK)
  4400. {
  4401. while (sqlite3_step(statement) == SQLITE_ROW)
  4402. {
  4403. int item_id = sqlite3_column_int(statement, 0);
  4404. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4405. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4406. }
  4407. sqlite3_finalize(statement);
  4408. }
  4409. return cart_count;
  4410. }
  4411. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4412. //{
  4413. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4414. // sqlite3 *db = [iSalesDB get_db];
  4415. // UIApplication * app = [UIApplication sharedApplication];
  4416. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4417. //
  4418. // NSString* orderCode=params[@"orderCode"];
  4419. //
  4420. // int sort = [[params objectForKey:@"sort"] intValue];
  4421. // NSString *sort_str = @"";
  4422. // switch (sort) {
  4423. // case 0:{
  4424. // sort_str = @"order by c.modify_time desc";
  4425. // }
  4426. // break;
  4427. // case 1:{
  4428. // sort_str = @"order by c.modify_time asc";
  4429. // }
  4430. // break;
  4431. // case 2:{
  4432. // sort_str = @"order by m.name asc";
  4433. // }
  4434. // break;
  4435. // case 3:{
  4436. // sort_str = @"order by m.name desc";
  4437. // }
  4438. // break;
  4439. // case 4:{
  4440. // sort_str = @"order by m.description asc";
  4441. // }
  4442. // break;
  4443. //
  4444. // default:
  4445. // break;
  4446. //
  4447. // }
  4448. //
  4449. //
  4450. //
  4451. // 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 ];
  4452. //
  4453. //
  4454. //// 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 ];
  4455. //
  4456. //
  4457. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4458. //
  4459. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4460. // sqlite3_stmt * statement;
  4461. //
  4462. //
  4463. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4464. //
  4465. // NSDate *date1 = [NSDate date];
  4466. //
  4467. // int count=0;
  4468. // int cart_count=0;
  4469. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4470. // if ( dbresult== SQLITE_OK)
  4471. // {
  4472. //
  4473. //
  4474. // while (sqlite3_step(statement) == SQLITE_ROW)
  4475. // {
  4476. //// NSDate *row_date = [NSDate date];
  4477. //
  4478. //
  4479. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4480. //
  4481. //
  4482. //
  4483. //
  4484. // int product_id = sqlite3_column_int(statement, 0);
  4485. //
  4486. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4487. //
  4488. // int item_id = sqlite3_column_int(statement, 7);
  4489. //
  4490. // NSString* Price=nil;
  4491. // if(str_price==nil)
  4492. // {
  4493. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4494. //// NSDate *price_date = [NSDate date];
  4495. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4496. //// DebugLog(@"price time interval");
  4497. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4498. //
  4499. // if(price==nil)
  4500. // Price=@"No Price.";
  4501. // else
  4502. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4503. // }
  4504. // else
  4505. // {
  4506. //
  4507. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4508. // }
  4509. //
  4510. //
  4511. // double discount = sqlite3_column_double(statement, 2);
  4512. // int item_count = sqlite3_column_int(statement, 3);
  4513. //
  4514. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4515. // NSString *nsline_note=nil;
  4516. // if(line_note!=nil)
  4517. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4518. //
  4519. //
  4520. // char *name = (char*)sqlite3_column_text(statement, 5);
  4521. // NSString *nsname=nil;
  4522. // if(name!=nil)
  4523. // nsname= [[NSString alloc]initWithUTF8String:name];
  4524. //
  4525. // char *description = (char*)sqlite3_column_text(statement, 6);
  4526. // NSString *nsdescription=nil;
  4527. // if(description!=nil)
  4528. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4529. //
  4530. //
  4531. //
  4532. // int stockUom = sqlite3_column_int(statement, 8);
  4533. // int _id = sqlite3_column_int(statement, 9);
  4534. // int availability = sqlite3_column_int(statement, 10);
  4535. //
  4536. //// NSDate *subtotal_date = [NSDate date];
  4537. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4538. //// DebugLog(@"subtotal_date time interval");
  4539. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4540. //
  4541. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4542. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4543. // int carton=[bsubtotaljson[@"carton"] intValue];
  4544. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4545. //
  4546. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4547. //// NSDate *img_date = [NSDate date];
  4548. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4549. //// DebugLog(@"img_date time interval");
  4550. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4551. //
  4552. // itemjson[@"model"]=nsname;
  4553. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4554. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4555. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4556. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4557. // itemjson[@"check"]=@"true";
  4558. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4559. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4560. // itemjson[@"unit_price"]=Price;
  4561. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4562. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4563. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4564. // itemjson[@"note"]=nsline_note;
  4565. // if (!appDelegate.can_create_backorder) {
  4566. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4567. // }
  4568. //// NSDate *date2 = [NSDate date];
  4569. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4570. //// DebugLog(@"model_bundle time interval");
  4571. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4572. //
  4573. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4574. // count++;
  4575. //
  4576. //// DebugLog(@"row time interval");
  4577. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4578. // }
  4579. //
  4580. //
  4581. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4582. // sqlite3_finalize(statement);
  4583. // }
  4584. //
  4585. //
  4586. // DebugLog(@"request cart total time interval");
  4587. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4588. //
  4589. //
  4590. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4591. //
  4592. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4593. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4594. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4595. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4596. //
  4597. // ret[@"mode"]=@"Regular Mode";
  4598. //
  4599. // [iSalesDB close_db:db];
  4600. //
  4601. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4602. //
  4603. // DebugLog(@"general notes :%@",general_note);
  4604. //
  4605. // ret[@"general_note"]= general_note;
  4606. //
  4607. // return [RAConvertor dict2data:ret];
  4608. //}
  4609. +(NSData*) offline_login :(NSMutableDictionary *) params
  4610. {
  4611. NSString* user = [params valueForKey:@"user"];
  4612. NSString* password = [params valueForKey:@"password"];
  4613. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4614. sqlite3 *db = [iSalesDB get_db];
  4615. 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"]];
  4616. DebugLog(@"offline_login sql:%@",sqlQuery);
  4617. sqlite3_stmt * statement;
  4618. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4619. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4620. {
  4621. if (sqlite3_step(statement) == SQLITE_ROW)
  4622. {
  4623. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4624. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4625. int can_show_price = sqlite3_column_int(statement, 0);
  4626. int can_see_price = sqlite3_column_int(statement, 1);
  4627. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4628. if(contact_id==nil)
  4629. contact_id="";
  4630. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4631. int user_type = sqlite3_column_int(statement, 3);
  4632. int can_cancel_order = sqlite3_column_int(statement, 4);
  4633. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4634. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4635. int can_delete_order = sqlite3_column_int(statement, 7);
  4636. int can_submit_order = sqlite3_column_int(statement, 8);
  4637. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4638. int can_create_order = sqlite3_column_int(statement, 10);
  4639. char *mode = (char*)sqlite3_column_text(statement, 11);
  4640. if(mode==nil)
  4641. mode="";
  4642. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4643. char *username = (char*)sqlite3_column_text(statement, 12);
  4644. if(username==nil)
  4645. username="";
  4646. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4647. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4648. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4649. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4650. [header setValue:nscontact_id forKey:@"contact_id"];
  4651. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4652. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4653. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4654. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4655. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4656. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4657. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4658. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4659. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4660. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4661. [header setValue:nsusername forKey:@"username"];
  4662. NSError* error=nil;
  4663. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4664. [header setValue:statusFilter forKey:@"statusFilter"];
  4665. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4666. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4667. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4668. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4669. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4670. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4671. [ret setObject:header forKey:@"header"];
  4672. [ret setValue:nsmode forKey:@"mode"];
  4673. }
  4674. sqlite3_finalize(statement);
  4675. }
  4676. [iSalesDB close_db:db];
  4677. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4678. return [RAConvertor dict2data:ret];
  4679. }
  4680. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4681. {
  4682. assert(params[@"mode"]!=nil);
  4683. NSString* contactId = [params valueForKey:@"contactId"];
  4684. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4685. sqlite3 *db = [iSalesDB get_db];
  4686. NSString *sqlQuery = nil;
  4687. {
  4688. 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];
  4689. }
  4690. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4691. sqlite3_stmt * statement;
  4692. [ret setValue:@"2" forKey:@"result"];
  4693. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4694. {
  4695. //int i = 0;
  4696. if (sqlite3_step(statement) == SQLITE_ROW)
  4697. {
  4698. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4699. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4700. // int editable = sqlite3_column_int(statement, 0);
  4701. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4702. NSString *nscompany_name =nil;
  4703. if(company_name==nil)
  4704. nscompany_name=@"";
  4705. else
  4706. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4707. char *country = (char*)sqlite3_column_text(statement, 2);
  4708. if(country==nil)
  4709. country="";
  4710. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4711. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4712. // if(addr==nil)
  4713. // addr="";
  4714. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4715. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4716. if(zipcode==nil)
  4717. zipcode="";
  4718. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4719. char *state = (char*)sqlite3_column_text(statement, 5);
  4720. if(state==nil)
  4721. state="";
  4722. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4723. char *city = (char*)sqlite3_column_text(statement, 6);
  4724. if(city==nil)
  4725. city="";
  4726. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4727. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4728. // NSString *nscontact_name = nil;
  4729. // if(contact_name==nil)
  4730. // nscontact_name=@"";
  4731. // else
  4732. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4733. char *phone = (char*)sqlite3_column_text(statement, 8);
  4734. NSString *nsphone = nil;
  4735. if(phone==nil)
  4736. nsphone=@"";
  4737. else
  4738. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4739. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4740. // if(contact_id==nil)
  4741. // contact_id="";
  4742. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4743. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4744. if(addr_1==nil)
  4745. addr_1="";
  4746. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4747. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4748. if(addr_2==nil)
  4749. addr_2="";
  4750. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4751. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4752. if(addr_3==nil)
  4753. addr_3="";
  4754. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4755. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4756. if(addr_4==nil)
  4757. addr_4="";
  4758. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4759. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4760. if(first_name==nil)
  4761. first_name="";
  4762. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4763. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4764. if(last_name==nil)
  4765. last_name="";
  4766. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4767. char *fax = (char*)sqlite3_column_text(statement, 16);
  4768. NSString *nsfax = nil;
  4769. if(fax==nil)
  4770. nsfax=@"";
  4771. else
  4772. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4773. char *email = (char*)sqlite3_column_text(statement, 17);
  4774. NSString *nsemail = nil;
  4775. if(email==nil)
  4776. nsemail=@"";
  4777. else
  4778. nsemail= [[NSString alloc]initWithUTF8String:email];
  4779. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4780. NSString *nsimg_0 = nil;
  4781. if(img_0==nil)
  4782. nsimg_0=@"";
  4783. else
  4784. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4785. [self copy_bcardImg:nsimg_0];
  4786. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4787. NSString *nsimg_1 = nil;
  4788. if(img_1==nil)
  4789. nsimg_1=@"";
  4790. else
  4791. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4792. [self copy_bcardImg:nsimg_1];
  4793. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4794. NSString *nsimg_2 = nil;
  4795. if(img_2==nil)
  4796. nsimg_2=@"";
  4797. else
  4798. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4799. [self copy_bcardImg:nsimg_2];
  4800. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4801. NSString *nsprice_type = nil;
  4802. if(price_type==nil)
  4803. nsprice_type=@"";
  4804. else
  4805. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4806. char *notes = (char*)sqlite3_column_text(statement, 22);
  4807. NSString *nsnotes = nil;
  4808. if(notes==nil)
  4809. nsnotes=@"";
  4810. else
  4811. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4812. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4813. NSString *nssalesrep = nil;
  4814. if(salesrep==nil)
  4815. nssalesrep=@"";
  4816. else
  4817. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4818. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4819. {
  4820. // decrypt
  4821. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4822. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4823. nsphone=[AESCrypt fastdecrypt:nsphone];
  4824. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4825. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4826. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4827. }
  4828. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4829. [arr_name addObject:nsfirst_name];
  4830. [arr_name addObject:nslast_name];
  4831. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4832. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4833. [arr_ext addObject:nsaddr_1];
  4834. [arr_ext addObject:nsaddr_2];
  4835. [arr_ext addObject:nsaddr_3];
  4836. [arr_ext addObject:nsaddr_4];
  4837. [arr_ext addObject:@"\r\n"];
  4838. [arr_ext addObject:nscity];
  4839. [arr_ext addObject:nsstate];
  4840. [arr_ext addObject:nszipcode];
  4841. [arr_ext addObject:nscountry];
  4842. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4843. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4844. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4845. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4846. [item setValue:nscountry forKey:@"customer_country"];
  4847. [item setValue:nsphone forKey:@"customer_phone"];
  4848. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4849. [item setValue:nscompany_name forKey:@"customer_name"];
  4850. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4851. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4852. [item setValue:nsext forKey:@"customer_contact_ext"];
  4853. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4854. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4855. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4856. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4857. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4858. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4859. [item setValue:nslast_name forKey:@"customer_last_name"];
  4860. [item setValue:nscity forKey:@"customer_city"];
  4861. [item setValue:nsstate forKey:@"customer_state"];
  4862. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4863. [item setValue:contactId forKey:@"customer_cid"];
  4864. [item setValue:nscontact_name forKey:@"customer_contact"];
  4865. [item setValue:nsfax forKey:@"customer_fax"];
  4866. [item setValue:nsemail forKey:@"customer_email"];
  4867. [item setValue:contact_type forKey:@"customer_contact_type"];
  4868. [ret setObject:item forKey:@"customerInfo"];
  4869. // i++;
  4870. }
  4871. // UIApplication * app = [UIApplication sharedApplication];
  4872. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4873. [ret setValue:params[@"mode"] forKey:@"mode"];
  4874. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4875. sqlite3_finalize(statement);
  4876. }
  4877. [iSalesDB close_db:db];
  4878. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4879. return ret;
  4880. }
  4881. + (bool) copy_bcardImg:(NSString*) filename
  4882. {
  4883. if(filename.length==0)
  4884. return false;
  4885. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4886. bool ret=false;
  4887. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4888. NSString *cachefolder = [paths objectAtIndex:0];
  4889. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4890. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4891. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4892. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4893. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4894. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4895. //
  4896. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4897. NSFileManager* fileManager = [NSFileManager defaultManager];
  4898. BOOL bdir=NO;
  4899. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4900. {
  4901. NSError *error = nil;
  4902. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4903. {
  4904. ret=false;
  4905. }
  4906. else
  4907. {
  4908. ret=true;
  4909. }
  4910. // NSError *error = nil;
  4911. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4912. //
  4913. // if(!bsuccess)
  4914. // {
  4915. // DebugLog(@"Create offline_createimg folder failed");
  4916. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4917. // return [RAConvertor dict2data:ret];
  4918. // }
  4919. // if(bsuccess)
  4920. // {
  4921. // sqlite3 *db = [self get_db];
  4922. //
  4923. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4924. // [iSalesDB close_db:db];
  4925. // }
  4926. }
  4927. return ret;
  4928. //
  4929. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4930. // if(bsuccess)
  4931. // {
  4932. // NSError *error = nil;
  4933. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4934. // {
  4935. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4936. // }
  4937. // else
  4938. // {
  4939. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4940. //
  4941. // ret[@"img_url_aname"]=filename;
  4942. // ret[@"img_url"]=savedImagePath;
  4943. // }
  4944. // }
  4945. }
  4946. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4947. {
  4948. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4949. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4950. NSString *cachefolder = [paths objectAtIndex:0];
  4951. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4952. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4953. NSFileManager* fileManager = [NSFileManager defaultManager];
  4954. BOOL bdir=YES;
  4955. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4956. {
  4957. NSError *error = nil;
  4958. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4959. if(!bsuccess)
  4960. {
  4961. DebugLog(@"Create offline_createimg folder failed");
  4962. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4963. return [RAConvertor dict2data:ret];
  4964. }
  4965. // if(bsuccess)
  4966. // {
  4967. // sqlite3 *db = [self get_db];
  4968. //
  4969. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4970. // [iSalesDB close_db:db];
  4971. // }
  4972. }
  4973. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4974. {
  4975. NSError *error = nil;
  4976. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4977. if(!bsuccess)
  4978. {
  4979. DebugLog(@"Create img_cache folder failed");
  4980. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4981. return [RAConvertor dict2data:ret];
  4982. }
  4983. // if(bsuccess)
  4984. // {
  4985. // sqlite3 *db = [self get_db];
  4986. //
  4987. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4988. // [iSalesDB close_db:db];
  4989. // }
  4990. }
  4991. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4992. //JEPG格式
  4993. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4994. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4995. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4996. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4997. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4998. if(bsuccess)
  4999. {
  5000. NSError *error = nil;
  5001. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  5002. {
  5003. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5004. }
  5005. else
  5006. {
  5007. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  5008. ret[@"img_url_aname"]=filename;
  5009. ret[@"img_url"]=filename;
  5010. }
  5011. }
  5012. else
  5013. {
  5014. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  5015. }
  5016. return [RAConvertor dict2data:ret];
  5017. }
  5018. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  5019. {
  5020. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  5021. category = [category substringToIndex:3];
  5022. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5023. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  5024. params[@"category"]= category;
  5025. ret[@"params"]= params;
  5026. [ret setValue:@"detail" forKey:@"target"];
  5027. [ret setValue:@"popup" forKey:@"action"];
  5028. [ret setValue:@"content" forKey:@"type"];
  5029. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  5030. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  5031. [ret setValue:@"true" forKey:@"single_row"];
  5032. [ret setValue:@"true" forKey:@"partial_refresh"];
  5033. // sqlite3 *db = [iSalesDB get_db];
  5034. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  5035. NSString *sqlQuery =nil;
  5036. #ifdef BUILD_NPD
  5037. 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 ;
  5038. #else
  5039. 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];
  5040. #endif
  5041. sqlite3_stmt * statement;
  5042. int count = 0;
  5043. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  5044. // int count=0;
  5045. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5046. {
  5047. int i=0;
  5048. while (sqlite3_step(statement) == SQLITE_ROW)
  5049. {
  5050. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5051. // char *name = (char*)sqlite3_column_text(statement, 1);
  5052. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5053. char *name = (char*)sqlite3_column_text(statement, 0);
  5054. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5055. int product_id = sqlite3_column_int(statement, 1);
  5056. char *url = (char*)sqlite3_column_text(statement, 2);
  5057. if(url==nil)
  5058. url="";
  5059. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5060. [item setValue:nsurl forKey:@"picture_path"];
  5061. [item setValue:nsname forKey:@"fash_name"];
  5062. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5063. [item setValue:category forKey:@"category"];
  5064. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5065. i++;
  5066. }
  5067. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5068. sqlite3_finalize(statement);
  5069. }
  5070. DebugLog(@"count:%d",count);
  5071. // [iSalesDB close_db:db];
  5072. return ret;
  5073. }
  5074. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  5075. {
  5076. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5077. [ret setValue:key forKey:@"key"];
  5078. [ret setValue:value forKey:@"val"];
  5079. [ret setValue:@"price" forKey:@"type"];
  5080. return ret;
  5081. }
  5082. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  5083. {
  5084. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5085. [ret setValue:key forKey:@"key"];
  5086. [ret setValue:value forKey:@"val"];
  5087. return ret;
  5088. }
  5089. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  5090. {
  5091. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5092. [ret setValue:@"0" forKey:@"img_count"];
  5093. // sqlite3 *db = [iSalesDB get_db];
  5094. 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 ;
  5095. sqlite3_stmt * statement;
  5096. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5097. {
  5098. int i=0;
  5099. if (sqlite3_step(statement) == SQLITE_ROW)
  5100. {
  5101. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5102. // char *name = (char*)sqlite3_column_text(statement, 1);
  5103. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5104. char *value = (char*)sqlite3_column_text(statement, 0);
  5105. if(value==nil)
  5106. value="";
  5107. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5108. char *key = (char*)sqlite3_column_text(statement, 1);
  5109. if(key==nil)
  5110. key="";
  5111. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5112. [item setValue:nsvalue forKey:@"val"];
  5113. [item setValue:nskey forKey:@"key"];
  5114. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5115. [ret setValue:@"1" forKey:@"count"];
  5116. i++;
  5117. }
  5118. sqlite3_finalize(statement);
  5119. }
  5120. // [iSalesDB close_db:db];
  5121. return ret;
  5122. }
  5123. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5124. {
  5125. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5126. [ret setValue:@"0" forKey:@"count"];
  5127. // sqlite3 *db = [iSalesDB get_db];
  5128. 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;
  5129. sqlite3_stmt * statement;
  5130. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5131. {
  5132. int i=0;
  5133. while (sqlite3_step(statement) == SQLITE_ROW)
  5134. {
  5135. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5136. // char *name = (char*)sqlite3_column_text(statement, 1);
  5137. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5138. char *value = (char*)sqlite3_column_text(statement, 0);
  5139. if(value==nil)
  5140. value="";
  5141. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5142. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5143. if(selector_display==nil)
  5144. selector_display="";
  5145. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5146. int product_id = sqlite3_column_int(statement, 2);
  5147. char *category = (char*)sqlite3_column_text(statement, 3);
  5148. if(category==nil)
  5149. category="";
  5150. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5151. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5152. [item setValue:nsvalue forKey:@"title"];
  5153. [item setValue:url forKey:@"pic_url"];
  5154. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5155. [params setValue:@"2" forKey:@"count"];
  5156. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5157. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5158. [param0 setValue:@"product_id" forKey:@"name"];
  5159. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5160. [param1 setValue:nscategory forKey:@"val"];
  5161. [param1 setValue:@"category" forKey:@"name"];
  5162. [params setObject:param0 forKey:@"param_0"];
  5163. [params setObject:param1 forKey:@"param_1"];
  5164. [item setObject:params forKey:@"params"];
  5165. [ret setValue:nsselector_display forKey:@"name"];
  5166. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5167. i++;
  5168. }
  5169. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5170. [ret setValue:@"switch" forKey:@"action"];
  5171. sqlite3_finalize(statement);
  5172. }
  5173. // [iSalesDB close_db:db];
  5174. return ret;
  5175. }
  5176. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5177. {
  5178. // model 在 category search 显示的图片。
  5179. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5180. // sqlite3 *db = [iSalesDB get_db];
  5181. 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];
  5182. sqlite3_stmt * statement;
  5183. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5184. {
  5185. while (sqlite3_step(statement) == SQLITE_ROW)
  5186. {
  5187. char *url = (char*)sqlite3_column_text(statement, 0);
  5188. if(url==nil)
  5189. url="";
  5190. int type = sqlite3_column_int(statement, 1);
  5191. if(type==0)
  5192. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5193. else
  5194. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5195. }
  5196. sqlite3_finalize(statement);
  5197. }
  5198. // [iSalesDB close_db:db];
  5199. return ret;
  5200. }
  5201. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5202. {
  5203. int item_id=-1;
  5204. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5205. sqlite3_stmt * statement;
  5206. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5207. {
  5208. if (sqlite3_step(statement) == SQLITE_ROW)
  5209. {
  5210. item_id = sqlite3_column_int(statement, 0);
  5211. }
  5212. sqlite3_finalize(statement);
  5213. }
  5214. return item_id;
  5215. }
  5216. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5217. {
  5218. // NSString* ret = @"";
  5219. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5220. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5221. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5222. sqlite3_stmt * statement;
  5223. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5224. {
  5225. while (sqlite3_step(statement) == SQLITE_ROW)
  5226. {
  5227. int bitem_id = sqlite3_column_int(statement, 0);
  5228. int bitem_qty = sqlite3_column_int(statement, 1);
  5229. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5230. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5231. }
  5232. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5233. sqlite3_finalize(statement);
  5234. }
  5235. // if(ret==nil)
  5236. // ret=@"";
  5237. *count=arr_count;
  5238. return arr_bundle;
  5239. }
  5240. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5241. {
  5242. // get default sold qty, return -1 if model not found;
  5243. int ret = -1;
  5244. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5245. sqlite3_stmt * statement;
  5246. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5247. {
  5248. if (sqlite3_step(statement) == SQLITE_ROW)
  5249. {
  5250. ret = sqlite3_column_int(statement, 0);
  5251. }
  5252. sqlite3_finalize(statement);
  5253. }
  5254. return ret;
  5255. }
  5256. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5257. {
  5258. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5259. [ret setValue:@"0" forKey:@"img_count"];
  5260. // sqlite3 *db = [iSalesDB get_db];
  5261. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5262. sqlite3_stmt * statement;
  5263. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5264. {
  5265. int i=0;
  5266. while (sqlite3_step(statement) == SQLITE_ROW)
  5267. {
  5268. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5269. // char *name = (char*)sqlite3_column_text(statement, 1);
  5270. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5271. char *url = (char*)sqlite3_column_text(statement, 0);
  5272. if(url==nil)
  5273. url="";
  5274. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5275. [item setValue:nsurl forKey:@"s"];
  5276. [item setValue:nsurl forKey:@"l"];
  5277. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5278. i++;
  5279. }
  5280. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5281. sqlite3_finalize(statement);
  5282. }
  5283. // [iSalesDB close_db:db];
  5284. return ret;
  5285. }
  5286. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5287. // UIApplication * app = [UIApplication sharedApplication];
  5288. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5289. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5290. sqlite3 *db = [iSalesDB get_db];
  5291. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5292. NSString* product_id=params[@"product_id"];
  5293. NSString *item_count_str = params[@"item_count"];
  5294. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5295. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5296. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5297. // NSString *sql = @"";
  5298. for(int i=0;i<arr.count;i++)
  5299. {
  5300. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5301. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5302. __block int cart_count = 0;
  5303. if (!item_count_str) {
  5304. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5305. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5306. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5307. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5308. cart_count = [[model_set_components lastObject] intValue];
  5309. }];
  5310. }
  5311. if(count==0)
  5312. {
  5313. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5314. sqlite3_stmt *stmt;
  5315. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5316. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5317. if (item_count_arr) {
  5318. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5319. } else {
  5320. sqlite3_bind_int(stmt,2,cart_count);
  5321. }
  5322. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5323. [iSalesDB execSql:@"ROLLBACK" db:db];
  5324. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5325. [iSalesDB close_db:db];
  5326. DebugLog(@"add to wishlist error");
  5327. return ret;
  5328. }
  5329. } else {
  5330. int qty = 0;
  5331. if (item_count_arr) {
  5332. qty = [item_count_arr[i] intValue];
  5333. } else {
  5334. qty = cart_count;
  5335. }
  5336. 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]];
  5337. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5338. [iSalesDB execSql:@"ROLLBACK" db:db];
  5339. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5340. [iSalesDB close_db:db];
  5341. DebugLog(@"add to wishlist error");
  5342. return ret;
  5343. }
  5344. }
  5345. }
  5346. // [iSalesDB execSql:sql db:db];
  5347. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5348. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5349. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5350. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5351. [iSalesDB close_db:db];
  5352. ret[@"wish_count"]=@(count);
  5353. ret[@"result"]= [NSNumber numberWithInt:2];
  5354. return ret;
  5355. }
  5356. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5357. // 0 category
  5358. // 1 search
  5359. // 2 itemsearch
  5360. NSData *ret = nil;
  5361. NSDictionary *items = nil;
  5362. switch (from) {
  5363. case 0:{
  5364. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5365. }
  5366. break;
  5367. case 1:{
  5368. items = [[self search:params limited:NO] objectForKey:@"items"];
  5369. }
  5370. break;
  5371. case 2:{
  5372. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5373. }
  5374. break;
  5375. default:
  5376. break;
  5377. }
  5378. if (!items) {
  5379. return ret;
  5380. }
  5381. int count = [[items objectForKey:@"count"] intValue];
  5382. NSMutableString *product_id_str = [@"" mutableCopy];
  5383. for (int i = 0; i < count; i++) {
  5384. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5385. NSDictionary *item = [items objectForKey:key];
  5386. NSString *product_id = [item objectForKey:@"product_id"];
  5387. if (i == 0) {
  5388. [product_id_str appendString:product_id];
  5389. } else {
  5390. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5391. }
  5392. }
  5393. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5394. if ([add_to isEqualToString:@"cart"]) {
  5395. NSString *order_code = [params objectForKey:@"orderCode"];
  5396. if (order_code.length) {
  5397. NSDictionary *newParams = @{
  5398. @"product_id" : product_id_str,
  5399. @"orderCode" : order_code,
  5400. @"can_create_backorder":params[@"can_create_backorder"]
  5401. };
  5402. ret = [self offline_add2cart:newParams.mutableCopy];
  5403. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5404. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5405. ret = [RAConvertor dict2data:retDic];
  5406. }
  5407. } else if([add_to isEqualToString:@"wishlist"]) {
  5408. NSDictionary *newParams = @{
  5409. @"product_id" : product_id_str
  5410. };
  5411. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5412. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5413. ret = [RAConvertor dict2data:retDic];
  5414. } else if([add_to isEqualToString:@"portfolio"]) {
  5415. NSDictionary *newParams = @{
  5416. @"product_id" : product_id_str
  5417. };
  5418. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5419. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5420. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5421. ret = [RAConvertor dict2data:retDic];
  5422. }
  5423. return ret;
  5424. }
  5425. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5426. {
  5427. return [self addAll:params from:0];
  5428. }
  5429. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5430. {
  5431. return [self addAll:params from:1];
  5432. }
  5433. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5434. {
  5435. return [self addAll:params from:2];
  5436. }
  5437. #pragma mark - Jack
  5438. #warning 做SQL操作时转义!!
  5439. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5440. // "val_227" : {
  5441. // "check" : 1,
  5442. // "value" : "US United States",
  5443. // "value_id" : "228"
  5444. // },
  5445. if (!countryCode) {
  5446. countryCode = @"US";
  5447. }
  5448. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5449. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5450. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5451. int code_id = sqlite3_column_int(stmt, 3); // id
  5452. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5453. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5454. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5455. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5456. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5457. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5458. }
  5459. long n = *count;
  5460. *count = n + 1;
  5461. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5462. [container setValue:countryDic forKey:key];
  5463. }] mutableCopy];
  5464. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5465. return ret;
  5466. }
  5467. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5468. countryCode = [self translateSingleQuote:countryCode];
  5469. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5470. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5471. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5472. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5473. if (name == NULL) {
  5474. name = "";
  5475. }
  5476. if (code == NULL) {
  5477. code = "";
  5478. }
  5479. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5480. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5481. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5482. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5483. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5484. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5485. }
  5486. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5487. [container setValue:stateDic forKey:key];
  5488. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5489. DebugLog(@"query all state error: %@",err_msg);
  5490. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5491. // [stateDic setValue:@"Other" forKey:@"value"];
  5492. // [stateDic setValue:@"" forKey:@"value_id"];
  5493. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5494. //
  5495. // if (state_code && [@"" isEqualToString:state_code]) {
  5496. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5497. // }
  5498. //
  5499. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5500. // [container setValue:stateDic forKey:key];
  5501. }] mutableCopy];
  5502. [ret removeObjectForKey:@"result"];
  5503. // failure 可以不用了,一样的
  5504. if (ret.allKeys.count == 0) {
  5505. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5506. [stateDic setValue:@"Other" forKey:@"value"];
  5507. [stateDic setValue:@"" forKey:@"value_id"];
  5508. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5509. if (state_code && [@"" isEqualToString:state_code]) {
  5510. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5511. }
  5512. NSString *key = [NSString stringWithFormat:@"val_0"];
  5513. [ret setValue:stateDic forKey:key];
  5514. }
  5515. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5516. return ret;
  5517. }
  5518. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5519. codeId = [self translateSingleQuote:codeId];
  5520. 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];
  5521. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5522. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5523. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5524. if (name == NULL) {
  5525. name = "";
  5526. }
  5527. if (code == NULL) {
  5528. code = "";
  5529. }
  5530. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5531. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5532. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5533. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5534. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5535. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5536. }
  5537. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5538. [container setValue:stateDic forKey:key];
  5539. }] mutableCopy];
  5540. [ret removeObjectForKey:@"result"];
  5541. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5542. return ret;
  5543. }
  5544. + (NSDictionary *)offline_getPrice {
  5545. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5546. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5547. char *name = (char *) sqlite3_column_text(stmt, 1);
  5548. int type = sqlite3_column_int(stmt, 2);
  5549. int orderBy = sqlite3_column_int(stmt, 3);
  5550. if (name == NULL) {
  5551. name = "";
  5552. }
  5553. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5554. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5555. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5556. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5557. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5558. if (orderBy == 0) {
  5559. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5560. }
  5561. [container setValue:priceDic forKey:key];
  5562. }] mutableCopy];
  5563. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5564. return ret;
  5565. }
  5566. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5567. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5568. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5569. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5570. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5571. // int _id = sqlite3_column_int(stmt, 0);
  5572. NSString *name = [self textAtColumn:1 statement:stmt];
  5573. NSDictionary *typeDic = @{
  5574. @"value_id" : name,
  5575. @"value" : name,
  5576. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5577. };
  5578. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5579. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5580. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5581. }];
  5582. return ret;
  5583. }
  5584. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5585. // 首先从offline_login表中取出sales_code
  5586. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5587. // NSString *user = app.user;
  5588. user = [self translateSingleQuote:user];
  5589. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5590. __block NSString *user_code = @"";
  5591. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5592. char *code = (char *)sqlite3_column_text(stmt, 0);
  5593. if (code == NULL) {
  5594. code = "";
  5595. }
  5596. user_code = [NSString stringWithUTF8String:code];
  5597. }];
  5598. // 再取所有salesRep
  5599. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5600. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5601. // 1 name 2 code 3 salesrep_id
  5602. char *name = (char *)sqlite3_column_text(stmt, 1);
  5603. char *code = (char *)sqlite3_column_text(stmt, 2);
  5604. int salesrep_id = sqlite3_column_int(stmt, 3);
  5605. if (name == NULL) {
  5606. name = "";
  5607. }
  5608. if (code == NULL) {
  5609. code = "";
  5610. }
  5611. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5612. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5613. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5614. // 比较code 相等则check
  5615. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5616. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5617. }
  5618. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5619. }] mutableCopy];
  5620. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5621. return ret;
  5622. }
  5623. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5624. zipcode = [self translateSingleQuote:zipcode];
  5625. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5626. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5627. char *country = (char *)sqlite3_column_text(stmt, 0);
  5628. char *state = (char *)sqlite3_column_text(stmt, 1);
  5629. char *city = (char *)sqlite3_column_text(stmt, 2);
  5630. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5631. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5632. if (country == NULL) {
  5633. country = "";
  5634. }
  5635. if (state == NULL) {
  5636. state = "";
  5637. }
  5638. if (city == NULL) {
  5639. city = "";
  5640. }
  5641. if (country_code == NULL) {
  5642. country_code = "";
  5643. }
  5644. if (state_code == NULL) {
  5645. state_code = "";
  5646. }
  5647. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5648. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5649. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5650. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5651. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5652. }] mutableCopy];
  5653. return ret;
  5654. }
  5655. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5656. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5657. [item setValue:value forKey:valueKey];
  5658. [dic setValue:item forKey:itemKey];
  5659. }
  5660. + (NSString *)countryCodeByid:(NSString *)code_id {
  5661. NSString *ret = nil;
  5662. code_id = [self translateSingleQuote:code_id];
  5663. sqlite3 *db = [iSalesDB get_db];
  5664. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5665. sqlite3_stmt * statement;
  5666. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5667. while (sqlite3_step(statement) == SQLITE_ROW) {
  5668. char *code = (char *)sqlite3_column_text(statement, 0);
  5669. if (code == NULL) {
  5670. code = "";
  5671. }
  5672. ret = [NSString stringWithUTF8String:code];
  5673. }
  5674. sqlite3_finalize(statement);
  5675. }
  5676. [iSalesDB close_db:db];
  5677. return ret;
  5678. }
  5679. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5680. NSString *ret = nil;
  5681. code = [self translateSingleQuote:code];
  5682. sqlite3 *db = [iSalesDB get_db];
  5683. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5684. sqlite3_stmt * statement;
  5685. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5686. while (sqlite3_step(statement) == SQLITE_ROW) {
  5687. char *_id = (char *)sqlite3_column_text(statement, 0);
  5688. if (_id == NULL) {
  5689. _id = "";
  5690. }
  5691. ret = [NSString stringWithFormat:@"%s",_id];
  5692. }
  5693. sqlite3_finalize(statement);
  5694. }
  5695. [iSalesDB close_db:db];
  5696. return ret;
  5697. }
  5698. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5699. NSString *name = nil;
  5700. codeId = [self translateSingleQuote:codeId];
  5701. sqlite3 *db = [iSalesDB get_db];
  5702. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5703. sqlite3_stmt * statement;
  5704. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5705. while (sqlite3_step(statement) == SQLITE_ROW) {
  5706. char *value = (char *)sqlite3_column_text(statement, 0);
  5707. if (value == NULL) {
  5708. value = "";
  5709. }
  5710. name = [NSString stringWithUTF8String:value];
  5711. }
  5712. sqlite3_finalize(statement);
  5713. }
  5714. [iSalesDB close_db:db];
  5715. return name;
  5716. }
  5717. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5718. NSString *ret = nil;
  5719. sqlite3 *db = [iSalesDB get_db];
  5720. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5721. sqlite3_stmt * statement;
  5722. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5723. while (sqlite3_step(statement) == SQLITE_ROW) {
  5724. char *name = (char *)sqlite3_column_text(statement, 0);
  5725. if (name == NULL) {
  5726. name = "";
  5727. }
  5728. ret = [NSString stringWithUTF8String:name];
  5729. }
  5730. sqlite3_finalize(statement);
  5731. }
  5732. [iSalesDB close_db:db];
  5733. return ret;
  5734. }
  5735. + (NSString *)salesRepCodeById:(NSString *)_id {
  5736. NSString *ret = nil;
  5737. _id = [self translateSingleQuote:_id];
  5738. sqlite3 *db = [iSalesDB get_db];
  5739. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5740. sqlite3_stmt * statement;
  5741. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5742. while (sqlite3_step(statement) == SQLITE_ROW) {
  5743. char *rep = (char *)sqlite3_column_text(statement, 0);
  5744. if (rep == NULL) {
  5745. rep = "";
  5746. }
  5747. ret = [NSString stringWithUTF8String:rep];
  5748. }
  5749. sqlite3_finalize(statement);
  5750. }
  5751. [iSalesDB close_db:db];
  5752. return ret;
  5753. }
  5754. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5755. char *tx = (char *)sqlite3_column_text(stmt, col);
  5756. if (tx == NULL) {
  5757. tx = "";
  5758. }
  5759. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5760. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5761. if (!text) {
  5762. text = @"";
  5763. }
  5764. // 将字符全部为' '的字符串干掉
  5765. int spaceCount = 0;
  5766. for (int i = 0; i < text.length; i++) {
  5767. if ([text characterAtIndex:i] == ' ') {
  5768. spaceCount++;
  5769. }
  5770. }
  5771. if (spaceCount == text.length) {
  5772. text = @"";
  5773. }
  5774. return text;
  5775. }
  5776. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5777. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5778. NSData *data = [NSData dataWithContentsOfFile:path];
  5779. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5780. return ret;
  5781. }
  5782. + (NSString *)textFileName:(NSString *)name {
  5783. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5784. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5785. if (!text) {
  5786. text = @"";
  5787. }
  5788. return text;
  5789. }
  5790. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5791. return [[dic objectForKey:key] mutableCopy];
  5792. }
  5793. + (id)translateSingleQuote:(NSString *)string {
  5794. if ([string isKindOfClass:[NSString class]])
  5795. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5796. return string;
  5797. }
  5798. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5799. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5800. }
  5801. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5802. NSString* ret= nil;
  5803. NSString *sqlQuery = nil;
  5804. // 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
  5805. 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];
  5806. sqlite3_stmt * statement;
  5807. // int count=0;
  5808. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5809. {
  5810. if (sqlite3_step(statement) == SQLITE_ROW)
  5811. {
  5812. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5813. if(imgurl==nil)
  5814. imgurl="";
  5815. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5816. ret=nsimgurl;
  5817. }
  5818. sqlite3_finalize(statement);
  5819. }
  5820. else
  5821. {
  5822. [ret setValue:@"8" forKey:@"result"];
  5823. }
  5824. // [iSalesDB close_db:db];
  5825. // DebugLog(@"data string: %@",ret );
  5826. return ret;
  5827. }
  5828. #pragma mark contact Advanced search
  5829. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5830. {
  5831. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5832. return [RAConvertor dict2data:contactAdvanceDic];
  5833. }
  5834. #pragma mark create new contact
  5835. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5836. assert(params[@"user"]!=nil);
  5837. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5838. NSData *data = [NSData dataWithContentsOfFile:path];
  5839. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5840. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5841. NSString *countryCode = nil;
  5842. NSString *countryCode_id = nil;
  5843. NSString *stateCode = nil;
  5844. NSString *city = nil;
  5845. NSString *zipCode = nil;
  5846. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5847. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5848. NSString *code_id = params[@"country"];
  5849. countryCode_id = code_id;
  5850. countryCode = [self countryCodeByid:code_id];
  5851. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5852. NSString *zip_code = params[@"zipcode"];
  5853. // 剔除全部为空格
  5854. int spaceCount = 0;
  5855. for (int i = 0; i < zip_code.length; i++) {
  5856. if ([zip_code characterAtIndex:i] == ' ') {
  5857. spaceCount++;
  5858. }
  5859. }
  5860. if (spaceCount == zip_code.length) {
  5861. zip_code = @"";
  5862. }
  5863. zipCode = zip_code;
  5864. if (zipCode.length > 0) {
  5865. countryCode_id = params[@"country"];
  5866. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5867. countryCode = [dic valueForKey:@"country_code"];
  5868. if (!countryCode) {
  5869. // countryCode = @"US";
  5870. NSString *code_id = params[@"country"];
  5871. countryCode = [self countryCodeByid:code_id];
  5872. }
  5873. stateCode = [dic valueForKey:@"state_code"];
  5874. if (!stateCode.length) {
  5875. stateCode = params[@"state"];
  5876. }
  5877. city = [dic valueForKey:@"city"];
  5878. if (!city.length) {
  5879. city = params[@"city"];
  5880. }
  5881. // zip code
  5882. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5883. [zipDic setValue:zipCode forKey:@"value"];
  5884. [section_0 setValue:zipDic forKey:@"item_11"];
  5885. } else {
  5886. NSString *code_id = params[@"country"];
  5887. countryCode = [self countryCodeByid:code_id];
  5888. stateCode = params[@"state"];
  5889. city = params[@"city"];
  5890. }
  5891. }
  5892. } else {
  5893. // default: US United States
  5894. countryCode = @"US";
  5895. countryCode_id = @"228";
  5896. }
  5897. // country
  5898. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5899. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5900. // state
  5901. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5902. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5903. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5904. // city
  5905. if (city) {
  5906. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5907. [cityDic setValue:city forKey:@"value"];
  5908. [section_0 setValue:cityDic forKey:@"item_13"];
  5909. }
  5910. // price type
  5911. NSDictionary *priceDic = [self offline_getPrice];
  5912. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5913. // contact type
  5914. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5915. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5916. // Sales Rep
  5917. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5918. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5919. [ret setValue:section_0 forKey:@"section_0"];
  5920. return [RAConvertor dict2data:ret];
  5921. }
  5922. #pragma mark save
  5923. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5924. NSString *addr = nil;
  5925. NSString *contact_name = nil;
  5926. NSString *companyName = [params objectForKey:@"company"];
  5927. if (companyName) {
  5928. companyName = [AESCrypt fastencrypt:companyName];
  5929. } else {
  5930. companyName = @"";
  5931. }
  5932. DebugLog(@"company");
  5933. companyName = [self translateSingleQuote:companyName];
  5934. NSString *addr1 = [params objectForKey:@"address"];
  5935. NSString *addr2 = [params objectForKey:@"address2"];
  5936. NSString *addr3 = [params objectForKey:@"address_3"];
  5937. NSString *addr4 = [params objectForKey:@"address_4"];
  5938. if (!addr2) {
  5939. addr2 = @"";
  5940. }
  5941. if (!addr3) {
  5942. addr3 = @"";
  5943. }
  5944. if (!addr4) {
  5945. addr4 = @"";
  5946. }
  5947. if (!addr1) {
  5948. addr1 = @"";
  5949. }
  5950. DebugLog(@"addr");
  5951. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5952. addr = [AESCrypt fastencrypt:addr];
  5953. addr = [self translateSingleQuote:addr];
  5954. if (addr1 && ![addr1 isEqualToString:@""]) {
  5955. addr1 = [AESCrypt fastencrypt:addr1];
  5956. }
  5957. addr1 = [self translateSingleQuote:addr1];
  5958. addr2 = [self translateSingleQuote:addr2];
  5959. addr3 = [self translateSingleQuote:addr3];
  5960. addr4 = [self translateSingleQuote:addr4];
  5961. NSString *country = [params objectForKey:@"country"];
  5962. if (country) {
  5963. country = [self countryNameByCountryCodeId:country];
  5964. } else {
  5965. country = @"";
  5966. }
  5967. DebugLog(@"country");
  5968. country = [self translateSingleQuote:country];
  5969. NSString *state = [params objectForKey:@"state"];
  5970. if (!state) {
  5971. state = @"";
  5972. }
  5973. DebugLog(@"state");
  5974. state = [self translateSingleQuote:state];
  5975. NSString *city = [params objectForKey:@"city"];
  5976. if (!city) {
  5977. city = @"";
  5978. }
  5979. city = [self translateSingleQuote:city];
  5980. NSString *zipcode = [params objectForKey:@"zipcode"];
  5981. if (!zipcode) {
  5982. zipcode = @"";
  5983. }
  5984. DebugLog(@"zip");
  5985. zipcode = [self translateSingleQuote:zipcode];
  5986. NSString *fistName = [params objectForKey:@"firstname"];
  5987. if (!fistName) {
  5988. fistName = @"";
  5989. }
  5990. NSString *lastName = [params objectForKey:@"lastname"];
  5991. if (!lastName) {
  5992. lastName = @"";
  5993. }
  5994. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5995. DebugLog(@"contact_name");
  5996. contact_name = [self translateSingleQuote:contact_name];
  5997. fistName = [self translateSingleQuote:fistName];
  5998. lastName = [self translateSingleQuote:lastName];
  5999. NSString *phone = [params objectForKey:@"phone"];
  6000. if (phone) {
  6001. phone = [AESCrypt fastencrypt:phone];
  6002. } else {
  6003. phone = @"";
  6004. }
  6005. DebugLog(@"PHONE");
  6006. phone = [self translateSingleQuote:phone];
  6007. NSString *fax = [params objectForKey:@"fax"];
  6008. if (!fax) {
  6009. fax = @"";
  6010. }
  6011. DebugLog(@"FAX");
  6012. fax = [self translateSingleQuote:fax];
  6013. NSString *email = [params objectForKey:@"email"];
  6014. if (!email) {
  6015. email = @"";
  6016. }
  6017. DebugLog(@"EMAIL:%@",email);
  6018. email = [self translateSingleQuote:email];
  6019. NSString *notes = [params objectForKey:@"contact_notes"];
  6020. if (!notes) {
  6021. notes = @"";
  6022. }
  6023. DebugLog(@"NOTE:%@",notes);
  6024. notes = [self translateSingleQuote:notes];
  6025. NSString *price = [params objectForKey:@"price_name"];
  6026. if (price) {
  6027. price = [self priceNameByPriceId:price];
  6028. } else {
  6029. price = @"";
  6030. }
  6031. DebugLog(@"PRICE");
  6032. price = [self translateSingleQuote:price];
  6033. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6034. if (salesRep) {
  6035. salesRep = [self salesRepCodeById:salesRep];
  6036. } else {
  6037. salesRep = @"";
  6038. }
  6039. salesRep = [self translateSingleQuote:salesRep];
  6040. NSString *img = [params objectForKey:@"business_card"];
  6041. NSArray *array = [img componentsSeparatedByString:@","];
  6042. NSString *img_0 = array[0];
  6043. if (!img_0) {
  6044. img_0 = @"";
  6045. }
  6046. img_0 = [self translateSingleQuote:img_0];
  6047. NSString *img_1 = array[1];
  6048. if (!img_1) {
  6049. img_1 = @"";
  6050. }
  6051. img_1 = [self translateSingleQuote:img_1];
  6052. NSString *img_2 = array[2];
  6053. if (!img_2) {
  6054. img_2 = @"";
  6055. }
  6056. img_2 = [self translateSingleQuote:img_2];
  6057. NSString *contact_id = [NSUUID UUID].UUIDString;
  6058. NSString *contact_type = [params objectForKey:@"type_name"];
  6059. if (!contact_type) {
  6060. contact_type = @"";
  6061. }
  6062. contact_type = [self translateSingleQuote:contact_type];
  6063. // 判断更新时是否为customer
  6064. if (update) {
  6065. contact_id = [params objectForKey:@"contact_id"];
  6066. if (!contact_id) {
  6067. contact_id = @"";
  6068. }
  6069. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6070. __block int customer = 0;
  6071. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6072. customer = sqlite3_column_int(stmt, 0);
  6073. }];
  6074. isCustomer = customer ? YES : NO;
  6075. }
  6076. NSMutableDictionary *sync_dic = [params mutableCopy];
  6077. if (isCustomer) {
  6078. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6079. } else {
  6080. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6081. }
  6082. NSString *sync_data = nil;
  6083. NSString *sql = nil;
  6084. if (update){
  6085. contact_id = [params objectForKey:@"contact_id"];
  6086. if (!contact_id) {
  6087. contact_id = @"";
  6088. }
  6089. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6090. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6091. sync_data = [RAConvertor dict2string:sync_dic];
  6092. sync_data = [self translateSingleQuote:sync_data];
  6093. 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];
  6094. } else {
  6095. contact_id = [self translateSingleQuote:contact_id];
  6096. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6097. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6098. sync_data = [RAConvertor dict2string:sync_dic];
  6099. sync_data = [self translateSingleQuote:sync_data];
  6100. 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];
  6101. }
  6102. int result = [iSalesDB execSql:sql];
  6103. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6104. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6105. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6106. }
  6107. #pragma mark save new contact
  6108. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6109. {
  6110. return [self offline_saveContact:params update:NO isCustomer:YES];
  6111. }
  6112. #pragma mark edit contact
  6113. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6114. {
  6115. assert(params[@"user"]!=nil);
  6116. // {
  6117. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6118. // password = 123456;
  6119. // user = EvanK;
  6120. // }
  6121. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6122. NSData *data = [NSData dataWithContentsOfFile:path];
  6123. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6124. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6125. NSString *countryCode = nil;
  6126. NSString *countryCode_id = nil;
  6127. NSString *stateCode = nil;
  6128. /*------contact infor------*/
  6129. __block NSString *country = nil;
  6130. __block NSString *company_name = nil;
  6131. __block NSString *contact_id = params[@"contact_id"];
  6132. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6133. __block NSString *zipcode = nil;
  6134. __block NSString *state = nil; // state_code
  6135. __block NSString *city = nil; //
  6136. __block NSString *firt_name,*last_name;
  6137. __block NSString *phone,*fax,*email;
  6138. __block NSString *notes,*price_type,*sales_rep;
  6139. __block NSString *img_0,*img_1,*img_2;
  6140. __block NSString *contact_type;
  6141. contact_id = [self translateSingleQuote:contact_id];
  6142. 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];
  6143. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6144. country = [self textAtColumn:0 statement:stmt]; // country name
  6145. company_name = [self textAtColumn:1 statement:stmt];
  6146. addr_1 = [self textAtColumn:2 statement:stmt];
  6147. addr_2 = [self textAtColumn:3 statement:stmt];
  6148. addr_3 = [self textAtColumn:4 statement:stmt];
  6149. addr_4 = [self textAtColumn:5 statement:stmt];
  6150. zipcode = [self textAtColumn:6 statement:stmt];
  6151. state = [self textAtColumn:7 statement:stmt]; // state code
  6152. city = [self textAtColumn:8 statement:stmt];
  6153. firt_name = [self textAtColumn:9 statement:stmt];
  6154. last_name = [self textAtColumn:10 statement:stmt];
  6155. phone = [self textAtColumn:11 statement:stmt];
  6156. fax = [self textAtColumn:12 statement:stmt];
  6157. email = [self textAtColumn:13 statement:stmt];
  6158. notes = [self textAtColumn:14 statement:stmt];
  6159. price_type = [self textAtColumn:15 statement:stmt]; // name
  6160. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6161. img_0 = [self textAtColumn:17 statement:stmt];
  6162. img_1 = [self textAtColumn:18 statement:stmt];
  6163. img_2 = [self textAtColumn:19 statement:stmt];
  6164. contact_type = [self textAtColumn:20 statement:stmt];
  6165. }];
  6166. // decrypt
  6167. if (company_name) {
  6168. company_name = [AESCrypt fastdecrypt:company_name];
  6169. }
  6170. if (addr_1) {
  6171. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6172. }
  6173. if (phone) {
  6174. phone = [AESCrypt fastdecrypt:phone];
  6175. }
  6176. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6177. countryCode = [iSalesDB jk_queryText:countrySql];
  6178. stateCode = state;
  6179. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6180. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6181. NSString *code_id = params[@"country"];
  6182. countryCode_id = code_id;
  6183. countryCode = [self countryCodeByid:code_id];
  6184. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6185. NSString *zip_code = params[@"zipcode"];
  6186. // 剔除全部为空格
  6187. int spaceCount = 0;
  6188. for (int i = 0; i < zip_code.length; i++) {
  6189. if ([zip_code characterAtIndex:i] == ' ') {
  6190. spaceCount++;
  6191. }
  6192. }
  6193. if (spaceCount == zip_code.length) {
  6194. zip_code = @"";
  6195. }
  6196. if (zipcode.length > 0) {
  6197. zipcode = zip_code;
  6198. countryCode_id = params[@"country"];
  6199. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6200. countryCode = [dic valueForKey:@"country_code"];
  6201. if (!countryCode) {
  6202. NSString *code_id = params[@"country"];
  6203. countryCode = [self countryCodeByid:code_id];
  6204. }
  6205. stateCode = [dic valueForKey:@"state_code"];
  6206. if (!stateCode.length) {
  6207. stateCode = params[@"state"];
  6208. }
  6209. city = [dic valueForKey:@"city"];
  6210. if (!city.length) {
  6211. city = params[@"city"];
  6212. }
  6213. // zip code
  6214. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6215. [zipDic setValue:zipcode forKey:@"value"];
  6216. [section_0 setValue:zipDic forKey:@"item_8"];
  6217. } else {
  6218. NSString *code_id = params[@"country"];
  6219. countryCode = [self countryCodeByid:code_id];
  6220. stateCode = params[@"state"];
  6221. city = params[@"city"];
  6222. }
  6223. }
  6224. }
  6225. // 0 Country
  6226. // 1 Company Name
  6227. // 2 Contact ID
  6228. // 3 Picture
  6229. // 4 Address 1
  6230. // 5 Address 2
  6231. // 6 Address 3
  6232. // 7 Address 4
  6233. // 8 Zip Code
  6234. // 9 State/Province
  6235. // 10 City
  6236. // 11 Contact First Name
  6237. // 12 Contact Last Name
  6238. // 13 Phone
  6239. // 14 Fax
  6240. // 15 Email
  6241. // 16 Contact Notes
  6242. // 17 Price Type
  6243. // 18 Contact Type
  6244. // 19 Sales Rep
  6245. // country
  6246. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6247. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6248. // company
  6249. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6250. // contact_id
  6251. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6252. // picture
  6253. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6254. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6255. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6256. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6257. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6258. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6259. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6260. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6261. // addr 1 2 3 4
  6262. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6263. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6264. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6265. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6266. // zip code
  6267. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6268. // state
  6269. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6270. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6271. // city
  6272. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6273. // first last
  6274. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6275. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6276. // phone fax email
  6277. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6278. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6279. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6280. // notes
  6281. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6282. // price
  6283. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6284. for (NSString *key in priceDic.allKeys) {
  6285. if ([key containsString:@"val_"]) {
  6286. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6287. if ([dic[@"value"] isEqualToString:price_type]) {
  6288. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6289. [priceDic setValue:dic forKey:key];
  6290. }
  6291. }
  6292. }
  6293. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6294. // Contact Rep
  6295. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6296. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6297. // Sales Rep
  6298. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6299. for (NSString *key in repDic.allKeys) {
  6300. if ([key containsString:@"val_"]) {
  6301. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6302. NSString *value = dic[@"value"];
  6303. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6304. if (code && [code isEqualToString:sales_rep]) {
  6305. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6306. [repDic setValue:dic forKey:key];
  6307. }
  6308. }
  6309. }
  6310. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6311. [ret setValue:section_0 forKey:@"section_0"];
  6312. return [RAConvertor dict2data:ret];
  6313. }
  6314. #pragma mark save contact
  6315. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6316. {
  6317. return [self offline_saveContact:params update:YES isCustomer:YES];
  6318. }
  6319. #pragma mark category
  6320. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6321. if (ck) {
  6322. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6323. for (NSString *key in res.allKeys) {
  6324. if (![key isEqualToString:@"count"]) {
  6325. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6326. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6327. if ([val[@"value"] isEqualToString:ck]) {
  6328. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6329. }
  6330. [res setValue:val forKey:key];
  6331. }
  6332. }
  6333. [dic setValue:res forKey:valueKey];
  6334. }
  6335. }
  6336. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6337. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6338. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6339. NSString* category = [params valueForKey:@"category"];
  6340. if (!category || [category isEqualToString:@""]) {
  6341. category = @"%";
  6342. }
  6343. category = [self translateSingleQuote:category];
  6344. int limit = [[params valueForKey:@"limit"] intValue];
  6345. int offset = [[params valueForKey:@"offset"] intValue];
  6346. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6347. NSString *limit_str = @"";
  6348. if (limited) {
  6349. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6350. }
  6351. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6352. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6353. sqlite3 *db = [iSalesDB get_db];
  6354. // [iSalesDB AddExFunction:db];
  6355. int count;
  6356. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6357. 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];
  6358. double price_min = 0;
  6359. double price_max = 0;
  6360. if ([params.allKeys containsObject:@"alert"]) {
  6361. // alert
  6362. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6363. NSString *alert = params[@"alert"];
  6364. if ([alert isEqualToString:@"Display All"]) {
  6365. alert = [NSString stringWithFormat:@""];
  6366. } else {
  6367. alert = [self translateSingleQuote:alert];
  6368. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6369. }
  6370. // available
  6371. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6372. NSString *available = params[@"available"];
  6373. NSString *available_condition;
  6374. if ([available isEqualToString:@"Display All"]) {
  6375. available_condition = @"";
  6376. } else if ([available isEqualToString:@"Available Now"]) {
  6377. available_condition = @"and availability > 0";
  6378. } else {
  6379. available_condition = @"and availability == 0";
  6380. }
  6381. // best seller
  6382. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6383. NSString *best_seller = @"";
  6384. NSString *order_best_seller = @"m.name asc";
  6385. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6386. best_seller = @"and best_seller > 0";
  6387. order_best_seller = @"m.best_seller desc,m.name asc";
  6388. }
  6389. // price
  6390. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6391. NSString *price = params[@"price"];
  6392. price_min = 0;
  6393. price_max = MAXFLOAT;
  6394. if (params[@"user"] && price != nil) {
  6395. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6396. NSMutableString *priceName = [NSMutableString string];
  6397. for (int i = 0; i < priceTypeArray.count; i++) {
  6398. NSString *pricetype = priceTypeArray[i];
  6399. pricetype = [self translateSingleQuote:pricetype];
  6400. if (i == 0) {
  6401. [priceName appendFormat:@"'%@'",pricetype];
  6402. } else {
  6403. [priceName appendFormat:@",'%@'",pricetype];
  6404. }
  6405. }
  6406. if ([price isEqualToString:@"Display All"]) {
  6407. price = [NSString stringWithFormat:@""];
  6408. } else if([price containsString:@"+"]){
  6409. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6410. price_min = [price doubleValue];
  6411. 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];
  6412. } else {
  6413. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6414. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6415. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6416. 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];
  6417. }
  6418. } else {
  6419. price = @"";
  6420. }
  6421. // sold_by_qty : Sold in quantities of %@
  6422. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6423. NSString *qty = params[@"sold_by_qty"];
  6424. if ([qty isEqualToString:@"Display All"]) {
  6425. qty = @"";
  6426. } else {
  6427. qty = [self translateSingleQuote:qty];
  6428. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6429. }
  6430. // model name;
  6431. NSString* modelname =params[@"modelName"];
  6432. if(modelname.length==0)
  6433. {
  6434. modelname=@"";
  6435. }
  6436. else
  6437. {
  6438. modelname = modelname.lowercaseString;
  6439. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6440. }
  6441. //modelDescription
  6442. NSString* modelDescription =params[@"modelDescription"];
  6443. if(modelDescription.length==0)
  6444. {
  6445. modelDescription=@"";
  6446. }
  6447. else
  6448. {
  6449. modelDescription = modelDescription.lowercaseString;
  6450. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6451. }
  6452. // cate
  6453. // category = [self translateSingleQuote:category];
  6454. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6455. // cate mutiple selection
  6456. NSString *category_id = params[@"category"];
  6457. NSMutableArray *cate_id_array = nil;
  6458. NSMutableString *cateWhere = [NSMutableString string];
  6459. if ([category_id isEqualToString:@""] || !category_id) {
  6460. [cateWhere appendString:@"1 = 1"];
  6461. } else {
  6462. if ([category_id containsString:@","]) {
  6463. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6464. } else {
  6465. cate_id_array = [@[category_id] mutableCopy];
  6466. }
  6467. [cateWhere appendString:@"("];
  6468. for (int i = 0; i < cate_id_array.count; i++) {
  6469. if (i == 0) {
  6470. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6471. } else {
  6472. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6473. }
  6474. }
  6475. [cateWhere appendString:@")"];
  6476. }
  6477. // where bestseller > 0 order by bestseller desc
  6478. // sql query: alert availability(int) best_seller(int) price qty
  6479. 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];
  6480. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6481. }
  6482. DebugLog(@"offline category where: %@",where);
  6483. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6484. if (!params[@"user"]) {
  6485. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6486. }
  6487. [ret setValue:filter forKey:@"filter"];
  6488. DebugLog(@"offline_category sql:%@",sqlQuery);
  6489. sqlite3_stmt * statement;
  6490. [ret setValue:@"2" forKey:@"result"];
  6491. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6492. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6493. // int count=0;
  6494. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6495. {
  6496. int i=0;
  6497. while (sqlite3_step(statement) == SQLITE_ROW)
  6498. {
  6499. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6500. char *name = (char*)sqlite3_column_text(statement, 0);
  6501. if(name==nil)
  6502. name="";
  6503. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6504. char *description = (char*)sqlite3_column_text(statement, 1);
  6505. if(description==nil)
  6506. description="";
  6507. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6508. int product_id = sqlite3_column_int(statement, 2);
  6509. int wid = sqlite3_column_int(statement, 3);
  6510. int closeout = sqlite3_column_int(statement, 4);
  6511. int cid = sqlite3_column_int(statement, 5);
  6512. int wisdelete = sqlite3_column_int(statement, 6);
  6513. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6514. int more_color = sqlite3_column_int(statement, 8);
  6515. // Defaul Category ID
  6516. __block NSString *categoryID = nil;
  6517. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6518. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6519. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6520. if(default_category==nil)
  6521. default_category="";
  6522. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6523. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6524. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6525. categoryID = nsdefault_category;
  6526. }];
  6527. if (!categoryID.length) {
  6528. NSString *cateIDs = params[@"category"];
  6529. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6530. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6531. for (NSString *cateID in requestCategoryArr) {
  6532. BOOL needBreak = NO;
  6533. for (NSString *itemCateIDBox in itemCategoryArr) {
  6534. if (itemCateIDBox.length > 4) {
  6535. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6536. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6537. if ([itemCategoryID isEqualToString:cateID]) {
  6538. needBreak = YES;
  6539. categoryID = itemCategoryID;
  6540. break;
  6541. }
  6542. }
  6543. }
  6544. if (needBreak) {
  6545. break;
  6546. }
  6547. }
  6548. }
  6549. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6550. if(wid !=0 && wisdelete != 1)
  6551. [item setValue:@"true" forKey:@"wish_exists"];
  6552. else
  6553. [item setValue:@"false" forKey:@"wish_exists"];
  6554. if(closeout==0)
  6555. [item setValue:@"false" forKey:@"is_closeout"];
  6556. else
  6557. [item setValue:@"true" forKey:@"is_closeout"];
  6558. if(cid==0)
  6559. [item setValue:@"false" forKey:@"cart_exists"];
  6560. else
  6561. [item setValue:@"true" forKey:@"cart_exists"];
  6562. if (more_color == 0) {
  6563. [item setObject:@(false) forKey:@"more_color"];
  6564. } else if (more_color == 1) {
  6565. [item setObject:@(true) forKey:@"more_color"];
  6566. }
  6567. [item addEntriesFromDictionary:imgjson];
  6568. // [item setValue:nsurl forKey:@"img"];
  6569. [item setValue:nsname forKey:@"name"];
  6570. [item setValue:nsdescription forKey:@"description"];
  6571. if (categoryID) {
  6572. [item setValue:categoryID forKey:@"item_category_id"];
  6573. }
  6574. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6575. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6576. i++;
  6577. }
  6578. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6579. [ret setObject:items forKey:@"items"];
  6580. sqlite3_finalize(statement);
  6581. } else {
  6582. DebugLog(@"nothing...");
  6583. }
  6584. DebugLog(@"count:%d",count);
  6585. [iSalesDB close_db:db];
  6586. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6587. return ret;
  6588. }
  6589. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6590. {
  6591. return [self categoryList:params limited:YES];
  6592. }
  6593. # pragma mark item search
  6594. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6595. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6596. // assert(params[@"order_code"]);
  6597. // params[@"user"]
  6598. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6599. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6600. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6601. // category
  6602. NSDictionary *category_menu = [self offline_category_menu];
  6603. [filter setValue:category_menu forKey:@"category"];
  6604. NSString* where= nil;
  6605. NSString* orderby= @"m.name";
  6606. if (!filterSearch) {
  6607. int covertype = [[params valueForKey:@"covertype"] intValue];
  6608. switch (covertype) {
  6609. case 0:
  6610. {
  6611. where=@"m.category like'%%#005#%%'";
  6612. break;
  6613. }
  6614. case 1:
  6615. {
  6616. where=@"m.alert like '%QS%'";
  6617. break;
  6618. }
  6619. case 2:
  6620. {
  6621. where=@"m.availability>0";
  6622. break;
  6623. }
  6624. case 3:
  6625. {
  6626. where=@"m.best_seller>0";
  6627. orderby=@"m.best_seller desc,m.name asc";
  6628. break;
  6629. }
  6630. default:
  6631. where=@"1=1";
  6632. break;
  6633. }
  6634. }
  6635. int limit = [[params valueForKey:@"limit"] intValue];
  6636. int offset = [[params valueForKey:@"offset"] intValue];
  6637. NSString *limit_str = @"";
  6638. if (limited) {
  6639. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6640. }
  6641. sqlite3 *db = [iSalesDB get_db];
  6642. // [iSalesDB AddExFunction:db];
  6643. int count;
  6644. NSString *sqlQuery = nil;
  6645. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6646. 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];
  6647. double price_min = 0;
  6648. double price_max = 0;
  6649. if (filterSearch) {
  6650. // alert
  6651. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6652. NSString *alert = params[@"alert"];
  6653. if ([alert isEqualToString:@"Display All"]) {
  6654. alert = [NSString stringWithFormat:@""];
  6655. } else {
  6656. alert = [self translateSingleQuote:alert];
  6657. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6658. }
  6659. // available
  6660. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6661. NSString *available = params[@"available"];
  6662. NSString *available_condition;
  6663. if ([available isEqualToString:@"Display All"]) {
  6664. available_condition = @"";
  6665. } else if ([available isEqualToString:@"Available Now"]) {
  6666. available_condition = @"and availability > 0";
  6667. } else {
  6668. available_condition = @"and availability == 0";
  6669. }
  6670. // best seller
  6671. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6672. NSString *best_seller = @"";
  6673. NSString *order_best_seller = @"m.name asc";
  6674. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6675. best_seller = @"and best_seller > 0";
  6676. order_best_seller = @"m.best_seller desc,m.name asc";
  6677. }
  6678. // price
  6679. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6680. NSString *price = params[@"price"];
  6681. price_min = 0;
  6682. price_max = MAXFLOAT;
  6683. if (params[@"user"]) {
  6684. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6685. NSMutableString *priceName = [NSMutableString string];
  6686. for (int i = 0; i < priceTypeArray.count; i++) {
  6687. NSString *pricetype = priceTypeArray[i];
  6688. pricetype = [self translateSingleQuote:pricetype];
  6689. if (i == 0) {
  6690. [priceName appendFormat:@"'%@'",pricetype];
  6691. } else {
  6692. [priceName appendFormat:@",'%@'",pricetype];
  6693. }
  6694. }
  6695. if ([price isEqualToString:@"Display All"]) {
  6696. price = [NSString stringWithFormat:@""];
  6697. } else if([price containsString:@"+"]){
  6698. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6699. price_min = [price doubleValue];
  6700. 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];
  6701. } else {
  6702. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6703. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6704. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6705. 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];
  6706. }
  6707. } else {
  6708. price = @"";
  6709. }
  6710. // sold_by_qty : Sold in quantities of %@
  6711. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6712. NSString *qty = params[@"sold_by_qty"];
  6713. if ([qty isEqualToString:@"Display All"]) {
  6714. qty = @"";
  6715. } else {
  6716. qty = [self translateSingleQuote:qty];
  6717. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6718. }
  6719. // model name;
  6720. NSString* modelname =params[@"modelName"];
  6721. if(modelname.length==0)
  6722. {
  6723. modelname=@"";
  6724. }
  6725. else
  6726. {
  6727. modelname = modelname.lowercaseString;
  6728. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6729. }
  6730. //modelDescription
  6731. NSString* modelDescription =params[@"modelDescription"];
  6732. if(modelDescription.length==0)
  6733. {
  6734. modelDescription=@"";
  6735. }
  6736. else
  6737. {
  6738. modelDescription = modelDescription.lowercaseString;
  6739. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6740. }
  6741. //lower(description) like'%%%@%%'
  6742. // category
  6743. NSString *category_id = params[@"ctgId"];
  6744. NSMutableArray *cate_id_array = nil;
  6745. NSMutableString *cateWhere = [NSMutableString string];
  6746. if ([category_id isEqualToString:@""] || !category_id) {
  6747. [cateWhere appendString:@"1 = 1"];
  6748. } else {
  6749. if ([category_id containsString:@","]) {
  6750. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6751. } else {
  6752. cate_id_array = [@[category_id] mutableCopy];
  6753. }
  6754. /*-----------*/
  6755. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6756. [cateWhere appendString:@"("];
  6757. for (int i = 0; i < cate_id_array.count; i++) {
  6758. for (NSString *key0 in cateDic.allKeys) {
  6759. if ([key0 containsString:@"category_"]) {
  6760. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6761. for (NSString *key1 in category0.allKeys) {
  6762. if ([key1 containsString:@"category_"]) {
  6763. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6764. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6765. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6766. cate_id_array[i] = [category1 objectForKey:@"id"];
  6767. if (i == 0) {
  6768. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6769. } else {
  6770. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6771. }
  6772. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6773. [category0 setValue:category1 forKey:key1];
  6774. [cateDic setValue:category0 forKey:key0];
  6775. }
  6776. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6777. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6778. cate_id_array[i] = [category0 objectForKey:@"id"];
  6779. if (i == 0) {
  6780. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6781. } else {
  6782. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6783. }
  6784. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6785. [cateDic setValue:category0 forKey:key0];
  6786. }
  6787. }
  6788. }
  6789. }
  6790. }
  6791. }
  6792. [cateWhere appendString:@")"];
  6793. [filter setValue:cateDic forKey:@"category"];
  6794. }
  6795. // where bestseller > 0 order by bestseller desc
  6796. // sql query: alert availability(int) best_seller(int) price qty
  6797. 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];
  6798. // count
  6799. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6800. }
  6801. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6802. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6803. if (!params[@"user"]) {
  6804. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6805. }
  6806. [ret setValue:filter forKey:@"filter"];
  6807. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6808. sqlite3_stmt * statement;
  6809. [ret setValue:@"2" forKey:@"result"];
  6810. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6811. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6812. // int count=0;
  6813. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6814. {
  6815. int i=0;
  6816. while (sqlite3_step(statement) == SQLITE_ROW)
  6817. {
  6818. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6819. char *name = (char*)sqlite3_column_text(statement, 0);
  6820. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6821. char *description = (char*)sqlite3_column_text(statement, 1);
  6822. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6823. int product_id = sqlite3_column_int(statement, 2);
  6824. int wid = sqlite3_column_int(statement, 3);
  6825. int closeout = sqlite3_column_int(statement, 4);
  6826. int cid = sqlite3_column_int(statement, 5);
  6827. int wisdelete = sqlite3_column_int(statement, 6);
  6828. int more_color = sqlite3_column_int(statement, 7);
  6829. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6830. if(wid !=0 && wisdelete != 1)
  6831. [item setValue:@"true" forKey:@"wish_exists"];
  6832. else
  6833. [item setValue:@"false" forKey:@"wish_exists"];
  6834. if(closeout==0)
  6835. [item setValue:@"false" forKey:@"is_closeout"];
  6836. else
  6837. [item setValue:@"true" forKey:@"is_closeout"];
  6838. if(cid==0)
  6839. [item setValue:@"false" forKey:@"cart_exists"];
  6840. else
  6841. [item setValue:@"true" forKey:@"cart_exists"];
  6842. if (more_color == 0) {
  6843. [item setObject:@(false) forKey:@"more_color"];
  6844. } else if (more_color == 1) {
  6845. [item setObject:@(true) forKey:@"more_color"];
  6846. }
  6847. [item addEntriesFromDictionary:imgjson];
  6848. // [item setValue:nsurl forKey:@"img"];
  6849. [item setValue:nsname forKey:@"fash_name"];
  6850. [item setValue:nsdescription forKey:@"description"];
  6851. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6852. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6853. i++;
  6854. }
  6855. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6856. [ret setObject:items forKey:@"items"];
  6857. sqlite3_finalize(statement);
  6858. }
  6859. [iSalesDB close_db:db];
  6860. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6861. return ret;
  6862. }
  6863. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6864. {
  6865. return [self itemsearch:params limited:YES];
  6866. }
  6867. #pragma mark order detail
  6868. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6869. if (str1.length == 0) {
  6870. str1 = @"&nbsp";
  6871. }
  6872. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6873. return str;
  6874. }
  6875. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6876. {
  6877. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6878. [fromformatter setDateFormat:from];
  6879. NSDate *date = [fromformatter dateFromString:datetime];
  6880. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6881. [toformatter setDateFormat:to];
  6882. NSString * ret = [toformatter stringFromDate:date];
  6883. return ret;
  6884. }
  6885. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6886. // 把毫秒去掉
  6887. if ([dateTime containsString:@"."]) {
  6888. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6889. }
  6890. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6891. formatter.dateFormat = formate;
  6892. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6893. NSDate *date = [formatter dateFromString:dateTime];
  6894. formatter.dateFormat = newFormate;
  6895. NSString *result = [formatter stringFromDate:date];
  6896. return result ? result : @"";
  6897. }
  6898. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6899. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6900. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6901. }
  6902. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6903. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6904. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6905. }
  6906. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6907. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6908. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6909. }
  6910. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6911. {
  6912. assert(params[@"mode"]!=nil);
  6913. assert(params[@"user"]!=nil);
  6914. DebugLog(@"offline oderdetail params: %@",params);
  6915. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6916. int orderId = [params[@"orderId"] intValue];
  6917. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6918. // decrypt card number and card security code
  6919. // 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 ];
  6920. 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];
  6921. sqlite3 *db = [iSalesDB get_db];
  6922. sqlite3_stmt * statement;
  6923. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6924. NSString *nssoid = @"";
  6925. NSString* orderinfo = @"";
  6926. NSString *moreInfo = @"";
  6927. double handlingFee = 0;
  6928. double payments_and_credist = 0;
  6929. double totalPrice = 0;
  6930. double shippingFee = 0;
  6931. double lift_gate = 0;
  6932. NSString *customer_contact = @"";
  6933. NSString *customer_email = @"";
  6934. NSString *customer_fax = @"";
  6935. NSString *customer_phone = @"";
  6936. NSString *customer_city = @"";
  6937. NSString *customer_state = @"";
  6938. NSString *customer_zipcode = @"";
  6939. NSString *customer_country = @"";
  6940. BOOL must_call = NO;
  6941. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6942. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6943. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6944. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6945. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6946. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6947. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6948. {
  6949. if (sqlite3_step(statement) == SQLITE_ROW)
  6950. {
  6951. // int order_id = sqlite3_column_int(statement, 0);
  6952. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6953. // ret[@"sign_url"] = sign_url;
  6954. section_1 = @{
  6955. @"data":sign_url,
  6956. @"title":@"Signature",
  6957. @"type":@"sign_url"
  6958. }.mutableCopy;
  6959. [ret setObject:section_1 forKey:@"section_1"];
  6960. customer_contact = [self textAtColumn:52 statement:statement];
  6961. customer_email = [self textAtColumn:53 statement:statement];
  6962. customer_phone = [self textAtColumn:54 statement:statement];
  6963. customer_fax = [self textAtColumn:55 statement:statement];
  6964. customer_city = [self textAtColumn:60 statement:statement];
  6965. customer_state = [self textAtColumn:61 statement:statement];
  6966. customer_zipcode = [self textAtColumn:62 statement:statement];
  6967. customer_country = [self textAtColumn:63 statement:statement];
  6968. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6969. int offline_edit=sqlite3_column_int(statement, 56);
  6970. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6971. char *soid = (char*)sqlite3_column_text(statement, 1);
  6972. if(soid==nil)
  6973. soid= "";
  6974. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6975. // so#
  6976. ret[@"so#"] = nssoid;
  6977. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6978. if(poNumber==nil)
  6979. poNumber= "";
  6980. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6981. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6982. if(create_time==nil)
  6983. create_time= "";
  6984. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6985. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6986. int status = sqlite3_column_int(statement, 4);
  6987. int erpStatus = sqlite3_column_int(statement, 49);
  6988. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6989. // status
  6990. if (status > 1 && status != 3) {
  6991. status = erpStatus;
  6992. } else if (status == 3) {
  6993. status = 15;
  6994. }
  6995. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6996. ret[@"order_status"] = nsstatus;
  6997. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6998. if(company_name==nil)
  6999. company_name= "";
  7000. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7001. // company name
  7002. ret[@"company_name"] = nscompany_name;
  7003. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7004. if(customer_contact==nil)
  7005. customer_contact= "";
  7006. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7007. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7008. if(addr_1==nil)
  7009. addr_1="";
  7010. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7011. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7012. if(addr_2==nil)
  7013. addr_2="";
  7014. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7015. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7016. if(addr_3==nil)
  7017. addr_3="";
  7018. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7019. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7020. if(addr_4==nil)
  7021. addr_4="";
  7022. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7023. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7024. [arr_addr addObject:nsaddr_1];
  7025. [arr_addr addObject:nsaddr_2];
  7026. [arr_addr addObject:nsaddr_3];
  7027. [arr_addr addObject:nsaddr_4];
  7028. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7029. [arr_addr addObject:customer_state];
  7030. [arr_addr addObject:customer_zipcode];
  7031. [arr_addr addObject:customer_country];
  7032. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7033. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7034. char *logist = (char*)sqlite3_column_text(statement, 11);
  7035. if(logist==nil)
  7036. logist= "";
  7037. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7038. if (/*status == -11 || */status == 10 || status == 11) {
  7039. nslogist = [self textAtColumn:59 statement:statement];
  7040. };
  7041. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7042. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7043. shipping = @"Shipping To Be Quoted";
  7044. } else {
  7045. shippingFee = sqlite3_column_double(statement, 12);
  7046. }
  7047. // Shipping
  7048. // ret[@"Shipping"] = shipping;
  7049. NSDictionary *shipping_item = @{
  7050. @"title":@"Shipping",
  7051. @"value":shipping
  7052. };
  7053. [price_data setObject:shipping_item forKey:@"item_1"];
  7054. int have_lift_gate = sqlite3_column_int(statement, 17);
  7055. lift_gate = sqlite3_column_double(statement, 13);
  7056. // Liftgate Fee(No loading dock)
  7057. if (!have_lift_gate) {
  7058. lift_gate = 0;
  7059. }
  7060. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7061. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7062. if (sqlite3_column_int(statement, 57)) {
  7063. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7064. liftgate_value = @"Shipping To Be Quoted";
  7065. }
  7066. NSDictionary *liftgate_item = @{
  7067. @"title":@"Liftgate Fee(No loading dock)",
  7068. @"value":liftgate_value
  7069. };
  7070. [price_data setObject:liftgate_item forKey:@"item_2"];
  7071. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7072. // [ret removeObjectForKey:@"Shipping"];
  7073. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7074. // }
  7075. //
  7076. // if (have_lift_gate) {
  7077. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7078. // }
  7079. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7080. if(general_notes==nil)
  7081. general_notes= "";
  7082. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7083. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7084. if(internal_notes==nil)
  7085. internal_notes= "";
  7086. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7087. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7088. if(payment_type==nil)
  7089. payment_type= "";
  7090. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7091. // order info
  7092. orderinfo = [self textFileName:@"order_info.html"];
  7093. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7094. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7095. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7096. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7097. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7098. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7099. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7100. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7101. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7102. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7103. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7104. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7105. NSString *payment = nil;
  7106. // id -> show
  7107. __block NSString *show_pay_type = nspayment_type;
  7108. [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) {
  7109. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7110. if (show_typ_ch != NULL) {
  7111. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7112. }
  7113. }];
  7114. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7115. {
  7116. payment = [self textFileName:@"creditcardpayment.html"];
  7117. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7118. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7119. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7120. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7121. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7122. NSString *card_type = [self textAtColumn:42 statement:statement];
  7123. if (card_type.length > 0) { // 显示星号
  7124. card_type = @"****";
  7125. }
  7126. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7127. if (card_number.length > 0 && card_number.length > 4) {
  7128. for (int i = 0; i < card_number.length - 4; i++) {
  7129. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7130. }
  7131. } else {
  7132. card_number = @"";
  7133. }
  7134. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7135. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7136. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7137. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7138. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7139. card_expiration = @"****";
  7140. }
  7141. NSString *card_city = [self textAtColumn:46 statement:statement];
  7142. NSString *card_state = [self textAtColumn:47 statement:statement];
  7143. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7144. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7145. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7146. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7147. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7148. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7149. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7150. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7151. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7152. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7153. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7154. }
  7155. else
  7156. {
  7157. payment=[self textFileName:@"normalpayment.html"];
  7158. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7159. }
  7160. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7161. ret[@"result"]= [NSNumber numberWithInt:2];
  7162. // more info
  7163. moreInfo = [self textFileName:@"more_info.html"];
  7164. /*****ship to******/
  7165. // ShipToCompany_or_&nbsp
  7166. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7167. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7168. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7169. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7170. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7171. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7172. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7173. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7174. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7175. /*****ship from******/
  7176. // ShipFromCompany_or_&nbsp
  7177. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7178. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7179. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7180. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7181. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7182. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7183. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7184. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7185. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7186. /*****freight to******/
  7187. // FreightBillToCompany_or_&nbsp
  7188. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7189. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7190. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7191. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7192. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7193. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7194. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7195. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7196. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7197. /*****merchandise to******/
  7198. // MerchandiseBillToCompany_or_&nbsp
  7199. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7200. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7201. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7202. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7203. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7204. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7205. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7206. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7207. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7208. /*****return to******/
  7209. // ReturnToCompany_or_&nbsp
  7210. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7211. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7212. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7213. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7214. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7215. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7216. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7217. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7218. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7219. //
  7220. // DebugLog(@"more info : %@",moreInfo);
  7221. // handling fee
  7222. handlingFee = sqlite3_column_double(statement, 33);
  7223. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7224. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7225. if (sqlite3_column_int(statement, 58)) {
  7226. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7227. handling_fee_value = @"Shipping To Be Quoted";
  7228. }
  7229. NSDictionary *handling_fee_item = @{
  7230. @"title":@"Handling Fee",
  7231. @"value":handling_fee_value
  7232. };
  7233. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7234. //
  7235. // customer info
  7236. customerID = [self textAtColumn:36 statement:statement];
  7237. // mode
  7238. ret[@"mode"] = params[@"mode"];
  7239. // model_count
  7240. ret[@"model_count"] = @(0);
  7241. }
  7242. sqlite3_finalize(statement);
  7243. }
  7244. [iSalesDB close_db:db];
  7245. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7246. // "customer_email" = "Shui Hu";
  7247. // "customer_fax" = "";
  7248. // "customer_first_name" = F;
  7249. // "customer_last_name" = L;
  7250. // "customer_name" = ",da He Xiang Dong Liu A";
  7251. // "customer_phone" = "Hey Xuan Feng";
  7252. contactInfo[@"customer_phone"] = customer_phone;
  7253. contactInfo[@"customer_fax"] = customer_fax;
  7254. contactInfo[@"customer_email"] = customer_email;
  7255. NSString *first_name = @"";
  7256. NSString *last_name = @"";
  7257. if ([customer_contact isEqualToString:@""]) {
  7258. } else if ([customer_contact containsString:@" "]) {
  7259. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7260. first_name = [customer_contact substringToIndex:first_space_index];
  7261. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7262. }
  7263. contactInfo[@"customer_first_name"] = first_name;
  7264. contactInfo[@"customer_last_name"] = last_name;
  7265. ret[@"customerInfo"] = contactInfo;
  7266. // models
  7267. if (nssoid) {
  7268. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7269. __block double TotalCuft = 0;
  7270. __block double TotalWeight = 0;
  7271. __block int TotalCarton = 0;
  7272. __block double allItemPrice = 0;
  7273. 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];
  7274. sqlite3 *db1 = [iSalesDB get_db];
  7275. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7276. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7277. int product_id = sqlite3_column_int(stmt, 0);
  7278. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7279. int item_id = sqlite3_column_int(stmt, 7);
  7280. NSString* Price=nil;
  7281. if(str_price==nil)
  7282. {
  7283. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7284. if(price==nil)
  7285. Price=@"No Price.";
  7286. else
  7287. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7288. }
  7289. else
  7290. {
  7291. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7292. }
  7293. double discount = sqlite3_column_double(stmt, 2);
  7294. int item_count = sqlite3_column_int(stmt, 3);
  7295. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7296. NSString *nsline_note=nil;
  7297. if(line_note!=nil)
  7298. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7299. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7300. // NSString *nsname = nil;
  7301. // if(name!=nil)
  7302. // nsname= [[NSString alloc]initWithUTF8String:name];
  7303. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7304. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7305. // NSString *nsdescription=nil;
  7306. // if(description!=nil)
  7307. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7308. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7309. // int stockUom = sqlite3_column_int(stmt, 8);
  7310. // int _id = sqlite3_column_int(stmt, 9);
  7311. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7312. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7313. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7314. int carton=[bsubtotaljson[@"carton"] intValue];
  7315. TotalCuft += cuft;
  7316. TotalWeight += weight;
  7317. TotalCarton += carton;
  7318. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7319. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7320. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7321. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7322. itemjson[@"note"]=nsline_note;
  7323. itemjson[@"origin_price"] = Price;
  7324. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7325. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7326. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7327. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7328. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7329. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7330. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7331. if(itemjson[@"combine"] != nil)
  7332. {
  7333. // int citem=0;
  7334. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7335. for(int bc=0;bc<bcount;bc++)
  7336. {
  7337. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7338. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7339. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7340. subTotal += uprice * modulus * item_count;
  7341. }
  7342. }
  7343. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7344. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7345. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7346. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7347. itemjson[@"type"] = @"order_item";
  7348. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7349. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7350. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7351. allItemPrice += subTotal;
  7352. }];
  7353. section_3[@"data"] = model_data;
  7354. section_3[@"type"] = @"sub_order";
  7355. section_3[@"title"] = @"Models";
  7356. section_3[@"switch"] = @(false);
  7357. ret[@"section_3"] = section_3;
  7358. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7359. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7360. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7361. // payments/Credits
  7362. // payments_and_credist = sqlite3_column_double(statement, 34);
  7363. payments_and_credist = allItemPrice;
  7364. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7365. NSDictionary *sub_total_item = @{
  7366. @"title":@"Sub-Total",
  7367. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7368. };
  7369. [price_data setObject:sub_total_item forKey:@"item_0"];
  7370. // // total
  7371. // totalPrice = sqlite3_column_double(statement, 35);
  7372. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7373. } else {
  7374. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7375. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7376. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7377. // payments/Credits
  7378. payments_and_credist = 0;
  7379. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7380. NSDictionary *sub_total_item = @{
  7381. @"title":@"Sub-Total",
  7382. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7383. };
  7384. [price_data setObject:sub_total_item forKey:@"item_0"];
  7385. }
  7386. // total
  7387. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7388. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7389. NSDictionary *total_item = @{
  7390. @"title":@"Total",
  7391. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7392. };
  7393. [price_data setObject:total_item forKey:@"item_4"];
  7394. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7395. section_4 = @{
  7396. @"data":price_data,
  7397. @"title":@"Price Info",
  7398. @"type":@"price_info"
  7399. }.mutableCopy;
  7400. ret[@"section_4"] = section_4;
  7401. // ret[@"order_info"]= orderinfo;
  7402. section_0 = @{
  7403. @"data":orderinfo,
  7404. @"title":@"Order Info",
  7405. @"type":@"order_info"
  7406. }.mutableCopy;
  7407. ret[@"section_0"] = section_0;
  7408. // ret[@"more_order_info"] = moreInfo;
  7409. section_2 = @{
  7410. @"data":moreInfo,
  7411. @"title":@"More Info",
  7412. @"type":@"more_order_info"
  7413. }.mutableCopy;
  7414. ret[@"section_2"] = section_2;
  7415. ret[@"count"] = @(5);
  7416. return [RAConvertor dict2data:ret];
  7417. }
  7418. #pragma mark order list
  7419. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7420. NSString *nsstatus = @"";
  7421. switch (status) {
  7422. case 0:
  7423. {
  7424. nsstatus=@"Temp Order";
  7425. break;
  7426. }
  7427. case 1:
  7428. {
  7429. nsstatus=@"Saved Order";
  7430. break;
  7431. }
  7432. case 2: {
  7433. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7434. break;
  7435. }
  7436. case 3:
  7437. case 15:
  7438. {
  7439. nsstatus=@"Cancelled";
  7440. break;
  7441. }
  7442. case 10:
  7443. {
  7444. nsstatus=@"Quote Submitted";
  7445. break;
  7446. }
  7447. case 11:
  7448. {
  7449. nsstatus=@"Sales Order Submitted";
  7450. break;
  7451. }
  7452. case 12:
  7453. {
  7454. nsstatus=@"Processing";
  7455. break;
  7456. }
  7457. case 13:
  7458. {
  7459. nsstatus=@"Shipped";
  7460. break;
  7461. }
  7462. case 14:
  7463. {
  7464. nsstatus=@"Closed";
  7465. break;
  7466. }
  7467. case -11:
  7468. {
  7469. nsstatus = @"Ready For Submit";
  7470. break;
  7471. }
  7472. default:
  7473. break;
  7474. }
  7475. return nsstatus;
  7476. }
  7477. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7478. {
  7479. double total = 0;
  7480. __block double payments_and_credist = 0;
  7481. __block double allItemPrice = 0;
  7482. // sqlite3 *db1 = [iSalesDB get_db];
  7483. if (so_id) {
  7484. // 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];
  7485. 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];
  7486. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7487. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7488. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7489. int product_id = sqlite3_column_int(stmt, 0);
  7490. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7491. int discount = sqlite3_column_int(stmt, 2);
  7492. int item_count = sqlite3_column_int(stmt, 3);
  7493. // int item_id = sqlite3_column_int(stmt, 7);
  7494. int item_id = sqlite3_column_int(stmt, 4);
  7495. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7496. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7497. NSString* Price=nil;
  7498. if(str_price==nil)
  7499. {
  7500. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7501. if(price==nil)
  7502. Price=@"No Price.";
  7503. else
  7504. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7505. }
  7506. else
  7507. {
  7508. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7509. }
  7510. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7511. itemjson[@"The unit price"]=Price;
  7512. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7513. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7514. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7515. if(itemjson[@"combine"] != nil)
  7516. {
  7517. // int citem=0;
  7518. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7519. for(int bc=0;bc<bcount;bc++)
  7520. {
  7521. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7522. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7523. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7524. subTotal += uprice * modulus * item_count;
  7525. }
  7526. }
  7527. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7528. allItemPrice += subTotal;
  7529. }];
  7530. payments_and_credist = allItemPrice;
  7531. } else {
  7532. // payments/Credits
  7533. payments_and_credist = 0;
  7534. }
  7535. // lift_gate handlingFee shippingFee
  7536. __block double lift_gate = 0;
  7537. __block double handlingFee = 0;
  7538. __block double shippingFee = 0;
  7539. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  7540. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7541. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7542. if (have_lift_gate) {
  7543. lift_gate = sqlite3_column_double(stmt, 1);
  7544. }
  7545. handlingFee = sqlite3_column_double(stmt, 2);
  7546. shippingFee = sqlite3_column_double(stmt, 3);
  7547. }];
  7548. // total
  7549. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7550. if (close) {
  7551. [iSalesDB close_db:db1];
  7552. }
  7553. return total;
  7554. }
  7555. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7556. {
  7557. assert(params[@"user"]!=nil);
  7558. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7559. int limit = [[params valueForKey:@"limit"] intValue];
  7560. int offset = [[params valueForKey:@"offset"] intValue];
  7561. NSString* keyword = [params valueForKey:@"keyWord"];
  7562. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7563. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7564. NSString* where=@"1 = 1";
  7565. if(keyword.length>0)
  7566. 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]];
  7567. if (orderStatus.length > 0) {
  7568. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7569. if (order_status_array.count == 1) {
  7570. int status_value = [[order_status_array firstObject] intValue];
  7571. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7572. if (status_value == 15 || status_value == 3) {
  7573. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7574. } else {
  7575. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7576. }
  7577. } else {
  7578. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7579. }
  7580. } else if (order_status_array.count > 1) {
  7581. for (int i = 0; i < order_status_array.count;i++) {
  7582. NSString *status = order_status_array[i];
  7583. NSString *condition = @" or";
  7584. if (i == 0) {
  7585. condition = @" and (";
  7586. }
  7587. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7588. int status_value = [status intValue];
  7589. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7590. if (status_value == 15 || status_value == 3) {
  7591. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7592. } else {
  7593. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7594. }
  7595. } else {
  7596. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7597. }
  7598. }
  7599. where = [where stringByAppendingString:@" )"];
  7600. }
  7601. }
  7602. 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];
  7603. // DebugLog(@"order list sql: %@",sqlQuery);
  7604. sqlite3 *db = [iSalesDB get_db];
  7605. sqlite3_stmt * statement;
  7606. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7607. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7608. {
  7609. int count=0;
  7610. while (sqlite3_step(statement) == SQLITE_ROW)
  7611. {
  7612. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7613. int order_id = sqlite3_column_double(statement, 0);
  7614. char *soid = (char*)sqlite3_column_text(statement, 1);
  7615. if(soid==nil)
  7616. soid= "";
  7617. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7618. int status = sqlite3_column_double(statement, 2);
  7619. int erpStatus = sqlite3_column_double(statement, 9);
  7620. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7621. if(sales_rep==nil)
  7622. sales_rep= "";
  7623. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7624. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7625. if(create_by==nil)
  7626. create_by= "";
  7627. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7628. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7629. if(company_name==nil)
  7630. company_name= "";
  7631. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7632. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7633. if(create_time==nil)
  7634. create_time= "";
  7635. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7636. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7637. // double total_price = sqlite3_column_double(statement, 7);
  7638. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7639. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7640. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7641. int offline_edit = sqlite3_column_int(statement, 10);
  7642. // ": "JH",
  7643. // "": "$8307.00",
  7644. // "": "MOB1608050001",
  7645. // "": "ArpithaT",
  7646. // "": "1st Stage Property Transformations",
  7647. // "": "JANICE SUTTON",
  7648. // "": 2255,
  7649. // "": "08/02/2016 09:49:18",
  7650. // "": 1,
  7651. // "": "Saved Order"
  7652. // "": "1470384050483",
  7653. // "model_count": "6 / 28"
  7654. item[@"sales_rep"]= nssales_rep;
  7655. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7656. item[@"so#"]= nssoid;
  7657. item[@"create_by"]= nscreate_by;
  7658. item[@"customer_name"]= nscompany_name;
  7659. item[@"customer_contact"] = customer_contact;
  7660. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7661. item[@"purchase_time"]= nscreate_time;
  7662. int statusCode = status;
  7663. if (statusCode == 2) {
  7664. statusCode = erpStatus;
  7665. } else if (statusCode == 3) {
  7666. statusCode = 15;
  7667. }
  7668. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7669. item[@"order_status"]= nsstatus;
  7670. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7671. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7672. // item[@"model_count"]
  7673. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7674. count++;
  7675. }
  7676. ret[@"count"]= [NSNumber numberWithInt:count];
  7677. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7678. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7679. ret[@"result"]= [NSNumber numberWithInt:2];
  7680. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7681. ret[@"time_zone"] = @"PST";
  7682. sqlite3_finalize(statement);
  7683. }
  7684. 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];
  7685. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7686. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7687. [iSalesDB close_db:db];
  7688. return [RAConvertor dict2data:ret];
  7689. }
  7690. #pragma mark update gnotes
  7691. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7692. {
  7693. DebugLog(@"params: %@",params);
  7694. // comments = Meyoyoyoyoyoyoy;
  7695. // orderCode = MOB1608110001;
  7696. // password = 123456;
  7697. // user = EvanK;
  7698. 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]];
  7699. int ret = [iSalesDB execSql:sql];
  7700. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7701. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7702. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7703. // [dic setValue:@"160409" forKey:@"min_ver"];
  7704. return [RAConvertor dict2data:dic];
  7705. }
  7706. #pragma mark move to wishlist
  7707. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7708. {
  7709. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7710. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7711. _id = [NSString stringWithFormat:@"(%@)",_id];
  7712. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7713. sqlite3 *db = [iSalesDB get_db];
  7714. __block NSString *product_id = @"";
  7715. __block NSString *item_count_str = @"";
  7716. // __block NSString *item_id = nil;
  7717. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7718. // product_id = [self textAtColumn:0 statement:stmt];
  7719. int item_count = sqlite3_column_int(stmt, 1);
  7720. // item_id = [self textAtColumn:2 statement:stmt];
  7721. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7722. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7723. if (p_id.length) {
  7724. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7725. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7726. }
  7727. }];
  7728. [iSalesDB close_db:db];
  7729. // 去除第一个,
  7730. if (product_id.length > 1) {
  7731. product_id = [product_id substringFromIndex:1];
  7732. }
  7733. if (item_count_str.length > 1) {
  7734. item_count_str = [item_count_str substringFromIndex:1];
  7735. }
  7736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7737. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7738. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7739. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7740. sqlite3 *db1 = [iSalesDB get_db];
  7741. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7742. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7743. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7744. // 删除
  7745. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7746. int ret = [iSalesDB execSql:deleteSql db:db1];
  7747. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7748. [iSalesDB close_db:db1];
  7749. return [RAConvertor dict2data:dic];
  7750. }
  7751. #pragma mark cart delete
  7752. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7753. {
  7754. // cartItemId = 548;
  7755. // orderCode = MOB1608110001;
  7756. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7757. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7758. _id = [NSString stringWithFormat:@"(%@)",_id];
  7759. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7760. int ret = [iSalesDB execSql:sql];
  7761. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7762. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7763. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7764. // [dic setValue:@"160409" forKey:@"min_ver"];
  7765. return [RAConvertor dict2data:dic];
  7766. }
  7767. #pragma mark set price
  7768. +(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7769. {
  7770. DebugLog(@"cart set price params: %@",params);
  7771. // "cartitem_id" = 1;
  7772. // discount = "0.000000";
  7773. // "item_note" = "";
  7774. // price = "269.000000";
  7775. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7776. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7777. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7778. // NSString *price = [self valueInParams:params key:@"price"];
  7779. // bool badd_price_changed=false;
  7780. // sqlite3* db=[iSalesDB get_db];
  7781. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7782. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7783. // NSRange range;
  7784. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7785. //
  7786. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7787. // badd_price_changed=true;
  7788. // [iSalesDB close_db:db];
  7789. //
  7790. // if(badd_price_changed)
  7791. // {
  7792. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7793. // }
  7794. //
  7795. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7796. NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7797. int ret = [iSalesDB execSql:sql];
  7798. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7799. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7800. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7801. // [dic setValue:@"160409" forKey:@"min_ver"];
  7802. // return [RAConvertor dict2data:dic];
  7803. return dic;
  7804. }
  7805. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7806. {
  7807. DebugLog(@"cart set price params: %@",params);
  7808. // "cartitem_id" = 1;
  7809. // discount = "0.000000";
  7810. // "item_note" = "";
  7811. // price = "269.000000";
  7812. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7813. NSString *notes = [self valueInParams:params key:@"item_note"];
  7814. NSString *discount = [self valueInParams:params key:@"discount"];
  7815. NSString *price = [self valueInParams:params key:@"price"];
  7816. // bool badd_price_changed=false;
  7817. // sqlite3* db=[iSalesDB get_db];
  7818. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7819. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7820. // NSRange range;
  7821. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7822. //
  7823. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7824. // badd_price_changed=true;
  7825. // [iSalesDB close_db:db];
  7826. //
  7827. // if(badd_price_changed)
  7828. // {
  7829. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7830. // }
  7831. //
  7832. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7833. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7834. int ret = [iSalesDB execSql:sql];
  7835. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7836. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7837. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7838. // [dic setValue:@"160409" forKey:@"min_ver"];
  7839. return [RAConvertor dict2data:dic];
  7840. }
  7841. #pragma mark set line notes
  7842. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7843. {
  7844. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7845. NSString *notes = [self valueInParams:params key:@"notes"];
  7846. notes = [self translateSingleQuote:notes];
  7847. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7848. int ret = [iSalesDB execSql:sql];
  7849. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7850. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7851. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7852. // [dic setValue:@"160409" forKey:@"min_ver"];
  7853. return [RAConvertor dict2data:dic];
  7854. }
  7855. #pragma mark set qty
  7856. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7857. {
  7858. assert(params[@"can_create_backorder"]!=nil);
  7859. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7860. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7861. int item_count = [params[@"inputInt"] intValue];
  7862. // 购买检查数量大于库存
  7863. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7864. if (!params[@"can_create_backorder"]) {
  7865. 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];
  7866. __block BOOL out_of_stock = NO;
  7867. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7868. int availability = sqlite3_column_int(stmt, 0);
  7869. if (availability < item_count) {
  7870. out_of_stock = YES;
  7871. }
  7872. }];
  7873. if (out_of_stock) { // 缺货
  7874. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7875. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7876. return [RAConvertor dict2data:dic];
  7877. }
  7878. }
  7879. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7880. int ret = [iSalesDB execSql:sql];
  7881. __block int item_id = 0;
  7882. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7883. item_id = sqlite3_column_int(stmt, 0);
  7884. }];
  7885. sqlite3 *db = [iSalesDB get_db];
  7886. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7887. [iSalesDB close_db:db];
  7888. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7889. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7890. // [dic setValue:@"160409" forKey:@"min_ver"];
  7891. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7892. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7893. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7894. return [RAConvertor dict2data:dic];
  7895. }
  7896. #pragma mark place order
  7897. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7898. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7899. if (aname) {
  7900. [dic setValue:aname forKey:@"aname"];
  7901. }
  7902. if (control) {
  7903. [dic setValue:control forKey:@"control"];
  7904. }
  7905. if (keyboard) {
  7906. [dic setValue:keyboard forKey:@"keyboard"];
  7907. }
  7908. if (name) {
  7909. [dic setValue:name forKey:@"name"];
  7910. }
  7911. if (value) {
  7912. [dic setValue:value forKey:@"value"];
  7913. }
  7914. return dic;
  7915. }
  7916. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7917. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7918. orderCode = [self translateSingleQuote:orderCode];
  7919. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7920. 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];
  7921. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7922. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7923. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7924. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7925. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7926. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7927. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7928. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7929. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7930. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7931. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7932. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7933. NSString *email = [self textAtColumn:11 statement:stmt];
  7934. NSString *phone = [self textAtColumn:12 statement:stmt];
  7935. NSString *fax = [self textAtColumn:13 statement:stmt];
  7936. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7937. NSString *city = [self textAtColumn:15 statement:stmt];
  7938. NSString *state = [self textAtColumn:16 statement:stmt];
  7939. NSString *country = [self textAtColumn:17 statement:stmt];
  7940. NSString *name = [self textAtColumn:18 statement:stmt];
  7941. // contact id
  7942. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7943. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7944. [contact_id_dic setValue:@"text" forKey:@"control"];
  7945. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7946. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7947. [contact_id_dic setValue:@"true" forKey:@"required"];
  7948. [contact_id_dic setValue:contact_id forKey:@"value"];
  7949. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7950. // business card
  7951. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7952. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7953. [business_card_dic setValue:@"img" forKey:@"control"];
  7954. [business_card_dic setValue:@"1" forKey:@"disable"];
  7955. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7956. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7957. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7958. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7959. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7960. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7961. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7962. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7963. // fax
  7964. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7965. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7966. // zipcode
  7967. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7968. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7969. // city
  7970. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7971. [customer_dic setValue:city_dic forKey:@"item_12"];
  7972. // state
  7973. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7974. [customer_dic setValue:state_dic forKey:@"item_13"];
  7975. // country
  7976. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7977. [customer_dic setValue:country_dic forKey:@"item_14"];
  7978. // company name
  7979. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7980. [customer_dic setValue:company_dic forKey:@"item_2"];
  7981. // addr_1
  7982. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7983. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7984. // addr_2
  7985. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7986. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7987. // addr_3
  7988. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7989. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7990. // addr_4
  7991. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7992. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7993. // Contact
  7994. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7995. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7996. // email
  7997. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7998. [customer_dic setValue:email_dic forKey:@"item_8"];
  7999. // phone
  8000. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8001. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8002. // title
  8003. [customer_dic setValue:@"Customer" forKey:@"title"];
  8004. // count
  8005. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8006. }];
  8007. // setting
  8008. NSDictionary *setting = params[@"setting"];
  8009. NSNumber *hide = setting[@"CustomerHide"];
  8010. [customer_dic setValue:hide forKey:@"hide"];
  8011. return customer_dic;
  8012. }
  8013. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8014. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8015. // setting
  8016. NSDictionary *setting = params[@"setting"];
  8017. NSNumber *hide = setting[@"ShipToHide"];
  8018. [dic setValue:hide forKey:@"hide"];
  8019. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8020. orderCode = [self translateSingleQuote:orderCode];
  8021. 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];
  8022. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8023. NSString *cid = [self textAtColumn:0 statement:stmt];
  8024. NSString *name = [self textAtColumn:1 statement:stmt];
  8025. NSString *ext = [self textAtColumn:2 statement:stmt];
  8026. NSString *contact = [self textAtColumn:3 statement:stmt];
  8027. NSString *email = [self textAtColumn:4 statement:stmt];
  8028. NSString *fax = [self textAtColumn:5 statement:stmt];
  8029. NSString *phone = [self textAtColumn:6 statement:stmt];
  8030. // count
  8031. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8032. // title
  8033. [dic setValue:@"Ship To" forKey:@"title"];
  8034. // choose
  8035. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8036. [choose_dic setValue:@"choose" forKey:@"aname"];
  8037. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8038. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8039. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8040. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8041. @"receive_contact" : @"customer_contact",
  8042. @"receive_email" : @"customer_email",
  8043. @"receive_ext" : @"customer_contact_ext",
  8044. @"receive_fax" : @"customer_fax",
  8045. @"receive_name" : @"customer_name",
  8046. @"receive_phone" : @"customer_phone"},
  8047. @"refresh" : [NSNumber numberWithInteger:1],
  8048. @"type" : @"pull"};
  8049. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8050. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8051. @"name" : @"Add new address",
  8052. @"refresh" : [NSNumber numberWithInteger:1],
  8053. @"value" : @"new_addr"
  8054. };
  8055. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8056. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8057. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8058. @"receive_contact" : @"customer_contact",
  8059. @"receive_email" : @"customer_email",
  8060. @"receive_ext" : @"customer_contact_ext",
  8061. @"receive_fax" : @"customer_fax",
  8062. @"receive_name" : @"customer_name",
  8063. @"receive_phone" : @"customer_phone"},
  8064. @"name" : @"select_ship_to",
  8065. @"refresh" : [NSNumber numberWithInteger:1],
  8066. @"value" : @"Sales_Order_Ship_To"};
  8067. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8068. [dic setValue:choose_dic forKey:@"item_0"];
  8069. // contact id
  8070. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8071. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8072. [contact_id_dic setValue:@"true" forKey:@"required"];
  8073. [dic setValue:contact_id_dic forKey:@"item_1"];
  8074. // company name
  8075. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8076. [dic setValue:company_name_dic forKey:@"item_2"];
  8077. // address
  8078. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8079. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8080. if ([required integerValue]) {
  8081. [ext_dic setValue:@"true" forKey:@"required"];
  8082. }
  8083. [dic setValue:ext_dic forKey:@"item_3"];
  8084. // contact
  8085. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8086. [dic setValue:contact_dic forKey:@"item_4"];
  8087. // phone
  8088. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8089. [dic setValue:phone_dic forKey:@"item_5"];
  8090. // fax
  8091. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8092. [dic setValue:fax_dic forKey:@"item_6"];
  8093. // email
  8094. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8095. [dic setValue:email_dic forKey:@"item_7"];
  8096. }];
  8097. return dic;
  8098. }
  8099. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8100. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8101. // setting
  8102. NSDictionary *setting = params[@"setting"];
  8103. NSNumber *hide = setting[@"ShipFromHide"];
  8104. [dic setValue:hide forKey:@"hide"];
  8105. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8106. orderCode = [self translateSingleQuote:orderCode];
  8107. 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];
  8108. 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';";
  8109. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8110. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8111. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8112. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8113. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8114. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8115. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8116. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8117. if (!cid.length) {
  8118. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8119. cid = [self textAtColumn:0 statement:statment];
  8120. name = [self textAtColumn:1 statement:statment];
  8121. ext = [self textAtColumn:2 statement:statment];
  8122. contact = [self textAtColumn:3 statement:statment];
  8123. email = [self textAtColumn:4 statement:statment];
  8124. fax = [self textAtColumn:5 statement:statment];
  8125. phone = [self textAtColumn:6 statement:statment];
  8126. }];
  8127. }
  8128. // count
  8129. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8130. // title
  8131. [dic setValue:@"Ship From" forKey:@"title"];
  8132. // hide
  8133. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8134. // choose
  8135. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8136. [choose_dic setValue:@"choose" forKey:@"aname"];
  8137. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8138. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8139. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8140. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8141. @"sender_contact" : @"customer_contact",
  8142. @"sender_email" : @"customer_email",
  8143. @"sender_ext" : @"customer_contact_ext",
  8144. @"sender_fax" : @"customer_fax",
  8145. @"sender_name" : @"customer_name",
  8146. @"sender_phone" : @"customer_phone"},
  8147. @"name" : @"select_cid",
  8148. @"refresh" : [NSNumber numberWithInteger:0],
  8149. @"value" : @"Sales_Order_Ship_From"};
  8150. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8151. [dic setValue:choose_dic forKey:@"item_0"];
  8152. // contact id
  8153. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8154. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8155. [contact_id_dic setValue:@"true" forKey:@"required"];
  8156. [dic setValue:contact_id_dic forKey:@"item_1"];
  8157. // company name
  8158. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8159. [dic setValue:company_name_dic forKey:@"item_2"];
  8160. // address
  8161. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8162. [dic setValue:ext_dic forKey:@"item_3"];
  8163. // contact
  8164. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8165. [dic setValue:contact_dic forKey:@"item_4"];
  8166. // phone
  8167. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8168. [dic setValue:phone_dic forKey:@"item_5"];
  8169. // fax
  8170. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8171. [dic setValue:fax_dic forKey:@"item_6"];
  8172. // email
  8173. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8174. [dic setValue:email_dic forKey:@"item_7"];
  8175. }];
  8176. return dic;
  8177. }
  8178. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8179. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8180. // setting
  8181. NSDictionary *setting = params[@"setting"];
  8182. NSNumber *hide = setting[@"FreightBillToHide"];
  8183. [dic setValue:hide forKey:@"hide"];
  8184. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8185. orderCode = [self translateSingleQuote:orderCode];
  8186. 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];
  8187. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8188. NSString *cid = [self textAtColumn:0 statement:stmt];
  8189. NSString *name = [self textAtColumn:1 statement:stmt];
  8190. NSString *ext = [self textAtColumn:2 statement:stmt];
  8191. NSString *contact = [self textAtColumn:3 statement:stmt];
  8192. NSString *email = [self textAtColumn:4 statement:stmt];
  8193. NSString *fax = [self textAtColumn:5 statement:stmt];
  8194. NSString *phone = [self textAtColumn:6 statement:stmt];
  8195. // count
  8196. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8197. // title
  8198. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8199. // hide
  8200. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8201. // choose
  8202. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8203. [choose_dic setValue:@"choose" forKey:@"aname"];
  8204. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8205. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8206. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8207. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8208. @"shipping_billto_contact" : @"receive_contact",
  8209. @"shipping_billto_email" : @"receive_email",
  8210. @"shipping_billto_ext" : @"receive_ext",
  8211. @"shipping_billto_fax" : @"receive_fax",
  8212. @"shipping_billto_name" : @"receive_name",
  8213. @"shipping_billto_phone" : @"receive_phone"},
  8214. @"type" : @"pull"};
  8215. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8216. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8217. @"type" : @"pull",
  8218. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8219. @"shipping_billto_contact" : @"customer_contact",
  8220. @"shipping_billto_email" : @"customer_email",
  8221. @"shipping_billto_ext" : @"customer_contact_ext",
  8222. @"shipping_billto_fax" : @"customer_fax",
  8223. @"shipping_billto_name" : @"customer_name",
  8224. @"shipping_billto_phone" : @"customer_phone"}
  8225. };
  8226. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8227. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8228. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8229. @"shipping_billto_contact" : @"sender_contact",
  8230. @"shipping_billto_email" : @"sender_email",
  8231. @"shipping_billto_ext" : @"sender_ext",
  8232. @"shipping_billto_fax" : @"sender_fax",
  8233. @"shipping_billto_name" : @"sender_name",
  8234. @"shipping_billto_phone" : @"sender_phone"},
  8235. @"type" : @"pull"
  8236. };
  8237. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8238. NSDictionary *select_freight_bill_to_dic = @{
  8239. @"aname" : @"Select freight bill to",
  8240. @"name" : @"select_cid",
  8241. @"refresh" : [NSNumber numberWithInteger:0],
  8242. @"value" : @"Sales_Order_Freight_Bill_To",
  8243. @"key_map" : @{
  8244. @"shipping_billto_cid" : @"customer_cid",
  8245. @"shipping_billto_contact" : @"customer_contact",
  8246. @"shipping_billto_email" : @"customer_email",
  8247. @"shipping_billto_ext" : @"customer_contact_ext",
  8248. @"shipping_billto_fax" : @"customer_fax",
  8249. @"shipping_billto_name" : @"customer_name",
  8250. @"shipping_billto_phone" : @"customer_phone"
  8251. }
  8252. };
  8253. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8254. [dic setValue:choose_dic forKey:@"item_0"];
  8255. // contact id
  8256. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8257. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8258. [contact_id_dic setValue:@"true" forKey:@"required"];
  8259. [dic setValue:contact_id_dic forKey:@"item_1"];
  8260. // company name
  8261. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8262. [dic setValue:company_name_dic forKey:@"item_2"];
  8263. // address
  8264. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8265. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8266. if ([ext_required integerValue]) {
  8267. [ext_dic setValue:@"true" forKey:@"required"];
  8268. }
  8269. [dic setValue:ext_dic forKey:@"item_3"];
  8270. // contact
  8271. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8272. [dic setValue:contact_dic forKey:@"item_4"];
  8273. // phone
  8274. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8275. [dic setValue:phone_dic forKey:@"item_5"];
  8276. // fax
  8277. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8278. [dic setValue:fax_dic forKey:@"item_6"];
  8279. // email
  8280. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8281. [dic setValue:email_dic forKey:@"item_7"];
  8282. }];
  8283. return dic;
  8284. }
  8285. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8286. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8287. // setting
  8288. NSDictionary *setting = params[@"setting"];
  8289. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8290. [dic setValue:hide forKey:@"hide"];
  8291. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8292. orderCode = [self translateSingleQuote:orderCode];
  8293. 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];
  8294. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8295. NSString *cid = [self textAtColumn:0 statement:stmt];
  8296. NSString *name = [self textAtColumn:1 statement:stmt];
  8297. NSString *ext = [self textAtColumn:2 statement:stmt];
  8298. NSString *contact = [self textAtColumn:3 statement:stmt];
  8299. NSString *email = [self textAtColumn:4 statement:stmt];
  8300. NSString *fax = [self textAtColumn:5 statement:stmt];
  8301. NSString *phone = [self textAtColumn:6 statement:stmt];
  8302. // count
  8303. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8304. // title
  8305. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8306. // hide
  8307. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8308. // choose
  8309. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8310. [choose_dic setValue:@"choose" forKey:@"aname"];
  8311. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8312. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8313. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8314. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8315. @"billing_contact" : @"receive_contact",
  8316. @"billing_email" : @"receive_email",
  8317. @"billing_ext" : @"receive_ext",
  8318. @"billing_fax" : @"receive_fax",
  8319. @"billing_name" : @"receive_name",
  8320. @"billing_phone" : @"receive_phone"},
  8321. @"type" : @"pull"};
  8322. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8323. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8324. @"type" : @"pull",
  8325. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8326. @"billing_contact" : @"customer_contact",
  8327. @"billing_email" : @"customer_email",
  8328. @"billing_ext" : @"customer_contact_ext",
  8329. @"billing_fax" : @"customer_fax",
  8330. @"billing_name" : @"customer_name",
  8331. @"billing_phone" : @"customer_phone"}
  8332. };
  8333. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8334. NSDictionary *select_bill_to_dic = @{
  8335. @"aname" : @"Select bill to",
  8336. @"name" : @"select_cid",
  8337. @"refresh" : [NSNumber numberWithInteger:0],
  8338. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8339. @"key_map" : @{
  8340. @"billing_cid" : @"customer_cid",
  8341. @"billing_contact" : @"customer_contact",
  8342. @"billing_email" : @"customer_email",
  8343. @"billing_ext" : @"customer_contact_ext",
  8344. @"billing_fax" : @"customer_fax",
  8345. @"billing_name" : @"customer_name",
  8346. @"billing_phone" : @"customer_phone"
  8347. }
  8348. };
  8349. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8350. [dic setValue:choose_dic forKey:@"item_0"];
  8351. // contact id
  8352. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8353. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8354. [contact_id_dic setValue:@"true" forKey:@"required"];
  8355. [dic setValue:contact_id_dic forKey:@"item_1"];
  8356. // company name
  8357. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8358. [dic setValue:company_name_dic forKey:@"item_2"];
  8359. // address
  8360. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8361. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8362. if ([ext_required integerValue]) {
  8363. [ext_dic setValue:@"true" forKey:@"required"];
  8364. }
  8365. [dic setValue:ext_dic forKey:@"item_3"];
  8366. // contact
  8367. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8368. [dic setValue:contact_dic forKey:@"item_4"];
  8369. // phone
  8370. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8371. [dic setValue:phone_dic forKey:@"item_5"];
  8372. // fax
  8373. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8374. [dic setValue:fax_dic forKey:@"item_6"];
  8375. // email
  8376. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8377. [dic setValue:email_dic forKey:@"item_7"];
  8378. }];
  8379. return dic;
  8380. }
  8381. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8382. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8383. // setting
  8384. NSDictionary *setting = params[@"setting"];
  8385. NSNumber *hide = setting[@"ReturnToHide"];
  8386. [dic setValue:hide forKey:@"hide"];
  8387. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8388. orderCode = [self translateSingleQuote:orderCode];
  8389. 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];
  8390. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8391. NSString *cid = [self textAtColumn:0 statement:stmt];
  8392. NSString *name = [self textAtColumn:1 statement:stmt];
  8393. NSString *ext = [self textAtColumn:2 statement:stmt];
  8394. NSString *contact = [self textAtColumn:3 statement:stmt];
  8395. NSString *email = [self textAtColumn:4 statement:stmt];
  8396. NSString *fax = [self textAtColumn:5 statement:stmt];
  8397. NSString *phone = [self textAtColumn:6 statement:stmt];
  8398. // count
  8399. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8400. // title
  8401. [dic setValue:@"Return To" forKey:@"title"];
  8402. // hide
  8403. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8404. // choose
  8405. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8406. [choose_dic setValue:@"choose" forKey:@"aname"];
  8407. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8408. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8409. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8410. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8411. @"returnto_contact" : @"sender_contact",
  8412. @"returnto_email" : @"sender_email",
  8413. @"returnto_ext" : @"sender_ext",
  8414. @"returnto_fax" : @"sender_fax",
  8415. @"returnto_name" : @"sender_name",
  8416. @"returnto_phone" : @"sender_phone"},
  8417. @"type" : @"pull"};
  8418. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8419. NSDictionary *select_return_to_dic = @{
  8420. @"aname" : @"Select return to",
  8421. @"name" : @"select_cid",
  8422. @"refresh" : [NSNumber numberWithInteger:0],
  8423. @"value" : @"Contact_Return_To",
  8424. @"key_map" : @{
  8425. @"returnto_cid" : @"customer_cid",
  8426. @"returnto_contact" : @"customer_contact",
  8427. @"returnto_email" : @"customer_email",
  8428. @"returnto_ext" : @"customer_contact_ext",
  8429. @"returnto_fax" : @"customer_fax",
  8430. @"returnto_name" : @"customer_name",
  8431. @"returnto_phone" : @"customer_phone"
  8432. }
  8433. };
  8434. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8435. [dic setValue:choose_dic forKey:@"item_0"];
  8436. // contact id
  8437. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8438. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8439. [contact_id_dic setValue:@"true" forKey:@"required"];
  8440. [dic setValue:contact_id_dic forKey:@"item_1"];
  8441. // company name
  8442. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8443. [dic setValue:company_name_dic forKey:@"item_2"];
  8444. // address
  8445. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8446. [dic setValue:ext_dic forKey:@"item_3"];
  8447. // contact
  8448. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8449. [dic setValue:contact_dic forKey:@"item_4"];
  8450. // phone
  8451. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8452. [dic setValue:phone_dic forKey:@"item_5"];
  8453. // fax
  8454. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8455. [dic setValue:fax_dic forKey:@"item_6"];
  8456. // email
  8457. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8458. [dic setValue:email_dic forKey:@"item_7"];
  8459. }];
  8460. return dic;
  8461. }
  8462. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8463. {
  8464. assert(params[@"user"]!=nil);
  8465. assert(params[@"contact_id"]!=nil);
  8466. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8467. orderCode = [self translateSingleQuote:orderCode];
  8468. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8469. __block double TotalCuft = 0;
  8470. __block double TotalWeight = 0;
  8471. __block int TotalCarton = 0;
  8472. __block double payments = 0;
  8473. // setting
  8474. NSDictionary *setting = params[@"setting"];
  8475. NSNumber *hide = setting[@"ModelInformationHide"];
  8476. [dic setValue:hide forKey:@"hide"];
  8477. 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];
  8478. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8479. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8480. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8481. // item id
  8482. int item_id = sqlite3_column_int(stmt, 0);
  8483. // count
  8484. int item_count = sqlite3_column_int(stmt, 1);
  8485. // stockUom
  8486. int stockUom = sqlite3_column_int(stmt, 2);
  8487. // unit price
  8488. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8489. NSString* Price=nil;
  8490. if([str_price isEqualToString:@""])
  8491. {
  8492. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8493. if(price==nil)
  8494. Price=@"No Price.";
  8495. else
  8496. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8497. }
  8498. else
  8499. {
  8500. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8501. }
  8502. // discount
  8503. double discount = sqlite3_column_double(stmt, 4);
  8504. // name
  8505. NSString *name = [self textAtColumn:5 statement:stmt];
  8506. // description
  8507. NSString *description = [self textAtColumn:6 statement:stmt];
  8508. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8509. // line note
  8510. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8511. int avaulability = sqlite3_column_int(stmt, 8);
  8512. // img
  8513. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8514. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8515. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8516. if(combine != nil)
  8517. {
  8518. // int citem=0;
  8519. int bcount=[[combine valueForKey:@"count"] intValue];
  8520. for(int bc=0;bc<bcount;bc++)
  8521. {
  8522. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8523. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8524. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8525. subTotal += uprice * modulus * item_count;
  8526. }
  8527. }
  8528. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8529. [model_dic setValue:@"model" forKey:@"control"];
  8530. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8531. [model_dic setValue:description forKey:@"description"];
  8532. [model_dic setValue:line_note forKey:@"note"];
  8533. [model_dic setValue:img forKey:@"img_url"];
  8534. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8535. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8536. [model_dic setValue:Price forKey:@"unit_price"];
  8537. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8538. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8539. if (combine) {
  8540. [model_dic setValue:combine forKey:@"combine"];
  8541. }
  8542. // well,what under the row is the info for total
  8543. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8544. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8545. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8546. int carton=[bsubtotaljson[@"carton"] intValue];
  8547. TotalCuft += cuft;
  8548. TotalWeight += weight;
  8549. TotalCarton += carton;
  8550. payments += subTotal;
  8551. //---------------------------
  8552. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8553. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8554. }];
  8555. [dic setValue:@"Model Information" forKey:@"title"];
  8556. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8557. return dic;
  8558. }
  8559. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8560. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8561. [dic setValue:@"Remarks Content" forKey:@"title"];
  8562. // setting
  8563. NSDictionary *setting = params[@"setting"];
  8564. NSNumber *hide = setting[@"RemarksContentHide"];
  8565. [dic setValue:hide forKey:@"hide"];
  8566. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8567. orderCode = [self translateSingleQuote:orderCode];
  8568. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8569. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8570. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8571. int mustCall = sqlite3_column_int(stmt, 1);
  8572. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8573. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8574. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8575. NSDictionary *po_dic = @{
  8576. @"aname" : @"PO#",
  8577. @"control" : @"edit",
  8578. @"keyboard" : @"text",
  8579. @"name" : @"poNumber",
  8580. @"value" : poNumber
  8581. };
  8582. #ifdef BUILD_CONTRAST
  8583. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8584. NSDictionary *schedule_dict = @{
  8585. @"aname" : @"Schdule Date",
  8586. @"control" : @"datepicker",
  8587. @"type": @"date",
  8588. @"required": @"true",
  8589. @"name" : @"schedule_date_str",
  8590. @"value" : nsdate
  8591. };
  8592. #endif
  8593. NSDictionary *must_call_dic = @{
  8594. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8595. @"control" : @"switch",
  8596. @"name" : @"must_call",
  8597. @"value" : mustCall ? @"true" : @"false"
  8598. };
  8599. NSDictionary *general_notes_dic = @{
  8600. @"aname" : @"General notes",
  8601. @"control" : @"text_view",
  8602. @"keyboard" : @"text",
  8603. @"name" : @"comments",
  8604. @"value" : generalNotes
  8605. };
  8606. // NSDictionary *internal_notes_dic = @{
  8607. // @"aname" : @"Internal notes",
  8608. // @"control" : @"text_view",
  8609. // @"keyboard" : @"text",
  8610. // @"name" : @"internal_notes",
  8611. // @"value" : internalNotes
  8612. // };
  8613. #ifdef BUILD_CONTRAST
  8614. [dic setValue:po_dic forKey:@"item_0"];
  8615. [dic setValue:schedule_dict forKey:@"item_1"];
  8616. [dic setValue:must_call_dic forKey:@"item_2"];
  8617. [dic setValue:general_notes_dic forKey:@"item_3"];
  8618. #else
  8619. [dic setValue:po_dic forKey:@"item_0"];
  8620. [dic setValue:must_call_dic forKey:@"item_1"];
  8621. [dic setValue:general_notes_dic forKey:@"item_2"];
  8622. #endif
  8623. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8624. }];
  8625. #ifdef BUILD_CONTRAST
  8626. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8627. #else
  8628. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8629. #endif
  8630. return dic;
  8631. }
  8632. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8633. // params
  8634. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8635. orderCode = [self translateSingleQuote:orderCode];
  8636. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8637. // setting
  8638. NSDictionary *setting = params[@"setting"];
  8639. NSNumber *hide = setting[@"OrderTotalHide"];
  8640. [dic setValue:hide forKey:@"hide"];
  8641. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8642. __block double lift_gate_value = 0;
  8643. __block double handling_fee = 0;
  8644. __block double shipping = 0;
  8645. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8646. int lift_gate = sqlite3_column_int(stmt, 0);
  8647. lift_gate_value = sqlite3_column_double(stmt, 1);
  8648. shipping = sqlite3_column_double(stmt, 2);
  8649. handling_fee = sqlite3_column_double(stmt, 3);
  8650. if (!lift_gate) {
  8651. lift_gate_value = 0;
  8652. }
  8653. }];
  8654. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8655. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8656. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8657. double payments = [[total valueForKey:@"payments"] doubleValue];
  8658. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8659. double totalPrice = payments;
  8660. [dic setValue:@"Order Total" forKey:@"title"];
  8661. NSDictionary *payments_dic = @{
  8662. @"align" : @"right",
  8663. @"aname" : @"Payments/Credits",
  8664. @"control" : @"text",
  8665. @"name" : @"paymentsAndCredits",
  8666. @"type" : @"price",
  8667. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8668. };
  8669. [dic setValue:payments_dic forKey:@"item_0"];
  8670. // version 1.71 remove
  8671. // NSDictionary *handling_fee_dic = @{
  8672. // @"align" : @"right",
  8673. // @"aname" : @"Handling Fee",
  8674. // @"control" : @"text",
  8675. // @"name" : @"handling_fee_value",
  8676. // @"required" : @"true",
  8677. // @"type" : @"price",
  8678. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8679. // };
  8680. NSDictionary *shipping_dic = @{
  8681. @"align" : @"right",
  8682. @"aname" : @"Shipping*",
  8683. @"control" : @"text",
  8684. @"name" : @"shipping",
  8685. @"required" : @"true",
  8686. @"type" : @"price",
  8687. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8688. };
  8689. NSDictionary *lift_gate_dic = @{
  8690. @"align" : @"right",
  8691. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8692. @"control" : @"text",
  8693. @"name" : @"lift_gate_value",
  8694. @"required" : @"true",
  8695. @"type" : @"price",
  8696. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8697. };
  8698. int item_count = 1;
  8699. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8700. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8701. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8702. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8703. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8704. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8705. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8706. } else {
  8707. }
  8708. }
  8709. // version 1.71 remove
  8710. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8711. NSDictionary *total_price_dic = @{
  8712. @"align" : @"right",
  8713. @"aname" : @"Total",
  8714. @"control" : @"text",
  8715. @"name" : @"totalPrice",
  8716. @"type" : @"price",
  8717. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8718. };
  8719. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8720. NSDictionary *total_cuft_dic = @{
  8721. @"align" : @"right",
  8722. @"aname" : @"Total Cuft",
  8723. @"control" : @"text",
  8724. @"name" : @"",
  8725. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8726. };
  8727. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8728. NSDictionary *total_weight_dic = @{
  8729. @"align" : @"right",
  8730. @"aname" : @"Total Weight",
  8731. @"control" : @"text",
  8732. @"name" : @"",
  8733. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8734. };
  8735. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8736. NSDictionary *total_carton_dic = @{
  8737. @"align" : @"right",
  8738. @"aname" : @"Total Carton",
  8739. @"control" : @"text",
  8740. @"name" : @"",
  8741. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8742. };
  8743. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8744. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8745. return dic;
  8746. }
  8747. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8748. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8749. orderCode = [self translateSingleQuote:orderCode];
  8750. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8751. // setting
  8752. NSDictionary *setting = params[@"setting"];
  8753. NSNumber *hide = setting[@"SignatureHide"];
  8754. [dic setValue:hide forKey:@"hide"];
  8755. [dic setValue:@"Signature" forKey:@"title"];
  8756. __block NSString *pic_path = @"";
  8757. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8758. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8759. pic_path = [self textAtColumn:0 statement:stmt];
  8760. }];
  8761. NSDictionary *pic_dic = @{
  8762. @"aname" : @"Signature",
  8763. @"avalue" :pic_path,
  8764. @"control" : @"signature",
  8765. @"name" : @"sign_picpath",
  8766. @"value" : pic_path
  8767. };
  8768. [dic setValue:pic_dic forKey:@"item_0"];
  8769. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8770. return dic;
  8771. }
  8772. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8773. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8774. // setting
  8775. NSDictionary *setting = params[@"setting"];
  8776. NSNumber *hide = setting[@"ShippingMethodHide"];
  8777. [dic setValue:hide forKey:@"hide"];
  8778. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8779. orderCode = [self translateSingleQuote:orderCode];
  8780. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8781. __block NSString *logist = @"";
  8782. __block NSString *lift_gate = @"";
  8783. __block int lift_gate_integer = 0;
  8784. __block NSString *logistic_note = @"";
  8785. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8786. logist = [self textAtColumn:0 statement:stmt];
  8787. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8788. logistic_note = [self textAtColumn:2 statement:stmt];
  8789. }];
  8790. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8791. [dic setValue:@"Shipping Method" forKey:@"title"];
  8792. // val_0
  8793. int PERSONAL_PICK_UP_check = 0;
  8794. int USE_MY_CARRIER_check = 0;
  8795. NSString *logistic_note_text = @"";
  8796. int will_call_check = 0;
  8797. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8798. will_call_check = 1;
  8799. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8800. PERSONAL_PICK_UP_check = 1;
  8801. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8802. USE_MY_CARRIER_check = 1;
  8803. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8804. logistic_note = [logistic_note_array firstObject];
  8805. if (logistic_note_array.count > 1) {
  8806. logistic_note_text = [logistic_note_array lastObject];
  8807. }
  8808. }
  8809. }
  8810. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8811. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8812. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8813. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8814. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8815. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8816. NSDictionary *val0_subItem_item0 = @{
  8817. @"aname" : @"Option",
  8818. @"cadedate" : @{
  8819. @"count" : [NSNumber numberWithInteger:2],
  8820. @"val_0" : @{
  8821. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8822. @"refresh" : [NSNumber numberWithInteger:0],
  8823. @"value" : @"PERSONAL PICK UP",
  8824. @"value_id" : @"PERSONAL PICK UP"
  8825. },
  8826. @"val_1" : @{
  8827. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8828. @"refresh" : [NSNumber numberWithInteger:0],
  8829. @"sub_item" : @{
  8830. @"count" : [NSNumber numberWithInteger:1],
  8831. @"item_0" : @{
  8832. @"aname" : @"BOL",
  8833. @"control" : @"edit",
  8834. @"keyboard" : @"text",
  8835. @"name" : @"logist_note_text",
  8836. @"refresh" : [NSNumber numberWithInteger:0],
  8837. @"required" : @"false",
  8838. @"value" : logistic_note_text
  8839. }
  8840. },
  8841. @"value" : @"USE MY CARRIER",
  8842. @"value_id" : @"USE MY CARRIER"
  8843. }
  8844. },
  8845. @"control" : @"enum",
  8846. @"name" : @"logistic_note",
  8847. @"required" : @"true",
  8848. @"single_select" : @"true"
  8849. };
  8850. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8851. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8852. // val_1
  8853. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8854. if([lift_gate isEqualToString:@""]) {
  8855. if (lift_gate_integer == 1) {
  8856. lift_gate = @"true";
  8857. } else {
  8858. lift_gate = @"false";
  8859. }
  8860. }
  8861. int common_carrier_check = 0;
  8862. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8863. common_carrier_check = 1;
  8864. [params setValue:lift_gate forKey:@"lift_gate"];
  8865. }
  8866. NSDictionary *val_1 = @{
  8867. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8868. @"sub_item" : @{
  8869. @"count" : [NSNumber numberWithInteger:1],
  8870. @"item_0" : @{
  8871. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8872. @"control" : @"switch",
  8873. @"name" : @"lift_gate",
  8874. @"refresh" : [NSNumber numberWithInteger:1],
  8875. @"required" : @"true",
  8876. @"value" : lift_gate
  8877. }
  8878. },
  8879. @"value" : @"COMMON CARRIER",
  8880. @"value_id" : @"COMMON CARRIER"
  8881. };
  8882. // cadedate
  8883. NSDictionary *cadedate = @{
  8884. @"count" : [NSNumber numberWithInteger:2],
  8885. @"val_0" : val_0,
  8886. @"val_1" : val_1
  8887. };
  8888. // item_0
  8889. NSMutableDictionary *item_0 = @{
  8890. @"aname" : @"Shipping",
  8891. @"cadedate" : cadedate,
  8892. @"control" : @"enum",
  8893. @"name" : @"logist",
  8894. @"refresh" : [NSNumber numberWithInteger:1],
  8895. @"single_select" : @"true",
  8896. }.mutableCopy;
  8897. NSNumber *required = setting[@"ShippingMethodRequire"];
  8898. if ([required integerValue]) {
  8899. [item_0 setValue:@"true" forKey:@"required"];
  8900. }
  8901. [dic setValue:item_0 forKey:@"item_0"];
  8902. if ([logist isEqualToString:@"WILL CALL"]) {
  8903. [dic removeObjectForKey:@"lift_gate"];
  8904. }
  8905. return dic;
  8906. }
  8907. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8908. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8909. orderCode = [self translateSingleQuote:orderCode];
  8910. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8911. __block int submit_as_integer = 0;
  8912. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8913. submit_as_integer = sqlite3_column_int(stmt, 0);
  8914. }];
  8915. int check11 = 0;
  8916. int check10 = 0;
  8917. if (submit_as_integer == 11) {
  8918. check11 = 1;
  8919. }
  8920. if (submit_as_integer == 10) {
  8921. check10 = 1;
  8922. }
  8923. // section_0
  8924. NSMutableDictionary *dic = @{
  8925. @"count" : @(1),
  8926. @"item_0" : @{
  8927. @"aname" : @"Submit Order As",
  8928. @"cadedate" : @{
  8929. @"count" : @(2),
  8930. @"val_0" : @{
  8931. @"check" : [NSNumber numberWithInteger:check11],
  8932. @"value" : @"Sales Order",
  8933. @"value_id" : @(11)
  8934. },
  8935. @"val_1" : @{
  8936. @"check" : [NSNumber numberWithInteger:check10],
  8937. @"value" : @"Quote",
  8938. @"value_id" : @(10)
  8939. }
  8940. },
  8941. @"control" : @"enum",
  8942. @"name" : @"erpOrderStatus",
  8943. @"required" : @"true",
  8944. @"single_select" : @"true"
  8945. },
  8946. @"title" : @"Order Type"
  8947. }.mutableCopy;
  8948. // setting
  8949. NSDictionary *setting = params[@"setting"];
  8950. NSNumber *hide = setting[@"OrderTypeHide"];
  8951. [dic setValue:hide forKey:@"hide"];
  8952. return dic;
  8953. }
  8954. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8955. countryCode = [self translateSingleQuote:countryCode];
  8956. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8957. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8958. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8959. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8960. if (name == NULL) {
  8961. name = "";
  8962. }
  8963. if (code == NULL) {
  8964. code = "";
  8965. }
  8966. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8967. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8968. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8969. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8970. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8971. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8972. }
  8973. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8974. [container setValue:stateDic forKey:key];
  8975. }] mutableCopy];
  8976. [ret removeObjectForKey:@"result"];
  8977. // failure 可以不用了,一样的
  8978. if (ret.allKeys.count == 0) {
  8979. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8980. [stateDic setValue:@"Other" forKey:@"value"];
  8981. [stateDic setValue:@"" forKey:@"value_id"];
  8982. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8983. if (state_code && [@"" isEqualToString:state_code]) {
  8984. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8985. }
  8986. NSString *key = [NSString stringWithFormat:@"val_0"];
  8987. [ret setValue:stateDic forKey:key];
  8988. }
  8989. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8990. return ret;
  8991. }
  8992. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8993. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8994. orderCode = [self translateSingleQuote:orderCode];
  8995. 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];
  8996. __block NSString *payType = @"";
  8997. __block NSString *firstName = @"";
  8998. __block NSString *lastName = @"";
  8999. __block NSString *addr1 = @"";
  9000. __block NSString *addr2 = @"";
  9001. __block NSString *zipcode = @"";
  9002. __block NSString *cardType = @"";
  9003. __block NSString *cardNumber = @"";
  9004. __block NSString *securityCode = @"";
  9005. __block NSString *month = @"";
  9006. __block NSString *year = @"";
  9007. __block NSString *city = @"";
  9008. __block NSString *state = @"";
  9009. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9010. payType = [self textAtColumn:0 statement:stmt];
  9011. firstName = [self textAtColumn:1 statement:stmt];
  9012. lastName = [self textAtColumn:2 statement:stmt];
  9013. addr1 = [self textAtColumn:3 statement:stmt];
  9014. addr2 = [self textAtColumn:4 statement:stmt];
  9015. zipcode = [self textAtColumn:5 statement:stmt];
  9016. cardType = [self textAtColumn:6 statement:stmt];
  9017. cardNumber = [self textAtColumn:7 statement:stmt];
  9018. securityCode = [self textAtColumn:8 statement:stmt];
  9019. month = [self textAtColumn:9 statement:stmt];
  9020. year = [self textAtColumn:10 statement:stmt];
  9021. city = [self textAtColumn:11 statement:stmt];
  9022. state = [self textAtColumn:12 statement:stmt];
  9023. }];
  9024. NSString *required = @"true";
  9025. // setting
  9026. NSDictionary *setting = params[@"setting"];
  9027. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9028. if ([requiredNumber integerValue]) {
  9029. required = @"true";
  9030. } else {
  9031. required = @"false";
  9032. }
  9033. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9034. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9035. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9036. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9037. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9038. NSString *type = [self textAtColumn:1 statement:stmt];
  9039. NSMutableDictionary *val = @{
  9040. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9041. @"value" : type,
  9042. @"value_id" : type_id
  9043. }.mutableCopy;
  9044. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9045. NSDictionary *sub_item =nil;
  9046. #ifdef BUILD_CONTRAST
  9047. sub_item = @{
  9048. @"count" : @(3),
  9049. @"item_0" : @{
  9050. @"aname" : @"Number",
  9051. @"control" : @"edit",
  9052. @"keyboard" : @"int",
  9053. @"length" : @"16",
  9054. @"name" : @"credit_card_number",
  9055. @"required" : @"true",
  9056. @"value" : cardNumber
  9057. },
  9058. @"item_1" : @{
  9059. @"aname" : @"Expiration Date",
  9060. @"control" : @"monthpicker",
  9061. @"name" : @"credit_card_expiration",
  9062. @"required" : @"true",
  9063. @"type" : @"date",
  9064. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9065. },
  9066. @"item_2" : @{
  9067. @"aname" : @"Security Code",
  9068. @"control" : @"edit",
  9069. @"keyboard" : @"int",
  9070. @"length" : @"4",
  9071. @"name" : @"credit_card_security_code",
  9072. @"required" : @"true",
  9073. @"value" : securityCode
  9074. },
  9075. };
  9076. #else
  9077. sub_item= @{
  9078. @"count" : @(3),
  9079. @"item_0" : @{
  9080. @"aname" : @"choose",
  9081. @"control" : @"multi_action",
  9082. @"count" : @(1),
  9083. @"item_0" : @{
  9084. @"aname" : @"Same as customer",
  9085. @"key_map" : @{
  9086. @"credit_card_address1" : @"customer_address1",
  9087. @"credit_card_address2" : @"customer_address2",
  9088. @"credit_card_city" : @"customer_city",
  9089. @"credit_card_first_name" : @"customer_first_name",
  9090. @"credit_card_last_name" : @"customer_last_name",
  9091. @"credit_card_state" : @"customer_state",
  9092. @"credit_card_zipcode" : @"customer_zipcode"
  9093. },
  9094. @"type" : @"pull"
  9095. }
  9096. },
  9097. @"item_1" : @{
  9098. @"aname" : @"",
  9099. @"color" : @"red",
  9100. @"control" : @"text",
  9101. @"name" : @"",
  9102. @"value" : @"USA Credit cards only"
  9103. },
  9104. @"item_2" : @{
  9105. @"aname" : @"Fill",
  9106. @"cadedate" : @{
  9107. @"count" : @(2),
  9108. @"val_0" : @{
  9109. @"check" : @(1),
  9110. @"sub_item" : @{
  9111. @"count" : @(11),
  9112. @"item_0" : @{
  9113. @"aname" : @"Type",
  9114. @"cadedate" : @{
  9115. @"count" : @(2),
  9116. @"val_0" : @{
  9117. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9118. @"value" : @"VISA",
  9119. @"value_id" : @"VISA"/*@(0)*/
  9120. },
  9121. @"val_1" : @{
  9122. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9123. @"value" : @"MASTER CARD",
  9124. @"value_id" : @"MASTER CARD"/*@(1)*/
  9125. }
  9126. },
  9127. @"control" : @"enum",
  9128. @"name" : @"credit_card_type",
  9129. @"required" : @"true",
  9130. @"single_select" : @"true"
  9131. },
  9132. @"item_1" : @{
  9133. @"aname" : @"Number",
  9134. @"control" : @"edit",
  9135. @"keyboard" : @"int",
  9136. @"length" : @"16",
  9137. @"name" : @"credit_card_number",
  9138. @"required" : @"true",
  9139. @"value" : cardNumber
  9140. },
  9141. @"item_10" : @{
  9142. @"aname" : @"State",
  9143. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9144. @"control" : @"enum",
  9145. @"enum" : @"true",
  9146. @"name" : @"credit_card_state",
  9147. @"required" : @"true",
  9148. @"single_select" : @"true"
  9149. },
  9150. @"item_2" : @{
  9151. @"aname" : @"Expiration Date",
  9152. @"control" : @"monthpicker",
  9153. @"name" : @"credit_card_expiration",
  9154. @"required" : @"true",
  9155. @"type" : @"date",
  9156. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9157. },
  9158. @"item_3" : @{
  9159. @"aname" : @"Security Code",
  9160. @"control" : @"edit",
  9161. @"keyboard" : @"int",
  9162. @"length" : @"3",
  9163. @"name" : @"credit_card_security_code",
  9164. @"required" : @"true",
  9165. @"value" : securityCode
  9166. },
  9167. @"item_4" : @{
  9168. @"aname" : @"First Name",
  9169. @"control" : @"edit",
  9170. @"keyboard" : @"text",
  9171. @"name" : @"credit_card_first_name",
  9172. @"required" : @"true",
  9173. @"value" : firstName
  9174. },
  9175. @"item_5" : @{
  9176. @"aname" : @"Last Name",
  9177. @"control" : @"edit",
  9178. @"keyboard" : @"text",
  9179. @"name" : @"credit_card_last_name",
  9180. @"required" : @"true",
  9181. @"value" : lastName
  9182. },
  9183. @"item_6" : @{
  9184. @"aname" : @"Address 1",
  9185. @"control" : @"edit",
  9186. @"keyboard" : @"text",
  9187. @"name" : @"credit_card_address1",
  9188. @"required" : @"true",
  9189. @"value" : addr1
  9190. },
  9191. @"item_7" : @{
  9192. @"aname" : @"Address 2",
  9193. @"control" : @"edit",
  9194. @"keyboard" : @"text",
  9195. @"name" : @"credit_card_address2",
  9196. @"value" : addr2
  9197. },
  9198. @"item_8" : @{
  9199. @"aname" : @"zip code",
  9200. @"control" : @"edit",
  9201. @"keyboard" : @"text",
  9202. @"name" : @"credit_card_zipcode",
  9203. @"required" : @"true",
  9204. @"value" : zipcode
  9205. },
  9206. @"item_9" : @{
  9207. @"aname" : @"City",
  9208. @"control" : @"edit",
  9209. @"keyboard" : @"text",
  9210. @"name" : @"credit_card_city",
  9211. @"required" : @"true",
  9212. @"value" : city
  9213. }
  9214. },
  9215. @"value" : @"Fill Now",
  9216. @"value_id" : @""
  9217. },
  9218. @"val_1" : @{
  9219. @"check" : @(0),
  9220. @"value" : @"Fill Later",
  9221. @"value_id" : @""
  9222. }
  9223. },
  9224. @"control" : @"enum",
  9225. @"name" : @"",
  9226. @"single_select" : @"true"
  9227. }
  9228. };
  9229. #endif;
  9230. [val setObject:sub_item forKey:@"sub_item"];
  9231. }
  9232. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9233. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9234. }];
  9235. // "section_2"
  9236. NSMutableDictionary *dic = @{
  9237. @"count" : @(1),
  9238. @"item_0" : @{
  9239. @"aname" : @"Payment",
  9240. @"required" : required,
  9241. @"cadedate" : cadedate,
  9242. // @{
  9243. // @"count" : @(6),
  9244. // @"val_3" : @{
  9245. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9246. // @"value" : @"Check",
  9247. // @"value_id" : @"Check"
  9248. // },
  9249. // @"val_2" : @{
  9250. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9251. // @"value" : @"Cash",
  9252. // @"value_id" : @"Cash"
  9253. // },
  9254. // @"val_1" : @{
  9255. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9256. // @"value" : @"NET 60",
  9257. // @"value_id" : @"NET 30"
  9258. // },
  9259. // @"val_4" : @{
  9260. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9261. // @"value" : @"Wire Transfer",
  9262. // @"value_id" : @"Wire Transfer"
  9263. // },
  9264. // @"val_0" : @{
  9265. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9266. // @"sub_item" : @{
  9267. // @"count" : @(3),
  9268. // @"item_0" : @{
  9269. // @"aname" : @"choose",
  9270. // @"control" : @"multi_action",
  9271. // @"count" : @(1),
  9272. // @"item_0" : @{
  9273. // @"aname" : @"Same as customer",
  9274. // @"key_map" : @{
  9275. // @"credit_card_address1" : @"customer_address1",
  9276. // @"credit_card_address2" : @"customer_address2",
  9277. // @"credit_card_city" : @"customer_city",
  9278. // @"credit_card_first_name" : @"customer_first_name",
  9279. // @"credit_card_last_name" : @"customer_last_name",
  9280. // @"credit_card_state" : @"customer_state",
  9281. // @"credit_card_zipcode" : @"customer_zipcode"
  9282. // },
  9283. // @"type" : @"pull"
  9284. // }
  9285. // },
  9286. // @"item_1" : @{
  9287. // @"aname" : @"",
  9288. // @"color" : @"red",
  9289. // @"control" : @"text",
  9290. // @"name" : @"",
  9291. // @"value" : @"USA Credit cards only"
  9292. // },
  9293. // @"item_2" : @{
  9294. // @"aname" : @"Fill",
  9295. // @"cadedate" : @{
  9296. // @"count" : @(2),
  9297. // @"val_0" : @{
  9298. // @"check" : @(1),
  9299. // @"sub_item" : @{
  9300. // @"count" : @(11),
  9301. // @"item_0" : @{
  9302. // @"aname" : @"Type",
  9303. // @"cadedate" : @{
  9304. // @"count" : @(2),
  9305. // @"val_0" : @{
  9306. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9307. // @"value" : @"VISA",
  9308. // @"value_id" : @(0)
  9309. // },
  9310. // @"val_1" : @{
  9311. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9312. // @"value" : @"MASTER CARD",
  9313. // @"value_id" : @(1)
  9314. // }
  9315. // },
  9316. // @"control" : @"enum",
  9317. // @"name" : @"credit_card_type",
  9318. // @"required" : @"true",
  9319. // @"single_select" : @"true"
  9320. // },
  9321. // @"item_1" : @{
  9322. // @"aname" : @"Number",
  9323. // @"control" : @"edit",
  9324. // @"keyboard" : @"int",
  9325. // @"length" : @"16",
  9326. // @"name" : @"credit_card_number",
  9327. // @"required" : @"true",
  9328. // @"value" : cardNumber
  9329. // },
  9330. // @"item_10" : @{
  9331. // @"aname" : @"State",
  9332. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9333. // @"control" : @"enum",
  9334. // @"enum" : @"true",
  9335. // @"name" : @"credit_card_state",
  9336. // @"required" : @"true",
  9337. // @"single_select" : @"true"
  9338. // },
  9339. // @"item_2" : @{
  9340. // @"aname" : @"Expiration Date",
  9341. // @"control" : @"monthpicker",
  9342. // @"name" : @"credit_card_expiration",
  9343. // @"required" : @"true",
  9344. // @"type" : @"date",
  9345. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9346. // },
  9347. // @"item_3" : @{
  9348. // @"aname" : @"Security Code",
  9349. // @"control" : @"edit",
  9350. // @"keyboard" : @"int",
  9351. // @"length" : @"3",
  9352. // @"name" : @"credit_card_security_code",
  9353. // @"required" : @"true",
  9354. // @"value" : securityCode
  9355. // },
  9356. // @"item_4" : @{
  9357. // @"aname" : @"First Name",
  9358. // @"control" : @"edit",
  9359. // @"keyboard" : @"text",
  9360. // @"name" : @"credit_card_first_name",
  9361. // @"required" : @"true",
  9362. // @"value" : firstName
  9363. // },
  9364. // @"item_5" : @{
  9365. // @"aname" : @"Last Name",
  9366. // @"control" : @"edit",
  9367. // @"keyboard" : @"text",
  9368. // @"name" : @"credit_card_last_name",
  9369. // @"required" : @"true",
  9370. // @"value" : lastName
  9371. // },
  9372. // @"item_6" : @{
  9373. // @"aname" : @"Address 1",
  9374. // @"control" : @"edit",
  9375. // @"keyboard" : @"text",
  9376. // @"name" : @"credit_card_address1",
  9377. // @"required" : @"true",
  9378. // @"value" : addr1
  9379. // },
  9380. // @"item_7" : @{
  9381. // @"aname" : @"Address 2",
  9382. // @"control" : @"edit",
  9383. // @"keyboard" : @"text",
  9384. // @"name" : @"credit_card_address2",
  9385. // @"value" : addr2
  9386. // },
  9387. // @"item_8" : @{
  9388. // @"aname" : @"zip code",
  9389. // @"control" : @"edit",
  9390. // @"keyboard" : @"text",
  9391. // @"name" : @"credit_card_zipcode",
  9392. // @"required" : @"true",
  9393. // @"value" : zipcode
  9394. // },
  9395. // @"item_9" : @{
  9396. // @"aname" : @"City",
  9397. // @"control" : @"edit",
  9398. // @"keyboard" : @"text",
  9399. // @"name" : @"credit_card_city",
  9400. // @"required" : @"true",
  9401. // @"value" : city
  9402. // }
  9403. // },
  9404. // @"value" : @"Fill Now",
  9405. // @"value_id" : @""
  9406. // },
  9407. // @"val_1" : @{
  9408. // @"check" : @(0),
  9409. // @"value" : @"Fill Later",
  9410. // @"value_id" : @""
  9411. // }
  9412. // },
  9413. // @"control" : @"enum",
  9414. // @"name" : @"",
  9415. // @"single_select" : @"true"
  9416. // }
  9417. // },
  9418. // @"value" : @"Visa/Master",
  9419. // @"value_id" : @"Credit Card"
  9420. // },
  9421. // @"val_5" : @{
  9422. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9423. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9424. // @"value_id" : @"FOLLOW EXISTING"
  9425. // }
  9426. // },
  9427. @"control" : @"enum",
  9428. @"name" : @"paymentType",
  9429. @"single_select" : @"true"
  9430. },
  9431. @"title" : @"Payment Information"
  9432. }.mutableCopy;
  9433. NSNumber *hide = setting[@"PaymentInformationHide"];
  9434. [dic setValue:hide forKey:@"hide"];
  9435. return dic;
  9436. }
  9437. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9438. // params
  9439. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9440. orderCode = [self translateSingleQuote:orderCode];
  9441. // 缺货检查
  9442. 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];
  9443. __block BOOL outOfStock = NO;
  9444. sqlite3 *database = db;
  9445. if (!db) {
  9446. database = [iSalesDB get_db];
  9447. }
  9448. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9449. int availability = sqlite3_column_int(stmt, 0);
  9450. int item_qty = sqlite3_column_int(stmt, 1);
  9451. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9452. }];
  9453. if (!db) {
  9454. [iSalesDB close_db:database];
  9455. }
  9456. return outOfStock;
  9457. }
  9458. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9459. {
  9460. assert(params[@"user"]!=nil);
  9461. assert(params[@"contact_id"]!=nil);
  9462. assert(params[@"can_create_backorder"]!=nil);
  9463. sqlite3 *db = [iSalesDB get_db];
  9464. // params
  9465. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9466. orderCode = [self translateSingleQuote:orderCode];
  9467. // 缺货检查
  9468. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9469. if (![params[@"can_create_backorder"] boolValue]) {
  9470. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9471. if (out_of_stock) {
  9472. [iSalesDB close_db:db];
  9473. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9474. [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"];
  9475. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9476. return [RAConvertor dict2data:resultDic];
  9477. }
  9478. }
  9479. // UISetting
  9480. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9481. NSString *cachefolder = [paths objectAtIndex:0];
  9482. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9483. NSData* json =nil;
  9484. json=[NSData dataWithContentsOfFile:img_cache];
  9485. NSError *error=nil;
  9486. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9487. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9488. [params setObject:setting forKey:@"setting"];
  9489. int section_count = 0;
  9490. // 0 Order Type 1 Shipping Method 2 Payment Information
  9491. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9492. // 0 Order Type
  9493. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9494. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9495. [ret setValue:order_type_dic forKey:key0];
  9496. // 1 Shipping Method
  9497. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9498. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9499. [ret setValue:shipping_method_dic forKey:key1];
  9500. // 2 Payment Information
  9501. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9502. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9503. [ret setValue:payment_info_dic forKey:key2];
  9504. // 3 Customer
  9505. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9506. NSDictionary *customer_dic = [self customerDic:params db:db];
  9507. [ret setValue:customer_dic forKey:key3];
  9508. // 4 Ship To
  9509. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9510. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9511. [ret setValue:ship_to_dic forKey:key4];
  9512. // 5 Ship From
  9513. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9514. if (![shipFromDisable integerValue]) {
  9515. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9516. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9517. [ret setValue:ship_from_dic forKey:key5];
  9518. }
  9519. // 6 Freight Bill To
  9520. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9521. if (![freightBillToDisable integerValue]) {
  9522. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9523. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9524. [ret setValue:freight_bill_to forKey:key6];
  9525. }
  9526. // 7 Merchandise Bill To
  9527. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9528. if (![merchandiseBillToDisable integerValue]) {
  9529. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9530. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9531. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9532. }
  9533. // 8 Return To
  9534. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9535. if (![returnToDisable integerValue]) {
  9536. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9537. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9538. [ret setValue:return_to_dic forKey:key8];
  9539. }
  9540. // 9 Model Information
  9541. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9542. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9543. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9544. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9545. [ret setValue:model_info_dic forKey:key9];
  9546. // 10 Remarks Content
  9547. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9548. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9549. [ret setValue:remarks_content_dic forKey:key10];
  9550. // 11 Order Total
  9551. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9552. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9553. [ret setValue:order_total_dic forKey:key11];
  9554. // 12 Signature
  9555. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9556. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9557. [ret setValue:sign_dic forKey:key12];
  9558. [ret setValue:@(section_count) forKey:@"section_count"];
  9559. [iSalesDB close_db:db];
  9560. return [RAConvertor dict2data:ret];
  9561. // return nil;
  9562. }
  9563. #pragma mark addr editor
  9564. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9565. NSMutableDictionary *new_item = [item mutableCopy];
  9566. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9567. return new_item;
  9568. }
  9569. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9570. {
  9571. // "is_subaction" = true;
  9572. // orderCode = MOB1608240002;
  9573. // password = 123456;
  9574. // "subaction_tag" = 1;
  9575. // user = EvanK;
  9576. // {
  9577. // "is_subaction" = true;
  9578. // orderCode = MOB1608240002;
  9579. // password = 123456;
  9580. // "refresh_trigger" = zipcode;
  9581. // "subaction_tag" = 1;
  9582. // user = EvanK;
  9583. // }
  9584. NSString *country_code = nil;
  9585. NSString *zipCode = nil;
  9586. NSString *stateCode = nil;
  9587. NSString *city = nil;
  9588. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9589. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9590. NSString *code_id = params[@"country"];
  9591. country_code = [self countryCodeByid:code_id];
  9592. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9593. NSString *zip_code = params[@"zipcode"];
  9594. // 剔除全部为空格
  9595. int spaceCount = 0;
  9596. for (int i = 0; i < zip_code.length; i++) {
  9597. if ([zip_code characterAtIndex:i] == ' ') {
  9598. spaceCount++;
  9599. }
  9600. }
  9601. if (spaceCount == zip_code.length) {
  9602. zip_code = @"";
  9603. }
  9604. zipCode = zip_code;
  9605. if (zipCode.length > 0) {
  9606. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9607. country_code = [dic valueForKey:@"country_code"];
  9608. if (!country_code) {
  9609. // country_code = @"US";
  9610. NSString *code_id = params[@"country"];
  9611. country_code = [self countryCodeByid:code_id];
  9612. }
  9613. stateCode = [dic valueForKey:@"state_code"];
  9614. if(!stateCode.length) {
  9615. stateCode = params[@"state"];
  9616. }
  9617. city = [dic valueForKey:@"city"];
  9618. if (!city.length) {
  9619. city = params[@"city"];
  9620. }
  9621. // zip code
  9622. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9623. // [zipDic setValue:zipCode forKey:@"value"];
  9624. // [section_0 setValue:zipDic forKey:@"item_11"];
  9625. } else {
  9626. NSString *code_id = params[@"country"];
  9627. country_code = [self countryCodeByid:code_id];
  9628. stateCode = params[@"state"];
  9629. city = params[@"city"];
  9630. }
  9631. }
  9632. }
  9633. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9634. // [ret removeObjectForKey:@"up_params"];
  9635. [ret setObject:@"New Address" forKey:@"title"];
  9636. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9637. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9638. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9639. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9640. // [country_dic removeObjectForKey:@"refresh"];
  9641. // [country_dic removeObjectForKey:@"restore"];
  9642. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9643. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9644. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9645. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9646. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9647. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9648. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9649. // [zip_code_dic removeObjectForKey:@"refresh"];
  9650. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9651. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9652. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9653. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9654. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9655. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9656. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9657. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9658. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9659. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9660. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9661. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9662. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9663. // country
  9664. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9665. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9666. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9667. // state
  9668. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9669. // NSDictionary *tmpDic = @{
  9670. // @"value" : @"Other",
  9671. // @"value_id" : @"",
  9672. // @"check" : [NSNumber numberWithInteger:0]
  9673. // };
  9674. //
  9675. // for (int i = 0; i < allState.allKeys.count; i++) {
  9676. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9677. //
  9678. // NSDictionary *tmp = allState[key];
  9679. // [allState setValue:tmpDic forKey:key];
  9680. // tmpDic = tmp;
  9681. // }
  9682. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9683. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9684. [ret setValue:new_section_0 forKey:@"section_0"];
  9685. return [RAConvertor dict2data:ret];
  9686. }
  9687. #pragma mark save addr
  9688. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9689. {
  9690. // NSString *companyName = [self valueInParams:params key:@"company"];
  9691. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9692. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9693. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9694. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9695. // NSString *countryId = [self valueInParams:params key:@"country"];
  9696. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9697. // NSString *city = [self valueInParams:params key:@"city"];
  9698. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9699. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9700. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9701. // NSString *phone = [self valueInParams:params key:@"phone"];
  9702. // NSString *fax = [self valueInParams:params key:@"fax"];
  9703. // NSString *email = [self valueInParams:params key:@"email"];
  9704. return [self offline_saveContact:params update:NO isCustomer:NO];
  9705. }
  9706. #pragma mark cancel order
  9707. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9708. {
  9709. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9710. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9711. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9712. if (order_id.length) {
  9713. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9714. }
  9715. int ret = [iSalesDB execSql:sql];
  9716. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9717. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9718. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9719. // [dic setValue:@"160409" forKey:@"min_ver"];
  9720. return [RAConvertor dict2data:dic];
  9721. }
  9722. #pragma mark sign order
  9723. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9724. {
  9725. //参考 offline_saveBusinesscard
  9726. DebugLog(@"sign order params: %@",params);
  9727. // orderCode = MOB1608240002;
  9728. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9729. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9730. orderCode = [self translateSingleQuote:orderCode];
  9731. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9732. picPath = [self translateSingleQuote:picPath];
  9733. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9734. int ret = [iSalesDB execSql:sql];
  9735. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9736. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9737. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9738. // [dic setValue:@"160409" forKey:@"min_ver"];
  9739. return [RAConvertor dict2data:dic];
  9740. }
  9741. #pragma mark save order
  9742. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9743. NSString *ret = [self valueInParams:params key:key];
  9744. if (translate) {
  9745. ret = [self translateSingleQuote:ret];
  9746. }
  9747. return ret;
  9748. }
  9749. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9750. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9751. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9752. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9753. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9754. if (schedule_date.length==0 ) {
  9755. schedule_date = @"";
  9756. } else {
  9757. schedule_date = [self rchangeDateFormate:schedule_date];
  9758. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9759. }
  9760. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9761. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9762. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9763. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9764. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9765. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9766. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9767. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9768. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9769. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9770. if (![total_price isEqualToString:@""]) {
  9771. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9772. } else {
  9773. total_price = @"";
  9774. }
  9775. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9776. if ([paymentsAndCredits isEqualToString:@""]) {
  9777. paymentsAndCredits = @"";
  9778. } else {
  9779. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9780. }
  9781. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9782. if ([handling_fee_value isEqualToString:@""]) {
  9783. handling_fee_value = @"";
  9784. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9785. handling_fee_value = @"";
  9786. } else {
  9787. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9788. }
  9789. NSString *handling_fee_placeholder = handling_fee_value;
  9790. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9791. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9792. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9793. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9794. NSString *lift_gate_placeholder = @"";
  9795. if ([lift_gate_value isEqualToString:@""]) {
  9796. lift_gate_placeholder = @"";
  9797. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9798. lift_gate_placeholder = @"";
  9799. } else {
  9800. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9801. }
  9802. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9803. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9804. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9805. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9806. 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];
  9807. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9808. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9809. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9810. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9811. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9812. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9813. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9814. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9815. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9816. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9817. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9818. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9819. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9820. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9821. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9822. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9823. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9824. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9825. if (!number_nil) {
  9826. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9827. }
  9828. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9829. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9830. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9831. if (!security_code_nil) {
  9832. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9833. }
  9834. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9835. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9836. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9837. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9838. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9839. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9840. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9841. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9842. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9843. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9844. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9845. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9846. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9847. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9848. NSString *contact_id = customer_cid;
  9849. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9850. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9851. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9852. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9853. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9854. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9855. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9856. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9857. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9858. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9859. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9860. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9861. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9862. //
  9863. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9864. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9865. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9866. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9867. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9868. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9869. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9870. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9871. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9872. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9873. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9874. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9875. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9876. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9877. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9878. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9879. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9880. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9881. 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];
  9882. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9883. if (receive_cid.length) {
  9884. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9885. }
  9886. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9887. if (receive_name.length) {
  9888. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9889. }
  9890. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9891. if (receive_ext.length) {
  9892. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9893. }
  9894. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9895. if (receive_contact.length) {
  9896. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9897. }
  9898. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9899. if (receive_phone.length) {
  9900. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9901. }
  9902. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9903. if (receive_email.length) {
  9904. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9905. }
  9906. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9907. if (receive_fax.length) {
  9908. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9909. }
  9910. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9911. if (sender_cid.length) {
  9912. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9913. }
  9914. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9915. if (sender_name.length) {
  9916. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9917. }
  9918. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9919. if (sender_ext.length) {
  9920. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9921. }
  9922. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9923. if(sender_contact.length) {
  9924. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9925. }
  9926. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9927. if (sender_phone.length) {
  9928. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9929. }
  9930. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9931. if (sender_fax.length) {
  9932. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9933. }
  9934. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9935. if (sender_email.length) {
  9936. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9937. }
  9938. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9939. if (shipping_billto_cid.length) {
  9940. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9941. }
  9942. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9943. if (shipping_billto_name.length) {
  9944. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9945. }
  9946. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9947. if (shipping_billto_ext.length) {
  9948. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9949. }
  9950. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9951. if (shipping_billto_contact.length) {
  9952. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9953. }
  9954. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9955. if (shipping_billto_phone.length) {
  9956. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9957. }
  9958. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9959. if (shipping_billto_fax.length) {
  9960. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9961. }
  9962. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9963. if (shipping_billto_email.length) {
  9964. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9965. }
  9966. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9967. if (billing_cid.length) {
  9968. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9969. }
  9970. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9971. if (billing_name.length) {
  9972. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9973. }
  9974. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9975. if (billing_ext.length) {
  9976. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9977. }
  9978. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9979. if (billing_contact.length) {
  9980. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9981. }
  9982. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9983. if (billing_phone.length) {
  9984. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9985. }
  9986. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9987. if (billing_fax.length) {
  9988. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9989. }
  9990. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9991. if (billing_email.length) {
  9992. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9993. }
  9994. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9995. if (returnto_cid.length) {
  9996. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9997. }
  9998. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9999. if (returnto_name.length) {
  10000. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10001. }
  10002. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10003. if (returnto_ext.length) {
  10004. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10005. }
  10006. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10007. if (returnto_contact.length) {
  10008. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10009. }
  10010. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10011. if (returnto_phone.length) {
  10012. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10013. }
  10014. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10015. if (returnto_fax.length) {
  10016. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10017. }
  10018. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10019. if (returnto_email.length) {
  10020. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10021. }
  10022. NSString *order_status = @"status = 1,";
  10023. if (submit) {
  10024. order_status = @"status = -11,";
  10025. }
  10026. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10027. NSString *sync_sql = @"";
  10028. if (submit) {
  10029. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10030. [param setValue:sales_rep forKey:@"sales_rep"];
  10031. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10032. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10033. }
  10034. 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];
  10035. DebugLog(@"save order contactSql: %@",contactSql);
  10036. DebugLog(@"save order orderSql: %@",orderSql);
  10037. // int contact_ret = [iSalesDB execSql:contactSql];
  10038. int order_ret = [iSalesDB execSql:orderSql];
  10039. int ret = order_ret;
  10040. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10041. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10042. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10043. // [dic setValue:@"160409" forKey:@"min_ver"];
  10044. [dic setObject:so_id forKey:@"so#"];
  10045. return [RAConvertor dict2data:dic];
  10046. }
  10047. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10048. {
  10049. // id foo = nil;
  10050. // NSMutableArray *a = @[].mutableCopy;
  10051. // [a addObject:foo];
  10052. return [self saveorder:param submit:NO];
  10053. }
  10054. #pragma mark add to cart by name
  10055. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10056. {
  10057. NSString *orderCode = [params objectForKey:@"orderCode"];
  10058. NSDictionary *newParams = @{
  10059. @"product_id" : params[@"product_id_string"],
  10060. @"orderCode" : orderCode,
  10061. @"can_create_backorder":params[@"can_create_backorder"]
  10062. };
  10063. return [self offline_add2cart:[newParams mutableCopy]];
  10064. }
  10065. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10066. {
  10067. NSString *orderCode = [params objectForKey:@"orderCode"];
  10068. NSString *upccode = [params objectForKey:@"upc_code"];
  10069. // product_name = [self translateSingleQuote:product_name];
  10070. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10071. // bool search_upc = [params[@"search_upc"] boolValue];
  10072. sqlite3 *db = [iSalesDB get_db];
  10073. __block NSMutableString *product_id_string = [NSMutableString string];
  10074. // NSString *name = [product_name_array objectAtIndex:i];
  10075. NSString *sql =nil;
  10076. 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];
  10077. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10078. int product_id = sqlite3_column_int(stmt, 0);
  10079. [product_id_string appendFormat:@"%d",product_id];
  10080. }];
  10081. if (!orderCode) {
  10082. orderCode = @"";
  10083. }
  10084. NSDictionary *newParams = @{
  10085. @"product_id" : product_id_string,
  10086. @"orderCode" : orderCode,
  10087. @"can_create_backorder":params[@"can_create_backorder"]
  10088. };
  10089. [iSalesDB close_db:db];
  10090. return [self offline_add2cart:[newParams mutableCopy]];
  10091. }
  10092. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10093. {
  10094. NSString *orderCode = [params objectForKey:@"orderCode"];
  10095. NSString *product_name = [params objectForKey:@"product_name"];
  10096. product_name = [self translateSingleQuote:product_name];
  10097. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10098. bool search_upc = [params[@"search_upc"] boolValue];
  10099. sqlite3 *db = [iSalesDB get_db];
  10100. __block NSMutableString *product_id_string = [NSMutableString string];
  10101. for (int i = 0; i < product_name_array.count; i++) {
  10102. NSString *name = [product_name_array objectAtIndex:i];
  10103. NSString *sql =nil;
  10104. if(search_upc)
  10105. 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];
  10106. else
  10107. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10108. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10109. int product_id = sqlite3_column_int(stmt, 0);
  10110. if (i == product_name_array.count - 1) {
  10111. [product_id_string appendFormat:@"%d",product_id];
  10112. } else {
  10113. [product_id_string appendFormat:@"%d,",product_id];
  10114. }
  10115. }];
  10116. }
  10117. if (!orderCode) {
  10118. orderCode = @"";
  10119. }
  10120. NSDictionary *newParams = @{
  10121. @"product_id" : product_id_string,
  10122. @"orderCode" : orderCode,
  10123. @"can_create_backorder":params[@"can_create_backorder"]
  10124. };
  10125. [iSalesDB close_db:db];
  10126. return [self offline_add2cart:[newParams mutableCopy]];
  10127. }
  10128. #pragma mark reset order
  10129. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10130. {
  10131. // UIApplication * app = [UIApplication sharedApplication];
  10132. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10133. [iSalesDB disable_trigger];
  10134. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10135. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10136. [iSalesDB enable_trigger];
  10137. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10138. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10139. return [RAConvertor dict2data:dic];
  10140. }
  10141. #pragma mark submit order
  10142. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10143. {
  10144. return [self saveorder:params submit:YES];
  10145. }
  10146. #pragma mark copy order
  10147. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10148. {
  10149. NSMutableDictionary *ret = @{}.mutableCopy;
  10150. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10151. sqlite3 *db = [iSalesDB get_db];
  10152. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10153. // 首先查看联系人是否active
  10154. 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];
  10155. __block int customer_is_active = 1;
  10156. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10157. customer_is_active = sqlite3_column_int(stmt, 0);
  10158. }];
  10159. if (!customer_is_active) {
  10160. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10161. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10162. [iSalesDB close_db:db];
  10163. return [RAConvertor dict2data:ret];
  10164. }
  10165. // new order
  10166. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10167. NSString *new_order_code = [self get_offline_soid:db];
  10168. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10169. user = [self translateSingleQuote:user];
  10170. 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
  10171. __block int result = 1;
  10172. result = [iSalesDB execSql:insert_order_sql db:db];
  10173. if (!result) {
  10174. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10175. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10176. [iSalesDB close_db:db];
  10177. return [RAConvertor dict2data:ret];
  10178. }
  10179. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10180. // __block NSString *product_id = @"";
  10181. __weak typeof(self) weakSelf = self;
  10182. 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];
  10183. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10184. int is_active = sqlite3_column_int(stmt, 1);
  10185. if (is_active) {
  10186. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10187. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10188. 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];
  10189. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10190. }
  10191. }];
  10192. // product_id = [product_id substringFromIndex:1];
  10193. //
  10194. // [self offline_add2cart:@{}.mutableCopy];
  10195. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10196. [iSalesDB close_db:db];
  10197. if (result) {
  10198. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10199. } else {
  10200. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10201. }
  10202. [ret setObject:new_order_code forKey:@"so_id"];
  10203. return [RAConvertor dict2data:ret];
  10204. }
  10205. #pragma mark move wish list to cart
  10206. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10207. assert(params[@"can_create_backorder"]!=nil);
  10208. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10209. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10210. NSString *collectId = params[@"collectId"];
  10211. 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];
  10212. __block NSString *product_id = @"";
  10213. __block NSString *qty = @"";
  10214. __block int number_of_outOfStock = 0;
  10215. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10216. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10217. int productId = sqlite3_column_int(stmt, 0);
  10218. int item_qty = sqlite3_column_int(stmt, 1);
  10219. int availability = sqlite3_column_int(stmt, 2);
  10220. int _id = sqlite3_column_int(stmt, 3);
  10221. if (![params[@"can_create_backorder"] boolValue]) {
  10222. // 库存小于购买量为缺货
  10223. if (availability >= item_qty) {
  10224. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10225. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10226. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10227. } else {
  10228. number_of_outOfStock++;
  10229. }
  10230. } else {
  10231. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10232. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10233. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10234. }
  10235. }];
  10236. NSMutableDictionary *retDic = nil;
  10237. if (![params[@"can_create_backorder"] boolValue]) {
  10238. if (delete_collectId.count == 0) {
  10239. retDic = [NSMutableDictionary dictionary];
  10240. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10241. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10242. return [RAConvertor dict2data:retDic];
  10243. }
  10244. }
  10245. if (product_id.length > 1) {
  10246. product_id = [product_id substringFromIndex:1];
  10247. }
  10248. if (qty.length > 1) {
  10249. qty = [qty substringFromIndex:1];
  10250. }
  10251. NSString *orderCode = params[@"orderCode"];
  10252. if (!orderCode) {
  10253. orderCode = @"";
  10254. }
  10255. NSDictionary *newParams = @{
  10256. @"product_id" : product_id,
  10257. @"orderCode" : orderCode,
  10258. @"qty" : qty,
  10259. @"can_create_backorder":params[@"can_create_backorder"]
  10260. };
  10261. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10262. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10263. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10264. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10265. NSInteger ret = [wish_return[@"result"] intValue];
  10266. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10267. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10268. }
  10269. if (![params[@"can_create_backorder"] boolValue]) {
  10270. if (number_of_outOfStock > 0) {
  10271. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10272. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10273. }
  10274. }
  10275. return [RAConvertor dict2data:retDic];
  10276. }
  10277. #pragma mark - portfolio
  10278. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10279. // assert(params[@"contact_id"]!=nil);
  10280. assert(params[@"user"]!=nil);
  10281. assert(params[@"can_see_price"]!=nil);
  10282. int sort = [[params valueForKey:@"sort"] intValue];
  10283. int offset = [[params valueForKey:@"offset"] intValue];
  10284. int limit = [[params valueForKey:@"limit"] intValue];
  10285. NSString *orderBy = @"";
  10286. switch (sort) {
  10287. case 0:{
  10288. orderBy = @"p.modify_time desc";
  10289. }
  10290. break;
  10291. case 1:{
  10292. orderBy = @"modify_time asc";
  10293. }
  10294. break;
  10295. case 2:{
  10296. orderBy = @"p.name asc";
  10297. }
  10298. break;
  10299. case 3:{
  10300. orderBy = @"p.name desc";
  10301. }
  10302. break;
  10303. case 4:{
  10304. orderBy = @"p.description asc";
  10305. }
  10306. break;
  10307. case 5: {
  10308. orderBy = @"m.default_category asc";
  10309. }
  10310. default:
  10311. break;
  10312. }
  10313. // 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];
  10314. 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];
  10315. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10316. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10317. sqlite3 *db = [iSalesDB get_db];
  10318. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10319. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10320. int product_id = sqlite3_column_int(stmt, 0);
  10321. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10322. NSString *name = [self textAtColumn:1 statement:stmt];
  10323. NSString *description = [self textAtColumn:2 statement:stmt];
  10324. double price = sqlite3_column_double(stmt, 3);
  10325. double discount = sqlite3_column_double(stmt,4);
  10326. int qty = sqlite3_column_int(stmt, 5);
  10327. int percentage = sqlite3_column_int(stmt, 6);
  10328. int item_id = sqlite3_column_int(stmt, 7);
  10329. // int fashion_id = sqlite3_column_int(stmt, 8);
  10330. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10331. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10332. double percent = sqlite3_column_double(stmt, 11);
  10333. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10334. if ([price_null isEqualToString:@"null"]) {
  10335. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10336. }
  10337. NSMutableDictionary *item = @{
  10338. @"linenotes": line_note,
  10339. @"check": @(1),
  10340. @"product_id": product_id_string,
  10341. @"available_qty": @(qty),
  10342. @"available_percent" : @(percent),
  10343. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10344. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10345. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10346. @"img": img_path,
  10347. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10348. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10349. }.mutableCopy;
  10350. if (percentage) {
  10351. [item removeObjectForKey:@"available_qty"];
  10352. } else {
  10353. [item removeObjectForKey:@"available_percent"];
  10354. }
  10355. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10356. if ([qty_null isEqualToString:@"null"]) {
  10357. [item removeObjectForKey:@"available_qty"];
  10358. }
  10359. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10360. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10361. }];
  10362. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10363. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10364. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10365. }
  10366. [iSalesDB close_db:db];
  10367. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10368. [dic setValue:@"" forKey:@"email_content"];
  10369. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10370. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10371. return [RAConvertor dict2data:dic];
  10372. }
  10373. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10374. __block int qty = 0;
  10375. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10376. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10377. qty = sqlite3_column_int(stmt, 0);
  10378. }];
  10379. return qty;
  10380. }
  10381. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10382. {
  10383. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10384. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10385. // NSMutableDictionary * values = params[@"replaceValue"];
  10386. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10387. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10388. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10389. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10390. NSString *pdf_path = @"";
  10391. if (direct) {
  10392. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10393. } else {
  10394. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10395. }
  10396. NSString *create_user = [self valueInParams:params key:@"user"];
  10397. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10398. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10399. // model info
  10400. // 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];
  10401. // V1.90 more color
  10402. 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];
  10403. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10404. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10405. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10406. sqlite3 *db = [iSalesDB get_db];
  10407. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10408. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10409. int product_id = sqlite3_column_int(stmt, 0);
  10410. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10411. double price = sqlite3_column_double(stmt, 1);
  10412. double discount = sqlite3_column_double(stmt,2);
  10413. int qty = sqlite3_column_int(stmt, 3);
  10414. int percentage = sqlite3_column_int(stmt, 4);
  10415. int item_id = sqlite3_column_int(stmt, 5);
  10416. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10417. double percent = sqlite3_column_double(stmt, 7);
  10418. int more_color = sqlite3_column_int(stmt, 8);
  10419. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10420. /* if ([price_null isEqualToString:@"null"]) {
  10421. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10422. }
  10423. */
  10424. [product_ids_string appendFormat:@"%@,",product_id_string];
  10425. // Regular Price
  10426. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10427. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10428. // QTY
  10429. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10430. if ([qty_null isEqualToString:@"null"]) {
  10431. // 查available
  10432. qty = [self model_QTY:product_id_string db:db];
  10433. }
  10434. if (percentage) {
  10435. qty = qty * percent / 100;
  10436. }
  10437. // Special Price
  10438. if ([price_null isEqualToString:@"null"]) {
  10439. // price = regular price
  10440. price = [regular_price_str doubleValue];
  10441. }
  10442. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10443. if (![discount_null isEqualToString:@"null"]) {
  10444. price = price * (1 - discount / 100.0);
  10445. }
  10446. NSMutableDictionary *item = @{
  10447. @"line_note": line_note,
  10448. @"product_id": product_id_string,
  10449. @"available_qty": @(qty),
  10450. @"more_color":@(more_color),
  10451. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10452. @"regular_price" : regular_price_str,
  10453. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10454. }.mutableCopy;
  10455. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10456. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10457. }];
  10458. [iSalesDB close_db:db];
  10459. if (product_ids_string.length > 0) {
  10460. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10461. }
  10462. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10463. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10464. return [RAConvertor dict2data:resultDictionary];
  10465. }
  10466. NSString *model_info = [RAConvertor dict2string:dic];
  10467. // 创建PDF
  10468. // 在preview情况下保存,则不需要新建了
  10469. if (direct) {
  10470. NSMutableDictionary *tear_sheet_params = params;
  10471. // if (tear_sheet_id) {
  10472. // tear_sheet_params = values;
  10473. // }
  10474. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10475. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10476. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10477. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10478. resultDictionary = pdfInfo.mutableCopy;
  10479. } else { // 创建PDF失败
  10480. return pdfData;
  10481. }
  10482. } else {
  10483. // pdf_path 就是本地路径
  10484. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10485. }
  10486. // 将文件移动到PDF Cache文件夹
  10487. NSString *newPath = [pdf_path lastPathComponent];
  10488. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10489. NSString *cachefolder = [paths objectAtIndex:0];
  10490. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10491. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10492. NSFileManager *fileManager = [NSFileManager defaultManager];
  10493. NSError *error = nil;
  10494. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10495. if (error) { // 移动文件失败
  10496. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10497. return [RAConvertor dict2data:resultDictionary];
  10498. }
  10499. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10500. pdf_path = [newPath lastPathComponent];
  10501. // 保存信息
  10502. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10503. NSString *off_params = [RAConvertor dict2string:params];
  10504. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10505. pdf_path = [self translateSingleQuote:pdf_path];
  10506. create_user = [self translateSingleQuote:create_user];
  10507. tear_note = [self translateSingleQuote:tear_note];
  10508. tear_name = [self translateSingleQuote:tear_name];
  10509. model_info = [self translateSingleQuote:model_info];
  10510. configureParams = [self translateSingleQuote:configureParams];
  10511. off_params = [self translateSingleQuote:off_params];
  10512. 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];
  10513. if (tear_sheet_id) {
  10514. int _id = [tear_sheet_id intValue];
  10515. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10516. }
  10517. int result = [iSalesDB execSql:save_pdf_sql];
  10518. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10519. //
  10520. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10521. if (remove_Item) {
  10522. // portfolioId
  10523. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10524. }
  10525. return [RAConvertor dict2data:resultDictionary];
  10526. }
  10527. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10528. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10529. }
  10530. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10531. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10532. }
  10533. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10534. int offset = [[params valueForKey:@"offset"] intValue];
  10535. int limit = [[params valueForKey:@"limit"] intValue];
  10536. NSString *keyword = [params valueForKey:@"keyWord"];
  10537. NSString *where = @"where is_delete is null or is_delete = 0";
  10538. if (keyword.length) {
  10539. keyword = [self translateSingleQuote:keyword];
  10540. 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];
  10541. }
  10542. 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
  10543. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10544. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10545. NSString *cachefolder = [paths objectAtIndex:0];
  10546. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10547. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10548. NSString *name = [self textAtColumn:0 statement:stmt];
  10549. NSString *note = [self textAtColumn:1 statement:stmt];
  10550. NSString *time = [self textAtColumn:2 statement:stmt];
  10551. NSString *user = [self textAtColumn:3 statement:stmt];
  10552. NSString *path = [self textAtColumn:4 statement:stmt];
  10553. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10554. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10555. path = [pdfFolder stringByAppendingPathComponent:path];
  10556. BOOL bdir=NO;
  10557. NSFileManager* fileManager = [NSFileManager defaultManager];
  10558. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10559. {
  10560. //pdf文件不存在
  10561. path=nil;
  10562. }
  10563. time = [self changeDateTimeFormate:time];
  10564. time = [time stringByAppendingString:@" PST"];
  10565. int sheet_id = sqlite3_column_int(stmt, 5);
  10566. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10567. item[@"tearsheetsId"]=@(sheet_id);
  10568. item[@"pdf_path"]=path;
  10569. item[@"create_time"]=time;
  10570. item[@"create_user"]=user;
  10571. item[@"tear_note"]=note;
  10572. item[@"tear_name"]=name;
  10573. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10574. item[@"model_info"]=model_info;
  10575. item[@"off_params"]=off_params;
  10576. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10577. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10578. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10579. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10580. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10581. }];
  10582. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10583. return [RAConvertor dict2data:dic];
  10584. }
  10585. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10586. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10587. // NSString *user = [params objectForKey:@"user"];
  10588. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  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 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];
  10590. int result = [iSalesDB execSql:sql];
  10591. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10592. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10593. if (result == RESULT_TRUE) {
  10594. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10595. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10596. sqlite3 *db = [iSalesDB get_db];
  10597. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10598. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10599. [iSalesDB close_db:db];
  10600. }
  10601. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10602. return [RAConvertor dict2data:dic];
  10603. }
  10604. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10605. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10606. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10607. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10608. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10609. NSDictionary *company_item = @{
  10610. @"control": @"text",
  10611. @"keyboard": @"text",
  10612. @"name": @"company_name",
  10613. @"value": COMPANY_FULL_NAME,
  10614. @"aname": @"Company Name"
  10615. };
  10616. [section_0 setObject:company_item forKey:@"item_0"];
  10617. [dic setObject:section_0 forKey:@"section_0"];
  10618. // Regurlar Price
  10619. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10620. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10621. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10622. __block long val_count = 0;
  10623. NSString *sql = @"select name,type,order_by from price order by order_by";
  10624. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10625. NSString *name = [self textAtColumn:0 statement:stmt];
  10626. int type = sqlite3_column_int(stmt, 1);
  10627. int order_by = sqlite3_column_int(stmt, 2);
  10628. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10629. NSDictionary *price_dic = @{
  10630. @"value" : name,
  10631. @"value_id" : [NSNumber numberWithInteger:type],
  10632. @"check" : order_by == 0 ? @(1) : @(0)
  10633. };
  10634. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10635. val_count = ++(*count);
  10636. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10637. val_count = 0;
  10638. }];
  10639. [cadedate setObject:@{@"value" : @"None",
  10640. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10641. val_count++;
  10642. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10643. [price setObject:cadedate forKey:@"cadedate"];
  10644. [section1 setObject:price forKey:@"item_2"];
  10645. [dic setObject:section1 forKey:@"section_1"];
  10646. return [RAConvertor dict2data:dic];
  10647. }
  10648. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10649. NSString *product_id = [params objectForKey:@"fashionId"];
  10650. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10651. __block int result = RESULT_TRUE;
  10652. __block int qty = 0;
  10653. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10654. qty = sqlite3_column_int(stmt, 0);
  10655. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10656. result = RESULT_FALSE;
  10657. }];
  10658. NSMutableDictionary *dic = @{
  10659. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10660. @"mode" : @"Regular Mode",
  10661. @"quantity_available" : @(qty),
  10662. @"result" : @(result),
  10663. }.mutableCopy;
  10664. return [RAConvertor dict2data:dic];
  10665. }
  10666. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10667. NSString *item_ids = [params objectForKey:@"item_id"];
  10668. NSString *line_notes = [params objectForKey:@"notes"];
  10669. NSString *price_str = [params objectForKey:@"price"];
  10670. NSString *discount_str = [params objectForKey:@"discount"];
  10671. NSString *percent = [params objectForKey:@"available_percent"];
  10672. NSString *qty = [params objectForKey:@"available_qty"];
  10673. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10674. int dot = 0;
  10675. if (line_notes) {
  10676. line_notes = [self translateSingleQuote:line_notes];
  10677. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10678. sql = [sql stringByAppendingString:line_notes];
  10679. dot = 1;
  10680. } else {
  10681. line_notes = @"";
  10682. }
  10683. if (price_str) {
  10684. if (dot) {
  10685. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10686. } else {
  10687. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10688. dot = 1;
  10689. }
  10690. sql = [sql stringByAppendingString:price_str];
  10691. } else {
  10692. price_str = @"";
  10693. }
  10694. if (discount_str) {
  10695. if (dot) {
  10696. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10697. } else {
  10698. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10699. dot = 1;
  10700. }
  10701. sql = [sql stringByAppendingString:discount_str];
  10702. } else {
  10703. discount_str = @"";
  10704. }
  10705. if (percent) {
  10706. if (dot) {
  10707. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10708. } else {
  10709. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10710. dot = 1;
  10711. }
  10712. sql = [sql stringByAppendingString:percent];
  10713. } else {
  10714. percent = @"";
  10715. }
  10716. if (qty) {
  10717. if (dot) {
  10718. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10719. } else {
  10720. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10721. dot = 1;
  10722. }
  10723. sql = [sql stringByAppendingString:qty];
  10724. } else {
  10725. qty = @"";
  10726. }
  10727. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10728. sql = [sql stringByAppendingString:where];
  10729. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10730. int result = [iSalesDB execSql:sql];
  10731. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10732. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10733. return [RAConvertor dict2data:dic];
  10734. }
  10735. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10736. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10737. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10738. sqlite3 *db = [iSalesDB get_db];
  10739. 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];
  10740. int result = [iSalesDB execSql:sql db:db];
  10741. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10742. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10743. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10744. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10745. [iSalesDB close_db:db];
  10746. return [RAConvertor dict2data:dic];
  10747. }
  10748. + (void)offline_removePDFWithName:(NSString *)name {
  10749. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10750. NSString *cachefolder = [paths objectAtIndex:0];
  10751. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10752. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10753. NSFileManager *fileManager = [NSFileManager defaultManager];
  10754. [fileManager removeItemAtPath:path error:nil];
  10755. }
  10756. + (void)offline_clear_PDFCache {
  10757. NSFileManager *fileManager = [NSFileManager defaultManager];
  10758. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10759. NSString *cachefolder = [paths objectAtIndex:0];
  10760. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10761. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10762. for (NSString *path in pdf_files) {
  10763. [self offline_removePDFWithName:[path lastPathComponent]];
  10764. }
  10765. }
  10766. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10767. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10768. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10769. NSString *user = [params objectForKey:@"user"];
  10770. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10771. if (![create_user isEqualToString:user]) {
  10772. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10773. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10774. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10775. return [RAConvertor dict2data:dic];
  10776. }
  10777. 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]];
  10778. __block int is_local = 0;
  10779. __block NSString *path = @"";
  10780. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10781. is_local = sqlite3_column_int(stmt, 0);
  10782. path = [self textAtColumn:1 statement:stmt];
  10783. }];
  10784. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10785. if (is_local == 1) {
  10786. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10787. // 删除文件
  10788. [self offline_removePDFWithName:path];
  10789. }
  10790. int result = [iSalesDB execSql:sql];
  10791. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10792. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10793. return [RAConvertor dict2data:dic];
  10794. }
  10795. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10796. {
  10797. // assert(add_params[@"contact_id"]!=nil);
  10798. assert(add_params[@"user"]!=nil);
  10799. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10800. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10801. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10802. NSString* where=@"1=1";
  10803. if (ver!=nil) {
  10804. where=@"is_dirty=1";
  10805. }
  10806. 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];
  10807. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10808. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10809. sqlite3 *db = [iSalesDB get_db];
  10810. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10811. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10812. NSInteger _id = sqlite3_column_int(stmt, 0);
  10813. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10814. NSString *name = [self textAtColumn:2 statement:stmt];
  10815. NSString *desc = [self textAtColumn:3 statement:stmt];
  10816. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10817. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10818. NSInteger qty = sqlite3_column_int(stmt, 6);
  10819. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10820. double percent = sqlite3_column_double(stmt, 8);
  10821. double price = sqlite3_column_double(stmt, 9);
  10822. double discount = sqlite3_column_double(stmt, 10);
  10823. NSString *img = [self textAtColumn:11 statement:stmt];
  10824. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10825. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10826. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10827. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10828. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10829. if ([price_null isEqualToString:@"null"]) {
  10830. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10831. }
  10832. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10833. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10834. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10835. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10836. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10837. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10838. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10839. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10840. if ([qty_null isEqualToString:@"null"]) {
  10841. // [item setValue:@"null" forKey:@"available_qty"];
  10842. } else {
  10843. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10844. }
  10845. if ([is_percent_null isEqualToString:@"null"]) {
  10846. // [item setValue:@"null" forKey:@"percentage"];
  10847. } else {
  10848. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10849. }
  10850. if ([percent_null isEqualToString:@"null"]) {
  10851. // [item setValue:@"null" forKey:@"percent"];
  10852. } else {
  10853. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10854. }
  10855. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10856. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10857. [item setValue:name forKey:@"name"];
  10858. [item setValue:desc forKey:@"description"];
  10859. [item setValue:img forKey:@"img"];
  10860. [item setValue:line_note forKey:@"line_note"];
  10861. [item setValue:create_time forKey:@"createtime"];
  10862. [item setValue:modify_time forKey:@"modifytime"];
  10863. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10864. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10865. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10866. }];
  10867. [iSalesDB close_db:db];
  10868. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10869. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10870. }
  10871. return ret;
  10872. }
  10873. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10874. {
  10875. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10876. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10877. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10878. NSString* where=@"1=1";
  10879. if (ver!=nil) {
  10880. where=@"is_dirty=1";
  10881. }
  10882. 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];
  10883. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10884. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10885. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10886. NSInteger _id = sqlite3_column_int(stmt, 0);
  10887. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10888. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10889. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10890. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10891. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10892. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10893. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10894. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10895. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10896. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10897. NSString *uuid = [NSUUID UUID].UUIDString;
  10898. int is_delete = sqlite3_column_int(stmt, 11);
  10899. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10900. int is_local = sqlite3_column_int(stmt, 12);
  10901. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10902. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10903. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10904. }
  10905. [item setObject:pdf_path forKey:@"pdf_path"];
  10906. [item setObject:create_user forKey:@"create_user"];
  10907. [item setObject:tear_note forKey:@"tear_note"];
  10908. [item setObject:tear_name forKey:@"tear_name"];
  10909. [item setObject:model_info forKey:@"model_info"];
  10910. [item setObject:createtime forKey:@"createtime"];
  10911. [item setObject:modifytime forKey:@"modifytime"];
  10912. [item setObject:urlParams forKey:@"urlParams"];
  10913. [item setObject:off_params forKey:@"off_params"];
  10914. [item setObject:uuid forKey:@"pdf_token"];
  10915. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10916. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10917. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10918. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10919. } else {
  10920. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10921. }
  10922. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10923. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10924. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10925. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10926. }];
  10927. return ret;
  10928. }
  10929. #pragma mark 2020
  10930. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10931. {
  10932. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10933. sqlite3 *db = [iSalesDB get_db];
  10934. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10935. if (contactType) {
  10936. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10937. } else {
  10938. contactType = @"1 = 1";
  10939. }
  10940. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10941. DebugLog(@"offline contact list keyword: %@",keyword);
  10942. // advanced search
  10943. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10944. if (contact_name) {
  10945. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10946. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10947. } else {
  10948. contact_name = @"";
  10949. }
  10950. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10951. if (customer_phone) {
  10952. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10953. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10954. } else {
  10955. customer_phone = @"";
  10956. }
  10957. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10958. if (customer_fax) {
  10959. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10960. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10961. } else {
  10962. customer_fax = @"";
  10963. }
  10964. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10965. if (customer_zipcode) {
  10966. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10967. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10968. } else {
  10969. customer_zipcode = @"";
  10970. }
  10971. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10972. if (customer_sales_rep) {
  10973. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10974. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10975. } else {
  10976. customer_sales_rep = @"";
  10977. }
  10978. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10979. if (customer_state) {
  10980. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10981. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10982. } else {
  10983. customer_state = @"";
  10984. }
  10985. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10986. if (customer_name) {
  10987. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10988. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10989. } else {
  10990. customer_name = @"";
  10991. }
  10992. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10993. if (customer_country) {
  10994. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10995. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10996. } else {
  10997. customer_country = @"";
  10998. }
  10999. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11000. if (customer_cid) {
  11001. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11002. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11003. } else {
  11004. customer_cid = @"";
  11005. }
  11006. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11007. if (customer_city) {
  11008. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11009. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11010. } else {
  11011. customer_city = @"";
  11012. }
  11013. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11014. if (customer_address) {
  11015. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11016. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11017. } else {
  11018. customer_address = @"";
  11019. }
  11020. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11021. if (customer_email) {
  11022. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11023. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11024. } else {
  11025. customer_email = @"";
  11026. }
  11027. NSString *price_name = [params valueForKey:@"price_name"];
  11028. if (price_name) {
  11029. if ([price_name containsString:@","]) {
  11030. // 首先从 price表中查处name
  11031. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11032. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11033. for (int i = 1;i < pArray.count;i++) {
  11034. NSString *p = pArray[i];
  11035. [mutablePStr appendFormat:@" or type = %@ ",p];
  11036. }
  11037. [mutablePStr appendString:@";"];
  11038. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11039. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11040. char *name = (char *)sqlite3_column_text(stmt, 0);
  11041. if (!name)
  11042. name = "";
  11043. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11044. }];
  11045. // 再根据name 拼sql
  11046. NSMutableString *mutable_price_name = [NSMutableString string];
  11047. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11048. for (int i = 1; i < price_name_array.count; i++) {
  11049. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11050. }
  11051. [mutable_price_name appendString:@")"];
  11052. price_name = mutable_price_name;
  11053. } else {
  11054. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11055. if ([price_name isEqualToString:@""]) {
  11056. price_name = @"";
  11057. } else {
  11058. __block NSString *price;
  11059. price_name = [self translateSingleQuote:price_name];
  11060. [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) {
  11061. char *p = (char *)sqlite3_column_text(stmt, 0);
  11062. if (p == NULL) {
  11063. p = "";
  11064. }
  11065. price = [NSString stringWithUTF8String:p];
  11066. }];
  11067. if ([price isEqualToString:@""]) {
  11068. price_name = @"";
  11069. } else {
  11070. price = [self translateSingleQuote:price];
  11071. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11072. }
  11073. }
  11074. }
  11075. } else {
  11076. price_name = @"";
  11077. }
  11078. int limit = [[params valueForKey:@"limit"] intValue];
  11079. int offset = [[params valueForKey:@"offset"] intValue];
  11080. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11081. 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];
  11082. 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];
  11083. // int result= [iSalesDB AddExFunction:db];
  11084. int count =0;
  11085. NSString *sqlQuery = nil;
  11086. if(keyword.length==0)
  11087. {
  11088. // 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];
  11089. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11090. sqlQuery = sql;
  11091. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11092. }
  11093. else
  11094. {
  11095. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11096. keyword = keyword.lowercaseString;
  11097. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11098. 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];
  11099. 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]];
  11100. }
  11101. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11102. sqlite3_stmt * statement;
  11103. [ret setValue:@"2" forKey:@"result"];
  11104. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11105. // 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";
  11106. int i = 0;
  11107. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11108. {
  11109. while (sqlite3_step(statement) == SQLITE_ROW)
  11110. {
  11111. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11112. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11113. int editable = sqlite3_column_int(statement, 0);
  11114. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11115. NSString *nscompany_name =nil;
  11116. if(company_name==nil)
  11117. nscompany_name=@"";
  11118. else
  11119. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11120. char *country = (char*)sqlite3_column_text(statement, 2);
  11121. if(country==nil)
  11122. country="";
  11123. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11124. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11125. // if(addr==nil)
  11126. // addr="";
  11127. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11128. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11129. if(zipcode==nil)
  11130. zipcode="";
  11131. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11132. char *state = (char*)sqlite3_column_text(statement, 5);
  11133. if(state==nil)
  11134. state="";
  11135. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11136. char *city = (char*)sqlite3_column_text(statement, 6);
  11137. if(city==nil)
  11138. city="";
  11139. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11140. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11141. // NSString *nscontact_name = nil;
  11142. // if(contact_name==nil)
  11143. // nscontact_name=@"";
  11144. // else
  11145. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11146. char *phone = (char*)sqlite3_column_text(statement, 8);
  11147. NSString *nsphone = nil;
  11148. if(phone==nil)
  11149. nsphone=@"";
  11150. else
  11151. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11152. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11153. if(contact_id==nil)
  11154. contact_id="";
  11155. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11156. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11157. if(addr_1==nil)
  11158. addr_1="";
  11159. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11160. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11161. if(addr_2==nil)
  11162. addr_2="";
  11163. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11164. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11165. if(addr_3==nil)
  11166. addr_3="";
  11167. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11168. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11169. if(addr_4==nil)
  11170. addr_4="";
  11171. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11172. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11173. if(first_name==nil)
  11174. first_name="";
  11175. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11176. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11177. if(last_name==nil)
  11178. last_name="";
  11179. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11180. char *fax = (char*)sqlite3_column_text(statement, 16);
  11181. NSString *nsfax = nil;
  11182. if(fax==nil)
  11183. nsfax=@"";
  11184. else
  11185. {
  11186. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11187. if(nsfax.length>0)
  11188. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11189. }
  11190. char *email = (char*)sqlite3_column_text(statement, 17);
  11191. NSString *nsemail = nil;
  11192. if(email==nil)
  11193. nsemail=@"";
  11194. else
  11195. {
  11196. nsemail= [[NSString alloc]initWithUTF8String:email];
  11197. if(nsemail.length>0)
  11198. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11199. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11200. }
  11201. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11202. [arr_name addObject:nsfirst_name];
  11203. [arr_name addObject:nslast_name];
  11204. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11205. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11206. {
  11207. // decrypt
  11208. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11209. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11210. nsphone=[AESCrypt fastdecrypt:nsphone];
  11211. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11212. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11213. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11214. }
  11215. [arr_addr addObject:nscompany_name];
  11216. [arr_addr addObject:nscontact_name];
  11217. [arr_addr addObject:@"<br>"];
  11218. [arr_addr addObject:nsaddr_1];
  11219. [arr_addr addObject:nsaddr_2];
  11220. [arr_addr addObject:nsaddr_3];
  11221. [arr_addr addObject:nsaddr_4];
  11222. //[arr_addr addObject:nsaddr];
  11223. [arr_addr addObject:nszipcode];
  11224. [arr_addr addObject:nscity];
  11225. [arr_addr addObject:nsstate];
  11226. [arr_addr addObject:nscountry];
  11227. [arr_addr addObject:@"<br>"];
  11228. [arr_addr addObject:nsphone];
  11229. [arr_addr addObject:nsfax];
  11230. [arr_addr addObject:nsemail];
  11231. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11232. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11233. [item setValue:name forKey:@"name"];
  11234. [item setValue:nscontact_id forKey:@"contact_id"];
  11235. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11236. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11237. i++;
  11238. }
  11239. sqlite3_finalize(statement);
  11240. }
  11241. [iSalesDB close_db:db];
  11242. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11243. dispatch_async(dispatch_get_main_queue(), ^{
  11244. UIApplication * app = [UIApplication sharedApplication];
  11245. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11246. [ret setValue:appDelegate.mode forKey:@"mode"];
  11247. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11248. result(ret);
  11249. });
  11250. return ;
  11251. });
  11252. }
  11253. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11254. //{
  11255. //
  11256. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11257. //
  11258. // NSString* contactId = [params valueForKey:@"contactId"];
  11259. //
  11260. //
  11261. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11262. //
  11263. //
  11264. //
  11265. //
  11266. //
  11267. //
  11268. // sqlite3 *db = [iSalesDB get_db];
  11269. //
  11270. //
  11271. //
  11272. //
  11273. //
  11274. // NSString *sqlQuery = nil;
  11275. //
  11276. //
  11277. // {
  11278. // 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];
  11279. //
  11280. // }
  11281. //
  11282. //
  11283. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11284. // sqlite3_stmt * statement;
  11285. //
  11286. //
  11287. // [ret setValue:@"2" forKey:@"result"];
  11288. //
  11289. //
  11290. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11291. // {
  11292. //
  11293. // //int i = 0;
  11294. // if (sqlite3_step(statement) == SQLITE_ROW)
  11295. // {
  11296. //
  11297. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11298. //
  11299. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11300. //
  11301. //
  11302. // // int editable = sqlite3_column_int(statement, 0);
  11303. //
  11304. //
  11305. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11306. // NSString *nscompany_name =nil;
  11307. // if(company_name==nil)
  11308. // nscompany_name=@"";
  11309. // else
  11310. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11311. //
  11312. //
  11313. // char *country = (char*)sqlite3_column_text(statement, 2);
  11314. // if(country==nil)
  11315. // country="";
  11316. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11317. //
  11318. //
  11319. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11320. // // if(addr==nil)
  11321. // // addr="";
  11322. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11323. //
  11324. //
  11325. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11326. // if(zipcode==nil)
  11327. // zipcode="";
  11328. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11329. //
  11330. //
  11331. // char *state = (char*)sqlite3_column_text(statement, 5);
  11332. // if(state==nil)
  11333. // state="";
  11334. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11335. //
  11336. // char *city = (char*)sqlite3_column_text(statement, 6);
  11337. // if(city==nil)
  11338. // city="";
  11339. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11340. //
  11341. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11342. // // NSString *nscontact_name = nil;
  11343. // // if(contact_name==nil)
  11344. // // nscontact_name=@"";
  11345. // // else
  11346. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11347. //
  11348. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11349. // NSString *nsphone = nil;
  11350. // if(phone==nil)
  11351. // nsphone=@"";
  11352. // else
  11353. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11354. //
  11355. //
  11356. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11357. // // if(contact_id==nil)
  11358. // // contact_id="";
  11359. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11360. //
  11361. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11362. // if(addr_1==nil)
  11363. // addr_1="";
  11364. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11365. //
  11366. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11367. // if(addr_2==nil)
  11368. // addr_2="";
  11369. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11370. //
  11371. //
  11372. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11373. // if(addr_3==nil)
  11374. // addr_3="";
  11375. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11376. //
  11377. //
  11378. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11379. // if(addr_4==nil)
  11380. // addr_4="";
  11381. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11382. //
  11383. //
  11384. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11385. // if(first_name==nil)
  11386. // first_name="";
  11387. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11388. //
  11389. //
  11390. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11391. // if(last_name==nil)
  11392. // last_name="";
  11393. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11394. //
  11395. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11396. // NSString *nsfax = nil;
  11397. // if(fax==nil)
  11398. // nsfax=@"";
  11399. // else
  11400. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11401. //
  11402. // char *email = (char*)sqlite3_column_text(statement, 17);
  11403. // NSString *nsemail = nil;
  11404. // if(email==nil)
  11405. // nsemail=@"";
  11406. // else
  11407. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11408. //
  11409. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11410. // NSString *nsimg_0 = nil;
  11411. // if(img_0==nil)
  11412. // nsimg_0=@"";
  11413. // else
  11414. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11415. //
  11416. // [self copy_bcardImg:nsimg_0];
  11417. //
  11418. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11419. // NSString *nsimg_1 = nil;
  11420. // if(img_1==nil)
  11421. // nsimg_1=@"";
  11422. // else
  11423. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11424. // [self copy_bcardImg:nsimg_1];
  11425. //
  11426. //
  11427. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11428. // NSString *nsimg_2 = nil;
  11429. // if(img_2==nil)
  11430. // nsimg_2=@"";
  11431. // else
  11432. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11433. // [self copy_bcardImg:nsimg_2];
  11434. //
  11435. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11436. // NSString *nsprice_type = nil;
  11437. // if(price_type==nil)
  11438. // nsprice_type=@"";
  11439. // else
  11440. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11441. //
  11442. //
  11443. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11444. // NSString *nsnotes = nil;
  11445. // if(notes==nil)
  11446. // nsnotes=@"";
  11447. // else
  11448. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11449. //
  11450. //
  11451. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11452. // NSString *nssalesrep = nil;
  11453. // if(salesrep==nil)
  11454. // nssalesrep=@"";
  11455. // else
  11456. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11457. //
  11458. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11459. //
  11460. //
  11461. // {
  11462. // // decrypt
  11463. //
  11464. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11465. //
  11466. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11467. //
  11468. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11469. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11470. //
  11471. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11472. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11473. //
  11474. // }
  11475. //
  11476. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11477. //
  11478. // [arr_name addObject:nsfirst_name];
  11479. // [arr_name addObject:nslast_name];
  11480. //
  11481. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11482. //
  11483. //
  11484. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11485. //
  11486. // [arr_ext addObject:nsaddr_1];
  11487. // [arr_ext addObject:nsaddr_2];
  11488. // [arr_ext addObject:nsaddr_3];
  11489. // [arr_ext addObject:nsaddr_4];
  11490. // [arr_ext addObject:@"\r\n"];
  11491. //
  11492. // [arr_ext addObject:nscity];
  11493. // [arr_ext addObject:nsstate];
  11494. // [arr_ext addObject:nszipcode];
  11495. // [arr_ext addObject:nscountry];
  11496. //
  11497. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11498. //
  11499. //
  11500. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11501. //
  11502. //
  11503. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11504. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11505. // [item setValue:nscountry forKey:@"customer_country"];
  11506. // [item setValue:nsphone forKey:@"customer_phone"];
  11507. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11508. // [item setValue:nscompany_name forKey:@"customer_name"];
  11509. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11510. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11511. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11512. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11513. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11514. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11515. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11516. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11517. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11518. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11519. // [item setValue:nscity forKey:@"customer_city"];
  11520. // [item setValue:nsstate forKey:@"customer_state"];
  11521. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11522. // [item setValue:contactId forKey:@"customer_cid"];
  11523. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11524. // [item setValue:nsfax forKey:@"customer_fax"];
  11525. // [item setValue:nsemail forKey:@"customer_email"];
  11526. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11527. //
  11528. //
  11529. // [ret setObject:item forKey:@"customerInfo"];
  11530. // // i++;
  11531. //
  11532. //
  11533. //
  11534. // }
  11535. //
  11536. //
  11537. //
  11538. //
  11539. // sqlite3_finalize(statement);
  11540. // }
  11541. //
  11542. //
  11543. //
  11544. // [iSalesDB close_db:db];
  11545. //
  11546. //
  11547. //
  11548. //
  11549. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11550. // dispatch_async(dispatch_get_main_queue(), ^{
  11551. // UIApplication * app = [UIApplication sharedApplication];
  11552. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11553. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11554. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11555. // result(ret);
  11556. // });
  11557. //
  11558. // return ;
  11559. // });
  11560. //}
  11561. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11562. {
  11563. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11564. NSString* order_code= appDelegate.order_code;
  11565. NSString* user = appDelegate.user;
  11566. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11567. NSString* category = [params valueForKey:@"category"];
  11568. if (!category || [category isEqualToString:@""]) {
  11569. category = @"%";
  11570. }
  11571. category = [self translateSingleQuote:category];
  11572. int limit = [[params valueForKey:@"limit"] intValue];
  11573. int offset = [[params valueForKey:@"offset"] intValue];
  11574. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11575. NSString *limit_str = @"";
  11576. if (limited) {
  11577. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11578. }
  11579. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11580. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11581. sqlite3 *db = [iSalesDB get_db];
  11582. // [iSalesDB AddExFunction:db];
  11583. int count;
  11584. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11585. 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];
  11586. double price_min = 0;
  11587. double price_max = 0;
  11588. if ([params.allKeys containsObject:@"alert"]) {
  11589. // alert
  11590. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11591. NSString *alert = params[@"alert"];
  11592. if ([alert isEqualToString:@"Display All"]) {
  11593. alert = [NSString stringWithFormat:@""];
  11594. } else {
  11595. alert = [self translateSingleQuote:alert];
  11596. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11597. }
  11598. // available
  11599. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11600. NSString *available = params[@"available"];
  11601. NSString *available_condition;
  11602. if ([available isEqualToString:@"Display All"]) {
  11603. available_condition = @"";
  11604. } else if ([available isEqualToString:@"Available Now"]) {
  11605. available_condition = @"and availability > 0";
  11606. } else {
  11607. available_condition = @"and availability == 0";
  11608. }
  11609. // best seller
  11610. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11611. NSString *best_seller = @"";
  11612. NSString *order_best_seller = @"m.name asc";
  11613. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11614. best_seller = @"and best_seller > 0";
  11615. order_best_seller = @"m.best_seller desc,m.name asc";
  11616. }
  11617. // price
  11618. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11619. NSString *price = params[@"price"];
  11620. price_min = 0;
  11621. price_max = MAXFLOAT;
  11622. if (user && price != nil) {
  11623. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11624. NSMutableString *priceName = [NSMutableString string];
  11625. for (int i = 0; i < priceTypeArray.count; i++) {
  11626. NSString *pricetype = priceTypeArray[i];
  11627. pricetype = [self translateSingleQuote:pricetype];
  11628. if (i == 0) {
  11629. [priceName appendFormat:@"'%@'",pricetype];
  11630. } else {
  11631. [priceName appendFormat:@",'%@'",pricetype];
  11632. }
  11633. }
  11634. if ([price isEqualToString:@"Display All"]) {
  11635. price = [NSString stringWithFormat:@""];
  11636. } else if([price containsString:@"+"]){
  11637. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11638. price_min = [price doubleValue];
  11639. 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];
  11640. } else {
  11641. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11642. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11643. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11644. 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];
  11645. }
  11646. } else {
  11647. price = @"";
  11648. }
  11649. // sold_by_qty : Sold in quantities of %@
  11650. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11651. NSString *qty = params[@"sold_by_qty"];
  11652. if ([qty isEqualToString:@"Display All"]) {
  11653. qty = @"";
  11654. } else {
  11655. qty = [self translateSingleQuote:qty];
  11656. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11657. }
  11658. // cate
  11659. // category = [self translateSingleQuote:category];
  11660. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11661. // cate mutiple selection
  11662. NSString *category_id = params[@"category"];
  11663. NSMutableArray *cate_id_array = nil;
  11664. NSMutableString *cateWhere = [NSMutableString string];
  11665. if ([category_id isEqualToString:@""] || !category_id) {
  11666. [cateWhere appendString:@"1 = 1"];
  11667. } else {
  11668. if ([category_id containsString:@","]) {
  11669. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11670. } else {
  11671. cate_id_array = [@[category_id] mutableCopy];
  11672. }
  11673. [cateWhere appendString:@"("];
  11674. for (int i = 0; i < cate_id_array.count; i++) {
  11675. if (i == 0) {
  11676. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11677. } else {
  11678. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11679. }
  11680. }
  11681. [cateWhere appendString:@")"];
  11682. }
  11683. // where bestseller > 0 order by bestseller desc
  11684. // sql query: alert availability(int) best_seller(int) price qty
  11685. 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];
  11686. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11687. }
  11688. DebugLog(@"offline category where: %@",where);
  11689. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11690. if (!user) {
  11691. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11692. }
  11693. [ret setValue:filter forKey:@"filter"];
  11694. DebugLog(@"offline_category sql:%@",sqlQuery);
  11695. sqlite3_stmt * statement;
  11696. [ret setValue:@"2" forKey:@"result"];
  11697. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11698. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11699. // int count=0;
  11700. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11701. {
  11702. int i=0;
  11703. while (sqlite3_step(statement) == SQLITE_ROW)
  11704. {
  11705. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11706. char *name = (char*)sqlite3_column_text(statement, 0);
  11707. if(name==nil)
  11708. name="";
  11709. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11710. char *description = (char*)sqlite3_column_text(statement, 1);
  11711. if(description==nil)
  11712. description="";
  11713. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11714. int product_id = sqlite3_column_int(statement, 2);
  11715. int wid = sqlite3_column_int(statement, 3);
  11716. int closeout = sqlite3_column_int(statement, 4);
  11717. int cid = sqlite3_column_int(statement, 5);
  11718. int wisdelete = sqlite3_column_int(statement, 6);
  11719. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11720. int more_color = sqlite3_column_int(statement, 8);
  11721. // Defaul Category ID
  11722. __block NSString *categoryID = nil;
  11723. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11724. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11725. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11726. if(default_category==nil)
  11727. default_category="";
  11728. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11729. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11730. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11731. categoryID = nsdefault_category;
  11732. }];
  11733. if (!categoryID.length) {
  11734. NSString *cateIDs = params[@"category"];
  11735. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11736. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11737. for (NSString *cateID in requestCategoryArr) {
  11738. BOOL needBreak = NO;
  11739. for (NSString *itemCateIDBox in itemCategoryArr) {
  11740. if (itemCateIDBox.length > 4) {
  11741. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11742. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11743. if ([itemCategoryID isEqualToString:cateID]) {
  11744. needBreak = YES;
  11745. categoryID = itemCategoryID;
  11746. break;
  11747. }
  11748. }
  11749. }
  11750. if (needBreak) {
  11751. break;
  11752. }
  11753. }
  11754. }
  11755. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11756. if(wid !=0 && wisdelete != 1)
  11757. [item setValue:@"true" forKey:@"wish_exists"];
  11758. else
  11759. [item setValue:@"false" forKey:@"wish_exists"];
  11760. if(closeout==0)
  11761. [item setValue:@"false" forKey:@"is_closeout"];
  11762. else
  11763. [item setValue:@"true" forKey:@"is_closeout"];
  11764. if(cid==0)
  11765. [item setValue:@"false" forKey:@"cart_exists"];
  11766. else
  11767. [item setValue:@"true" forKey:@"cart_exists"];
  11768. if (more_color == 0) {
  11769. [item setObject:@(false) forKey:@"more_color"];
  11770. } else if (more_color == 1) {
  11771. [item setObject:@(true) forKey:@"more_color"];
  11772. }
  11773. [item addEntriesFromDictionary:imgjson];
  11774. // [item setValue:nsurl forKey:@"img"];
  11775. [item setValue:nsname forKey:@"name"];
  11776. [item setValue:nsdescription forKey:@"description"];
  11777. if (categoryID) {
  11778. [item setValue:categoryID forKey:@"item_category_id"];
  11779. }
  11780. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11781. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11782. i++;
  11783. }
  11784. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11785. [ret setObject:items forKey:@"items"];
  11786. sqlite3_finalize(statement);
  11787. } else {
  11788. DebugLog(@"nothing...");
  11789. }
  11790. DebugLog(@"count:%d",count);
  11791. [iSalesDB close_db:db];
  11792. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11793. dispatch_async(dispatch_get_main_queue(), ^{
  11794. // UIApplication * app = [UIApplication sharedApplication];
  11795. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11796. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11797. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11798. result(ret);
  11799. });
  11800. });
  11801. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11802. }
  11803. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11804. {
  11805. [self categoryList:params limited:YES completionHandler:result];
  11806. }
  11807. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11808. {
  11809. UIApplication * app = [UIApplication sharedApplication];
  11810. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11811. NSString* order_code = appDelegate.order_code;
  11812. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11813. NSString* keyword = [params valueForKey:@"keyword"];
  11814. keyword=keyword.lowercaseString;
  11815. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11816. int limit = [[params valueForKey:@"limit"] intValue];
  11817. int offset = [[params valueForKey:@"offset"] intValue];
  11818. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11819. NSString *limit_str = @"";
  11820. if (limited) {
  11821. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11822. }
  11823. sqlite3 *db = [iSalesDB get_db];
  11824. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11825. NSString *sqlQuery = nil;
  11826. if(exactMatch )
  11827. 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 ;
  11828. else
  11829. 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
  11830. DebugLog(@"offline_search sql:%@",sqlQuery);
  11831. sqlite3_stmt * statement;
  11832. [ret setValue:@"2" forKey:@"result"];
  11833. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11834. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11835. // int count=0;
  11836. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11837. {
  11838. int i=0;
  11839. while (sqlite3_step(statement) == SQLITE_ROW)
  11840. {
  11841. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11842. // char *name = (char*)sqlite3_column_text(statement, 1);
  11843. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11844. char *name = (char*)sqlite3_column_text(statement, 0);
  11845. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11846. char *description = (char*)sqlite3_column_text(statement, 1);
  11847. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11848. int product_id = sqlite3_column_int(statement, 2);
  11849. // char *url = (char*)sqlite3_column_text(statement, 3);
  11850. // if(url==nil)
  11851. // url="";
  11852. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11853. int wid = sqlite3_column_int(statement, 3);
  11854. int closeout = sqlite3_column_int(statement, 4);
  11855. int cid = sqlite3_column_int(statement, 5);
  11856. int wisdelete = sqlite3_column_int(statement, 6);
  11857. int more_color = sqlite3_column_int(statement, 7);
  11858. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11859. if(wid !=0 && wisdelete != 1)
  11860. [item setValue:@"true" forKey:@"wish_exists"];
  11861. else
  11862. [item setValue:@"false" forKey:@"wish_exists"];
  11863. if(closeout==0)
  11864. [item setValue:@"false" forKey:@"is_closeout"];
  11865. else
  11866. [item setValue:@"true" forKey:@"is_closeout"];
  11867. if(cid==0)
  11868. [item setValue:@"false" forKey:@"cart_exists"];
  11869. else
  11870. [item setValue:@"true" forKey:@"cart_exists"];
  11871. if (more_color == 0) {
  11872. [item setObject:@(false) forKey:@"more_color"];
  11873. } else if (more_color == 1) {
  11874. [item setObject:@(true) forKey:@"more_color"];
  11875. }
  11876. [item addEntriesFromDictionary:imgjson];
  11877. // [item setValue:nsurl forKey:@"img"];
  11878. [item setValue:nsname forKey:@"fash_name"];
  11879. [item setValue:nsdescription forKey:@"description"];
  11880. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11881. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11882. i++;
  11883. }
  11884. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11885. [ret setObject:items forKey:@"items"];
  11886. sqlite3_finalize(statement);
  11887. }
  11888. DebugLog(@"count:%d",count);
  11889. [iSalesDB close_db:db];
  11890. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11891. dispatch_async(dispatch_get_main_queue(), ^{
  11892. result(ret);
  11893. });
  11894. });
  11895. }
  11896. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11897. {
  11898. [self search:params limited:YES completionHandler:result];
  11899. }
  11900. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11901. //{
  11902. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11903. // NSString* order_code = appDelegate.order_code;
  11904. // NSString* user = appDelegate.user;
  11905. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11906. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11907. //
  11908. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11909. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11910. //
  11911. // // category
  11912. // NSDictionary *category_menu = [self offline_category_menu];
  11913. // [filter setValue:category_menu forKey:@"category"];
  11914. //
  11915. // NSString* where= nil;
  11916. // NSString* orderby= @"m.name";
  11917. // if (!filterSearch) {
  11918. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11919. //
  11920. // switch (covertype) {
  11921. // case 0:
  11922. // {
  11923. // where=@"m.category like'%%#005#%%'";
  11924. // break;
  11925. // }
  11926. // case 1:
  11927. // {
  11928. // where=@"m.alert like '%QS%'";
  11929. // break;
  11930. // }
  11931. // case 2:
  11932. // {
  11933. // where=@"m.availability>0";
  11934. // break;
  11935. // }
  11936. // case 3:
  11937. // {
  11938. // where=@"m.best_seller>0";
  11939. // orderby=@"m.best_seller desc,m.name asc";
  11940. // break;
  11941. // }
  11942. // default:
  11943. // where=@"1=1";
  11944. // break;
  11945. // }
  11946. //
  11947. // }
  11948. //
  11949. //
  11950. // int limit = [[params valueForKey:@"limit"] intValue];
  11951. // int offset = [[params valueForKey:@"offset"] intValue];
  11952. //
  11953. // NSString *limit_str = @"";
  11954. // if (limited) {
  11955. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11956. // }
  11957. //
  11958. //
  11959. // sqlite3 *db = [iSalesDB get_db];
  11960. // // [iSalesDB AddExFunction:db];
  11961. //
  11962. // int count;
  11963. //
  11964. // NSString *sqlQuery = nil;
  11965. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11966. // 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];
  11967. //
  11968. //
  11969. // double price_min = 0;
  11970. // double price_max = 0;
  11971. // if (filterSearch) {
  11972. // // alert
  11973. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11974. // NSString *alert = params[@"alert"];
  11975. // if ([alert isEqualToString:@"Display All"]) {
  11976. // alert = [NSString stringWithFormat:@""];
  11977. // } else {
  11978. // alert = [self translateSingleQuote:alert];
  11979. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11980. // }
  11981. //
  11982. // // available
  11983. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11984. // NSString *available = params[@"available"];
  11985. // NSString *available_condition;
  11986. // if ([available isEqualToString:@"Display All"]) {
  11987. // available_condition = @"";
  11988. // } else if ([available isEqualToString:@"Available Now"]) {
  11989. // available_condition = @"and availability > 0";
  11990. // } else {
  11991. // available_condition = @"and availability == 0";
  11992. // }
  11993. //
  11994. // // best seller
  11995. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11996. // NSString *best_seller = @"";
  11997. // NSString *order_best_seller = @"m.name asc";
  11998. //
  11999. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12000. // best_seller = @"and best_seller > 0";
  12001. // order_best_seller = @"m.best_seller desc,m.name asc";
  12002. // }
  12003. //
  12004. // // price
  12005. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12006. // NSString *price = params[@"price"];
  12007. // price_min = 0;
  12008. // price_max = MAXFLOAT;
  12009. // if (user) {
  12010. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12011. // NSMutableString *priceName = [NSMutableString string];
  12012. // for (int i = 0; i < priceTypeArray.count; i++) {
  12013. // NSString *pricetype = priceTypeArray[i];
  12014. // pricetype = [self translateSingleQuote:pricetype];
  12015. // if (i == 0) {
  12016. // [priceName appendFormat:@"'%@'",pricetype];
  12017. // } else {
  12018. // [priceName appendFormat:@",'%@'",pricetype];
  12019. // }
  12020. // }
  12021. //
  12022. // if ([price isEqualToString:@"Display All"]) {
  12023. // price = [NSString stringWithFormat:@""];
  12024. // } else if([price containsString:@"+"]){
  12025. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12026. // price_min = [price doubleValue];
  12027. // 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];
  12028. // } else {
  12029. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12030. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12031. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12032. // 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];
  12033. // }
  12034. // } else {
  12035. // price = @"";
  12036. // }
  12037. //
  12038. // // sold_by_qty : Sold in quantities of %@
  12039. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12040. // NSString *qty = params[@"sold_by_qty"];
  12041. // if ([qty isEqualToString:@"Display All"]) {
  12042. // qty = @"";
  12043. // } else {
  12044. // qty = [self translateSingleQuote:qty];
  12045. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12046. // }
  12047. //
  12048. // // category
  12049. // NSString *category_id = params[@"ctgId"];
  12050. // NSMutableArray *cate_id_array = nil;
  12051. // NSMutableString *cateWhere = [NSMutableString string];
  12052. //
  12053. // if ([category_id isEqualToString:@""] || !category_id) {
  12054. // [cateWhere appendString:@"1 = 1"];
  12055. // } else {
  12056. //
  12057. // if ([category_id containsString:@","]) {
  12058. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12059. // } else {
  12060. // cate_id_array = [@[category_id] mutableCopy];
  12061. // }
  12062. // /*-----------*/
  12063. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12064. //
  12065. // [cateWhere appendString:@"("];
  12066. // for (int i = 0; i < cate_id_array.count; i++) {
  12067. //
  12068. // for (NSString *key0 in cateDic.allKeys) {
  12069. //
  12070. // if ([key0 containsString:@"category_"]) {
  12071. //
  12072. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12073. //
  12074. // for (NSString *key1 in category0.allKeys) {
  12075. //
  12076. // if ([key1 containsString:@"category_"]) {
  12077. //
  12078. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12079. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12080. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12081. //
  12082. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12083. // if (i == 0) {
  12084. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12085. // } else {
  12086. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12087. // }
  12088. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12089. // [category0 setValue:category1 forKey:key1];
  12090. // [cateDic setValue:category0 forKey:key0];
  12091. //
  12092. // }
  12093. //
  12094. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12095. //
  12096. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12097. //
  12098. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12099. // if (i == 0) {
  12100. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12101. // } else {
  12102. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12103. // }
  12104. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12105. // [cateDic setValue:category0 forKey:key0];
  12106. //
  12107. // }
  12108. //
  12109. // }
  12110. //
  12111. // }
  12112. //
  12113. // }
  12114. //
  12115. // }
  12116. //
  12117. // }
  12118. // [cateWhere appendString:@")"];
  12119. //
  12120. // [filter setValue:cateDic forKey:@"category"];
  12121. // }
  12122. //
  12123. // // where bestseller > 0 order by bestseller desc
  12124. // // sql query: alert availability(int) best_seller(int) price qty
  12125. //
  12126. // 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];
  12127. //
  12128. //
  12129. // // count
  12130. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12131. //
  12132. // }
  12133. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12134. //
  12135. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12136. //
  12137. //
  12138. //
  12139. //
  12140. // if (!user) {
  12141. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12142. // }
  12143. //
  12144. // [ret setValue:filter forKey:@"filter"];
  12145. //
  12146. //
  12147. //
  12148. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12149. //
  12150. // sqlite3_stmt * statement;
  12151. // [ret setValue:@"2" forKey:@"result"];
  12152. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12153. //
  12154. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12155. // // int count=0;
  12156. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12157. // {
  12158. //
  12159. // int i=0;
  12160. // while (sqlite3_step(statement) == SQLITE_ROW)
  12161. // {
  12162. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12163. //
  12164. //
  12165. // char *name = (char*)sqlite3_column_text(statement, 0);
  12166. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12167. //
  12168. // char *description = (char*)sqlite3_column_text(statement, 1);
  12169. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12170. //
  12171. //
  12172. //
  12173. // int product_id = sqlite3_column_int(statement, 2);
  12174. //
  12175. //
  12176. // int wid = sqlite3_column_int(statement, 3);
  12177. // int closeout = sqlite3_column_int(statement, 4);
  12178. // int cid = sqlite3_column_int(statement, 5);
  12179. // int wisdelete = sqlite3_column_int(statement, 6);
  12180. // int more_color = sqlite3_column_int(statement, 7);
  12181. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12182. //
  12183. // if(wid !=0 && wisdelete != 1)
  12184. // [item setValue:@"true" forKey:@"wish_exists"];
  12185. // else
  12186. // [item setValue:@"false" forKey:@"wish_exists"];
  12187. //
  12188. // if(closeout==0)
  12189. // [item setValue:@"false" forKey:@"is_closeout"];
  12190. // else
  12191. // [item setValue:@"true" forKey:@"is_closeout"];
  12192. //
  12193. // if(cid==0)
  12194. // [item setValue:@"false" forKey:@"cart_exists"];
  12195. // else
  12196. // [item setValue:@"true" forKey:@"cart_exists"];
  12197. //
  12198. // if (more_color == 0) {
  12199. // [item setObject:@(false) forKey:@"more_color"];
  12200. // } else if (more_color == 1) {
  12201. // [item setObject:@(true) forKey:@"more_color"];
  12202. // }
  12203. //
  12204. // [item addEntriesFromDictionary:imgjson];
  12205. //
  12206. //
  12207. // // [item setValue:nsurl forKey:@"img"];
  12208. // [item setValue:nsname forKey:@"fash_name"];
  12209. // [item setValue:nsdescription forKey:@"description"];
  12210. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12211. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12212. // i++;
  12213. // }
  12214. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12215. // [ret setObject:items forKey:@"items"];
  12216. // sqlite3_finalize(statement);
  12217. // }
  12218. //
  12219. // [iSalesDB close_db:db];
  12220. //
  12221. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12222. //
  12223. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12224. // dispatch_async(dispatch_get_main_queue(), ^{
  12225. //
  12226. // result(ret);
  12227. // });
  12228. //
  12229. // });
  12230. //
  12231. //
  12232. //
  12233. //}
  12234. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12235. //{
  12236. // [self itemsearch:params limited:YES completionHandler:result];
  12237. //}
  12238. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12239. {
  12240. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12241. NSString* offline_command=params[@"offline_Command"];
  12242. NSMutableDictionary* ret=nil;
  12243. if([offline_command isEqualToString:@"model_NIYMAL"])
  12244. {
  12245. NSString* category = params[@"category"];
  12246. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12247. }
  12248. dispatch_async(dispatch_get_main_queue(), ^{
  12249. result(ret);
  12250. });
  12251. });
  12252. }
  12253. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12254. {
  12255. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12256. int sort = [[params objectForKey:@"sort"] intValue];
  12257. NSString *sort_str = @"";
  12258. switch (sort) {
  12259. case 0:{
  12260. sort_str = @"order by w.modify_time desc";
  12261. }
  12262. break;
  12263. case 1:{
  12264. sort_str = @"order by w.modify_time asc";
  12265. }
  12266. break;
  12267. case 2:{
  12268. sort_str = @"order by m.name asc";
  12269. }
  12270. break;
  12271. case 3:{
  12272. sort_str = @"order by m.name desc";
  12273. }
  12274. break;
  12275. case 4:{
  12276. sort_str = @"order by m.description asc";
  12277. }
  12278. break;
  12279. default:
  12280. break;
  12281. }
  12282. // NSString* user = appDelegate.user;
  12283. sqlite3 *db = [iSalesDB get_db];
  12284. // order by w.create_time
  12285. 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];
  12286. // 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];
  12287. sqlite3_stmt * statement;
  12288. NSDate *date1 = [NSDate date];
  12289. // NSDate *date2 = nil;
  12290. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12291. int count=0;
  12292. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12293. {
  12294. while (sqlite3_step(statement) == SQLITE_ROW)
  12295. {
  12296. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12297. int product_id = sqlite3_column_double(statement, 0);
  12298. char *description = (char*)sqlite3_column_text(statement, 1);
  12299. if(description==nil)
  12300. description= "";
  12301. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12302. int item_id = sqlite3_column_double(statement, 2);
  12303. NSDate *date_image = [NSDate date];
  12304. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12305. printf("image : ");
  12306. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12307. // char *url = (char*)sqlite3_column_text(statement, 3);
  12308. // if(url==nil)
  12309. // url="";
  12310. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12311. int qty = sqlite3_column_int(statement, 3);
  12312. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12313. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12314. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12315. item[@"description"]= nsdescription;
  12316. item[@"img"]= nsurl;
  12317. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12318. count++;
  12319. }
  12320. printf("total time:");
  12321. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12322. ret[@"count"]= [NSNumber numberWithInt:count];
  12323. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12324. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12325. ret[@"result"]= [NSNumber numberWithInt:2];
  12326. sqlite3_finalize(statement);
  12327. }
  12328. [iSalesDB close_db:db];
  12329. dispatch_async(dispatch_get_main_queue(), ^{
  12330. UIApplication * app = [UIApplication sharedApplication];
  12331. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12332. appDelegate.wish_count =count;
  12333. [appDelegate update_count_mark];
  12334. result(ret);
  12335. });
  12336. });
  12337. }
  12338. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12339. {
  12340. // UIApplication * app = [UIApplication sharedApplication];
  12341. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12342. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12343. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12344. sqlite3 *db = [iSalesDB get_db];
  12345. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12346. NSString* product_id=params[@"product_id"];
  12347. NSString *item_count_str = params[@"item_count"];
  12348. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12349. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12350. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12351. // NSString *sql = @"";
  12352. for(int i=0;i<arr.count;i++)
  12353. {
  12354. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12355. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12356. __block int cart_count = 0;
  12357. if (!item_count_str) {
  12358. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12359. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12360. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12361. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12362. cart_count = [[model_set_components lastObject] intValue];
  12363. }];
  12364. }
  12365. if(count==0)
  12366. {
  12367. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12368. sqlite3_stmt *stmt;
  12369. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12370. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12371. if (item_count_arr) {
  12372. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12373. } else {
  12374. sqlite3_bind_int(stmt,2,cart_count);
  12375. }
  12376. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12377. [iSalesDB execSql:@"ROLLBACK" db:db];
  12378. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12379. [iSalesDB close_db:db];
  12380. DebugLog(@"add to wishlist error");
  12381. dispatch_async(dispatch_get_main_queue(), ^{
  12382. result(ret);
  12383. });
  12384. }
  12385. } else {
  12386. int qty = 0;
  12387. if (item_count_arr) {
  12388. qty = [item_count_arr[i] intValue];
  12389. } else {
  12390. qty = cart_count;
  12391. }
  12392. 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]];
  12393. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12394. [iSalesDB execSql:@"ROLLBACK" db:db];
  12395. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12396. [iSalesDB close_db:db];
  12397. DebugLog(@"add to wishlist error");
  12398. dispatch_async(dispatch_get_main_queue(), ^{
  12399. result(ret);
  12400. });
  12401. }
  12402. }
  12403. }
  12404. // [iSalesDB execSql:sql db:db];
  12405. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12406. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12407. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12408. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12409. [iSalesDB close_db:db];
  12410. ret[@"result"]= [NSNumber numberWithInt:2];
  12411. dispatch_async(dispatch_get_main_queue(), ^{
  12412. UIApplication * app = [UIApplication sharedApplication];
  12413. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12414. appDelegate.wish_count =count;
  12415. [appDelegate update_count_mark];
  12416. result(ret);
  12417. });
  12418. });
  12419. }
  12420. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12421. {
  12422. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12423. bool bLogin = appDelegate.bLogin;
  12424. __block NSString* contact_id = appDelegate.contact_id;
  12425. __block NSString* user = appDelegate.user;
  12426. __block NSString* order_code = appDelegate.order_code;
  12427. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12428. NSString* model_name = [params valueForKey:@"product_name"];
  12429. NSString* product_id = [params valueForKey:@"product_id"];
  12430. NSString* upc_code = [params valueForKey:@"upc_code"];
  12431. NSString* category = [params valueForKey:@"category"];
  12432. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12433. if(category==nil) {
  12434. category = default_category_id;
  12435. } else {
  12436. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12437. // 参数重有多个category id
  12438. if (arr_0.count > 1) {
  12439. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12440. // 取交集
  12441. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12442. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12443. [set_0 intersectSet:set_1];
  12444. if (set_0.count == 1) {
  12445. category = (NSString *)[set_0 anyObject];
  12446. } else {
  12447. if ([set_0 containsObject:default_category_id]) {
  12448. category = default_category_id;
  12449. } else {
  12450. category = (NSString *)[set_0 anyObject];
  12451. }
  12452. }
  12453. }
  12454. }
  12455. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12456. sqlite3 *db = [iSalesDB get_db];
  12457. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12458. NSString *sqlQuery = nil;
  12459. if(product_id==nil)
  12460. if(model_name==nil)
  12461. {
  12462. 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='%@';
  12463. }
  12464. else
  12465. {
  12466. 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='%@';
  12467. }
  12468. else
  12469. 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=%@;
  12470. sqlite3_stmt * statement;
  12471. [ret setValue:@"2" forKey:@"result"];
  12472. [ret setValue:@"3" forKey:@"detail_section_count"];
  12473. // int count=0;
  12474. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12475. {
  12476. if (sqlite3_step(statement) == SQLITE_ROW)
  12477. {
  12478. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12479. char *name = (char*)sqlite3_column_text(statement, 0);
  12480. if(name==nil)
  12481. name="";
  12482. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12483. char *description = (char*)sqlite3_column_text(statement, 1);
  12484. if(description==nil)
  12485. description="";
  12486. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12487. int product_id = sqlite3_column_int(statement, 2);
  12488. char *color = (char*)sqlite3_column_text(statement, 3);
  12489. if(color==nil)
  12490. color="";
  12491. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12492. //
  12493. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12494. // if(legcolor==nil)
  12495. // legcolor="";
  12496. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12497. //
  12498. //
  12499. int availability = sqlite3_column_int(statement, 5);
  12500. //
  12501. int incoming_stock = sqlite3_column_int(statement, 6);
  12502. char *demension = (char*)sqlite3_column_text(statement, 7);
  12503. if(demension==nil)
  12504. demension="";
  12505. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12506. // ,,,,,,,,,
  12507. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12508. if(seat_height==nil)
  12509. seat_height="";
  12510. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12511. char *material = (char*)sqlite3_column_text(statement, 9);
  12512. if(material==nil)
  12513. material="";
  12514. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12515. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12516. if(box_dim==nil)
  12517. box_dim="";
  12518. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12519. char *volume = (char*)sqlite3_column_text(statement, 11);
  12520. if(volume==nil)
  12521. volume="";
  12522. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12523. double weight = sqlite3_column_double(statement, 12);
  12524. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12525. if(model_set==nil)
  12526. model_set="";
  12527. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12528. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12529. if(load_ability==nil)
  12530. load_ability="";
  12531. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12532. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12533. // if(default_category==nil)
  12534. // default_category="";
  12535. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12536. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12537. if(fabric_content==nil)
  12538. fabric_content="";
  12539. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12540. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12541. if(assembling==nil)
  12542. assembling="";
  12543. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12544. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12545. if(made_in==nil)
  12546. made_in="";
  12547. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12548. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12549. if(special_remarks==nil)
  12550. special_remarks="";
  12551. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12552. int stockUcom = sqlite3_column_double(statement, 20);
  12553. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12554. if(product_group==nil)
  12555. product_group="";
  12556. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12557. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12558. // if(fashion_selector==nil)
  12559. // fashion_selector="";
  12560. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12561. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12562. if(selector_field==nil)
  12563. selector_field="";
  12564. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12565. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12566. if(property_field==nil)
  12567. property_field="";
  12568. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12569. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12570. if(packaging==nil)
  12571. packaging="";
  12572. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12573. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12574. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12575. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12576. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12577. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12578. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12579. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12580. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12581. NSString* Availability=nil;
  12582. if(availability>0)
  12583. Availability=[NSString stringWithFormat:@"%d",availability];
  12584. else
  12585. Availability = @"Out of Stock";
  12586. [img_section setValue:Availability forKey:@"Availability"];
  12587. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12588. char *eta = (char*)sqlite3_column_text(statement, 25);
  12589. if(eta==nil)
  12590. eta="";
  12591. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12592. if ([nseta isEqualToString:@"null"]) {
  12593. nseta = @"";
  12594. }
  12595. if (availability <= 0) {
  12596. [img_section setValue:nseta forKey:@"ETA"];
  12597. }
  12598. int item_id = sqlite3_column_int(statement, 26);
  12599. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12600. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12601. NSString* Price=nil;
  12602. if(bLogin==false)
  12603. Price=@"Must Sign in.";
  12604. else
  12605. {
  12606. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12607. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12608. if(price==nil)
  12609. Price=@"No Price.";
  12610. else
  12611. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12612. }
  12613. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12614. [img_section setValue:Price forKey:@"price"];
  12615. [img_section setValue:nsname forKey:@"model_name"];
  12616. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12617. if (order_code) { // 离线order code即so#
  12618. 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];
  12619. __block int cartQTY = 0;
  12620. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12621. cartQTY = sqlite3_column_int(stmt, 0);
  12622. }];
  12623. if (cartQTY > 0) {
  12624. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12625. }
  12626. }
  12627. [ret setObject:img_section forKey:@"img_section"];
  12628. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12629. int detail0_item_count=0;
  12630. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12631. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12632. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12633. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12634. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12635. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12636. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12637. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12638. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12639. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12640. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12641. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12642. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12643. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12644. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12645. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12646. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12647. {
  12648. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12649. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12650. }
  12651. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12652. [detail0_section setValue:@"kv" forKey:@"type"];
  12653. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12654. [ret setObject:detail0_section forKey:@"detail_0"];
  12655. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12656. // [detail1_section setValue:@"detail" forKey:@"target"];
  12657. // [detail1_section setValue:@"popup" forKey:@"action"];
  12658. // [detail1_section setValue:@"content" forKey:@"type"];
  12659. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12660. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12661. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12662. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12663. [ret setObject:detail1_section forKey:@"detail_1"];
  12664. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12665. [detail2_section setValue:@"detail" forKey:@"target"];
  12666. [detail2_section setValue:@"popup" forKey:@"action"];
  12667. [detail2_section setValue:@"content" forKey:@"type"];
  12668. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12669. [detail2_section setValue:@"true" forKey:@"single_row"];
  12670. [detail2_section setValue:@"local" forKey:@"data"];
  12671. [ret setObject:detail2_section forKey:@"detail_2"];
  12672. }
  12673. sqlite3_finalize(statement);
  12674. }
  12675. else
  12676. {
  12677. [ret setValue:@"8" forKey:@"result"];
  12678. }
  12679. // DebugLog(@"count:%d",count);
  12680. [iSalesDB close_db:db];
  12681. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12682. dispatch_async(dispatch_get_main_queue(), ^{
  12683. result(ret);
  12684. });
  12685. });
  12686. }
  12687. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12688. //{
  12689. //
  12690. // UIApplication * app = [UIApplication sharedApplication];
  12691. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12692. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12693. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12694. // sqlite3 *db = [iSalesDB get_db];
  12695. // NSString* collectId=params[@"collectId"];
  12696. //
  12697. //
  12698. //
  12699. //
  12700. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12701. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12702. // [iSalesDB execSql:sqlQuery db:db];
  12703. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12704. // [iSalesDB close_db:db];
  12705. //
  12706. //
  12707. // ret[@"result"]= [NSNumber numberWithInt:2];
  12708. // dispatch_async(dispatch_get_main_queue(), ^{
  12709. //
  12710. // appDelegate.wish_count =count;
  12711. //
  12712. // [appDelegate update_count_mark];
  12713. // result(ret);
  12714. // });
  12715. //
  12716. // });
  12717. //}
  12718. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12719. {
  12720. UIApplication * app = [UIApplication sharedApplication];
  12721. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12722. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12723. [iSalesDB disable_trigger];
  12724. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12725. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12726. [iSalesDB enable_trigger];
  12727. dispatch_async(dispatch_get_main_queue(), ^{
  12728. //
  12729. // NSString* user = [params valueForKey:@"user"];
  12730. //
  12731. // NSString* password = [params valueForKey:@"password"];
  12732. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12733. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12734. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12735. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12736. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12737. [appDelegate update_count_mark];
  12738. appDelegate.can_show_price =false;
  12739. appDelegate.can_see_price =false;
  12740. appDelegate.can_create_portfolio =false;
  12741. appDelegate.can_create_order =false;
  12742. appDelegate.can_cancel_order =false;
  12743. appDelegate.can_set_cart_price =false;
  12744. appDelegate.can_delete_order =false;
  12745. appDelegate.can_submit_order =false;
  12746. appDelegate.can_set_tearsheet_price =false;
  12747. appDelegate.can_update_contact_info = false;
  12748. appDelegate.save_order_logout = false;
  12749. appDelegate.submit_order_logout = false;
  12750. appDelegate.alert_sold_in_quantities = false;
  12751. appDelegate.ipad_perm =nil ;
  12752. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12753. appDelegate.OrderFilter= nil;
  12754. [appDelegate SetSo:nil];
  12755. [appDelegate set_main_button_panel];
  12756. result(ret);
  12757. });
  12758. });
  12759. }
  12760. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12761. {
  12762. // iSalesDB.
  12763. // UIApplication * app = [UIApplication sharedApplication];
  12764. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12765. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12766. [iSalesDB disable_trigger];
  12767. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12768. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12769. [iSalesDB enable_trigger];
  12770. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12771. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12772. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12773. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12774. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12775. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12776. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12777. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12778. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12779. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12780. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12781. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12782. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12783. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12784. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12785. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12786. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12787. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12788. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12789. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12790. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12791. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12792. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12793. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12794. [arr_name addObject:customer_first_name];
  12795. [arr_name addObject:customer_last_name];
  12796. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12797. // default ship from
  12798. 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';";
  12799. __block NSString *cid = @"";
  12800. __block NSString *name = @"";
  12801. __block NSString *ext = @"";
  12802. __block NSString *contact = @"";
  12803. __block NSString *email = @"";
  12804. __block NSString *fax = @"";
  12805. __block NSString *phone = @"";
  12806. sqlite3 *db = [iSalesDB get_db];
  12807. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12808. cid = [self textAtColumn:0 statement:statment];
  12809. name = [self textAtColumn:1 statement:statment];
  12810. ext = [self textAtColumn:2 statement:statment];
  12811. contact = [self textAtColumn:3 statement:statment];
  12812. email = [self textAtColumn:4 statement:statment];
  12813. fax = [self textAtColumn:5 statement:statment];
  12814. phone = [self textAtColumn:6 statement:statment];
  12815. }];
  12816. NSString* so_id = [self get_offline_soid:db];
  12817. if(so_id==nil)
  12818. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12819. 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];
  12820. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12821. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12822. //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'
  12823. //soId
  12824. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12825. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12826. sqlite3_stmt * statement;
  12827. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12828. {
  12829. if (sqlite3_step(statement) == SQLITE_ROW)
  12830. {
  12831. // char *name = (char*)sqlite3_column_text(statement, 1);
  12832. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12833. //ret = sqlite3_column_int(statement, 0);
  12834. char *soId = (char*)sqlite3_column_text(statement, 0);
  12835. if(soId==nil)
  12836. soId="";
  12837. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12838. [ret setValue:nssoId forKey:@"soId"];
  12839. [ret setValue:nssoId forKey:@"orderCode"];
  12840. }
  12841. sqlite3_finalize(statement);
  12842. }
  12843. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12844. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12845. [iSalesDB close_db:db];
  12846. dispatch_async(dispatch_get_main_queue(), ^{
  12847. result(ret);
  12848. });
  12849. });
  12850. }
  12851. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12852. {
  12853. UIApplication * app = [UIApplication sharedApplication];
  12854. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12855. bool can_create_backorder= appDelegate.can_create_backorder;
  12856. NSString* user = appDelegate.user;
  12857. NSString* contact_id = appDelegate.contact_id;
  12858. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12859. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12860. sqlite3 *db = [iSalesDB get_db];
  12861. NSString* orderCode=params[@"orderCode"];
  12862. int sort = [[params objectForKey:@"sort"] intValue];
  12863. NSString *sort_str = @"";
  12864. switch (sort) {
  12865. case 0:{
  12866. sort_str = @"order by c.modify_time desc";
  12867. }
  12868. break;
  12869. case 1:{
  12870. sort_str = @"order by c.modify_time asc";
  12871. }
  12872. break;
  12873. case 2:{
  12874. sort_str = @"order by m.name asc";
  12875. }
  12876. break;
  12877. case 3:{
  12878. sort_str = @"order by m.name desc";
  12879. }
  12880. break;
  12881. case 4:{
  12882. sort_str = @"order by m.description asc";
  12883. }
  12884. break;
  12885. default:
  12886. break;
  12887. }
  12888. 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 ];
  12889. // 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 ];
  12890. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12891. DebugLog(@"offline_login sql:%@",sqlQuery);
  12892. sqlite3_stmt * statement;
  12893. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12894. NSDate *date1 = [NSDate date];
  12895. int count=0;
  12896. int cart_count=0;
  12897. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12898. if ( dbresult== SQLITE_OK)
  12899. {
  12900. while (sqlite3_step(statement) == SQLITE_ROW)
  12901. {
  12902. // NSDate *row_date = [NSDate date];
  12903. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12904. int product_id = sqlite3_column_int(statement, 0);
  12905. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12906. int item_id = sqlite3_column_int(statement, 7);
  12907. NSString* Price=nil;
  12908. if(str_price==nil)
  12909. {
  12910. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12911. // NSDate *price_date = [NSDate date];
  12912. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12913. // DebugLog(@"price time interval");
  12914. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12915. if(price==nil)
  12916. Price=@"No Price.";
  12917. else
  12918. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12919. }
  12920. else
  12921. {
  12922. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12923. }
  12924. double discount = sqlite3_column_double(statement, 2);
  12925. int item_count = sqlite3_column_int(statement, 3);
  12926. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12927. NSString *nsline_note=nil;
  12928. if(line_note!=nil)
  12929. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12930. char *name = (char*)sqlite3_column_text(statement, 5);
  12931. NSString *nsname=nil;
  12932. if(name!=nil)
  12933. nsname= [[NSString alloc]initWithUTF8String:name];
  12934. char *description = (char*)sqlite3_column_text(statement, 6);
  12935. NSString *nsdescription=nil;
  12936. if(description!=nil)
  12937. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12938. int stockUom = sqlite3_column_int(statement, 8);
  12939. int _id = sqlite3_column_int(statement, 9);
  12940. int availability = sqlite3_column_int(statement, 10);
  12941. // NSDate *subtotal_date = [NSDate date];
  12942. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12943. // DebugLog(@"subtotal_date time interval");
  12944. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12945. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12946. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12947. int carton=[bsubtotaljson[@"carton"] intValue];
  12948. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12949. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12950. // NSDate *img_date = [NSDate date];
  12951. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12952. // DebugLog(@"img_date time interval");
  12953. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12954. itemjson[@"model"]=nsname;
  12955. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12956. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12957. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12958. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12959. itemjson[@"check"]=@"true";
  12960. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12961. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12962. itemjson[@"unit_price"]=Price;
  12963. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12964. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12965. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12966. itemjson[@"note"]=nsline_note;
  12967. if (!can_create_backorder) {
  12968. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12969. }
  12970. // NSDate *date2 = [NSDate date];
  12971. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12972. // DebugLog(@"model_bundle time interval");
  12973. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12974. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12975. count++;
  12976. // DebugLog(@"row time interval");
  12977. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12978. }
  12979. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12980. sqlite3_finalize(statement);
  12981. }
  12982. DebugLog(@"request cart total time interval");
  12983. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12984. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12985. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12986. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12987. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12988. ret[@"count"]=[NSNumber numberWithInt:count ];
  12989. ret[@"mode"]=@"Regular Mode";
  12990. [iSalesDB close_db:db];
  12991. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12992. DebugLog(@"general notes :%@",general_note);
  12993. ret[@"general_note"]= general_note;
  12994. dispatch_async(dispatch_get_main_queue(), ^{
  12995. result(ret);
  12996. });
  12997. });
  12998. }
  12999. @end