OLDataProvider.m 686 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. //#import "UIProgressView+AFNetworking.h"
  15. //#import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "RASingleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. #import "ActiveViewController.h"
  24. @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) save2submitScanOrder
  1547. {
  1548. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1549. // NSString *documents = [paths objectAtIndex:0];
  1550. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1551. NSString *userdir = [OLDataProvider getUserPath];
  1552. NSMutableDictionary* orderlist = nil;
  1553. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1554. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1555. // order list 为空,不用维护
  1556. #ifdef RA_NOTIFICATION
  1557. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1558. #else
  1559. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1560. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1561. #endif
  1562. return;
  1563. // [orderlist addObject:appDelegate.order_code];
  1564. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1565. }
  1566. else
  1567. {
  1568. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1569. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1570. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1571. }
  1572. int count = [orderlist[@"count"] intValue];
  1573. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1574. int newcount = 0;
  1575. for(int i=0;i<count;i++)
  1576. {
  1577. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1578. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1579. continue;
  1580. else
  1581. {
  1582. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1583. newcount++;
  1584. }
  1585. }
  1586. newlist[@"count"] = @(newcount);
  1587. [RAUtils dicttofile:orderlistPath dict:newlist];
  1588. #ifdef RA_NOTIFICATION
  1589. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1590. #else
  1591. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1592. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1593. #endif
  1594. }
  1595. +(void) createScanTempFolder
  1596. {
  1597. NSString* scanFilePath = [self getScanTempPath];
  1598. BOOL bdir=YES;
  1599. NSFileManager* fileManager = [NSFileManager defaultManager];
  1600. if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1601. {
  1602. NSError *error = nil;
  1603. bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1604. if(!bsuccess)
  1605. DebugLog(@"Create cache folder failed");
  1606. // if(bsuccess)
  1607. // {
  1608. // sqlite3 *db = [self get_db];
  1609. //
  1610. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1611. // [iSalesDB close_db:db];
  1612. // }
  1613. }
  1614. }
  1615. +(NSString*) getScanOrderPath
  1616. {
  1617. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1618. NSString *documents = [paths objectAtIndex:0];
  1619. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1620. NSString* servername = addressDic[@"name"];
  1621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1622. NSString* user = appDelegate.user;
  1623. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1624. orderpath = [orderpath stringByAppendingPathComponent:user];
  1625. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1626. BOOL bdir=YES;
  1627. NSFileManager* fileManager = [NSFileManager defaultManager];
  1628. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1629. {
  1630. NSError *error = nil;
  1631. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1632. if(!bsuccess)
  1633. DebugLog(@"Create cache folder failed");
  1634. // if(bsuccess)
  1635. // {
  1636. // sqlite3 *db = [self get_db];
  1637. //
  1638. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1639. // [iSalesDB close_db:db];
  1640. // }
  1641. }
  1642. return orderpath;
  1643. }
  1644. +(NSString*) getScanTempPath
  1645. {
  1646. if(RASingleton.sharedInstance.scan_temp_code == nil)
  1647. RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1648. return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1649. }
  1650. +(void) moveScanTemp2Order
  1651. {
  1652. NSString *orderdir = [OLDataProvider getScanOrderPath];
  1653. NSError *error = nil;
  1654. NSString* tempdir = [OLDataProvider getScanTempPath];
  1655. NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1656. for(int i=0;i<arr_files.count;i++)
  1657. {
  1658. NSString* file=arr_files[i];
  1659. }
  1660. [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1661. }
  1662. +(NSString*) getUserPath
  1663. {
  1664. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1665. NSString *documents = [paths objectAtIndex:0];
  1666. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1667. NSString* servername = addressDic[@"name"];
  1668. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1669. NSString* user = appDelegate.user;
  1670. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1671. userpath = [userpath stringByAppendingPathComponent:user];
  1672. return userpath;
  1673. }
  1674. +(NSString*) getScanPath
  1675. {
  1676. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1677. if(appDelegate.order_code!=nil)
  1678. {
  1679. return [self getScanOrderPath];
  1680. }
  1681. else
  1682. {
  1683. [self createScanTempFolder];
  1684. return [self getScanTempPath];
  1685. }
  1686. }
  1687. +(NSString*) createScanTempCode
  1688. {
  1689. return [[NSUUID new] UUIDString];
  1690. }
  1691. +(NSString*) scanTemplatePath:(NSString*)file
  1692. {
  1693. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1694. NSString *documents = [paths objectAtIndex:0];
  1695. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1696. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1697. return templatefile;
  1698. }
  1699. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1700. {
  1701. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1702. NSString *documents = [paths objectAtIndex:0];
  1703. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1704. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1705. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1706. return [dict mutableCopy];
  1707. }
  1708. +(void) deleteScanOrder:(NSString*) order_code
  1709. {
  1710. NSString *userdir = [OLDataProvider getUserPath];
  1711. if(order_code.length==0)
  1712. return;
  1713. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1714. // NSString *documents = [paths objectAtIndex:0];
  1715. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1716. if([appDelegate.order_code isEqualToString:order_code])
  1717. {
  1718. //要删除的订单是当前打开的订单;
  1719. appDelegate.order_code = nil;
  1720. appDelegate.customerInfo = nil;
  1721. RASingleton.sharedInstance.scan_cart = nil;
  1722. appDelegate.contact_id = nil;
  1723. // [appDelegate updateScanButton:false];
  1724. [appDelegate update_count_mark];
  1725. }
  1726. //删除订单目录
  1727. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1728. //维护订单列表
  1729. NSMutableDictionary* orderlist = nil;
  1730. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1731. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1732. // order list 为空,不用维护
  1733. #ifdef RA_NOTIFICATION
  1734. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1735. #else
  1736. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1737. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1738. #endif
  1739. return;
  1740. // [orderlist addObject:appDelegate.order_code];
  1741. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1742. }
  1743. else
  1744. {
  1745. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1746. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1747. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1748. }
  1749. int count = [orderlist[@"count"] intValue];
  1750. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1751. int newcount = 0;
  1752. for(int i=0;i<count;i++)
  1753. {
  1754. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1755. if([item[@"order_code"] isEqualToString: order_code ])
  1756. continue;
  1757. else
  1758. {
  1759. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1760. newcount++;
  1761. }
  1762. }
  1763. newlist[@"count"] = @(newcount);
  1764. [RAUtils dicttofile:orderlistPath dict:newlist];
  1765. #ifdef RA_NOTIFICATION
  1766. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1767. #else
  1768. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1769. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1770. #endif
  1771. }
  1772. +(void) saveScanCart:(NSMutableDictionary*) cart
  1773. {
  1774. if(cart==nil)
  1775. return;
  1776. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1777. // NSString *documents = [paths objectAtIndex:0];
  1778. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1779. NSString *orderdir = [self getScanPath];
  1780. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1781. [RAUtils dicttofile:cartpath dict:cart];
  1782. }
  1783. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  1784. {
  1785. assert(add_params[@"user"]!=nil);
  1786. // assert(add_params[@"contact_id"]!=nil);
  1787. // assert(add_params[@"password"]!=nil);
  1788. NSString* serial= [[NSUUID UUID] UUIDString];
  1789. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1790. NSString *cachefolder = [paths objectAtIndex:0];
  1791. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1792. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1793. NSFileManager* fileManager = [NSFileManager defaultManager];
  1794. BOOL bdir=YES;
  1795. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1796. {
  1797. NSError *error = nil;
  1798. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1799. if(!bsuccess)
  1800. DebugLog(@"Create UPLOAD folder failed");
  1801. }
  1802. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1803. ret[@"contact"]=[self prepareContact:serial];
  1804. ret[@"wishlist"]=[self prepareWishlist:serial];
  1805. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  1806. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  1807. ret[@"view_portfolio"] = [self preparePDF:serial];
  1808. NSString* str= [RAConvertor dict2string:ret];
  1809. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1810. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1811. NSError *error=nil;
  1812. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1813. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1814. ZipArchive* zip = [[ZipArchive alloc] init];
  1815. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1816. for(int i=0;i<arr_files.count;i++)
  1817. {
  1818. NSString* file=arr_files[i];
  1819. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1820. }
  1821. if( ![zip CloseZipFile2] )
  1822. {
  1823. zippath = @"";
  1824. }
  1825. ret[@"file"]=zippath;
  1826. return ret;
  1827. }
  1828. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1829. {
  1830. if(filename.length==0)
  1831. return false;
  1832. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1833. bool ret=false;
  1834. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1835. NSString *cachefolder = [paths objectAtIndex:0];
  1836. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1837. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1838. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1839. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1840. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1841. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1842. NSFileManager* fileManager = [NSFileManager defaultManager];
  1843. BOOL bdir=NO;
  1844. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1845. {
  1846. NSError *error = nil;
  1847. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1848. {
  1849. ret=false;
  1850. }
  1851. else
  1852. {
  1853. ret=true;
  1854. }
  1855. }
  1856. return ret;
  1857. }
  1858. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1859. {
  1860. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1861. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1862. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1863. NSString* where=@" 1=1";
  1864. if (ver!=nil) {
  1865. where=@"is_dirty=1";
  1866. }
  1867. sqlite3 *db = [iSalesDB get_db];
  1868. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1869. sqlite3_stmt * statement;
  1870. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1871. int count=0;
  1872. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1873. if ( dbresult== SQLITE_OK)
  1874. {
  1875. while (sqlite3_step(statement) == SQLITE_ROW)
  1876. {
  1877. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1878. int _id = sqlite3_column_int(statement, 0);
  1879. int product_id = sqlite3_column_int(statement, 1);
  1880. int item_id = sqlite3_column_int(statement, 2);
  1881. int qty = sqlite3_column_int(statement, 3);
  1882. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1883. NSString* nscreate_time=nil;
  1884. if(create_time!=nil)
  1885. {
  1886. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1887. }
  1888. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1889. NSString* nsmodify_time=nil;
  1890. if(modify_time!=nil)
  1891. {
  1892. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1893. }
  1894. int is_delete = sqlite3_column_int(statement, 6);
  1895. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1896. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1897. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1898. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1899. itemjson[@"create_time"]=nscreate_time;
  1900. itemjson[@"modify_time"]=nsmodify_time;
  1901. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1902. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1903. count++;
  1904. }
  1905. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1906. sqlite3_finalize(statement);
  1907. }
  1908. ret[@"count"]=[NSNumber numberWithInt:count ];
  1909. [iSalesDB close_db:db];
  1910. return ret;
  1911. }
  1912. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  1913. {
  1914. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1915. // UIApplication * app = [UIApplication sharedApplication];
  1916. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1917. assert(add_params[@"user"]!=nil);
  1918. // assert(add_params[@"password"]!=nil);
  1919. 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 ];
  1920. // 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 ];
  1921. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1922. DebugLog(@"offline_login sql:%@",sqlQuery);
  1923. sqlite3_stmt * statement;
  1924. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1925. int count=0;
  1926. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1927. if ( dbresult== SQLITE_OK)
  1928. {
  1929. while (sqlite3_step(statement) == SQLITE_ROW)
  1930. {
  1931. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1932. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1933. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1934. int product_id = sqlite3_column_int(statement, 0);
  1935. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1936. int item_id = sqlite3_column_int(statement, 7);
  1937. NSString* Price=nil;
  1938. if(str_price==nil)
  1939. {
  1940. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1941. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  1942. if(price==nil)
  1943. Price=@"No Price.";
  1944. else
  1945. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1946. }
  1947. else
  1948. {
  1949. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1950. }
  1951. double discount = sqlite3_column_double(statement, 2);
  1952. int item_count = sqlite3_column_int(statement, 3);
  1953. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1954. NSString *nsline_note=nil;
  1955. if(line_note!=nil)
  1956. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1957. char *name = (char*)sqlite3_column_text(statement, 5);
  1958. NSString *nsname=nil;
  1959. if(name!=nil)
  1960. nsname= [[NSString alloc]initWithUTF8String:name];
  1961. char *description = (char*)sqlite3_column_text(statement, 6);
  1962. NSString *nsdescription=nil;
  1963. if(description!=nil)
  1964. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1965. // int stockUom = sqlite3_column_int(statement, 8);
  1966. // int _id = sqlite3_column_int(statement, 9);
  1967. //
  1968. //
  1969. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1970. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1971. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1972. // int carton=[bsubtotaljson[@"carton"] intValue];
  1973. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1974. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1975. itemjson[@"model"]=nsname;
  1976. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1977. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1978. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1979. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1980. // itemjson[@"check"]=@"true";
  1981. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1982. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1983. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1984. itemjson[@"unit_price"]=Price;
  1985. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1986. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1987. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1988. itemjson[@"note"]=nsline_note;
  1989. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  1990. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1991. count++;
  1992. }
  1993. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1994. sqlite3_finalize(statement);
  1995. }
  1996. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1997. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1998. return ret;
  1999. }
  2000. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2001. {
  2002. assert(add_params[@"user"]!=nil);
  2003. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2004. sqlite3 *db = [iSalesDB get_db];
  2005. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  2006. // for(int i=0;i<arr_soid.count;i++)
  2007. sqlite3_stmt * statement;
  2008. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2009. //int count=0;
  2010. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2011. if ( dbresult== SQLITE_OK)
  2012. {
  2013. int count=0;
  2014. while (sqlite3_step(statement) == SQLITE_ROW)
  2015. {
  2016. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2017. int _id = sqlite3_column_int(statement, 0);
  2018. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2019. NSString* nssync_data=nil;
  2020. if(sync_data!=nil)
  2021. {
  2022. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2023. }
  2024. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2025. NSString* nsimg_1=nil;
  2026. if(img_1!=nil)
  2027. {
  2028. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2029. [self copy_upImg:serial file:nsimg_1];
  2030. }
  2031. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2032. NSString* nsso_no=nil;
  2033. if(so_no!=nil)
  2034. {
  2035. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2036. }
  2037. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2038. itemjson[@"order_type"]=@"submit order";
  2039. else
  2040. itemjson[@"order_type"]=@"archive order";
  2041. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2042. itemjson[@"json_data"]= nssync_data;
  2043. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2044. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2045. count++;
  2046. }
  2047. ret[@"count"]=[NSNumber numberWithInt:count ];
  2048. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2049. }
  2050. sqlite3_finalize(statement);
  2051. [iSalesDB close_db:db];
  2052. return ret;
  2053. }
  2054. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2055. {
  2056. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2057. sqlite3 *db = [iSalesDB get_db];
  2058. // UIApplication * app = [UIApplication sharedApplication];
  2059. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2060. 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";
  2061. sqlite3_stmt * statement;
  2062. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2063. int count=0;
  2064. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2065. if ( dbresult== SQLITE_OK)
  2066. {
  2067. while (sqlite3_step(statement) == SQLITE_ROW)
  2068. {
  2069. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2070. int _id = sqlite3_column_int(statement, 0);
  2071. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2072. NSString* nsimg_0=nil;
  2073. if(img_0!=nil)
  2074. {
  2075. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2076. [self copy_upImg:serial file:nsimg_0];
  2077. }
  2078. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2079. NSString* nsimg_1=nil;
  2080. if(img_1!=nil)
  2081. {
  2082. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2083. [self copy_upImg:serial file:nsimg_1];
  2084. }
  2085. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2086. NSString* nsimg_2=nil;
  2087. if(img_2!=nil)
  2088. {
  2089. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2090. [self copy_upImg:serial file:nsimg_2];
  2091. }
  2092. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2093. NSString* nssync_data=nil;
  2094. if(sync_data!=nil)
  2095. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2096. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2097. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2098. itemjson[@"json_data"]= nssync_data;
  2099. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2100. count++;
  2101. }
  2102. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2103. sqlite3_finalize(statement);
  2104. }
  2105. ret[@"count"]=[NSNumber numberWithInt:count ];
  2106. [iSalesDB close_db:db];
  2107. return ret;
  2108. }
  2109. +(bool) check_offlinedata
  2110. {
  2111. // UIApplication * app = [UIApplication sharedApplication];
  2112. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2113. sqlite3 *db = [iSalesDB get_db];
  2114. NSString * where=@"1=1";
  2115. // if(appDelegate.user!=nil)
  2116. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2117. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2118. [iSalesDB close_db:db];
  2119. if(count==0)
  2120. {
  2121. return false;
  2122. }
  2123. return true;
  2124. //
  2125. //[iSalesDB close_db:db];
  2126. }
  2127. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2128. {
  2129. // UIApplication * app = [UIApplication sharedApplication];
  2130. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2131. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2132. sqlite3 *db = [iSalesDB get_db];
  2133. NSString* collectId=params[@"collectId"];
  2134. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2135. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2136. [iSalesDB execSql:sqlQuery db:db];
  2137. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2138. [iSalesDB close_db:db];
  2139. // appDelegate.wish_count =count;
  2140. //
  2141. // [appDelegate update_count_mark];
  2142. ret[@"result"]= [NSNumber numberWithInt:2];
  2143. ret[@"wish_count"]=@(count);
  2144. return ret;
  2145. }
  2146. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2147. //{
  2148. //
  2149. // UIApplication * app = [UIApplication sharedApplication];
  2150. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2151. //
  2152. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2153. // sqlite3 *db = [iSalesDB get_db];
  2154. // NSString* product_id=params[@"product_id"];
  2155. //
  2156. //
  2157. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2158. //
  2159. //
  2160. // for(int i=0;i<arr.count;i++)
  2161. // {
  2162. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2163. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2164. // if(count==0)
  2165. // {
  2166. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2167. // [iSalesDB execSql:sqlQuery db:db];
  2168. // }
  2169. // }
  2170. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2171. // [iSalesDB close_db:db];
  2172. //
  2173. // appDelegate.wish_count =count;
  2174. //
  2175. // [appDelegate update_count_mark];
  2176. // ret[@"result"]= [NSNumber numberWithInt:2];
  2177. // return ret;
  2178. // //
  2179. // //return ret;
  2180. //}
  2181. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2182. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2183. DebugLog(@"time interval: %lf",interval);
  2184. }
  2185. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2186. //{
  2187. // UIApplication * app = [UIApplication sharedApplication];
  2188. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2189. //
  2190. // int sort = [[params objectForKey:@"sort"] intValue];
  2191. // NSString *sort_str = @"";
  2192. // switch (sort) {
  2193. // case 0:{
  2194. // sort_str = @"order by w.modify_time desc";
  2195. // }
  2196. // break;
  2197. // case 1:{
  2198. // sort_str = @"order by w.modify_time asc";
  2199. // }
  2200. // break;
  2201. // case 2:{
  2202. // sort_str = @"order by m.name asc";
  2203. // }
  2204. // break;
  2205. // case 3:{
  2206. // sort_str = @"order by m.name desc";
  2207. // }
  2208. // break;
  2209. // case 4:{
  2210. // sort_str = @"order by m.description asc";
  2211. // }
  2212. // break;
  2213. //
  2214. // default:
  2215. // break;
  2216. //
  2217. // }
  2218. //
  2219. //
  2220. //// NSString* user = appDelegate.user;
  2221. //
  2222. // sqlite3 *db = [iSalesDB get_db];
  2223. //
  2224. // // order by w.create_time
  2225. // 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];
  2226. //
  2227. //// 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];
  2228. //
  2229. //
  2230. // sqlite3_stmt * statement;
  2231. //
  2232. // NSDate *date1 = [NSDate date];
  2233. //// NSDate *date2 = nil;
  2234. //
  2235. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2236. // int count=0;
  2237. //
  2238. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2239. // {
  2240. //
  2241. //
  2242. // while (sqlite3_step(statement) == SQLITE_ROW)
  2243. // {
  2244. //
  2245. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2246. //
  2247. // int product_id = sqlite3_column_double(statement, 0);
  2248. //
  2249. //
  2250. //
  2251. //
  2252. //
  2253. // char *description = (char*)sqlite3_column_text(statement, 1);
  2254. // if(description==nil)
  2255. // description= "";
  2256. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2257. //
  2258. // int item_id = sqlite3_column_double(statement, 2);
  2259. //
  2260. // NSDate *date_image = [NSDate date];
  2261. //
  2262. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2263. //
  2264. // printf("image : ");
  2265. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2266. //
  2267. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2268. // // if(url==nil)
  2269. // // url="";
  2270. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2271. //
  2272. // int qty = sqlite3_column_int(statement, 3);
  2273. //
  2274. //
  2275. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2276. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2277. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2278. // item[@"description"]= nsdescription;
  2279. // item[@"img"]= nsurl;
  2280. //
  2281. //
  2282. //
  2283. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2284. // count++;
  2285. //
  2286. // }
  2287. // printf("total time:");
  2288. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2289. //
  2290. // ret[@"count"]= [NSNumber numberWithInt:count];
  2291. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2292. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2293. // ret[@"result"]= [NSNumber numberWithInt:2];
  2294. //
  2295. //
  2296. // appDelegate.wish_count =count;
  2297. //
  2298. // [appDelegate update_count_mark];
  2299. // sqlite3_finalize(statement);
  2300. //
  2301. //
  2302. //
  2303. //
  2304. // }
  2305. //
  2306. // [iSalesDB close_db:db];
  2307. //
  2308. // return ret;
  2309. //}
  2310. +(NSDictionary*) offline_notimpl
  2311. {
  2312. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2313. ret[@"result"]=@"8";
  2314. ret[@"err_msg"]=@"offline mode does not support this function.";
  2315. return ret;
  2316. }
  2317. +(NSDictionary*) offline_home
  2318. {
  2319. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2320. NSString *cachefolder = [paths objectAtIndex:0];
  2321. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2322. NSData* json =nil;
  2323. json=[NSData dataWithContentsOfFile:img_cache];
  2324. if(json==nil)
  2325. return nil;
  2326. NSError *error=nil;
  2327. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2328. return menu;
  2329. }
  2330. +(NSDictionary*) offline_category_menu
  2331. {
  2332. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2333. NSString *cachefolder = [paths objectAtIndex:0];
  2334. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2335. NSData* json =nil;
  2336. json=[NSData dataWithContentsOfFile:img_cache];
  2337. if(json==nil)
  2338. return nil;
  2339. NSError *error=nil;
  2340. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2341. return menu;
  2342. }
  2343. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2344. {
  2345. NSString* offline_command=params[@"offline_Command"];
  2346. NSDictionary* ret=nil;
  2347. if([offline_command isEqualToString:@"model_NIYMAL"])
  2348. {
  2349. NSString* category = params[@"category"];
  2350. ret = [self refresh_model_NIYMAL:category];
  2351. }
  2352. return ret;
  2353. }
  2354. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2355. {
  2356. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2357. [ret setValue:@"2" forKey:@"result"];
  2358. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2359. sqlite3* db= [iSalesDB get_db];
  2360. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2361. [iSalesDB close_db:db];
  2362. [ret setObject:detail1_section forKey:@"detail_1"];
  2363. return ret;
  2364. }
  2365. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2366. {
  2367. //assert(user!=nil);
  2368. // UIApplication * app = [UIApplication sharedApplication];
  2369. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2370. // NSArray* arr1 = [self get_user_all_price_type];
  2371. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2372. // NSSet *set1 = [NSSet setWithArray:arr1];
  2373. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2374. // if(appDelegate.contact_id==nil)
  2375. // set2=[set1 mutableCopy];
  2376. // else
  2377. // [set2 intersectsSet:set1];
  2378. // NSArray *retarr = [set2 allObjects];
  2379. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2380. // sqlite3 *db = [iSalesDB get_db];
  2381. NSString* sqlQuery = nil;
  2382. if(contact_id==nil)
  2383. {
  2384. if(!blogin)
  2385. return nil;
  2386. 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];
  2387. }
  2388. else
  2389. 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];
  2390. sqlite3_stmt * statement;
  2391. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2392. int count=0;
  2393. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2394. {
  2395. while (sqlite3_step(statement) == SQLITE_ROW)
  2396. {
  2397. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2398. char *name = (char*)sqlite3_column_text(statement, 0);
  2399. if(name==nil)
  2400. name="";
  2401. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2402. // double price = sqlite3_column_double(statement, 1);
  2403. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2404. // if(isnull==nil)
  2405. // item[nsname]= @"No Price";
  2406. // else
  2407. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2408. char *price = (char*)sqlite3_column_text(statement, 1);
  2409. if(price!=nil)
  2410. {
  2411. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2412. nsprice=[AESCrypt fastdecrypt:nsprice];
  2413. if(nsprice.length>0)
  2414. {
  2415. double dp= [nsprice doubleValue];
  2416. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2417. }
  2418. }
  2419. else
  2420. {
  2421. item[nsname]= @"No Price";
  2422. }
  2423. // int type= sqlite3_column_int(statement, 2);
  2424. //item[@"type"]=@"price";
  2425. // item[nsname]= nsprice;
  2426. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2427. count++;
  2428. }
  2429. ret[@"count"]= [NSNumber numberWithInt:count];
  2430. sqlite3_finalize(statement);
  2431. }
  2432. // [iSalesDB close_db:db];
  2433. return ret;
  2434. }
  2435. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2436. //{
  2437. // NSArray* arr1 = [self get_user_all_price_type:db];
  2438. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2439. //
  2440. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2441. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2442. // // [set2 intersectsSet:set1];
  2443. // //
  2444. // //
  2445. // // NSArray *retarr = [set2 allObjects];
  2446. //
  2447. // NSString* whereprice=nil;
  2448. // if(contact_id==nil)
  2449. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2450. // else
  2451. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2452. //
  2453. //
  2454. // // sqlite3 *db = [iSalesDB get_db];
  2455. //
  2456. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2457. // sqlite3_stmt * statement;
  2458. //
  2459. //
  2460. // NSNumber* ret = nil;
  2461. // double dprice=DBL_MAX;
  2462. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2463. // {
  2464. //
  2465. //
  2466. // while (sqlite3_step(statement) == SQLITE_ROW)
  2467. // {
  2468. //
  2469. // // double val = sqlite3_column_double(statement, 0);
  2470. // char *price = (char*)sqlite3_column_text(statement, 0);
  2471. // if(price!=nil)
  2472. // {
  2473. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2474. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2475. // if(nsprice.length>0)
  2476. // {
  2477. // double dp= [nsprice doubleValue];
  2478. // if(dp<dprice)
  2479. // dprice=dp;
  2480. // }
  2481. // }
  2482. // }
  2483. //
  2484. //
  2485. //
  2486. //
  2487. // sqlite3_finalize(statement);
  2488. //
  2489. //
  2490. //
  2491. //
  2492. // }
  2493. //
  2494. // // [iSalesDB close_db:db];
  2495. //
  2496. // if(dprice==DBL_MAX)
  2497. // ret= nil;
  2498. // else
  2499. // ret= [NSNumber numberWithDouble:dprice];
  2500. // return ret;
  2501. //}
  2502. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2503. {
  2504. assert(user!=nil);
  2505. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2506. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2507. NSString* whereprice=nil;
  2508. if(contact_id==nil)
  2509. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2510. else
  2511. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2512. // sqlite3 *db = [iSalesDB get_db];
  2513. NSString *productIdCondition = @"1 = 1";
  2514. if (product_id) {
  2515. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2516. }
  2517. NSString *itemIdCondition = @"";
  2518. if (item_id) {
  2519. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2520. }
  2521. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2522. sqlite3_stmt * statement;
  2523. NSNumber* ret = nil;
  2524. double dprice=DBL_MAX;
  2525. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2526. {
  2527. while (sqlite3_step(statement) == SQLITE_ROW)
  2528. {
  2529. // double val = sqlite3_column_double(statement, 0);
  2530. char *price = (char*)sqlite3_column_text(statement, 0);
  2531. if(price!=nil)
  2532. {
  2533. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2534. nsprice=[AESCrypt fastdecrypt:nsprice];
  2535. if(nsprice.length>0)
  2536. {
  2537. double dp= [nsprice doubleValue];
  2538. if(dp<dprice)
  2539. dprice=dp;
  2540. }
  2541. }
  2542. }
  2543. sqlite3_finalize(statement);
  2544. }
  2545. // [iSalesDB close_db:db];
  2546. if(dprice==DBL_MAX)
  2547. ret= nil;
  2548. else
  2549. ret= [NSNumber numberWithDouble:dprice];
  2550. return ret;
  2551. }
  2552. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2553. {
  2554. assert(user!=nil);
  2555. NSArray* ret=nil;
  2556. // sqlite3 *db = [iSalesDB get_db];
  2557. // no customer assigned , use login user contact_id
  2558. // UIApplication * app = [UIApplication sharedApplication];
  2559. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2560. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2561. sqlite3_stmt * statement;
  2562. // int count=0;
  2563. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2564. {
  2565. if (sqlite3_step(statement) == SQLITE_ROW)
  2566. {
  2567. char *val = (char*)sqlite3_column_text(statement, 0);
  2568. if(val==nil)
  2569. val="";
  2570. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2571. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2572. }
  2573. sqlite3_finalize(statement);
  2574. }
  2575. // [iSalesDB close_db:db];
  2576. return ret;
  2577. }
  2578. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2579. {
  2580. // sqlite3 *db = [iSalesDB get_db];
  2581. if(contact_id==nil)
  2582. {
  2583. // no customer assigned , use login user contact_id
  2584. // UIApplication * app = [UIApplication sharedApplication];
  2585. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2586. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2587. sqlite3_stmt * statement;
  2588. // int count=0;
  2589. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2590. {
  2591. if (sqlite3_step(statement) == SQLITE_ROW)
  2592. {
  2593. char *val = (char*)sqlite3_column_text(statement, 0);
  2594. if(val==nil)
  2595. val="";
  2596. contact_id = [[NSString alloc]initWithUTF8String:val];
  2597. }
  2598. sqlite3_finalize(statement);
  2599. }
  2600. if(contact_id.length<=0)
  2601. {
  2602. // [iSalesDB close_db:db];
  2603. return nil;
  2604. }
  2605. }
  2606. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2607. sqlite3_stmt * statement;
  2608. NSArray* ret=nil;
  2609. // int count=0;
  2610. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2611. {
  2612. if (sqlite3_step(statement) == SQLITE_ROW)
  2613. {
  2614. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2615. if(price_type==nil)
  2616. price_type="";
  2617. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2618. if(nsprice_type.length>0)
  2619. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2620. else
  2621. ret=nil;
  2622. }
  2623. sqlite3_finalize(statement);
  2624. }
  2625. // [iSalesDB close_db:db];
  2626. return ret;
  2627. }
  2628. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2629. {
  2630. NSString* ret= nil;
  2631. // sqlite3 *db = [iSalesDB get_db];
  2632. NSString *sqlQuery = nil;
  2633. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2634. // 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;
  2635. // 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
  2636. if(product_id==nil && model_name)
  2637. 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;
  2638. else if (product_id)
  2639. 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
  2640. sqlite3_stmt * statement;
  2641. // int count=0;
  2642. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2643. {
  2644. if (sqlite3_step(statement) == SQLITE_ROW)
  2645. {
  2646. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2647. if(imgurl==nil)
  2648. imgurl="";
  2649. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2650. ret=nsimgurl;
  2651. }
  2652. sqlite3_finalize(statement);
  2653. }
  2654. else
  2655. {
  2656. [ret setValue:@"8" forKey:@"result"];
  2657. }
  2658. // [iSalesDB close_db:db];
  2659. DebugLog(@"data string: %@",ret );
  2660. return ret;
  2661. }
  2662. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2663. {
  2664. NSString* ret= nil;
  2665. sqlite3 *db = [iSalesDB get_db];
  2666. NSString *sqlQuery = nil;
  2667. if(product_id==nil)
  2668. if(upc_code==nil)
  2669. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2670. else
  2671. 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='%@';
  2672. else
  2673. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2674. sqlite3_stmt * statement;
  2675. // int count=0;
  2676. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2677. {
  2678. if (sqlite3_step(statement) == SQLITE_ROW)
  2679. {
  2680. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2681. if(default_category==nil)
  2682. default_category="";
  2683. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2684. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2685. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2686. ret=nsdefault_category;
  2687. }
  2688. sqlite3_finalize(statement);
  2689. }
  2690. else
  2691. {
  2692. [ret setValue:@"8" forKey:@"result"];
  2693. }
  2694. [iSalesDB close_db:db];
  2695. DebugLog(@"data string: %@",ret );
  2696. return ret;
  2697. }
  2698. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2699. NSString *sql = nil;
  2700. if (product_id != nil) {
  2701. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2702. } else {
  2703. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2704. }
  2705. NSString *result_set = [iSalesDB jk_queryText:sql];
  2706. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2707. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2708. return result_set;
  2709. }
  2710. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2711. //{
  2712. //
  2713. //
  2714. // assert(params[@"user"]!=nil);
  2715. //
  2716. //
  2717. //
  2718. // NSString* model_name = [params valueForKey:@"product_name"];
  2719. //
  2720. // NSString* product_id = [params valueForKey:@"product_id"];
  2721. //
  2722. // NSString* category = [params valueForKey:@"category"];
  2723. //
  2724. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2725. // if(category==nil) {
  2726. // category = default_category_id;
  2727. // } else {
  2728. //
  2729. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2730. // // 参数重有多个category id
  2731. // if (arr_0.count > 1) {
  2732. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2733. //
  2734. // // 取交集
  2735. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2736. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2737. // [set_0 intersectSet:set_1];
  2738. //
  2739. // if (set_0.count == 1) {
  2740. // category = (NSString *)[set_0 anyObject];
  2741. // } else {
  2742. // if ([set_0 containsObject:default_category_id]) {
  2743. // category = default_category_id;
  2744. // } else {
  2745. // category = (NSString *)[set_0 anyObject];
  2746. // }
  2747. // }
  2748. // }
  2749. // }
  2750. //
  2751. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2752. //
  2753. //
  2754. //
  2755. //
  2756. //
  2757. //
  2758. // sqlite3 *db = [iSalesDB get_db];
  2759. //
  2760. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2761. //
  2762. //
  2763. // NSString *sqlQuery = nil;
  2764. //
  2765. // if(product_id==nil)
  2766. // 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='%@';
  2767. // else
  2768. //
  2769. // 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=%@;
  2770. //
  2771. //
  2772. // sqlite3_stmt * statement;
  2773. // [ret setValue:@"2" forKey:@"result"];
  2774. // [ret setValue:@"3" forKey:@"detail_section_count"];
  2775. //
  2776. // // int count=0;
  2777. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2778. // {
  2779. //
  2780. //
  2781. // if (sqlite3_step(statement) == SQLITE_ROW)
  2782. // {
  2783. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2784. //
  2785. //
  2786. // char *name = (char*)sqlite3_column_text(statement, 0);
  2787. // if(name==nil)
  2788. // name="";
  2789. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2790. //
  2791. // char *description = (char*)sqlite3_column_text(statement, 1);
  2792. // if(description==nil)
  2793. // description="";
  2794. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2795. //
  2796. //
  2797. // int product_id = sqlite3_column_int(statement, 2);
  2798. //
  2799. //
  2800. // char *color = (char*)sqlite3_column_text(statement, 3);
  2801. // if(color==nil)
  2802. // color="";
  2803. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2804. // //
  2805. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2806. // // if(legcolor==nil)
  2807. // // legcolor="";
  2808. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2809. // //
  2810. // //
  2811. // int availability = sqlite3_column_int(statement, 5);
  2812. // //
  2813. // int incoming_stock = sqlite3_column_int(statement, 6);
  2814. //
  2815. //
  2816. // char *demension = (char*)sqlite3_column_text(statement, 7);
  2817. // if(demension==nil)
  2818. // demension="";
  2819. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2820. //
  2821. //
  2822. //
  2823. // // ,,,,,,,,,
  2824. //
  2825. //
  2826. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2827. // if(seat_height==nil)
  2828. // seat_height="";
  2829. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2830. //
  2831. //
  2832. // char *material = (char*)sqlite3_column_text(statement, 9);
  2833. // if(material==nil)
  2834. // material="";
  2835. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2836. //
  2837. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2838. // if(box_dim==nil)
  2839. // box_dim="";
  2840. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2841. //
  2842. // char *volume = (char*)sqlite3_column_text(statement, 11);
  2843. // if(volume==nil)
  2844. // volume="";
  2845. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2846. //
  2847. // double weight = sqlite3_column_double(statement, 12);
  2848. //
  2849. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  2850. // if(model_set==nil)
  2851. // model_set="";
  2852. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2853. //
  2854. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2855. // if(load_ability==nil)
  2856. // load_ability="";
  2857. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2858. //
  2859. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  2860. //// if(default_category==nil)
  2861. //// default_category="";
  2862. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2863. //
  2864. //
  2865. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2866. // if(fabric_content==nil)
  2867. // fabric_content="";
  2868. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2869. //
  2870. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  2871. // if(assembling==nil)
  2872. // assembling="";
  2873. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2874. //
  2875. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  2876. // if(made_in==nil)
  2877. // made_in="";
  2878. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2879. //
  2880. //
  2881. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2882. // if(special_remarks==nil)
  2883. // special_remarks="";
  2884. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2885. //
  2886. //
  2887. // int stockUcom = sqlite3_column_double(statement, 20);
  2888. //
  2889. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  2890. // if(product_group==nil)
  2891. // product_group="";
  2892. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2893. //
  2894. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2895. // // if(fashion_selector==nil)
  2896. // // fashion_selector="";
  2897. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2898. //
  2899. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2900. // if(selector_field==nil)
  2901. // selector_field="";
  2902. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2903. //
  2904. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  2905. // if(property_field==nil)
  2906. // property_field="";
  2907. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2908. //
  2909. //
  2910. //
  2911. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  2912. // if(packaging==nil)
  2913. // packaging="";
  2914. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2915. //
  2916. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2917. //
  2918. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2919. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2920. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2921. //
  2922. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  2923. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2924. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2925. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2926. //
  2927. //
  2928. //
  2929. //
  2930. //
  2931. // NSString* Availability=nil;
  2932. // if(availability>0)
  2933. // Availability=[NSString stringWithFormat:@"%d",availability];
  2934. // else
  2935. // Availability = @"Out of Stock";
  2936. //
  2937. // [img_section setValue:Availability forKey:@"Availability"];
  2938. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2939. //
  2940. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2941. //
  2942. //
  2943. // char *eta = (char*)sqlite3_column_text(statement, 25);
  2944. // if(eta==nil)
  2945. // eta="";
  2946. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2947. // if ([nseta isEqualToString:@"null"]) {
  2948. // nseta = @"";
  2949. // }
  2950. // if (availability <= 0) {
  2951. // [img_section setValue:nseta forKey:@"ETA"];
  2952. // }
  2953. //
  2954. //
  2955. // int item_id = sqlite3_column_int(statement, 26);
  2956. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2957. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  2958. //
  2959. // NSString* Price=nil;
  2960. // if(appDelegate.bLogin==false)
  2961. // Price=@"Must Sign in.";
  2962. // else
  2963. // {
  2964. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2965. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2966. // if(price==nil)
  2967. // Price=@"No Price.";
  2968. // else
  2969. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2970. // }
  2971. //
  2972. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  2973. // [img_section setValue:Price forKey:@"price"];
  2974. // [img_section setValue:nsname forKey:@"model_name"];
  2975. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  2976. //
  2977. // if (appDelegate.order_code) { // 离线order code即so#
  2978. //
  2979. // 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];
  2980. // __block int cartQTY = 0;
  2981. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2982. //
  2983. // cartQTY = sqlite3_column_int(stmt, 0);
  2984. //
  2985. // }];
  2986. //
  2987. // if (cartQTY > 0) {
  2988. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2989. // }
  2990. // }
  2991. //
  2992. // [ret setObject:img_section forKey:@"img_section"];
  2993. //
  2994. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2995. // int detail0_item_count=0;
  2996. //
  2997. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2998. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2999. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3000. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3001. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3002. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3003. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3004. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3005. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3006. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3007. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3008. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3009. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3010. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3011. //
  3012. //
  3013. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3014. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3015. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3016. // {
  3017. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3018. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3019. // }
  3020. //
  3021. //
  3022. //
  3023. //
  3024. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3025. // [detail0_section setValue:@"kv" forKey:@"type"];
  3026. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3027. //
  3028. // [ret setObject:detail0_section forKey:@"detail_0"];
  3029. //
  3030. //
  3031. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3032. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3033. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3034. // // [detail1_section setValue:@"content" forKey:@"type"];
  3035. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3036. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3037. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3038. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3039. // [ret setObject:detail1_section forKey:@"detail_1"];
  3040. //
  3041. //
  3042. //
  3043. //
  3044. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3045. // [detail2_section setValue:@"detail" forKey:@"target"];
  3046. // [detail2_section setValue:@"popup" forKey:@"action"];
  3047. // [detail2_section setValue:@"content" forKey:@"type"];
  3048. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3049. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3050. // [detail2_section setValue:@"local" forKey:@"data"];
  3051. // [ret setObject:detail2_section forKey:@"detail_2"];
  3052. // }
  3053. //
  3054. //
  3055. //
  3056. //
  3057. // sqlite3_finalize(statement);
  3058. // }
  3059. // else
  3060. // {
  3061. // [ret setValue:@"8" forKey:@"result"];
  3062. // }
  3063. //// DebugLog(@"count:%d",count);
  3064. //
  3065. //
  3066. // [iSalesDB close_db:db];
  3067. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3068. //
  3069. // return ret;
  3070. //}
  3071. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3072. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3073. NSString* keyword = [params valueForKey:@"keyword"];
  3074. keyword=keyword.lowercaseString;
  3075. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3076. int limit = [[params valueForKey:@"limit"] intValue];
  3077. int offset = [[params valueForKey:@"offset"] intValue];
  3078. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3079. NSString *limit_str = @"";
  3080. if (limited) {
  3081. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3082. }
  3083. sqlite3 *db = [iSalesDB get_db];
  3084. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3085. // UIApplication * app = [UIApplication sharedApplication];
  3086. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3087. NSString *sqlQuery = nil;
  3088. if(exactMatch )
  3089. 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 ;
  3090. else
  3091. 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
  3092. DebugLog(@"offline_search sql:%@",sqlQuery);
  3093. sqlite3_stmt * statement;
  3094. [ret setValue:@"2" forKey:@"result"];
  3095. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3096. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3097. // int count=0;
  3098. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3099. {
  3100. int i=0;
  3101. while (sqlite3_step(statement) == SQLITE_ROW)
  3102. {
  3103. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3104. // char *name = (char*)sqlite3_column_text(statement, 1);
  3105. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3106. char *name = (char*)sqlite3_column_text(statement, 0);
  3107. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3108. char *description = (char*)sqlite3_column_text(statement, 1);
  3109. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3110. int product_id = sqlite3_column_int(statement, 2);
  3111. // char *url = (char*)sqlite3_column_text(statement, 3);
  3112. // if(url==nil)
  3113. // url="";
  3114. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3115. int wid = sqlite3_column_int(statement, 3);
  3116. int closeout = sqlite3_column_int(statement, 4);
  3117. int cid = sqlite3_column_int(statement, 5);
  3118. int wisdelete = sqlite3_column_int(statement, 6);
  3119. int more_color = sqlite3_column_int(statement, 7);
  3120. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3121. if(wid !=0 && wisdelete != 1)
  3122. [item setValue:@"true" forKey:@"wish_exists"];
  3123. else
  3124. [item setValue:@"false" forKey:@"wish_exists"];
  3125. if(closeout==0)
  3126. [item setValue:@"false" forKey:@"is_closeout"];
  3127. else
  3128. [item setValue:@"true" forKey:@"is_closeout"];
  3129. if(cid==0)
  3130. [item setValue:@"false" forKey:@"cart_exists"];
  3131. else
  3132. [item setValue:@"true" forKey:@"cart_exists"];
  3133. if (more_color == 0) {
  3134. [item setObject:@(false) forKey:@"more_color"];
  3135. } else if (more_color == 1) {
  3136. [item setObject:@(true) forKey:@"more_color"];
  3137. }
  3138. [item addEntriesFromDictionary:imgjson];
  3139. // [item setValue:nsurl forKey:@"img"];
  3140. [item setValue:nsname forKey:@"fash_name"];
  3141. [item setValue:nsdescription forKey:@"description"];
  3142. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3143. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3144. i++;
  3145. }
  3146. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3147. [ret setObject:items forKey:@"items"];
  3148. sqlite3_finalize(statement);
  3149. }
  3150. DebugLog(@"count:%d",count);
  3151. [iSalesDB close_db:db];
  3152. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3153. return ret;
  3154. }
  3155. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3156. {
  3157. return [self search:params limited:YES];
  3158. }
  3159. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3160. //{
  3161. // UIApplication * app = [UIApplication sharedApplication];
  3162. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3163. //
  3164. //// [iSalesDB disable_trigger]
  3165. // [iSalesDB disable_trigger];
  3166. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3167. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3168. //
  3169. // [iSalesDB enable_trigger];
  3170. //// [iSalesDB enable_trigger]
  3171. // //
  3172. // // NSString* user = [params valueForKey:@"user"];
  3173. // //
  3174. // // NSString* password = [params valueForKey:@"password"];
  3175. //
  3176. //
  3177. //
  3178. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3179. //
  3180. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3181. //
  3182. //
  3183. //
  3184. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3185. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3186. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3187. //
  3188. // [appDelegate update_count_mark];
  3189. //
  3190. //
  3191. // appDelegate.can_show_price =false;
  3192. // appDelegate.can_see_price =false;
  3193. // appDelegate.can_create_portfolio =false;
  3194. // appDelegate.can_create_order =false;
  3195. //
  3196. //
  3197. // appDelegate.can_cancel_order =false;
  3198. // appDelegate.can_set_cart_price =false;
  3199. // appDelegate.can_delete_order =false;
  3200. // appDelegate.can_submit_order =false;
  3201. // appDelegate.can_set_tearsheet_price =false;
  3202. // appDelegate.can_update_contact_info = false;
  3203. //
  3204. // appDelegate.save_order_logout = false;
  3205. // appDelegate.submit_order_logout = false;
  3206. // appDelegate.alert_sold_in_quantities = false;
  3207. //
  3208. // appDelegate.ipad_perm =nil ;
  3209. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3210. // appDelegate.OrderFilter= nil;
  3211. // [appDelegate SetSo:nil];
  3212. // [appDelegate set_main_button_panel];
  3213. //
  3214. //
  3215. // // sqlite3 *db = [iSalesDB get_db];
  3216. // //
  3217. // //
  3218. // //
  3219. // //
  3220. // //
  3221. // // 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"]];
  3222. // //
  3223. // //
  3224. // //
  3225. // //
  3226. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3227. // // sqlite3_stmt * statement;
  3228. // //
  3229. // //
  3230. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3231. // //
  3232. // //
  3233. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3234. // // {
  3235. // //
  3236. // //
  3237. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3238. // // {
  3239. // //
  3240. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3241. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3242. // //
  3243. // //
  3244. // //
  3245. // // int can_show_price = sqlite3_column_int(statement, 0);
  3246. // // int can_see_price = sqlite3_column_int(statement, 1);
  3247. // //
  3248. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3249. // // if(contact_id==nil)
  3250. // // contact_id="";
  3251. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3252. // //
  3253. // // int user_type = sqlite3_column_int(statement, 3);
  3254. // //
  3255. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3256. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3257. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3258. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3259. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3260. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3261. // // int can_create_order = sqlite3_column_int(statement, 10);
  3262. // //
  3263. // //
  3264. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3265. // // if(mode==nil)
  3266. // // mode="";
  3267. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3268. // //
  3269. // //
  3270. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3271. // // if(username==nil)
  3272. // // username="";
  3273. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3274. // //
  3275. // //
  3276. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3277. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3278. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3279. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3280. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3281. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3282. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3283. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3284. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3285. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3286. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3287. // //
  3288. // // [header setValue:nsusername forKey:@"username"];
  3289. // //
  3290. // //
  3291. // // [ret setObject:header forKey:@"header"];
  3292. // // [ret setValue:nsmode forKey:@"mode"];
  3293. // //
  3294. // //
  3295. // // }
  3296. // //
  3297. // //
  3298. // //
  3299. // // sqlite3_finalize(statement);
  3300. // // }
  3301. // //
  3302. // //
  3303. // //
  3304. // // [iSalesDB close_db:db];
  3305. // //
  3306. // //
  3307. // //
  3308. // //
  3309. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3310. //
  3311. // return ret;
  3312. //}
  3313. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3314. //{
  3315. //
  3316. // UIApplication * app = [UIApplication sharedApplication];
  3317. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3318. //
  3319. // [iSalesDB disable_trigger];
  3320. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3321. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3322. // [iSalesDB enable_trigger];
  3323. //
  3324. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3325. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3326. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3327. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3328. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3329. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3330. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3331. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3332. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3333. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3334. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3335. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3336. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3337. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3338. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3339. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3340. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3341. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3342. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3343. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3344. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3345. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3346. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3347. //
  3348. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3349. //
  3350. // [arr_name addObject:customer_first_name];
  3351. // [arr_name addObject:customer_last_name];
  3352. //
  3353. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3354. //
  3355. // // default ship from
  3356. // 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';";
  3357. //
  3358. // __block NSString *cid = @"";
  3359. // __block NSString *name = @"";
  3360. // __block NSString *ext = @"";
  3361. // __block NSString *contact = @"";
  3362. // __block NSString *email = @"";
  3363. // __block NSString *fax = @"";
  3364. // __block NSString *phone = @"";
  3365. //
  3366. // sqlite3 *db = [iSalesDB get_db];
  3367. //
  3368. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3369. //
  3370. // cid = [self textAtColumn:0 statement:statment];
  3371. // name = [self textAtColumn:1 statement:statment];
  3372. // ext = [self textAtColumn:2 statement:statment];
  3373. // contact = [self textAtColumn:3 statement:statment];
  3374. // email = [self textAtColumn:4 statement:statment];
  3375. // fax = [self textAtColumn:5 statement:statment];
  3376. // phone = [self textAtColumn:6 statement:statment];
  3377. //
  3378. // }];
  3379. //
  3380. // NSString* so_id = [self get_offline_soid:db];
  3381. // if(so_id==nil)
  3382. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3383. //
  3384. // 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];
  3385. //
  3386. //
  3387. //
  3388. // int result =[iSalesDB execSql:sql_neworder db:db];
  3389. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3390. //
  3391. //
  3392. //
  3393. // //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'
  3394. // //soId
  3395. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3396. //
  3397. //
  3398. //
  3399. //
  3400. //
  3401. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3402. // sqlite3_stmt * statement;
  3403. //
  3404. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3405. // {
  3406. // if (sqlite3_step(statement) == SQLITE_ROW)
  3407. // {
  3408. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3409. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3410. //
  3411. // //ret = sqlite3_column_int(statement, 0);
  3412. //
  3413. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3414. // if(soId==nil)
  3415. // soId="";
  3416. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3417. // [ret setValue:nssoId forKey:@"soId"];
  3418. // [ret setValue:nssoId forKey:@"orderCode"];
  3419. //
  3420. // }
  3421. // sqlite3_finalize(statement);
  3422. // }
  3423. //
  3424. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3425. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3426. //
  3427. //
  3428. // [iSalesDB close_db:db];
  3429. //
  3430. // return [RAConvertor dict2data:ret];
  3431. //
  3432. //}
  3433. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3434. {
  3435. //assert(params[@"order_code"]);
  3436. // assert(params[@"order_code"]);
  3437. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3438. NSString* orderCode = [params valueForKey:@"orderCode"];
  3439. NSString* app_order_code= params[@"appDelegate.order_code"];
  3440. // UIApplication * app = [UIApplication sharedApplication];
  3441. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3442. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3443. {
  3444. [iSalesDB disable_trigger];
  3445. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3446. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3447. [iSalesDB enable_trigger];
  3448. }
  3449. sqlite3 *db = [iSalesDB get_db];
  3450. int cart_count=[self query_ordercartcount:orderCode db:db];
  3451. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3452. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3453. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3454. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3455. [iSalesDB close_db:db];
  3456. return [RAConvertor dict2data:ret];
  3457. }
  3458. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3459. {
  3460. assert(params[@"can_create_backorder"]!=nil);
  3461. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3462. sqlite3 *db = [iSalesDB get_db];
  3463. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3464. int count =0;
  3465. if(params[@"count"]!=nil)
  3466. {
  3467. count = [params[@"count"] intValue];
  3468. }
  3469. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3470. NSString* product_id=params[@"product_id"];
  3471. NSString* orderCode=params[@"orderCode"];
  3472. NSString *qty = params[@"qty"];
  3473. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3474. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3475. __block int number_of_outOfStock = 0;
  3476. for(int i=0;i<arr_id.count;i++)
  3477. {
  3478. NSInteger item_qty= count;
  3479. if (qty) {
  3480. item_qty = [qty_arr[i] integerValue];
  3481. }
  3482. if(item_qty==0)
  3483. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3484. // 检查新加Model数量是否大于库存
  3485. if (![params[@"can_create_backorder"] boolValue]) {
  3486. __block BOOL needContinue = NO;
  3487. [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) {
  3488. int availability = sqlite3_column_int(stmt, 0);
  3489. // 库存小于购买量为缺货
  3490. if (availability < item_qty || availability <= 0) {
  3491. number_of_outOfStock++;
  3492. needContinue = YES;
  3493. }
  3494. }];
  3495. if (needContinue) {
  3496. continue;
  3497. }
  3498. }
  3499. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3500. NSString* sql=nil;
  3501. sqlite3_stmt *stmt = nil;
  3502. BOOL shouldStep = NO;
  3503. if(_id<0)
  3504. {
  3505. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3506. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3507. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3508. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3509. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3510. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3511. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3512. shouldStep = YES;
  3513. }
  3514. else
  3515. {
  3516. if (qty) { // wish list move to cart
  3517. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3518. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3519. sqlite3_bind_int(stmt, 1, _id);
  3520. shouldStep = YES;
  3521. } else {
  3522. 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];
  3523. __block BOOL update = YES;
  3524. if (![params[@"can_create_backorder"] boolValue]) {
  3525. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3526. int newQTY = sqlite3_column_int(stmt, 0);
  3527. int availability = sqlite3_column_int(stmt, 1);
  3528. if (newQTY > availability) { // 库存不够
  3529. update = NO;
  3530. number_of_outOfStock++;
  3531. }
  3532. }];
  3533. }
  3534. if (update) {
  3535. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3536. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3537. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3538. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3539. shouldStep = YES;
  3540. }
  3541. }
  3542. }
  3543. if (shouldStep) {
  3544. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3545. [iSalesDB execSql:@"ROLLBACK" db:db];
  3546. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3547. [iSalesDB close_db:db];
  3548. DebugLog(@"add to cart error");
  3549. return [RAConvertor dict2data:ret];
  3550. }
  3551. }
  3552. }
  3553. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3554. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3555. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3556. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3557. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3558. if (![params[@"can_create_backorder"] boolValue]) {
  3559. if (number_of_outOfStock > 0) {
  3560. ret[@"result"]=[NSNumber numberWithInt:8];
  3561. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3562. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3563. }
  3564. }
  3565. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3566. [iSalesDB close_db:db];
  3567. return [RAConvertor dict2data:ret];
  3568. }
  3569. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3570. {
  3571. // UIApplication * app = [UIApplication sharedApplication];
  3572. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3573. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3574. 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];
  3575. sqlite3_stmt * statement;
  3576. int count=0;
  3577. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3578. {
  3579. while (sqlite3_step(statement) == SQLITE_ROW)
  3580. {
  3581. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3582. int bitem_id = sqlite3_column_int(statement, 0);
  3583. int bitem_qty = sqlite3_column_int(statement, 1);
  3584. char *name = (char*)sqlite3_column_text(statement, 2);
  3585. if(name==nil)
  3586. name="";
  3587. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3588. char *description = (char*)sqlite3_column_text(statement, 3);
  3589. if(description==nil)
  3590. description="";
  3591. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3592. double unit_price = sqlite3_column_double(statement, 4);
  3593. int use_unitprice = sqlite3_column_int(statement, 5);
  3594. if(use_unitprice!=1)
  3595. {
  3596. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3597. }
  3598. itemjson[@"model"]=nsname;
  3599. itemjson[@"description"]=nsdescription;
  3600. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3601. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3602. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3603. if(compute)
  3604. {
  3605. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3606. }
  3607. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3608. count++;
  3609. }
  3610. sqlite3_finalize(statement);
  3611. }
  3612. ret[@"count"]=@(count);
  3613. if(count==0)
  3614. return nil;
  3615. else
  3616. return ret;
  3617. }
  3618. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3619. {
  3620. //compute: add part to subtotal;
  3621. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3622. dict_item[@(item_id)]=@"1";
  3623. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3624. double cuft=0;
  3625. double weight=0;
  3626. int carton=0;
  3627. 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];
  3628. sqlite3_stmt * statement;
  3629. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3630. {
  3631. if (sqlite3_step(statement) == SQLITE_ROW)
  3632. {
  3633. double ulength = sqlite3_column_double(statement, 0);
  3634. double uwidth = sqlite3_column_double(statement, 1);
  3635. double uheight = sqlite3_column_double(statement, 2);
  3636. double uweight = sqlite3_column_double(statement, 3);
  3637. double mlength = sqlite3_column_double(statement, 4);
  3638. double mwidth = sqlite3_column_double(statement, 5);
  3639. double mheight = sqlite3_column_double(statement, 6);
  3640. double mweight = sqlite3_column_double(statement, 7);
  3641. double ilength = sqlite3_column_double(statement, 8);
  3642. double iwidth = sqlite3_column_double(statement, 9);
  3643. double iheight = sqlite3_column_double(statement, 10);
  3644. double iweight = sqlite3_column_double(statement, 11);
  3645. // int pcs = sqlite3_column_int(statement,12);
  3646. int mpack = sqlite3_column_int(statement, 13);
  3647. int ipack = sqlite3_column_int(statement, 14);
  3648. double ucbf = sqlite3_column_double(statement, 15);
  3649. // double icbf = sqlite3_column_double(statement, 16);
  3650. // double mcbf = sqlite3_column_double(statement, 17);
  3651. if(ipack==0)
  3652. {
  3653. carton= count/mpack ;
  3654. weight = mweight*carton;
  3655. cuft= carton*(mlength*mwidth*mheight);
  3656. int remain=count%mpack;
  3657. if(remain==0)
  3658. {
  3659. //do nothing;
  3660. }
  3661. else
  3662. {
  3663. carton++;
  3664. weight += uweight*remain;
  3665. cuft += (ulength*uwidth*uheight)*remain;
  3666. }
  3667. }
  3668. else
  3669. {
  3670. carton = count/(mpack*ipack);
  3671. weight = mweight*carton;
  3672. cuft= carton*(mlength*mwidth*mheight);
  3673. int remain=count%(mpack*ipack);
  3674. if(remain==0)
  3675. {
  3676. // do nothing;
  3677. }
  3678. else
  3679. {
  3680. carton++;
  3681. int icarton =remain/ipack;
  3682. int iremain=remain%ipack;
  3683. weight += iweight*icarton;
  3684. cuft += (ilength*iwidth*iheight)*icarton;
  3685. if(iremain==0)
  3686. {
  3687. //do nothing;
  3688. }
  3689. else
  3690. {
  3691. weight += uweight*iremain;
  3692. cuft += (ulength*uwidth*uheight)*iremain;
  3693. }
  3694. }
  3695. }
  3696. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3697. cuft=ucbf*count;
  3698. weight= uweight*count;
  3699. #endif
  3700. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3701. cuft=ucbf*count;
  3702. weight= uweight*count;
  3703. #endif
  3704. }
  3705. sqlite3_finalize(statement);
  3706. }
  3707. if(compute)
  3708. {
  3709. NSArray * arr_count=nil;
  3710. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3711. for(int i=0;i<arr_bundle.count;i++)
  3712. {
  3713. dict_item[arr_bundle[i]]=@"1";
  3714. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3715. cuft+=[bundlejson[@"cuft"] doubleValue];
  3716. weight+=[bundlejson[@"weight"] doubleValue];
  3717. carton+=[bundlejson[@"carton"] intValue];
  3718. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3719. }
  3720. }
  3721. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3722. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3723. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3724. ret[@"items"]=dict_item;
  3725. return ret;
  3726. }
  3727. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3728. {
  3729. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3730. // 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 ];
  3731. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3732. DebugLog(@"offline_login sql:%@",sqlQuery);
  3733. sqlite3_stmt * statement;
  3734. int cart_count=0;
  3735. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3736. if ( dbresult== SQLITE_OK)
  3737. {
  3738. while (sqlite3_step(statement) == SQLITE_ROW)
  3739. {
  3740. int item_id = sqlite3_column_int(statement, 0);
  3741. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3742. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3743. }
  3744. sqlite3_finalize(statement);
  3745. }
  3746. return cart_count;
  3747. }
  3748. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3749. //{
  3750. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3751. // sqlite3 *db = [iSalesDB get_db];
  3752. // UIApplication * app = [UIApplication sharedApplication];
  3753. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3754. //
  3755. // NSString* orderCode=params[@"orderCode"];
  3756. //
  3757. // int sort = [[params objectForKey:@"sort"] intValue];
  3758. // NSString *sort_str = @"";
  3759. // switch (sort) {
  3760. // case 0:{
  3761. // sort_str = @"order by c.modify_time desc";
  3762. // }
  3763. // break;
  3764. // case 1:{
  3765. // sort_str = @"order by c.modify_time asc";
  3766. // }
  3767. // break;
  3768. // case 2:{
  3769. // sort_str = @"order by m.name asc";
  3770. // }
  3771. // break;
  3772. // case 3:{
  3773. // sort_str = @"order by m.name desc";
  3774. // }
  3775. // break;
  3776. // case 4:{
  3777. // sort_str = @"order by m.description asc";
  3778. // }
  3779. // break;
  3780. //
  3781. // default:
  3782. // break;
  3783. //
  3784. // }
  3785. //
  3786. //
  3787. //
  3788. // 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 ];
  3789. //
  3790. //
  3791. //// 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 ];
  3792. //
  3793. //
  3794. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3795. //
  3796. // DebugLog(@"offline_login sql:%@",sqlQuery);
  3797. // sqlite3_stmt * statement;
  3798. //
  3799. //
  3800. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3801. //
  3802. // NSDate *date1 = [NSDate date];
  3803. //
  3804. // int count=0;
  3805. // int cart_count=0;
  3806. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3807. // if ( dbresult== SQLITE_OK)
  3808. // {
  3809. //
  3810. //
  3811. // while (sqlite3_step(statement) == SQLITE_ROW)
  3812. // {
  3813. //// NSDate *row_date = [NSDate date];
  3814. //
  3815. //
  3816. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3817. //
  3818. //
  3819. //
  3820. //
  3821. // int product_id = sqlite3_column_int(statement, 0);
  3822. //
  3823. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  3824. //
  3825. // int item_id = sqlite3_column_int(statement, 7);
  3826. //
  3827. // NSString* Price=nil;
  3828. // if(str_price==nil)
  3829. // {
  3830. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3831. //// NSDate *price_date = [NSDate date];
  3832. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3833. //// DebugLog(@"price time interval");
  3834. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3835. //
  3836. // if(price==nil)
  3837. // Price=@"No Price.";
  3838. // else
  3839. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3840. // }
  3841. // else
  3842. // {
  3843. //
  3844. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3845. // }
  3846. //
  3847. //
  3848. // double discount = sqlite3_column_double(statement, 2);
  3849. // int item_count = sqlite3_column_int(statement, 3);
  3850. //
  3851. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  3852. // NSString *nsline_note=nil;
  3853. // if(line_note!=nil)
  3854. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3855. //
  3856. //
  3857. // char *name = (char*)sqlite3_column_text(statement, 5);
  3858. // NSString *nsname=nil;
  3859. // if(name!=nil)
  3860. // nsname= [[NSString alloc]initWithUTF8String:name];
  3861. //
  3862. // char *description = (char*)sqlite3_column_text(statement, 6);
  3863. // NSString *nsdescription=nil;
  3864. // if(description!=nil)
  3865. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  3866. //
  3867. //
  3868. //
  3869. // int stockUom = sqlite3_column_int(statement, 8);
  3870. // int _id = sqlite3_column_int(statement, 9);
  3871. // int availability = sqlite3_column_int(statement, 10);
  3872. //
  3873. //// NSDate *subtotal_date = [NSDate date];
  3874. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3875. //// DebugLog(@"subtotal_date time interval");
  3876. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3877. //
  3878. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3879. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  3880. // int carton=[bsubtotaljson[@"carton"] intValue];
  3881. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3882. //
  3883. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3884. //// NSDate *img_date = [NSDate date];
  3885. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3886. //// DebugLog(@"img_date time interval");
  3887. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3888. //
  3889. // itemjson[@"model"]=nsname;
  3890. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3891. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3892. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3893. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3894. // itemjson[@"check"]=@"true";
  3895. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3896. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3897. // itemjson[@"unit_price"]=Price;
  3898. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3899. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3900. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3901. // itemjson[@"note"]=nsline_note;
  3902. // if (!appDelegate.can_create_backorder) {
  3903. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3904. // }
  3905. //// NSDate *date2 = [NSDate date];
  3906. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3907. //// DebugLog(@"model_bundle time interval");
  3908. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3909. //
  3910. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3911. // count++;
  3912. //
  3913. //// DebugLog(@"row time interval");
  3914. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3915. // }
  3916. //
  3917. //
  3918. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3919. // sqlite3_finalize(statement);
  3920. // }
  3921. //
  3922. //
  3923. // DebugLog(@"request cart total time interval");
  3924. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3925. //
  3926. //
  3927. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3928. //
  3929. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3930. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3931. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3932. // ret[@"count"]=[NSNumber numberWithInt:count ];
  3933. //
  3934. // ret[@"mode"]=@"Regular Mode";
  3935. //
  3936. // [iSalesDB close_db:db];
  3937. //
  3938. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3939. //
  3940. // DebugLog(@"general notes :%@",general_note);
  3941. //
  3942. // ret[@"general_note"]= general_note;
  3943. //
  3944. // return [RAConvertor dict2data:ret];
  3945. //}
  3946. +(NSData*) offline_login :(NSMutableDictionary *) params
  3947. {
  3948. NSString* user = [params valueForKey:@"user"];
  3949. NSString* password = [params valueForKey:@"password"];
  3950. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3951. sqlite3 *db = [iSalesDB get_db];
  3952. 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"]];
  3953. DebugLog(@"offline_login sql:%@",sqlQuery);
  3954. sqlite3_stmt * statement;
  3955. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3956. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3957. {
  3958. if (sqlite3_step(statement) == SQLITE_ROW)
  3959. {
  3960. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3961. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3962. int can_show_price = sqlite3_column_int(statement, 0);
  3963. int can_see_price = sqlite3_column_int(statement, 1);
  3964. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3965. if(contact_id==nil)
  3966. contact_id="";
  3967. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3968. int user_type = sqlite3_column_int(statement, 3);
  3969. int can_cancel_order = sqlite3_column_int(statement, 4);
  3970. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3971. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3972. int can_delete_order = sqlite3_column_int(statement, 7);
  3973. int can_submit_order = sqlite3_column_int(statement, 8);
  3974. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3975. int can_create_order = sqlite3_column_int(statement, 10);
  3976. char *mode = (char*)sqlite3_column_text(statement, 11);
  3977. if(mode==nil)
  3978. mode="";
  3979. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3980. char *username = (char*)sqlite3_column_text(statement, 12);
  3981. if(username==nil)
  3982. username="";
  3983. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3984. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3985. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3986. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3987. [header setValue:nscontact_id forKey:@"contact_id"];
  3988. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3989. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3990. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3991. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3992. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3993. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3994. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3995. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3996. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3997. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3998. [header setValue:nsusername forKey:@"username"];
  3999. NSError* error=nil;
  4000. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4001. [header setValue:statusFilter forKey:@"statusFilter"];
  4002. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4003. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4004. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4005. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4006. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4007. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4008. [ret setObject:header forKey:@"header"];
  4009. [ret setValue:nsmode forKey:@"mode"];
  4010. }
  4011. sqlite3_finalize(statement);
  4012. }
  4013. [iSalesDB close_db:db];
  4014. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4015. return [RAConvertor dict2data:ret];
  4016. }
  4017. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4018. {
  4019. assert(params[@"mode"]!=nil);
  4020. NSString* contactId = [params valueForKey:@"contactId"];
  4021. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4022. sqlite3 *db = [iSalesDB get_db];
  4023. NSString *sqlQuery = nil;
  4024. {
  4025. 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];
  4026. }
  4027. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4028. sqlite3_stmt * statement;
  4029. [ret setValue:@"2" forKey:@"result"];
  4030. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4031. {
  4032. //int i = 0;
  4033. if (sqlite3_step(statement) == SQLITE_ROW)
  4034. {
  4035. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4036. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4037. // int editable = sqlite3_column_int(statement, 0);
  4038. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4039. NSString *nscompany_name =nil;
  4040. if(company_name==nil)
  4041. nscompany_name=@"";
  4042. else
  4043. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4044. char *country = (char*)sqlite3_column_text(statement, 2);
  4045. if(country==nil)
  4046. country="";
  4047. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4048. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4049. // if(addr==nil)
  4050. // addr="";
  4051. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4052. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4053. if(zipcode==nil)
  4054. zipcode="";
  4055. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4056. char *state = (char*)sqlite3_column_text(statement, 5);
  4057. if(state==nil)
  4058. state="";
  4059. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4060. char *city = (char*)sqlite3_column_text(statement, 6);
  4061. if(city==nil)
  4062. city="";
  4063. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4064. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4065. // NSString *nscontact_name = nil;
  4066. // if(contact_name==nil)
  4067. // nscontact_name=@"";
  4068. // else
  4069. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4070. char *phone = (char*)sqlite3_column_text(statement, 8);
  4071. NSString *nsphone = nil;
  4072. if(phone==nil)
  4073. nsphone=@"";
  4074. else
  4075. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4076. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4077. // if(contact_id==nil)
  4078. // contact_id="";
  4079. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4080. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4081. if(addr_1==nil)
  4082. addr_1="";
  4083. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4084. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4085. if(addr_2==nil)
  4086. addr_2="";
  4087. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4088. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4089. if(addr_3==nil)
  4090. addr_3="";
  4091. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4092. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4093. if(addr_4==nil)
  4094. addr_4="";
  4095. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4096. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4097. if(first_name==nil)
  4098. first_name="";
  4099. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4100. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4101. if(last_name==nil)
  4102. last_name="";
  4103. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4104. char *fax = (char*)sqlite3_column_text(statement, 16);
  4105. NSString *nsfax = nil;
  4106. if(fax==nil)
  4107. nsfax=@"";
  4108. else
  4109. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4110. char *email = (char*)sqlite3_column_text(statement, 17);
  4111. NSString *nsemail = nil;
  4112. if(email==nil)
  4113. nsemail=@"";
  4114. else
  4115. nsemail= [[NSString alloc]initWithUTF8String:email];
  4116. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4117. NSString *nsimg_0 = nil;
  4118. if(img_0==nil)
  4119. nsimg_0=@"";
  4120. else
  4121. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4122. [self copy_bcardImg:nsimg_0];
  4123. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4124. NSString *nsimg_1 = nil;
  4125. if(img_1==nil)
  4126. nsimg_1=@"";
  4127. else
  4128. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4129. [self copy_bcardImg:nsimg_1];
  4130. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4131. NSString *nsimg_2 = nil;
  4132. if(img_2==nil)
  4133. nsimg_2=@"";
  4134. else
  4135. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4136. [self copy_bcardImg:nsimg_2];
  4137. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4138. NSString *nsprice_type = nil;
  4139. if(price_type==nil)
  4140. nsprice_type=@"";
  4141. else
  4142. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4143. char *notes = (char*)sqlite3_column_text(statement, 22);
  4144. NSString *nsnotes = nil;
  4145. if(notes==nil)
  4146. nsnotes=@"";
  4147. else
  4148. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4149. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4150. NSString *nssalesrep = nil;
  4151. if(salesrep==nil)
  4152. nssalesrep=@"";
  4153. else
  4154. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4155. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4156. {
  4157. // decrypt
  4158. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4159. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4160. nsphone=[AESCrypt fastdecrypt:nsphone];
  4161. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4162. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4163. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4164. }
  4165. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4166. [arr_name addObject:nsfirst_name];
  4167. [arr_name addObject:nslast_name];
  4168. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4169. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4170. [arr_ext addObject:nsaddr_1];
  4171. [arr_ext addObject:nsaddr_2];
  4172. [arr_ext addObject:nsaddr_3];
  4173. [arr_ext addObject:nsaddr_4];
  4174. [arr_ext addObject:@"\r\n"];
  4175. [arr_ext addObject:nscity];
  4176. [arr_ext addObject:nsstate];
  4177. [arr_ext addObject:nszipcode];
  4178. [arr_ext addObject:nscountry];
  4179. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4180. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4181. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4182. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4183. [item setValue:nscountry forKey:@"customer_country"];
  4184. [item setValue:nsphone forKey:@"customer_phone"];
  4185. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4186. [item setValue:nscompany_name forKey:@"customer_name"];
  4187. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4188. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4189. [item setValue:nsext forKey:@"customer_contact_ext"];
  4190. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4191. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4192. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4193. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4194. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4195. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4196. [item setValue:nslast_name forKey:@"customer_last_name"];
  4197. [item setValue:nscity forKey:@"customer_city"];
  4198. [item setValue:nsstate forKey:@"customer_state"];
  4199. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4200. [item setValue:contactId forKey:@"customer_cid"];
  4201. [item setValue:nscontact_name forKey:@"customer_contact"];
  4202. [item setValue:nsfax forKey:@"customer_fax"];
  4203. [item setValue:nsemail forKey:@"customer_email"];
  4204. [item setValue:contact_type forKey:@"customer_contact_type"];
  4205. [ret setObject:item forKey:@"customerInfo"];
  4206. // i++;
  4207. }
  4208. // UIApplication * app = [UIApplication sharedApplication];
  4209. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4210. [ret setValue:params[@"mode"] forKey:@"mode"];
  4211. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4212. sqlite3_finalize(statement);
  4213. }
  4214. [iSalesDB close_db:db];
  4215. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4216. return ret;
  4217. }
  4218. + (bool) copy_bcardImg:(NSString*) filename
  4219. {
  4220. if(filename.length==0)
  4221. return false;
  4222. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4223. bool ret=false;
  4224. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4225. NSString *cachefolder = [paths objectAtIndex:0];
  4226. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4227. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4228. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4229. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4230. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4231. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4232. //
  4233. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4234. NSFileManager* fileManager = [NSFileManager defaultManager];
  4235. BOOL bdir=NO;
  4236. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4237. {
  4238. NSError *error = nil;
  4239. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4240. {
  4241. ret=false;
  4242. }
  4243. else
  4244. {
  4245. ret=true;
  4246. }
  4247. // NSError *error = nil;
  4248. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4249. //
  4250. // if(!bsuccess)
  4251. // {
  4252. // DebugLog(@"Create offline_createimg folder failed");
  4253. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4254. // return [RAConvertor dict2data:ret];
  4255. // }
  4256. // if(bsuccess)
  4257. // {
  4258. // sqlite3 *db = [self get_db];
  4259. //
  4260. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4261. // [iSalesDB close_db:db];
  4262. // }
  4263. }
  4264. return ret;
  4265. //
  4266. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4267. // if(bsuccess)
  4268. // {
  4269. // NSError *error = nil;
  4270. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4271. // {
  4272. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4273. // }
  4274. // else
  4275. // {
  4276. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4277. //
  4278. // ret[@"img_url_aname"]=filename;
  4279. // ret[@"img_url"]=savedImagePath;
  4280. // }
  4281. // }
  4282. }
  4283. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4284. {
  4285. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4286. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4287. NSString *cachefolder = [paths objectAtIndex:0];
  4288. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4289. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4290. NSFileManager* fileManager = [NSFileManager defaultManager];
  4291. BOOL bdir=YES;
  4292. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4293. {
  4294. NSError *error = nil;
  4295. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4296. if(!bsuccess)
  4297. {
  4298. DebugLog(@"Create offline_createimg folder failed");
  4299. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4300. return [RAConvertor dict2data:ret];
  4301. }
  4302. // if(bsuccess)
  4303. // {
  4304. // sqlite3 *db = [self get_db];
  4305. //
  4306. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4307. // [iSalesDB close_db:db];
  4308. // }
  4309. }
  4310. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4311. {
  4312. NSError *error = nil;
  4313. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4314. if(!bsuccess)
  4315. {
  4316. DebugLog(@"Create img_cache folder failed");
  4317. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4318. return [RAConvertor dict2data:ret];
  4319. }
  4320. // if(bsuccess)
  4321. // {
  4322. // sqlite3 *db = [self get_db];
  4323. //
  4324. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4325. // [iSalesDB close_db:db];
  4326. // }
  4327. }
  4328. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4329. //JEPG格式
  4330. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4331. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  4332. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4333. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4334. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4335. if(bsuccess)
  4336. {
  4337. NSError *error = nil;
  4338. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4339. {
  4340. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4341. }
  4342. else
  4343. {
  4344. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4345. ret[@"img_url_aname"]=filename;
  4346. ret[@"img_url"]=filename;
  4347. }
  4348. }
  4349. else
  4350. {
  4351. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4352. }
  4353. return [RAConvertor dict2data:ret];
  4354. }
  4355. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4356. {
  4357. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4358. category = [category substringToIndex:3];
  4359. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4360. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4361. params[@"category"]= category;
  4362. ret[@"params"]= params;
  4363. [ret setValue:@"detail" forKey:@"target"];
  4364. [ret setValue:@"popup" forKey:@"action"];
  4365. [ret setValue:@"content" forKey:@"type"];
  4366. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4367. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4368. [ret setValue:@"true" forKey:@"single_row"];
  4369. [ret setValue:@"true" forKey:@"partial_refresh"];
  4370. // sqlite3 *db = [iSalesDB get_db];
  4371. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4372. NSString *sqlQuery =nil;
  4373. #ifdef BUILD_NPD
  4374. 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 ;
  4375. #else
  4376. 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];
  4377. #endif
  4378. sqlite3_stmt * statement;
  4379. int count = 0;
  4380. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4381. // int count=0;
  4382. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4383. {
  4384. int i=0;
  4385. while (sqlite3_step(statement) == SQLITE_ROW)
  4386. {
  4387. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4388. // char *name = (char*)sqlite3_column_text(statement, 1);
  4389. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4390. char *name = (char*)sqlite3_column_text(statement, 0);
  4391. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4392. int product_id = sqlite3_column_int(statement, 1);
  4393. char *url = (char*)sqlite3_column_text(statement, 2);
  4394. if(url==nil)
  4395. url="";
  4396. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4397. [item setValue:nsurl forKey:@"picture_path"];
  4398. [item setValue:nsname forKey:@"fash_name"];
  4399. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4400. [item setValue:category forKey:@"category"];
  4401. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4402. i++;
  4403. }
  4404. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4405. sqlite3_finalize(statement);
  4406. }
  4407. DebugLog(@"count:%d",count);
  4408. // [iSalesDB close_db:db];
  4409. return ret;
  4410. }
  4411. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4412. {
  4413. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4414. [ret setValue:key forKey:@"key"];
  4415. [ret setValue:value forKey:@"val"];
  4416. [ret setValue:@"price" forKey:@"type"];
  4417. return ret;
  4418. }
  4419. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4420. {
  4421. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4422. [ret setValue:key forKey:@"key"];
  4423. [ret setValue:value forKey:@"val"];
  4424. return ret;
  4425. }
  4426. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4427. {
  4428. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4429. [ret setValue:@"0" forKey:@"img_count"];
  4430. // sqlite3 *db = [iSalesDB get_db];
  4431. 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 ;
  4432. sqlite3_stmt * statement;
  4433. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4434. {
  4435. int i=0;
  4436. if (sqlite3_step(statement) == SQLITE_ROW)
  4437. {
  4438. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4439. // char *name = (char*)sqlite3_column_text(statement, 1);
  4440. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4441. char *value = (char*)sqlite3_column_text(statement, 0);
  4442. if(value==nil)
  4443. value="";
  4444. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4445. char *key = (char*)sqlite3_column_text(statement, 1);
  4446. if(key==nil)
  4447. key="";
  4448. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4449. [item setValue:nsvalue forKey:@"val"];
  4450. [item setValue:nskey forKey:@"key"];
  4451. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4452. [ret setValue:@"1" forKey:@"count"];
  4453. i++;
  4454. }
  4455. sqlite3_finalize(statement);
  4456. }
  4457. // [iSalesDB close_db:db];
  4458. return ret;
  4459. }
  4460. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4461. {
  4462. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4463. [ret setValue:@"0" forKey:@"count"];
  4464. // sqlite3 *db = [iSalesDB get_db];
  4465. 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;
  4466. sqlite3_stmt * statement;
  4467. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4468. {
  4469. int i=0;
  4470. while (sqlite3_step(statement) == SQLITE_ROW)
  4471. {
  4472. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4473. // char *name = (char*)sqlite3_column_text(statement, 1);
  4474. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4475. char *value = (char*)sqlite3_column_text(statement, 0);
  4476. if(value==nil)
  4477. value="";
  4478. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4479. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4480. if(selector_display==nil)
  4481. selector_display="";
  4482. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4483. int product_id = sqlite3_column_int(statement, 2);
  4484. char *category = (char*)sqlite3_column_text(statement, 3);
  4485. if(category==nil)
  4486. category="";
  4487. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4488. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4489. [item setValue:nsvalue forKey:@"title"];
  4490. [item setValue:url forKey:@"pic_url"];
  4491. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4492. [params setValue:@"2" forKey:@"count"];
  4493. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4494. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4495. [param0 setValue:@"product_id" forKey:@"name"];
  4496. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4497. [param1 setValue:nscategory forKey:@"val"];
  4498. [param1 setValue:@"category" forKey:@"name"];
  4499. [params setObject:param0 forKey:@"param_0"];
  4500. [params setObject:param1 forKey:@"param_1"];
  4501. [item setObject:params forKey:@"params"];
  4502. [ret setValue:nsselector_display forKey:@"name"];
  4503. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4504. i++;
  4505. }
  4506. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4507. [ret setValue:@"switch" forKey:@"action"];
  4508. sqlite3_finalize(statement);
  4509. }
  4510. // [iSalesDB close_db:db];
  4511. return ret;
  4512. }
  4513. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4514. {
  4515. // model 在 category search 显示的图片。
  4516. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4517. // sqlite3 *db = [iSalesDB get_db];
  4518. 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];
  4519. sqlite3_stmt * statement;
  4520. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4521. {
  4522. while (sqlite3_step(statement) == SQLITE_ROW)
  4523. {
  4524. char *url = (char*)sqlite3_column_text(statement, 0);
  4525. if(url==nil)
  4526. url="";
  4527. int type = sqlite3_column_int(statement, 1);
  4528. if(type==0)
  4529. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4530. else
  4531. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4532. }
  4533. sqlite3_finalize(statement);
  4534. }
  4535. // [iSalesDB close_db:db];
  4536. return ret;
  4537. }
  4538. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4539. {
  4540. int item_id=-1;
  4541. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4542. sqlite3_stmt * statement;
  4543. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4544. {
  4545. if (sqlite3_step(statement) == SQLITE_ROW)
  4546. {
  4547. item_id = sqlite3_column_int(statement, 0);
  4548. }
  4549. sqlite3_finalize(statement);
  4550. }
  4551. return item_id;
  4552. }
  4553. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4554. {
  4555. // NSString* ret = @"";
  4556. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4557. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4558. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4559. sqlite3_stmt * statement;
  4560. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4561. {
  4562. while (sqlite3_step(statement) == SQLITE_ROW)
  4563. {
  4564. int bitem_id = sqlite3_column_int(statement, 0);
  4565. int bitem_qty = sqlite3_column_int(statement, 1);
  4566. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4567. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4568. }
  4569. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4570. sqlite3_finalize(statement);
  4571. }
  4572. // if(ret==nil)
  4573. // ret=@"";
  4574. *count=arr_count;
  4575. return arr_bundle;
  4576. }
  4577. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4578. {
  4579. // get default sold qty, return -1 if model not found;
  4580. int ret = -1;
  4581. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4582. sqlite3_stmt * statement;
  4583. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4584. {
  4585. if (sqlite3_step(statement) == SQLITE_ROW)
  4586. {
  4587. ret = sqlite3_column_int(statement, 0);
  4588. }
  4589. sqlite3_finalize(statement);
  4590. }
  4591. return ret;
  4592. }
  4593. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4594. {
  4595. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4596. [ret setValue:@"0" forKey:@"img_count"];
  4597. // sqlite3 *db = [iSalesDB get_db];
  4598. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4599. sqlite3_stmt * statement;
  4600. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4601. {
  4602. int i=0;
  4603. while (sqlite3_step(statement) == SQLITE_ROW)
  4604. {
  4605. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4606. // char *name = (char*)sqlite3_column_text(statement, 1);
  4607. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4608. char *url = (char*)sqlite3_column_text(statement, 0);
  4609. if(url==nil)
  4610. url="";
  4611. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4612. [item setValue:nsurl forKey:@"s"];
  4613. [item setValue:nsurl forKey:@"l"];
  4614. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4615. i++;
  4616. }
  4617. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4618. sqlite3_finalize(statement);
  4619. }
  4620. // [iSalesDB close_db:db];
  4621. return ret;
  4622. }
  4623. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4624. // UIApplication * app = [UIApplication sharedApplication];
  4625. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4626. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4627. sqlite3 *db = [iSalesDB get_db];
  4628. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4629. NSString* product_id=params[@"product_id"];
  4630. NSString *item_count_str = params[@"item_count"];
  4631. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4632. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4633. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4634. // NSString *sql = @"";
  4635. for(int i=0;i<arr.count;i++)
  4636. {
  4637. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4638. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4639. __block int cart_count = 0;
  4640. if (!item_count_str) {
  4641. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4642. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4643. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4644. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4645. cart_count = [[model_set_components lastObject] intValue];
  4646. }];
  4647. }
  4648. if(count==0)
  4649. {
  4650. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4651. sqlite3_stmt *stmt;
  4652. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4653. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4654. if (item_count_arr) {
  4655. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4656. } else {
  4657. sqlite3_bind_int(stmt,2,cart_count);
  4658. }
  4659. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4660. [iSalesDB execSql:@"ROLLBACK" db:db];
  4661. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4662. [iSalesDB close_db:db];
  4663. DebugLog(@"add to wishlist error");
  4664. return ret;
  4665. }
  4666. } else {
  4667. int qty = 0;
  4668. if (item_count_arr) {
  4669. qty = [item_count_arr[i] intValue];
  4670. } else {
  4671. qty = cart_count;
  4672. }
  4673. 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]];
  4674. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4675. [iSalesDB execSql:@"ROLLBACK" db:db];
  4676. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4677. [iSalesDB close_db:db];
  4678. DebugLog(@"add to wishlist error");
  4679. return ret;
  4680. }
  4681. }
  4682. }
  4683. // [iSalesDB execSql:sql db:db];
  4684. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4685. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4686. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4687. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4688. [iSalesDB close_db:db];
  4689. ret[@"wish_count"]=@(count);
  4690. ret[@"result"]= [NSNumber numberWithInt:2];
  4691. return ret;
  4692. }
  4693. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4694. // 0 category
  4695. // 1 search
  4696. // 2 itemsearch
  4697. NSData *ret = nil;
  4698. NSDictionary *items = nil;
  4699. switch (from) {
  4700. case 0:{
  4701. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4702. }
  4703. break;
  4704. case 1:{
  4705. items = [[self search:params limited:NO] objectForKey:@"items"];
  4706. }
  4707. break;
  4708. case 2:{
  4709. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4710. }
  4711. break;
  4712. default:
  4713. break;
  4714. }
  4715. if (!items) {
  4716. return ret;
  4717. }
  4718. int count = [[items objectForKey:@"count"] intValue];
  4719. NSMutableString *product_id_str = [@"" mutableCopy];
  4720. for (int i = 0; i < count; i++) {
  4721. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4722. NSDictionary *item = [items objectForKey:key];
  4723. NSString *product_id = [item objectForKey:@"product_id"];
  4724. if (i == 0) {
  4725. [product_id_str appendString:product_id];
  4726. } else {
  4727. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4728. }
  4729. }
  4730. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4731. if ([add_to isEqualToString:@"cart"]) {
  4732. NSString *order_code = [params objectForKey:@"orderCode"];
  4733. if (order_code.length) {
  4734. NSDictionary *newParams = @{
  4735. @"product_id" : product_id_str,
  4736. @"orderCode" : order_code,
  4737. @"can_create_backorder":params[@"can_create_backorder"]
  4738. };
  4739. ret = [self offline_add2cart:newParams.mutableCopy];
  4740. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4741. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4742. ret = [RAConvertor dict2data:retDic];
  4743. }
  4744. } else if([add_to isEqualToString:@"wishlist"]) {
  4745. NSDictionary *newParams = @{
  4746. @"product_id" : product_id_str
  4747. };
  4748. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4749. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4750. ret = [RAConvertor dict2data:retDic];
  4751. } else if([add_to isEqualToString:@"portfolio"]) {
  4752. NSDictionary *newParams = @{
  4753. @"product_id" : product_id_str
  4754. };
  4755. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4756. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4757. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4758. ret = [RAConvertor dict2data:retDic];
  4759. }
  4760. return ret;
  4761. }
  4762. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4763. {
  4764. return [self addAll:params from:0];
  4765. }
  4766. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4767. {
  4768. return [self addAll:params from:1];
  4769. }
  4770. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4771. {
  4772. return [self addAll:params from:2];
  4773. }
  4774. #pragma mark - Jack
  4775. #warning 做SQL操作时转义!!
  4776. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4777. // "val_227" : {
  4778. // "check" : 1,
  4779. // "value" : "US United States",
  4780. // "value_id" : "228"
  4781. // },
  4782. if (!countryCode) {
  4783. countryCode = @"US";
  4784. }
  4785. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4786. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4787. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4788. int code_id = sqlite3_column_int(stmt, 3); // id
  4789. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4790. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4791. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4792. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4793. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4794. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4795. }
  4796. long n = *count;
  4797. *count = n + 1;
  4798. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4799. [container setValue:countryDic forKey:key];
  4800. }] mutableCopy];
  4801. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4802. return ret;
  4803. }
  4804. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4805. countryCode = [self translateSingleQuote:countryCode];
  4806. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4807. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4808. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4809. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4810. if (name == NULL) {
  4811. name = "";
  4812. }
  4813. if (code == NULL) {
  4814. code = "";
  4815. }
  4816. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4817. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4818. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4819. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4820. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4821. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4822. }
  4823. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4824. [container setValue:stateDic forKey:key];
  4825. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4826. DebugLog(@"query all state error: %@",err_msg);
  4827. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4828. // [stateDic setValue:@"Other" forKey:@"value"];
  4829. // [stateDic setValue:@"" forKey:@"value_id"];
  4830. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4831. //
  4832. // if (state_code && [@"" isEqualToString:state_code]) {
  4833. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4834. // }
  4835. //
  4836. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4837. // [container setValue:stateDic forKey:key];
  4838. }] mutableCopy];
  4839. [ret removeObjectForKey:@"result"];
  4840. // failure 可以不用了,一样的
  4841. if (ret.allKeys.count == 0) {
  4842. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4843. [stateDic setValue:@"Other" forKey:@"value"];
  4844. [stateDic setValue:@"" forKey:@"value_id"];
  4845. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4846. if (state_code && [@"" isEqualToString:state_code]) {
  4847. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4848. }
  4849. NSString *key = [NSString stringWithFormat:@"val_0"];
  4850. [ret setValue:stateDic forKey:key];
  4851. }
  4852. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4853. return ret;
  4854. }
  4855. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4856. codeId = [self translateSingleQuote:codeId];
  4857. 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];
  4858. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4859. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4860. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4861. if (name == NULL) {
  4862. name = "";
  4863. }
  4864. if (code == NULL) {
  4865. code = "";
  4866. }
  4867. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4868. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4869. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4870. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4871. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4872. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4873. }
  4874. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4875. [container setValue:stateDic forKey:key];
  4876. }] mutableCopy];
  4877. [ret removeObjectForKey:@"result"];
  4878. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4879. return ret;
  4880. }
  4881. + (NSDictionary *)offline_getPrice {
  4882. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4883. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4884. char *name = (char *) sqlite3_column_text(stmt, 1);
  4885. int type = sqlite3_column_int(stmt, 2);
  4886. int orderBy = sqlite3_column_int(stmt, 3);
  4887. if (name == NULL) {
  4888. name = "";
  4889. }
  4890. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4891. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4892. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4893. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4894. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4895. if (orderBy == 0) {
  4896. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4897. }
  4898. [container setValue:priceDic forKey:key];
  4899. }] mutableCopy];
  4900. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4901. return ret;
  4902. }
  4903. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4904. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4905. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4906. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4907. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4908. // int _id = sqlite3_column_int(stmt, 0);
  4909. NSString *name = [self textAtColumn:1 statement:stmt];
  4910. NSDictionary *typeDic = @{
  4911. @"value_id" : name,
  4912. @"value" : name,
  4913. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4914. };
  4915. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4916. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  4917. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4918. }];
  4919. return ret;
  4920. }
  4921. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  4922. // 首先从offline_login表中取出sales_code
  4923. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4924. // NSString *user = app.user;
  4925. user = [self translateSingleQuote:user];
  4926. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4927. __block NSString *user_code = @"";
  4928. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4929. char *code = (char *)sqlite3_column_text(stmt, 0);
  4930. if (code == NULL) {
  4931. code = "";
  4932. }
  4933. user_code = [NSString stringWithUTF8String:code];
  4934. }];
  4935. // 再取所有salesRep
  4936. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4937. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4938. // 1 name 2 code 3 salesrep_id
  4939. char *name = (char *)sqlite3_column_text(stmt, 1);
  4940. char *code = (char *)sqlite3_column_text(stmt, 2);
  4941. int salesrep_id = sqlite3_column_int(stmt, 3);
  4942. if (name == NULL) {
  4943. name = "";
  4944. }
  4945. if (code == NULL) {
  4946. code = "";
  4947. }
  4948. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4949. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4950. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4951. // 比较code 相等则check
  4952. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4953. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4954. }
  4955. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4956. }] mutableCopy];
  4957. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4958. return ret;
  4959. }
  4960. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4961. zipcode = [self translateSingleQuote:zipcode];
  4962. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4963. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4964. char *country = (char *)sqlite3_column_text(stmt, 0);
  4965. char *state = (char *)sqlite3_column_text(stmt, 1);
  4966. char *city = (char *)sqlite3_column_text(stmt, 2);
  4967. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4968. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4969. if (country == NULL) {
  4970. country = "";
  4971. }
  4972. if (state == NULL) {
  4973. state = "";
  4974. }
  4975. if (city == NULL) {
  4976. city = "";
  4977. }
  4978. if (country_code == NULL) {
  4979. country_code = "";
  4980. }
  4981. if (state_code == NULL) {
  4982. state_code = "";
  4983. }
  4984. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4985. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4986. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4987. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4988. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4989. }] mutableCopy];
  4990. return ret;
  4991. }
  4992. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4993. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4994. [item setValue:value forKey:valueKey];
  4995. [dic setValue:item forKey:itemKey];
  4996. }
  4997. + (NSString *)countryCodeByid:(NSString *)code_id {
  4998. NSString *ret = nil;
  4999. code_id = [self translateSingleQuote:code_id];
  5000. sqlite3 *db = [iSalesDB get_db];
  5001. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5002. sqlite3_stmt * statement;
  5003. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5004. while (sqlite3_step(statement) == SQLITE_ROW) {
  5005. char *code = (char *)sqlite3_column_text(statement, 0);
  5006. if (code == NULL) {
  5007. code = "";
  5008. }
  5009. ret = [NSString stringWithUTF8String:code];
  5010. }
  5011. sqlite3_finalize(statement);
  5012. }
  5013. [iSalesDB close_db:db];
  5014. return ret;
  5015. }
  5016. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5017. NSString *ret = nil;
  5018. code = [self translateSingleQuote:code];
  5019. sqlite3 *db = [iSalesDB get_db];
  5020. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5021. sqlite3_stmt * statement;
  5022. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5023. while (sqlite3_step(statement) == SQLITE_ROW) {
  5024. char *_id = (char *)sqlite3_column_text(statement, 0);
  5025. if (_id == NULL) {
  5026. _id = "";
  5027. }
  5028. ret = [NSString stringWithFormat:@"%s",_id];
  5029. }
  5030. sqlite3_finalize(statement);
  5031. }
  5032. [iSalesDB close_db:db];
  5033. return ret;
  5034. }
  5035. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5036. NSString *name = nil;
  5037. codeId = [self translateSingleQuote:codeId];
  5038. sqlite3 *db = [iSalesDB get_db];
  5039. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5040. sqlite3_stmt * statement;
  5041. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5042. while (sqlite3_step(statement) == SQLITE_ROW) {
  5043. char *value = (char *)sqlite3_column_text(statement, 0);
  5044. if (value == NULL) {
  5045. value = "";
  5046. }
  5047. name = [NSString stringWithUTF8String:value];
  5048. }
  5049. sqlite3_finalize(statement);
  5050. }
  5051. [iSalesDB close_db:db];
  5052. return name;
  5053. }
  5054. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5055. NSString *ret = nil;
  5056. sqlite3 *db = [iSalesDB get_db];
  5057. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5058. sqlite3_stmt * statement;
  5059. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5060. while (sqlite3_step(statement) == SQLITE_ROW) {
  5061. char *name = (char *)sqlite3_column_text(statement, 0);
  5062. if (name == NULL) {
  5063. name = "";
  5064. }
  5065. ret = [NSString stringWithUTF8String:name];
  5066. }
  5067. sqlite3_finalize(statement);
  5068. }
  5069. [iSalesDB close_db:db];
  5070. return ret;
  5071. }
  5072. + (NSString *)salesRepCodeById:(NSString *)_id {
  5073. NSString *ret = nil;
  5074. _id = [self translateSingleQuote:_id];
  5075. sqlite3 *db = [iSalesDB get_db];
  5076. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5077. sqlite3_stmt * statement;
  5078. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5079. while (sqlite3_step(statement) == SQLITE_ROW) {
  5080. char *rep = (char *)sqlite3_column_text(statement, 0);
  5081. if (rep == NULL) {
  5082. rep = "";
  5083. }
  5084. ret = [NSString stringWithUTF8String:rep];
  5085. }
  5086. sqlite3_finalize(statement);
  5087. }
  5088. [iSalesDB close_db:db];
  5089. return ret;
  5090. }
  5091. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5092. char *tx = (char *)sqlite3_column_text(stmt, col);
  5093. if (tx == NULL) {
  5094. tx = "";
  5095. }
  5096. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5097. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5098. if (!text) {
  5099. text = @"";
  5100. }
  5101. // 将字符全部为' '的字符串干掉
  5102. int spaceCount = 0;
  5103. for (int i = 0; i < text.length; i++) {
  5104. if ([text characterAtIndex:i] == ' ') {
  5105. spaceCount++;
  5106. }
  5107. }
  5108. if (spaceCount == text.length) {
  5109. text = @"";
  5110. }
  5111. return text;
  5112. }
  5113. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5114. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5115. NSData *data = [NSData dataWithContentsOfFile:path];
  5116. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5117. return ret;
  5118. }
  5119. + (NSString *)textFileName:(NSString *)name {
  5120. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5121. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5122. if (!text) {
  5123. text = @"";
  5124. }
  5125. return text;
  5126. }
  5127. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5128. return [[dic objectForKey:key] mutableCopy];
  5129. }
  5130. + (id)translateSingleQuote:(NSString *)string {
  5131. if ([string isKindOfClass:[NSString class]])
  5132. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5133. return string;
  5134. }
  5135. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5136. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5137. }
  5138. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5139. NSString* ret= nil;
  5140. NSString *sqlQuery = nil;
  5141. // 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
  5142. 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];
  5143. sqlite3_stmt * statement;
  5144. // int count=0;
  5145. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5146. {
  5147. if (sqlite3_step(statement) == SQLITE_ROW)
  5148. {
  5149. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5150. if(imgurl==nil)
  5151. imgurl="";
  5152. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5153. ret=nsimgurl;
  5154. }
  5155. sqlite3_finalize(statement);
  5156. }
  5157. else
  5158. {
  5159. [ret setValue:@"8" forKey:@"result"];
  5160. }
  5161. // [iSalesDB close_db:db];
  5162. // DebugLog(@"data string: %@",ret );
  5163. return ret;
  5164. }
  5165. #pragma mark contact Advanced search
  5166. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5167. {
  5168. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5169. return [RAConvertor dict2data:contactAdvanceDic];
  5170. }
  5171. #pragma mark create new contact
  5172. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5173. assert(params[@"user"]!=nil);
  5174. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5175. NSData *data = [NSData dataWithContentsOfFile:path];
  5176. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5177. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5178. NSString *countryCode = nil;
  5179. NSString *countryCode_id = nil;
  5180. NSString *stateCode = nil;
  5181. NSString *city = nil;
  5182. NSString *zipCode = nil;
  5183. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5184. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5185. NSString *code_id = params[@"country"];
  5186. countryCode_id = code_id;
  5187. countryCode = [self countryCodeByid:code_id];
  5188. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5189. NSString *zip_code = params[@"zipcode"];
  5190. // 剔除全部为空格
  5191. int spaceCount = 0;
  5192. for (int i = 0; i < zip_code.length; i++) {
  5193. if ([zip_code characterAtIndex:i] == ' ') {
  5194. spaceCount++;
  5195. }
  5196. }
  5197. if (spaceCount == zip_code.length) {
  5198. zip_code = @"";
  5199. }
  5200. zipCode = zip_code;
  5201. if (zipCode.length > 0) {
  5202. countryCode_id = params[@"country"];
  5203. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5204. countryCode = [dic valueForKey:@"country_code"];
  5205. if (!countryCode) {
  5206. // countryCode = @"US";
  5207. NSString *code_id = params[@"country"];
  5208. countryCode = [self countryCodeByid:code_id];
  5209. }
  5210. stateCode = [dic valueForKey:@"state_code"];
  5211. if (!stateCode.length) {
  5212. stateCode = params[@"state"];
  5213. }
  5214. city = [dic valueForKey:@"city"];
  5215. if (!city.length) {
  5216. city = params[@"city"];
  5217. }
  5218. // zip code
  5219. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5220. [zipDic setValue:zipCode forKey:@"value"];
  5221. [section_0 setValue:zipDic forKey:@"item_11"];
  5222. } else {
  5223. NSString *code_id = params[@"country"];
  5224. countryCode = [self countryCodeByid:code_id];
  5225. stateCode = params[@"state"];
  5226. city = params[@"city"];
  5227. }
  5228. }
  5229. } else {
  5230. // default: US United States
  5231. countryCode = @"US";
  5232. countryCode_id = @"228";
  5233. }
  5234. // country
  5235. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5236. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5237. // state
  5238. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5239. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5240. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5241. // city
  5242. if (city) {
  5243. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5244. [cityDic setValue:city forKey:@"value"];
  5245. [section_0 setValue:cityDic forKey:@"item_13"];
  5246. }
  5247. // price type
  5248. NSDictionary *priceDic = [self offline_getPrice];
  5249. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5250. // contact type
  5251. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5252. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5253. // Sales Rep
  5254. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5255. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5256. [ret setValue:section_0 forKey:@"section_0"];
  5257. return [RAConvertor dict2data:ret];
  5258. }
  5259. #pragma mark save
  5260. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5261. NSString *addr = nil;
  5262. NSString *contact_name = nil;
  5263. NSString *companyName = [params objectForKey:@"company"];
  5264. if (companyName) {
  5265. companyName = [AESCrypt fastencrypt:companyName];
  5266. } else {
  5267. companyName = @"";
  5268. }
  5269. DebugLog(@"company");
  5270. companyName = [self translateSingleQuote:companyName];
  5271. NSString *addr1 = [params objectForKey:@"address"];
  5272. NSString *addr2 = [params objectForKey:@"address2"];
  5273. NSString *addr3 = [params objectForKey:@"address_3"];
  5274. NSString *addr4 = [params objectForKey:@"address_4"];
  5275. if (!addr2) {
  5276. addr2 = @"";
  5277. }
  5278. if (!addr3) {
  5279. addr3 = @"";
  5280. }
  5281. if (!addr4) {
  5282. addr4 = @"";
  5283. }
  5284. if (!addr1) {
  5285. addr1 = @"";
  5286. }
  5287. DebugLog(@"addr");
  5288. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5289. addr = [AESCrypt fastencrypt:addr];
  5290. addr = [self translateSingleQuote:addr];
  5291. if (addr1 && ![addr1 isEqualToString:@""]) {
  5292. addr1 = [AESCrypt fastencrypt:addr1];
  5293. }
  5294. addr1 = [self translateSingleQuote:addr1];
  5295. addr2 = [self translateSingleQuote:addr2];
  5296. addr3 = [self translateSingleQuote:addr3];
  5297. addr4 = [self translateSingleQuote:addr4];
  5298. NSString *country = [params objectForKey:@"country"];
  5299. if (country) {
  5300. country = [self countryNameByCountryCodeId:country];
  5301. } else {
  5302. country = @"";
  5303. }
  5304. DebugLog(@"country");
  5305. country = [self translateSingleQuote:country];
  5306. NSString *state = [params objectForKey:@"state"];
  5307. if (!state) {
  5308. state = @"";
  5309. }
  5310. DebugLog(@"state");
  5311. state = [self translateSingleQuote:state];
  5312. NSString *city = [params objectForKey:@"city"];
  5313. if (!city) {
  5314. city = @"";
  5315. }
  5316. city = [self translateSingleQuote:city];
  5317. NSString *zipcode = [params objectForKey:@"zipcode"];
  5318. if (!zipcode) {
  5319. zipcode = @"";
  5320. }
  5321. DebugLog(@"zip");
  5322. zipcode = [self translateSingleQuote:zipcode];
  5323. NSString *fistName = [params objectForKey:@"firstname"];
  5324. if (!fistName) {
  5325. fistName = @"";
  5326. }
  5327. NSString *lastName = [params objectForKey:@"lastname"];
  5328. if (!lastName) {
  5329. lastName = @"";
  5330. }
  5331. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5332. DebugLog(@"contact_name");
  5333. contact_name = [self translateSingleQuote:contact_name];
  5334. fistName = [self translateSingleQuote:fistName];
  5335. lastName = [self translateSingleQuote:lastName];
  5336. NSString *phone = [params objectForKey:@"phone"];
  5337. if (phone) {
  5338. phone = [AESCrypt fastencrypt:phone];
  5339. } else {
  5340. phone = @"";
  5341. }
  5342. DebugLog(@"PHONE");
  5343. phone = [self translateSingleQuote:phone];
  5344. NSString *fax = [params objectForKey:@"fax"];
  5345. if (!fax) {
  5346. fax = @"";
  5347. }
  5348. DebugLog(@"FAX");
  5349. fax = [self translateSingleQuote:fax];
  5350. NSString *email = [params objectForKey:@"email"];
  5351. if (!email) {
  5352. email = @"";
  5353. }
  5354. DebugLog(@"EMAIL:%@",email);
  5355. email = [self translateSingleQuote:email];
  5356. NSString *notes = [params objectForKey:@"contact_notes"];
  5357. if (!notes) {
  5358. notes = @"";
  5359. }
  5360. DebugLog(@"NOTE:%@",notes);
  5361. notes = [self translateSingleQuote:notes];
  5362. NSString *price = [params objectForKey:@"price_name"];
  5363. if (price) {
  5364. price = [self priceNameByPriceId:price];
  5365. } else {
  5366. price = @"";
  5367. }
  5368. DebugLog(@"PRICE");
  5369. price = [self translateSingleQuote:price];
  5370. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5371. if (salesRep) {
  5372. salesRep = [self salesRepCodeById:salesRep];
  5373. } else {
  5374. salesRep = @"";
  5375. }
  5376. salesRep = [self translateSingleQuote:salesRep];
  5377. NSString *img = [params objectForKey:@"business_card"];
  5378. NSArray *array = [img componentsSeparatedByString:@","];
  5379. NSString *img_0 = array[0];
  5380. if (!img_0) {
  5381. img_0 = @"";
  5382. }
  5383. img_0 = [self translateSingleQuote:img_0];
  5384. NSString *img_1 = array[1];
  5385. if (!img_1) {
  5386. img_1 = @"";
  5387. }
  5388. img_1 = [self translateSingleQuote:img_1];
  5389. NSString *img_2 = array[2];
  5390. if (!img_2) {
  5391. img_2 = @"";
  5392. }
  5393. img_2 = [self translateSingleQuote:img_2];
  5394. NSString *contact_id = [NSUUID UUID].UUIDString;
  5395. NSString *contact_type = [params objectForKey:@"type_name"];
  5396. if (!contact_type) {
  5397. contact_type = @"";
  5398. }
  5399. contact_type = [self translateSingleQuote:contact_type];
  5400. // 判断更新时是否为customer
  5401. if (update) {
  5402. contact_id = [params objectForKey:@"contact_id"];
  5403. if (!contact_id) {
  5404. contact_id = @"";
  5405. }
  5406. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5407. __block int customer = 0;
  5408. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5409. customer = sqlite3_column_int(stmt, 0);
  5410. }];
  5411. isCustomer = customer ? YES : NO;
  5412. }
  5413. NSMutableDictionary *sync_dic = [params mutableCopy];
  5414. if (isCustomer) {
  5415. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5416. } else {
  5417. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5418. }
  5419. NSString *sync_data = nil;
  5420. NSString *sql = nil;
  5421. if (update){
  5422. contact_id = [params objectForKey:@"contact_id"];
  5423. if (!contact_id) {
  5424. contact_id = @"";
  5425. }
  5426. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5427. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5428. sync_data = [RAConvertor dict2string:sync_dic];
  5429. sync_data = [self translateSingleQuote:sync_data];
  5430. 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];
  5431. } else {
  5432. contact_id = [self translateSingleQuote:contact_id];
  5433. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5434. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5435. sync_data = [RAConvertor dict2string:sync_dic];
  5436. sync_data = [self translateSingleQuote:sync_data];
  5437. 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];
  5438. }
  5439. int result = [iSalesDB execSql:sql];
  5440. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5441. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5442. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5443. }
  5444. #pragma mark save new contact
  5445. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5446. {
  5447. return [self offline_saveContact:params update:NO isCustomer:YES];
  5448. }
  5449. #pragma mark edit contact
  5450. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5451. {
  5452. assert(params[@"user"]!=nil);
  5453. // {
  5454. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5455. // password = 123456;
  5456. // user = EvanK;
  5457. // }
  5458. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5459. NSData *data = [NSData dataWithContentsOfFile:path];
  5460. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5461. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5462. NSString *countryCode = nil;
  5463. NSString *countryCode_id = nil;
  5464. NSString *stateCode = nil;
  5465. /*------contact infor------*/
  5466. __block NSString *country = nil;
  5467. __block NSString *company_name = nil;
  5468. __block NSString *contact_id = params[@"contact_id"];
  5469. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5470. __block NSString *zipcode = nil;
  5471. __block NSString *state = nil; // state_code
  5472. __block NSString *city = nil; //
  5473. __block NSString *firt_name,*last_name;
  5474. __block NSString *phone,*fax,*email;
  5475. __block NSString *notes,*price_type,*sales_rep;
  5476. __block NSString *img_0,*img_1,*img_2;
  5477. __block NSString *contact_type;
  5478. contact_id = [self translateSingleQuote:contact_id];
  5479. 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];
  5480. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5481. country = [self textAtColumn:0 statement:stmt]; // country name
  5482. company_name = [self textAtColumn:1 statement:stmt];
  5483. addr_1 = [self textAtColumn:2 statement:stmt];
  5484. addr_2 = [self textAtColumn:3 statement:stmt];
  5485. addr_3 = [self textAtColumn:4 statement:stmt];
  5486. addr_4 = [self textAtColumn:5 statement:stmt];
  5487. zipcode = [self textAtColumn:6 statement:stmt];
  5488. state = [self textAtColumn:7 statement:stmt]; // state code
  5489. city = [self textAtColumn:8 statement:stmt];
  5490. firt_name = [self textAtColumn:9 statement:stmt];
  5491. last_name = [self textAtColumn:10 statement:stmt];
  5492. phone = [self textAtColumn:11 statement:stmt];
  5493. fax = [self textAtColumn:12 statement:stmt];
  5494. email = [self textAtColumn:13 statement:stmt];
  5495. notes = [self textAtColumn:14 statement:stmt];
  5496. price_type = [self textAtColumn:15 statement:stmt]; // name
  5497. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5498. img_0 = [self textAtColumn:17 statement:stmt];
  5499. img_1 = [self textAtColumn:18 statement:stmt];
  5500. img_2 = [self textAtColumn:19 statement:stmt];
  5501. contact_type = [self textAtColumn:20 statement:stmt];
  5502. }];
  5503. // decrypt
  5504. if (company_name) {
  5505. company_name = [AESCrypt fastdecrypt:company_name];
  5506. }
  5507. if (addr_1) {
  5508. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5509. }
  5510. if (phone) {
  5511. phone = [AESCrypt fastdecrypt:phone];
  5512. }
  5513. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5514. countryCode = [iSalesDB jk_queryText:countrySql];
  5515. stateCode = state;
  5516. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5517. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5518. NSString *code_id = params[@"country"];
  5519. countryCode_id = code_id;
  5520. countryCode = [self countryCodeByid:code_id];
  5521. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5522. NSString *zip_code = params[@"zipcode"];
  5523. // 剔除全部为空格
  5524. int spaceCount = 0;
  5525. for (int i = 0; i < zip_code.length; i++) {
  5526. if ([zip_code characterAtIndex:i] == ' ') {
  5527. spaceCount++;
  5528. }
  5529. }
  5530. if (spaceCount == zip_code.length) {
  5531. zip_code = @"";
  5532. }
  5533. if (zipcode.length > 0) {
  5534. zipcode = zip_code;
  5535. countryCode_id = params[@"country"];
  5536. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5537. countryCode = [dic valueForKey:@"country_code"];
  5538. if (!countryCode) {
  5539. NSString *code_id = params[@"country"];
  5540. countryCode = [self countryCodeByid:code_id];
  5541. }
  5542. stateCode = [dic valueForKey:@"state_code"];
  5543. if (!stateCode.length) {
  5544. stateCode = params[@"state"];
  5545. }
  5546. city = [dic valueForKey:@"city"];
  5547. if (!city.length) {
  5548. city = params[@"city"];
  5549. }
  5550. // zip code
  5551. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5552. [zipDic setValue:zipcode forKey:@"value"];
  5553. [section_0 setValue:zipDic forKey:@"item_8"];
  5554. } else {
  5555. NSString *code_id = params[@"country"];
  5556. countryCode = [self countryCodeByid:code_id];
  5557. stateCode = params[@"state"];
  5558. city = params[@"city"];
  5559. }
  5560. }
  5561. }
  5562. // 0 Country
  5563. // 1 Company Name
  5564. // 2 Contact ID
  5565. // 3 Picture
  5566. // 4 Address 1
  5567. // 5 Address 2
  5568. // 6 Address 3
  5569. // 7 Address 4
  5570. // 8 Zip Code
  5571. // 9 State/Province
  5572. // 10 City
  5573. // 11 Contact First Name
  5574. // 12 Contact Last Name
  5575. // 13 Phone
  5576. // 14 Fax
  5577. // 15 Email
  5578. // 16 Contact Notes
  5579. // 17 Price Type
  5580. // 18 Contact Type
  5581. // 19 Sales Rep
  5582. // country
  5583. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5584. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5585. // company
  5586. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5587. // contact_id
  5588. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5589. // picture
  5590. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5591. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5592. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5593. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5594. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5595. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5596. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5597. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5598. // addr 1 2 3 4
  5599. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5600. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5601. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5602. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5603. // zip code
  5604. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5605. // state
  5606. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5607. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5608. // city
  5609. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5610. // first last
  5611. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5612. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5613. // phone fax email
  5614. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5615. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5616. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5617. // notes
  5618. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5619. // price
  5620. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5621. for (NSString *key in priceDic.allKeys) {
  5622. if ([key containsString:@"val_"]) {
  5623. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5624. if ([dic[@"value"] isEqualToString:price_type]) {
  5625. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5626. [priceDic setValue:dic forKey:key];
  5627. }
  5628. }
  5629. }
  5630. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5631. // Contact Rep
  5632. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5633. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5634. // Sales Rep
  5635. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5636. for (NSString *key in repDic.allKeys) {
  5637. if ([key containsString:@"val_"]) {
  5638. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5639. NSString *value = dic[@"value"];
  5640. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5641. if (code && [code isEqualToString:sales_rep]) {
  5642. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5643. [repDic setValue:dic forKey:key];
  5644. }
  5645. }
  5646. }
  5647. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5648. [ret setValue:section_0 forKey:@"section_0"];
  5649. return [RAConvertor dict2data:ret];
  5650. }
  5651. #pragma mark save contact
  5652. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5653. {
  5654. return [self offline_saveContact:params update:YES isCustomer:YES];
  5655. }
  5656. #pragma mark category
  5657. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5658. if (ck) {
  5659. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5660. for (NSString *key in res.allKeys) {
  5661. if (![key isEqualToString:@"count"]) {
  5662. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5663. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5664. if ([val[@"value"] isEqualToString:ck]) {
  5665. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5666. }
  5667. [res setValue:val forKey:key];
  5668. }
  5669. }
  5670. [dic setValue:res forKey:valueKey];
  5671. }
  5672. }
  5673. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5674. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5675. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5676. NSString* category = [params valueForKey:@"category"];
  5677. if (!category || [category isEqualToString:@""]) {
  5678. category = @"%";
  5679. }
  5680. category = [self translateSingleQuote:category];
  5681. int limit = [[params valueForKey:@"limit"] intValue];
  5682. int offset = [[params valueForKey:@"offset"] intValue];
  5683. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5684. NSString *limit_str = @"";
  5685. if (limited) {
  5686. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5687. }
  5688. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5689. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5690. sqlite3 *db = [iSalesDB get_db];
  5691. // [iSalesDB AddExFunction:db];
  5692. int count;
  5693. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5694. 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];
  5695. double price_min = 0;
  5696. double price_max = 0;
  5697. if ([params.allKeys containsObject:@"alert"]) {
  5698. // alert
  5699. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5700. NSString *alert = params[@"alert"];
  5701. if ([alert isEqualToString:@"Display All"]) {
  5702. alert = [NSString stringWithFormat:@""];
  5703. } else {
  5704. alert = [self translateSingleQuote:alert];
  5705. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5706. }
  5707. // available
  5708. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5709. NSString *available = params[@"available"];
  5710. NSString *available_condition;
  5711. if ([available isEqualToString:@"Display All"]) {
  5712. available_condition = @"";
  5713. } else if ([available isEqualToString:@"Available Now"]) {
  5714. available_condition = @"and availability > 0";
  5715. } else {
  5716. available_condition = @"and availability == 0";
  5717. }
  5718. // best seller
  5719. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5720. NSString *best_seller = @"";
  5721. NSString *order_best_seller = @"m.name asc";
  5722. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5723. best_seller = @"and best_seller > 0";
  5724. order_best_seller = @"m.best_seller desc,m.name asc";
  5725. }
  5726. // price
  5727. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5728. NSString *price = params[@"price"];
  5729. price_min = 0;
  5730. price_max = MAXFLOAT;
  5731. if (params[@"user"] && price != nil) {
  5732. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5733. NSMutableString *priceName = [NSMutableString string];
  5734. for (int i = 0; i < priceTypeArray.count; i++) {
  5735. NSString *pricetype = priceTypeArray[i];
  5736. pricetype = [self translateSingleQuote:pricetype];
  5737. if (i == 0) {
  5738. [priceName appendFormat:@"'%@'",pricetype];
  5739. } else {
  5740. [priceName appendFormat:@",'%@'",pricetype];
  5741. }
  5742. }
  5743. if ([price isEqualToString:@"Display All"]) {
  5744. price = [NSString stringWithFormat:@""];
  5745. } else if([price containsString:@"+"]){
  5746. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5747. price_min = [price doubleValue];
  5748. 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];
  5749. } else {
  5750. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5751. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5752. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5753. 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];
  5754. }
  5755. } else {
  5756. price = @"";
  5757. }
  5758. // sold_by_qty : Sold in quantities of %@
  5759. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5760. NSString *qty = params[@"sold_by_qty"];
  5761. if ([qty isEqualToString:@"Display All"]) {
  5762. qty = @"";
  5763. } else {
  5764. qty = [self translateSingleQuote:qty];
  5765. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5766. }
  5767. // model name;
  5768. NSString* modelname =params[@"modelName"];
  5769. if(modelname.length==0)
  5770. {
  5771. modelname=@"";
  5772. }
  5773. else
  5774. {
  5775. modelname = modelname.lowercaseString;
  5776. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5777. }
  5778. //modelDescription
  5779. NSString* modelDescription =params[@"modelDescription"];
  5780. if(modelDescription.length==0)
  5781. {
  5782. modelDescription=@"";
  5783. }
  5784. else
  5785. {
  5786. modelDescription = modelDescription.lowercaseString;
  5787. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5788. }
  5789. // cate
  5790. // category = [self translateSingleQuote:category];
  5791. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5792. // cate mutiple selection
  5793. NSString *category_id = params[@"category"];
  5794. NSMutableArray *cate_id_array = nil;
  5795. NSMutableString *cateWhere = [NSMutableString string];
  5796. if ([category_id isEqualToString:@""] || !category_id) {
  5797. [cateWhere appendString:@"1 = 1"];
  5798. } else {
  5799. if ([category_id containsString:@","]) {
  5800. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5801. } else {
  5802. cate_id_array = [@[category_id] mutableCopy];
  5803. }
  5804. [cateWhere appendString:@"("];
  5805. for (int i = 0; i < cate_id_array.count; i++) {
  5806. if (i == 0) {
  5807. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5808. } else {
  5809. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5810. }
  5811. }
  5812. [cateWhere appendString:@")"];
  5813. }
  5814. // where bestseller > 0 order by bestseller desc
  5815. // sql query: alert availability(int) best_seller(int) price qty
  5816. 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];
  5817. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5818. }
  5819. DebugLog(@"offline category where: %@",where);
  5820. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5821. if (!params[@"user"]) {
  5822. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5823. }
  5824. [ret setValue:filter forKey:@"filter"];
  5825. DebugLog(@"offline_category sql:%@",sqlQuery);
  5826. sqlite3_stmt * statement;
  5827. [ret setValue:@"2" forKey:@"result"];
  5828. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5829. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5830. // int count=0;
  5831. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5832. {
  5833. int i=0;
  5834. while (sqlite3_step(statement) == SQLITE_ROW)
  5835. {
  5836. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5837. char *name = (char*)sqlite3_column_text(statement, 0);
  5838. if(name==nil)
  5839. name="";
  5840. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5841. char *description = (char*)sqlite3_column_text(statement, 1);
  5842. if(description==nil)
  5843. description="";
  5844. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5845. int product_id = sqlite3_column_int(statement, 2);
  5846. int wid = sqlite3_column_int(statement, 3);
  5847. int closeout = sqlite3_column_int(statement, 4);
  5848. int cid = sqlite3_column_int(statement, 5);
  5849. int wisdelete = sqlite3_column_int(statement, 6);
  5850. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5851. int more_color = sqlite3_column_int(statement, 8);
  5852. // Defaul Category ID
  5853. __block NSString *categoryID = nil;
  5854. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5855. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5856. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5857. if(default_category==nil)
  5858. default_category="";
  5859. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5860. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5861. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5862. categoryID = nsdefault_category;
  5863. }];
  5864. if (!categoryID.length) {
  5865. NSString *cateIDs = params[@"category"];
  5866. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5867. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5868. for (NSString *cateID in requestCategoryArr) {
  5869. BOOL needBreak = NO;
  5870. for (NSString *itemCateIDBox in itemCategoryArr) {
  5871. if (itemCateIDBox.length > 4) {
  5872. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5873. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5874. if ([itemCategoryID isEqualToString:cateID]) {
  5875. needBreak = YES;
  5876. categoryID = itemCategoryID;
  5877. break;
  5878. }
  5879. }
  5880. }
  5881. if (needBreak) {
  5882. break;
  5883. }
  5884. }
  5885. }
  5886. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5887. if(wid !=0 && wisdelete != 1)
  5888. [item setValue:@"true" forKey:@"wish_exists"];
  5889. else
  5890. [item setValue:@"false" forKey:@"wish_exists"];
  5891. if(closeout==0)
  5892. [item setValue:@"false" forKey:@"is_closeout"];
  5893. else
  5894. [item setValue:@"true" forKey:@"is_closeout"];
  5895. if(cid==0)
  5896. [item setValue:@"false" forKey:@"cart_exists"];
  5897. else
  5898. [item setValue:@"true" forKey:@"cart_exists"];
  5899. if (more_color == 0) {
  5900. [item setObject:@(false) forKey:@"more_color"];
  5901. } else if (more_color == 1) {
  5902. [item setObject:@(true) forKey:@"more_color"];
  5903. }
  5904. [item addEntriesFromDictionary:imgjson];
  5905. // [item setValue:nsurl forKey:@"img"];
  5906. [item setValue:nsname forKey:@"name"];
  5907. [item setValue:nsdescription forKey:@"description"];
  5908. if (categoryID) {
  5909. [item setValue:categoryID forKey:@"item_category_id"];
  5910. }
  5911. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5912. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5913. i++;
  5914. }
  5915. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5916. [ret setObject:items forKey:@"items"];
  5917. sqlite3_finalize(statement);
  5918. } else {
  5919. DebugLog(@"nothing...");
  5920. }
  5921. DebugLog(@"count:%d",count);
  5922. [iSalesDB close_db:db];
  5923. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5924. return ret;
  5925. }
  5926. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5927. {
  5928. return [self categoryList:params limited:YES];
  5929. }
  5930. # pragma mark item search
  5931. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5932. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5933. // assert(params[@"order_code"]);
  5934. // params[@"user"]
  5935. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5936. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5937. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5938. // category
  5939. NSDictionary *category_menu = [self offline_category_menu];
  5940. [filter setValue:category_menu forKey:@"category"];
  5941. NSString* where= nil;
  5942. NSString* orderby= @"m.name";
  5943. if (!filterSearch) {
  5944. int covertype = [[params valueForKey:@"covertype"] intValue];
  5945. switch (covertype) {
  5946. case 0:
  5947. {
  5948. where=@"m.category like'%%#005#%%'";
  5949. break;
  5950. }
  5951. case 1:
  5952. {
  5953. where=@"m.alert like '%QS%'";
  5954. break;
  5955. }
  5956. case 2:
  5957. {
  5958. where=@"m.availability>0";
  5959. break;
  5960. }
  5961. case 3:
  5962. {
  5963. where=@"m.best_seller>0";
  5964. orderby=@"m.best_seller desc,m.name asc";
  5965. break;
  5966. }
  5967. default:
  5968. where=@"1=1";
  5969. break;
  5970. }
  5971. }
  5972. int limit = [[params valueForKey:@"limit"] intValue];
  5973. int offset = [[params valueForKey:@"offset"] intValue];
  5974. NSString *limit_str = @"";
  5975. if (limited) {
  5976. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5977. }
  5978. sqlite3 *db = [iSalesDB get_db];
  5979. // [iSalesDB AddExFunction:db];
  5980. int count;
  5981. NSString *sqlQuery = nil;
  5982. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5983. 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];
  5984. double price_min = 0;
  5985. double price_max = 0;
  5986. if (filterSearch) {
  5987. // alert
  5988. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5989. NSString *alert = params[@"alert"];
  5990. if ([alert isEqualToString:@"Display All"]) {
  5991. alert = [NSString stringWithFormat:@""];
  5992. } else {
  5993. alert = [self translateSingleQuote:alert];
  5994. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5995. }
  5996. // available
  5997. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5998. NSString *available = params[@"available"];
  5999. NSString *available_condition;
  6000. if ([available isEqualToString:@"Display All"]) {
  6001. available_condition = @"";
  6002. } else if ([available isEqualToString:@"Available Now"]) {
  6003. available_condition = @"and availability > 0";
  6004. } else {
  6005. available_condition = @"and availability == 0";
  6006. }
  6007. // best seller
  6008. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6009. NSString *best_seller = @"";
  6010. NSString *order_best_seller = @"m.name asc";
  6011. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6012. best_seller = @"and best_seller > 0";
  6013. order_best_seller = @"m.best_seller desc,m.name asc";
  6014. }
  6015. // price
  6016. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6017. NSString *price = params[@"price"];
  6018. price_min = 0;
  6019. price_max = MAXFLOAT;
  6020. if (params[@"user"]) {
  6021. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6022. NSMutableString *priceName = [NSMutableString string];
  6023. for (int i = 0; i < priceTypeArray.count; i++) {
  6024. NSString *pricetype = priceTypeArray[i];
  6025. pricetype = [self translateSingleQuote:pricetype];
  6026. if (i == 0) {
  6027. [priceName appendFormat:@"'%@'",pricetype];
  6028. } else {
  6029. [priceName appendFormat:@",'%@'",pricetype];
  6030. }
  6031. }
  6032. if ([price isEqualToString:@"Display All"]) {
  6033. price = [NSString stringWithFormat:@""];
  6034. } else if([price containsString:@"+"]){
  6035. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6036. price_min = [price doubleValue];
  6037. 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];
  6038. } else {
  6039. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6040. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6041. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6042. 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];
  6043. }
  6044. } else {
  6045. price = @"";
  6046. }
  6047. // sold_by_qty : Sold in quantities of %@
  6048. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6049. NSString *qty = params[@"sold_by_qty"];
  6050. if ([qty isEqualToString:@"Display All"]) {
  6051. qty = @"";
  6052. } else {
  6053. qty = [self translateSingleQuote:qty];
  6054. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6055. }
  6056. // model name;
  6057. NSString* modelname =params[@"modelName"];
  6058. if(modelname.length==0)
  6059. {
  6060. modelname=@"";
  6061. }
  6062. else
  6063. {
  6064. modelname = modelname.lowercaseString;
  6065. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6066. }
  6067. //modelDescription
  6068. NSString* modelDescription =params[@"modelDescription"];
  6069. if(modelDescription.length==0)
  6070. {
  6071. modelDescription=@"";
  6072. }
  6073. else
  6074. {
  6075. modelDescription = modelDescription.lowercaseString;
  6076. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6077. }
  6078. //lower(description) like'%%%@%%'
  6079. // category
  6080. NSString *category_id = params[@"ctgId"];
  6081. NSMutableArray *cate_id_array = nil;
  6082. NSMutableString *cateWhere = [NSMutableString string];
  6083. if ([category_id isEqualToString:@""] || !category_id) {
  6084. [cateWhere appendString:@"1 = 1"];
  6085. } else {
  6086. if ([category_id containsString:@","]) {
  6087. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6088. } else {
  6089. cate_id_array = [@[category_id] mutableCopy];
  6090. }
  6091. /*-----------*/
  6092. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6093. [cateWhere appendString:@"("];
  6094. for (int i = 0; i < cate_id_array.count; i++) {
  6095. for (NSString *key0 in cateDic.allKeys) {
  6096. if ([key0 containsString:@"category_"]) {
  6097. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6098. for (NSString *key1 in category0.allKeys) {
  6099. if ([key1 containsString:@"category_"]) {
  6100. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6101. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6102. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6103. cate_id_array[i] = [category1 objectForKey:@"id"];
  6104. if (i == 0) {
  6105. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6106. } else {
  6107. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6108. }
  6109. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6110. [category0 setValue:category1 forKey:key1];
  6111. [cateDic setValue:category0 forKey:key0];
  6112. }
  6113. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6114. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6115. cate_id_array[i] = [category0 objectForKey:@"id"];
  6116. if (i == 0) {
  6117. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6118. } else {
  6119. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6120. }
  6121. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6122. [cateDic setValue:category0 forKey:key0];
  6123. }
  6124. }
  6125. }
  6126. }
  6127. }
  6128. }
  6129. [cateWhere appendString:@")"];
  6130. [filter setValue:cateDic forKey:@"category"];
  6131. }
  6132. // where bestseller > 0 order by bestseller desc
  6133. // sql query: alert availability(int) best_seller(int) price qty
  6134. 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];
  6135. // count
  6136. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6137. }
  6138. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6139. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6140. if (!params[@"user"]) {
  6141. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6142. }
  6143. [ret setValue:filter forKey:@"filter"];
  6144. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6145. sqlite3_stmt * statement;
  6146. [ret setValue:@"2" forKey:@"result"];
  6147. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6148. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6149. // int count=0;
  6150. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6151. {
  6152. int i=0;
  6153. while (sqlite3_step(statement) == SQLITE_ROW)
  6154. {
  6155. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6156. char *name = (char*)sqlite3_column_text(statement, 0);
  6157. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6158. char *description = (char*)sqlite3_column_text(statement, 1);
  6159. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6160. int product_id = sqlite3_column_int(statement, 2);
  6161. int wid = sqlite3_column_int(statement, 3);
  6162. int closeout = sqlite3_column_int(statement, 4);
  6163. int cid = sqlite3_column_int(statement, 5);
  6164. int wisdelete = sqlite3_column_int(statement, 6);
  6165. int more_color = sqlite3_column_int(statement, 7);
  6166. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6167. if(wid !=0 && wisdelete != 1)
  6168. [item setValue:@"true" forKey:@"wish_exists"];
  6169. else
  6170. [item setValue:@"false" forKey:@"wish_exists"];
  6171. if(closeout==0)
  6172. [item setValue:@"false" forKey:@"is_closeout"];
  6173. else
  6174. [item setValue:@"true" forKey:@"is_closeout"];
  6175. if(cid==0)
  6176. [item setValue:@"false" forKey:@"cart_exists"];
  6177. else
  6178. [item setValue:@"true" forKey:@"cart_exists"];
  6179. if (more_color == 0) {
  6180. [item setObject:@(false) forKey:@"more_color"];
  6181. } else if (more_color == 1) {
  6182. [item setObject:@(true) forKey:@"more_color"];
  6183. }
  6184. [item addEntriesFromDictionary:imgjson];
  6185. // [item setValue:nsurl forKey:@"img"];
  6186. [item setValue:nsname forKey:@"fash_name"];
  6187. [item setValue:nsdescription forKey:@"description"];
  6188. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6189. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6190. i++;
  6191. }
  6192. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6193. [ret setObject:items forKey:@"items"];
  6194. sqlite3_finalize(statement);
  6195. }
  6196. [iSalesDB close_db:db];
  6197. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6198. return ret;
  6199. }
  6200. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6201. {
  6202. return [self itemsearch:params limited:YES];
  6203. }
  6204. #pragma mark order detail
  6205. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6206. if (str1.length == 0) {
  6207. str1 = @"&nbsp";
  6208. }
  6209. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6210. return str;
  6211. }
  6212. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6213. {
  6214. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6215. [fromformatter setDateFormat:from];
  6216. NSDate *date = [fromformatter dateFromString:datetime];
  6217. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6218. [toformatter setDateFormat:to];
  6219. NSString * ret = [toformatter stringFromDate:date];
  6220. return ret;
  6221. }
  6222. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6223. // 把毫秒去掉
  6224. if ([dateTime containsString:@"."]) {
  6225. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6226. }
  6227. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6228. formatter.dateFormat = formate;
  6229. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6230. NSDate *date = [formatter dateFromString:dateTime];
  6231. formatter.dateFormat = newFormate;
  6232. NSString *result = [formatter stringFromDate:date];
  6233. return result ? result : @"";
  6234. }
  6235. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6236. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6237. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6238. }
  6239. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6240. {
  6241. assert(params[@"mode"]!=nil);
  6242. assert(params[@"user"]!=nil);
  6243. DebugLog(@"offline oderdetail params: %@",params);
  6244. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6245. int orderId = [params[@"orderId"] intValue];
  6246. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6247. // decrypt card number and card security code
  6248. // 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 ];
  6249. 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];
  6250. sqlite3 *db = [iSalesDB get_db];
  6251. sqlite3_stmt * statement;
  6252. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6253. NSString *nssoid = @"";
  6254. NSString* orderinfo = @"";
  6255. NSString *moreInfo = @"";
  6256. double handlingFee = 0;
  6257. double payments_and_credist = 0;
  6258. double totalPrice = 0;
  6259. double shippingFee = 0;
  6260. double lift_gate = 0;
  6261. NSString *customer_contact = @"";
  6262. NSString *customer_email = @"";
  6263. NSString *customer_fax = @"";
  6264. NSString *customer_phone = @"";
  6265. NSString *customer_city = @"";
  6266. NSString *customer_state = @"";
  6267. NSString *customer_zipcode = @"";
  6268. NSString *customer_country = @"";
  6269. BOOL must_call = NO;
  6270. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6271. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6272. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6273. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6274. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6275. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6276. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6277. {
  6278. if (sqlite3_step(statement) == SQLITE_ROW)
  6279. {
  6280. // int order_id = sqlite3_column_int(statement, 0);
  6281. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6282. // ret[@"sign_url"] = sign_url;
  6283. section_1 = @{
  6284. @"data":sign_url,
  6285. @"title":@"Signature",
  6286. @"type":@"sign_url"
  6287. }.mutableCopy;
  6288. [ret setObject:section_1 forKey:@"section_1"];
  6289. customer_contact = [self textAtColumn:52 statement:statement];
  6290. customer_email = [self textAtColumn:53 statement:statement];
  6291. customer_phone = [self textAtColumn:54 statement:statement];
  6292. customer_fax = [self textAtColumn:55 statement:statement];
  6293. customer_city = [self textAtColumn:60 statement:statement];
  6294. customer_state = [self textAtColumn:61 statement:statement];
  6295. customer_zipcode = [self textAtColumn:62 statement:statement];
  6296. customer_country = [self textAtColumn:63 statement:statement];
  6297. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6298. int offline_edit=sqlite3_column_int(statement, 56);
  6299. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6300. char *soid = (char*)sqlite3_column_text(statement, 1);
  6301. if(soid==nil)
  6302. soid= "";
  6303. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6304. // so#
  6305. ret[@"so#"] = nssoid;
  6306. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6307. if(poNumber==nil)
  6308. poNumber= "";
  6309. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6310. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6311. if(create_time==nil)
  6312. create_time= "";
  6313. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6314. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6315. int status = sqlite3_column_int(statement, 4);
  6316. int erpStatus = sqlite3_column_int(statement, 49);
  6317. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6318. // status
  6319. if (status > 1 && status != 3) {
  6320. status = erpStatus;
  6321. } else if (status == 3) {
  6322. status = 15;
  6323. }
  6324. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6325. ret[@"order_status"] = nsstatus;
  6326. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6327. if(company_name==nil)
  6328. company_name= "";
  6329. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6330. // company name
  6331. ret[@"company_name"] = nscompany_name;
  6332. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6333. if(customer_contact==nil)
  6334. customer_contact= "";
  6335. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6336. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6337. if(addr_1==nil)
  6338. addr_1="";
  6339. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6340. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6341. if(addr_2==nil)
  6342. addr_2="";
  6343. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6344. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6345. if(addr_3==nil)
  6346. addr_3="";
  6347. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6348. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6349. if(addr_4==nil)
  6350. addr_4="";
  6351. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6352. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6353. [arr_addr addObject:nsaddr_1];
  6354. [arr_addr addObject:nsaddr_2];
  6355. [arr_addr addObject:nsaddr_3];
  6356. [arr_addr addObject:nsaddr_4];
  6357. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6358. [arr_addr addObject:customer_state];
  6359. [arr_addr addObject:customer_zipcode];
  6360. [arr_addr addObject:customer_country];
  6361. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6362. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6363. char *logist = (char*)sqlite3_column_text(statement, 11);
  6364. if(logist==nil)
  6365. logist= "";
  6366. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6367. if (/*status == -11 || */status == 10 || status == 11) {
  6368. nslogist = [self textAtColumn:59 statement:statement];
  6369. };
  6370. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6371. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6372. shipping = @"Shipping To Be Quoted";
  6373. } else {
  6374. shippingFee = sqlite3_column_double(statement, 12);
  6375. }
  6376. // Shipping
  6377. // ret[@"Shipping"] = shipping;
  6378. NSDictionary *shipping_item = @{
  6379. @"title":@"Shipping",
  6380. @"value":shipping
  6381. };
  6382. [price_data setObject:shipping_item forKey:@"item_1"];
  6383. int have_lift_gate = sqlite3_column_int(statement, 17);
  6384. lift_gate = sqlite3_column_double(statement, 13);
  6385. // Liftgate Fee(No loading dock)
  6386. if (!have_lift_gate) {
  6387. lift_gate = 0;
  6388. }
  6389. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6390. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6391. if (sqlite3_column_int(statement, 57)) {
  6392. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6393. liftgate_value = @"Shipping To Be Quoted";
  6394. }
  6395. NSDictionary *liftgate_item = @{
  6396. @"title":@"Liftgate Fee(No loading dock)",
  6397. @"value":liftgate_value
  6398. };
  6399. [price_data setObject:liftgate_item forKey:@"item_2"];
  6400. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6401. // [ret removeObjectForKey:@"Shipping"];
  6402. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6403. // }
  6404. //
  6405. // if (have_lift_gate) {
  6406. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6407. // }
  6408. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6409. if(general_notes==nil)
  6410. general_notes= "";
  6411. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6412. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6413. if(internal_notes==nil)
  6414. internal_notes= "";
  6415. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6416. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6417. if(payment_type==nil)
  6418. payment_type= "";
  6419. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6420. // order info
  6421. orderinfo = [self textFileName:@"order_info.html"];
  6422. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6423. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6424. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6425. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6426. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6427. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6428. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6429. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6430. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6431. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6432. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6433. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6434. NSString *payment = nil;
  6435. // id -> show
  6436. __block NSString *show_pay_type = nspayment_type;
  6437. [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) {
  6438. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6439. if (show_typ_ch != NULL) {
  6440. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6441. }
  6442. }];
  6443. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6444. {
  6445. payment = [self textFileName:@"creditcardpayment.html"];
  6446. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6447. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6448. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6449. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6450. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6451. NSString *card_type = [self textAtColumn:42 statement:statement];
  6452. if (card_type.length > 0) { // 显示星号
  6453. card_type = @"****";
  6454. }
  6455. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6456. if (card_number.length > 0 && card_number.length > 4) {
  6457. for (int i = 0; i < card_number.length - 4; i++) {
  6458. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6459. }
  6460. } else {
  6461. card_number = @"";
  6462. }
  6463. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6464. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6465. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6466. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6467. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6468. card_expiration = @"****";
  6469. }
  6470. NSString *card_city = [self textAtColumn:46 statement:statement];
  6471. NSString *card_state = [self textAtColumn:47 statement:statement];
  6472. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6473. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6474. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6475. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6476. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6477. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6478. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6479. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6480. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6481. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6482. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6483. }
  6484. else
  6485. {
  6486. payment=[self textFileName:@"normalpayment.html"];
  6487. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6488. }
  6489. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6490. ret[@"result"]= [NSNumber numberWithInt:2];
  6491. // more info
  6492. moreInfo = [self textFileName:@"more_info.html"];
  6493. /*****ship to******/
  6494. // ShipToCompany_or_&nbsp
  6495. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6496. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6497. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6498. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6499. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6500. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6501. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6502. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6503. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6504. /*****ship from******/
  6505. // ShipFromCompany_or_&nbsp
  6506. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6507. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6508. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6509. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6510. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6511. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6512. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6513. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6514. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6515. /*****freight to******/
  6516. // FreightBillToCompany_or_&nbsp
  6517. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6518. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6519. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6520. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6521. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6522. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6523. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6524. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6525. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6526. /*****merchandise to******/
  6527. // MerchandiseBillToCompany_or_&nbsp
  6528. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6529. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6530. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6531. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6532. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6533. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6534. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6535. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6536. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6537. /*****return to******/
  6538. // ReturnToCompany_or_&nbsp
  6539. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6540. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6541. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6542. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6543. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6544. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6545. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6546. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6547. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6548. //
  6549. // DebugLog(@"more info : %@",moreInfo);
  6550. // handling fee
  6551. handlingFee = sqlite3_column_double(statement, 33);
  6552. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6553. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6554. if (sqlite3_column_int(statement, 58)) {
  6555. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6556. handling_fee_value = @"Shipping To Be Quoted";
  6557. }
  6558. NSDictionary *handling_fee_item = @{
  6559. @"title":@"Handling Fee",
  6560. @"value":handling_fee_value
  6561. };
  6562. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6563. //
  6564. // customer info
  6565. customerID = [self textAtColumn:36 statement:statement];
  6566. // mode
  6567. ret[@"mode"] = params[@"mode"];
  6568. // model_count
  6569. ret[@"model_count"] = @(0);
  6570. }
  6571. sqlite3_finalize(statement);
  6572. }
  6573. [iSalesDB close_db:db];
  6574. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6575. // "customer_email" = "Shui Hu";
  6576. // "customer_fax" = "";
  6577. // "customer_first_name" = F;
  6578. // "customer_last_name" = L;
  6579. // "customer_name" = ",da He Xiang Dong Liu A";
  6580. // "customer_phone" = "Hey Xuan Feng";
  6581. contactInfo[@"customer_phone"] = customer_phone;
  6582. contactInfo[@"customer_fax"] = customer_fax;
  6583. contactInfo[@"customer_email"] = customer_email;
  6584. NSString *first_name = @"";
  6585. NSString *last_name = @"";
  6586. if ([customer_contact isEqualToString:@""]) {
  6587. } else if ([customer_contact containsString:@" "]) {
  6588. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6589. first_name = [customer_contact substringToIndex:first_space_index];
  6590. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6591. }
  6592. contactInfo[@"customer_first_name"] = first_name;
  6593. contactInfo[@"customer_last_name"] = last_name;
  6594. ret[@"customerInfo"] = contactInfo;
  6595. // models
  6596. if (nssoid) {
  6597. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6598. __block double TotalCuft = 0;
  6599. __block double TotalWeight = 0;
  6600. __block int TotalCarton = 0;
  6601. __block double allItemPrice = 0;
  6602. 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];
  6603. sqlite3 *db1 = [iSalesDB get_db];
  6604. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6605. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6606. int product_id = sqlite3_column_int(stmt, 0);
  6607. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6608. int item_id = sqlite3_column_int(stmt, 7);
  6609. NSString* Price=nil;
  6610. if(str_price==nil)
  6611. {
  6612. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6613. if(price==nil)
  6614. Price=@"No Price.";
  6615. else
  6616. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6617. }
  6618. else
  6619. {
  6620. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6621. }
  6622. double discount = sqlite3_column_double(stmt, 2);
  6623. int item_count = sqlite3_column_int(stmt, 3);
  6624. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6625. NSString *nsline_note=nil;
  6626. if(line_note!=nil)
  6627. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6628. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6629. // NSString *nsname = nil;
  6630. // if(name!=nil)
  6631. // nsname= [[NSString alloc]initWithUTF8String:name];
  6632. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6633. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6634. // NSString *nsdescription=nil;
  6635. // if(description!=nil)
  6636. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6637. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6638. // int stockUom = sqlite3_column_int(stmt, 8);
  6639. // int _id = sqlite3_column_int(stmt, 9);
  6640. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6641. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6642. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6643. int carton=[bsubtotaljson[@"carton"] intValue];
  6644. TotalCuft += cuft;
  6645. TotalWeight += weight;
  6646. TotalCarton += carton;
  6647. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6648. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6649. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6650. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6651. itemjson[@"note"]=nsline_note;
  6652. itemjson[@"origin_price"] = Price;
  6653. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6654. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6655. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6656. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6657. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6658. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6659. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6660. if(itemjson[@"combine"] != nil)
  6661. {
  6662. // int citem=0;
  6663. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6664. for(int bc=0;bc<bcount;bc++)
  6665. {
  6666. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6667. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6668. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6669. subTotal += uprice * modulus * item_count;
  6670. }
  6671. }
  6672. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6673. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6674. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6675. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6676. itemjson[@"type"] = @"order_item";
  6677. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6678. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6679. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6680. allItemPrice += subTotal;
  6681. }];
  6682. section_3[@"data"] = model_data;
  6683. section_3[@"type"] = @"sub_order";
  6684. section_3[@"title"] = @"Models";
  6685. section_3[@"switch"] = @(false);
  6686. ret[@"section_3"] = section_3;
  6687. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6688. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6689. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6690. // payments/Credits
  6691. // payments_and_credist = sqlite3_column_double(statement, 34);
  6692. payments_and_credist = allItemPrice;
  6693. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6694. NSDictionary *sub_total_item = @{
  6695. @"title":@"Sub-Total",
  6696. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6697. };
  6698. [price_data setObject:sub_total_item forKey:@"item_0"];
  6699. // // total
  6700. // totalPrice = sqlite3_column_double(statement, 35);
  6701. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6702. } else {
  6703. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6704. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6705. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6706. // payments/Credits
  6707. payments_and_credist = 0;
  6708. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6709. NSDictionary *sub_total_item = @{
  6710. @"title":@"Sub-Total",
  6711. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6712. };
  6713. [price_data setObject:sub_total_item forKey:@"item_0"];
  6714. }
  6715. // total
  6716. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6717. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6718. NSDictionary *total_item = @{
  6719. @"title":@"Total",
  6720. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6721. };
  6722. [price_data setObject:total_item forKey:@"item_4"];
  6723. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6724. section_4 = @{
  6725. @"data":price_data,
  6726. @"title":@"Price Info",
  6727. @"type":@"price_info"
  6728. }.mutableCopy;
  6729. ret[@"section_4"] = section_4;
  6730. // ret[@"order_info"]= orderinfo;
  6731. section_0 = @{
  6732. @"data":orderinfo,
  6733. @"title":@"Order Info",
  6734. @"type":@"order_info"
  6735. }.mutableCopy;
  6736. ret[@"section_0"] = section_0;
  6737. // ret[@"more_order_info"] = moreInfo;
  6738. section_2 = @{
  6739. @"data":moreInfo,
  6740. @"title":@"More Info",
  6741. @"type":@"more_order_info"
  6742. }.mutableCopy;
  6743. ret[@"section_2"] = section_2;
  6744. ret[@"count"] = @(5);
  6745. return [RAConvertor dict2data:ret];
  6746. }
  6747. #pragma mark order list
  6748. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6749. NSString *nsstatus = @"";
  6750. switch (status) {
  6751. case 0:
  6752. {
  6753. nsstatus=@"Temp Order";
  6754. break;
  6755. }
  6756. case 1:
  6757. {
  6758. nsstatus=@"Saved Order";
  6759. break;
  6760. }
  6761. case 2: {
  6762. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6763. break;
  6764. }
  6765. case 3:
  6766. case 15:
  6767. {
  6768. nsstatus=@"Cancelled";
  6769. break;
  6770. }
  6771. case 10:
  6772. {
  6773. nsstatus=@"Quote Submitted";
  6774. break;
  6775. }
  6776. case 11:
  6777. {
  6778. nsstatus=@"Sales Order Submitted";
  6779. break;
  6780. }
  6781. case 12:
  6782. {
  6783. nsstatus=@"Processing";
  6784. break;
  6785. }
  6786. case 13:
  6787. {
  6788. nsstatus=@"Shipped";
  6789. break;
  6790. }
  6791. case 14:
  6792. {
  6793. nsstatus=@"Closed";
  6794. break;
  6795. }
  6796. case -11:
  6797. {
  6798. nsstatus = @"Ready For Submit";
  6799. break;
  6800. }
  6801. default:
  6802. break;
  6803. }
  6804. return nsstatus;
  6805. }
  6806. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  6807. {
  6808. double total = 0;
  6809. __block double payments_and_credist = 0;
  6810. __block double allItemPrice = 0;
  6811. // sqlite3 *db1 = [iSalesDB get_db];
  6812. if (so_id) {
  6813. // 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];
  6814. 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];
  6815. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6816. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6817. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6818. int product_id = sqlite3_column_int(stmt, 0);
  6819. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6820. int discount = sqlite3_column_int(stmt, 2);
  6821. int item_count = sqlite3_column_int(stmt, 3);
  6822. // int item_id = sqlite3_column_int(stmt, 7);
  6823. int item_id = sqlite3_column_int(stmt, 4);
  6824. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6825. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6826. NSString* Price=nil;
  6827. if(str_price==nil)
  6828. {
  6829. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  6830. if(price==nil)
  6831. Price=@"No Price.";
  6832. else
  6833. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6834. }
  6835. else
  6836. {
  6837. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6838. }
  6839. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  6840. itemjson[@"The unit price"]=Price;
  6841. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6842. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6843. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6844. if(itemjson[@"combine"] != nil)
  6845. {
  6846. // int citem=0;
  6847. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6848. for(int bc=0;bc<bcount;bc++)
  6849. {
  6850. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6851. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6852. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6853. subTotal += uprice * modulus * item_count;
  6854. }
  6855. }
  6856. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6857. allItemPrice += subTotal;
  6858. }];
  6859. payments_and_credist = allItemPrice;
  6860. } else {
  6861. // payments/Credits
  6862. payments_and_credist = 0;
  6863. }
  6864. // lift_gate handlingFee shippingFee
  6865. __block double lift_gate = 0;
  6866. __block double handlingFee = 0;
  6867. __block double shippingFee = 0;
  6868. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6869. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6870. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6871. if (have_lift_gate) {
  6872. lift_gate = sqlite3_column_double(stmt, 1);
  6873. }
  6874. handlingFee = sqlite3_column_double(stmt, 2);
  6875. shippingFee = sqlite3_column_double(stmt, 3);
  6876. }];
  6877. // total
  6878. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6879. if (close) {
  6880. [iSalesDB close_db:db1];
  6881. }
  6882. return total;
  6883. }
  6884. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6885. {
  6886. assert(params[@"user"]!=nil);
  6887. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6888. int limit = [[params valueForKey:@"limit"] intValue];
  6889. int offset = [[params valueForKey:@"offset"] intValue];
  6890. NSString* keyword = [params valueForKey:@"keyWord"];
  6891. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6892. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6893. NSString* where=@"1 = 1";
  6894. if(keyword.length>0)
  6895. 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]];
  6896. if (orderStatus.length > 0) {
  6897. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6898. if (order_status_array.count == 1) {
  6899. int status_value = [[order_status_array firstObject] intValue];
  6900. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6901. if (status_value == 15 || status_value == 3) {
  6902. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6903. } else {
  6904. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6905. }
  6906. } else {
  6907. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6908. }
  6909. } else if (order_status_array.count > 1) {
  6910. for (int i = 0; i < order_status_array.count;i++) {
  6911. NSString *status = order_status_array[i];
  6912. NSString *condition = @" or";
  6913. if (i == 0) {
  6914. condition = @" and (";
  6915. }
  6916. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6917. int status_value = [status intValue];
  6918. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6919. if (status_value == 15 || status_value == 3) {
  6920. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6921. } else {
  6922. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6923. }
  6924. } else {
  6925. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6926. }
  6927. }
  6928. where = [where stringByAppendingString:@" )"];
  6929. }
  6930. }
  6931. 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];
  6932. // DebugLog(@"order list sql: %@",sqlQuery);
  6933. sqlite3 *db = [iSalesDB get_db];
  6934. sqlite3_stmt * statement;
  6935. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6936. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6937. {
  6938. int count=0;
  6939. while (sqlite3_step(statement) == SQLITE_ROW)
  6940. {
  6941. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6942. int order_id = sqlite3_column_double(statement, 0);
  6943. char *soid = (char*)sqlite3_column_text(statement, 1);
  6944. if(soid==nil)
  6945. soid= "";
  6946. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6947. int status = sqlite3_column_double(statement, 2);
  6948. int erpStatus = sqlite3_column_double(statement, 9);
  6949. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6950. if(sales_rep==nil)
  6951. sales_rep= "";
  6952. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6953. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6954. if(create_by==nil)
  6955. create_by= "";
  6956. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6957. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6958. if(company_name==nil)
  6959. company_name= "";
  6960. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6961. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6962. if(create_time==nil)
  6963. create_time= "";
  6964. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6965. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6966. // double total_price = sqlite3_column_double(statement, 7);
  6967. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  6968. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6969. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6970. int offline_edit = sqlite3_column_int(statement, 10);
  6971. // ": "JH",
  6972. // "": "$8307.00",
  6973. // "": "MOB1608050001",
  6974. // "": "ArpithaT",
  6975. // "": "1st Stage Property Transformations",
  6976. // "": "JANICE SUTTON",
  6977. // "": 2255,
  6978. // "": "08/02/2016 09:49:18",
  6979. // "": 1,
  6980. // "": "Saved Order"
  6981. // "": "1470384050483",
  6982. // "model_count": "6 / 28"
  6983. item[@"sales_rep"]= nssales_rep;
  6984. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6985. item[@"so#"]= nssoid;
  6986. item[@"create_by"]= nscreate_by;
  6987. item[@"customer_name"]= nscompany_name;
  6988. item[@"customer_contact"] = customer_contact;
  6989. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6990. item[@"purchase_time"]= nscreate_time;
  6991. int statusCode = status;
  6992. if (statusCode == 2) {
  6993. statusCode = erpStatus;
  6994. } else if (statusCode == 3) {
  6995. statusCode = 15;
  6996. }
  6997. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6998. item[@"order_status"]= nsstatus;
  6999. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7000. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7001. // item[@"model_count"]
  7002. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7003. count++;
  7004. }
  7005. ret[@"count"]= [NSNumber numberWithInt:count];
  7006. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7007. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7008. ret[@"result"]= [NSNumber numberWithInt:2];
  7009. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7010. ret[@"time_zone"] = @"PST";
  7011. sqlite3_finalize(statement);
  7012. }
  7013. 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];
  7014. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7015. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7016. [iSalesDB close_db:db];
  7017. return [RAConvertor dict2data:ret];
  7018. }
  7019. #pragma mark update gnotes
  7020. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7021. {
  7022. DebugLog(@"params: %@",params);
  7023. // comments = Meyoyoyoyoyoyoy;
  7024. // orderCode = MOB1608110001;
  7025. // password = 123456;
  7026. // user = EvanK;
  7027. 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]];
  7028. int ret = [iSalesDB execSql:sql];
  7029. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7030. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7031. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7032. // [dic setValue:@"160409" forKey:@"min_ver"];
  7033. return [RAConvertor dict2data:dic];
  7034. }
  7035. #pragma mark move to wishlist
  7036. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7037. {
  7038. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7039. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7040. _id = [NSString stringWithFormat:@"(%@)",_id];
  7041. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7042. sqlite3 *db = [iSalesDB get_db];
  7043. __block NSString *product_id = @"";
  7044. __block NSString *item_count_str = @"";
  7045. // __block NSString *item_id = nil;
  7046. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7047. // product_id = [self textAtColumn:0 statement:stmt];
  7048. int item_count = sqlite3_column_int(stmt, 1);
  7049. // item_id = [self textAtColumn:2 statement:stmt];
  7050. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7051. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7052. if (p_id.length) {
  7053. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7054. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7055. }
  7056. }];
  7057. [iSalesDB close_db:db];
  7058. // 去除第一个,
  7059. if (product_id.length > 1) {
  7060. product_id = [product_id substringFromIndex:1];
  7061. }
  7062. if (item_count_str.length > 1) {
  7063. item_count_str = [item_count_str substringFromIndex:1];
  7064. }
  7065. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7066. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7067. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7068. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7069. sqlite3 *db1 = [iSalesDB get_db];
  7070. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7071. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7072. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7073. // 删除
  7074. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7075. int ret = [iSalesDB execSql:deleteSql db:db1];
  7076. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7077. [iSalesDB close_db:db1];
  7078. return [RAConvertor dict2data:dic];
  7079. }
  7080. #pragma mark cart delete
  7081. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7082. {
  7083. // cartItemId = 548;
  7084. // orderCode = MOB1608110001;
  7085. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7086. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7087. _id = [NSString stringWithFormat:@"(%@)",_id];
  7088. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7089. int ret = [iSalesDB execSql:sql];
  7090. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7091. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7092. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7093. // [dic setValue:@"160409" forKey:@"min_ver"];
  7094. return [RAConvertor dict2data:dic];
  7095. }
  7096. #pragma mark set price
  7097. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7098. {
  7099. DebugLog(@"cart set price params: %@",params);
  7100. // "cartitem_id" = 1;
  7101. // discount = "0.000000";
  7102. // "item_note" = "";
  7103. // price = "269.000000";
  7104. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7105. NSString *notes = [self valueInParams:params key:@"item_note"];
  7106. NSString *discount = [self valueInParams:params key:@"discount"];
  7107. NSString *price = [self valueInParams:params key:@"price"];
  7108. // bool badd_price_changed=false;
  7109. // sqlite3* db=[iSalesDB get_db];
  7110. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7111. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7112. // NSRange range;
  7113. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7114. //
  7115. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7116. // badd_price_changed=true;
  7117. // [iSalesDB close_db:db];
  7118. //
  7119. // if(badd_price_changed)
  7120. // {
  7121. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7122. // }
  7123. //
  7124. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7125. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7126. int ret = [iSalesDB execSql:sql];
  7127. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7128. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7129. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7130. // [dic setValue:@"160409" forKey:@"min_ver"];
  7131. return [RAConvertor dict2data:dic];
  7132. }
  7133. #pragma mark set line notes
  7134. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7135. {
  7136. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7137. NSString *notes = [self valueInParams:params key:@"notes"];
  7138. notes = [self translateSingleQuote:notes];
  7139. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7140. int ret = [iSalesDB execSql:sql];
  7141. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7142. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7143. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7144. // [dic setValue:@"160409" forKey:@"min_ver"];
  7145. return [RAConvertor dict2data:dic];
  7146. }
  7147. #pragma mark set qty
  7148. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7149. {
  7150. assert(params[@"can_create_backorder"]!=nil);
  7151. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7152. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7153. int item_count = [params[@"inputInt"] intValue];
  7154. // 购买检查数量大于库存
  7155. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7156. if (!params[@"can_create_backorder"]) {
  7157. 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];
  7158. __block BOOL out_of_stock = NO;
  7159. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7160. int availability = sqlite3_column_int(stmt, 0);
  7161. if (availability < item_count) {
  7162. out_of_stock = YES;
  7163. }
  7164. }];
  7165. if (out_of_stock) { // 缺货
  7166. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7167. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7168. return [RAConvertor dict2data:dic];
  7169. }
  7170. }
  7171. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7172. int ret = [iSalesDB execSql:sql];
  7173. __block int item_id = 0;
  7174. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7175. item_id = sqlite3_column_int(stmt, 0);
  7176. }];
  7177. sqlite3 *db = [iSalesDB get_db];
  7178. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7179. [iSalesDB close_db:db];
  7180. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7181. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7182. // [dic setValue:@"160409" forKey:@"min_ver"];
  7183. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7184. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7185. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7186. return [RAConvertor dict2data:dic];
  7187. }
  7188. #pragma mark place order
  7189. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7190. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7191. if (aname) {
  7192. [dic setValue:aname forKey:@"aname"];
  7193. }
  7194. if (control) {
  7195. [dic setValue:control forKey:@"control"];
  7196. }
  7197. if (keyboard) {
  7198. [dic setValue:keyboard forKey:@"keyboard"];
  7199. }
  7200. if (name) {
  7201. [dic setValue:name forKey:@"name"];
  7202. }
  7203. if (value) {
  7204. [dic setValue:value forKey:@"value"];
  7205. }
  7206. return dic;
  7207. }
  7208. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7209. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7210. orderCode = [self translateSingleQuote:orderCode];
  7211. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7212. 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];
  7213. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7214. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7215. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7216. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7217. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7218. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7219. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7220. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7221. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7222. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7223. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7224. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7225. NSString *email = [self textAtColumn:11 statement:stmt];
  7226. NSString *phone = [self textAtColumn:12 statement:stmt];
  7227. NSString *fax = [self textAtColumn:13 statement:stmt];
  7228. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7229. NSString *city = [self textAtColumn:15 statement:stmt];
  7230. NSString *state = [self textAtColumn:16 statement:stmt];
  7231. NSString *country = [self textAtColumn:17 statement:stmt];
  7232. NSString *name = [self textAtColumn:18 statement:stmt];
  7233. // contact id
  7234. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7235. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7236. [contact_id_dic setValue:@"text" forKey:@"control"];
  7237. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7238. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7239. [contact_id_dic setValue:@"true" forKey:@"required"];
  7240. [contact_id_dic setValue:contact_id forKey:@"value"];
  7241. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7242. // business card
  7243. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7244. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7245. [business_card_dic setValue:@"img" forKey:@"control"];
  7246. [business_card_dic setValue:@"1" forKey:@"disable"];
  7247. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7248. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7249. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7250. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7251. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7252. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7253. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7254. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7255. // fax
  7256. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7257. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7258. // zipcode
  7259. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7260. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7261. // city
  7262. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7263. [customer_dic setValue:city_dic forKey:@"item_12"];
  7264. // state
  7265. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7266. [customer_dic setValue:state_dic forKey:@"item_13"];
  7267. // country
  7268. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7269. [customer_dic setValue:country_dic forKey:@"item_14"];
  7270. // company name
  7271. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7272. [customer_dic setValue:company_dic forKey:@"item_2"];
  7273. // addr_1
  7274. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7275. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7276. // addr_2
  7277. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7278. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7279. // addr_3
  7280. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7281. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7282. // addr_4
  7283. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7284. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7285. // Contact
  7286. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7287. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7288. // email
  7289. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7290. [customer_dic setValue:email_dic forKey:@"item_8"];
  7291. // phone
  7292. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7293. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7294. // title
  7295. [customer_dic setValue:@"Customer" forKey:@"title"];
  7296. // count
  7297. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7298. }];
  7299. // setting
  7300. NSDictionary *setting = params[@"setting"];
  7301. NSNumber *hide = setting[@"CustomerHide"];
  7302. [customer_dic setValue:hide forKey:@"hide"];
  7303. return customer_dic;
  7304. }
  7305. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7306. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7307. // setting
  7308. NSDictionary *setting = params[@"setting"];
  7309. NSNumber *hide = setting[@"ShipToHide"];
  7310. [dic setValue:hide forKey:@"hide"];
  7311. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7312. orderCode = [self translateSingleQuote:orderCode];
  7313. 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];
  7314. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7315. NSString *cid = [self textAtColumn:0 statement:stmt];
  7316. NSString *name = [self textAtColumn:1 statement:stmt];
  7317. NSString *ext = [self textAtColumn:2 statement:stmt];
  7318. NSString *contact = [self textAtColumn:3 statement:stmt];
  7319. NSString *email = [self textAtColumn:4 statement:stmt];
  7320. NSString *fax = [self textAtColumn:5 statement:stmt];
  7321. NSString *phone = [self textAtColumn:6 statement:stmt];
  7322. // count
  7323. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7324. // title
  7325. [dic setValue:@"Ship To" forKey:@"title"];
  7326. // choose
  7327. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7328. [choose_dic setValue:@"choose" forKey:@"aname"];
  7329. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7330. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7331. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7332. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7333. @"receive_contact" : @"customer_contact",
  7334. @"receive_email" : @"customer_email",
  7335. @"receive_ext" : @"customer_contact_ext",
  7336. @"receive_fax" : @"customer_fax",
  7337. @"receive_name" : @"customer_name",
  7338. @"receive_phone" : @"customer_phone"},
  7339. @"refresh" : [NSNumber numberWithInteger:1],
  7340. @"type" : @"pull"};
  7341. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7342. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7343. @"name" : @"Add new address",
  7344. @"refresh" : [NSNumber numberWithInteger:1],
  7345. @"value" : @"new_addr"
  7346. };
  7347. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7348. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7349. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7350. @"receive_contact" : @"customer_contact",
  7351. @"receive_email" : @"customer_email",
  7352. @"receive_ext" : @"customer_contact_ext",
  7353. @"receive_fax" : @"customer_fax",
  7354. @"receive_name" : @"customer_name",
  7355. @"receive_phone" : @"customer_phone"},
  7356. @"name" : @"select_ship_to",
  7357. @"refresh" : [NSNumber numberWithInteger:1],
  7358. @"value" : @"Sales_Order_Ship_To"};
  7359. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7360. [dic setValue:choose_dic forKey:@"item_0"];
  7361. // contact id
  7362. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7363. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7364. [contact_id_dic setValue:@"true" forKey:@"required"];
  7365. [dic setValue:contact_id_dic forKey:@"item_1"];
  7366. // company name
  7367. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7368. [dic setValue:company_name_dic forKey:@"item_2"];
  7369. // address
  7370. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7371. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7372. if ([required integerValue]) {
  7373. [ext_dic setValue:@"true" forKey:@"required"];
  7374. }
  7375. [dic setValue:ext_dic forKey:@"item_3"];
  7376. // contact
  7377. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7378. [dic setValue:contact_dic forKey:@"item_4"];
  7379. // phone
  7380. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7381. [dic setValue:phone_dic forKey:@"item_5"];
  7382. // fax
  7383. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7384. [dic setValue:fax_dic forKey:@"item_6"];
  7385. // email
  7386. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7387. [dic setValue:email_dic forKey:@"item_7"];
  7388. }];
  7389. return dic;
  7390. }
  7391. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7392. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7393. // setting
  7394. NSDictionary *setting = params[@"setting"];
  7395. NSNumber *hide = setting[@"ShipFromHide"];
  7396. [dic setValue:hide forKey:@"hide"];
  7397. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7398. orderCode = [self translateSingleQuote:orderCode];
  7399. 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];
  7400. 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';";
  7401. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7402. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7403. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7404. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7405. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7406. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7407. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7408. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7409. if (!cid.length) {
  7410. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7411. cid = [self textAtColumn:0 statement:statment];
  7412. name = [self textAtColumn:1 statement:statment];
  7413. ext = [self textAtColumn:2 statement:statment];
  7414. contact = [self textAtColumn:3 statement:statment];
  7415. email = [self textAtColumn:4 statement:statment];
  7416. fax = [self textAtColumn:5 statement:statment];
  7417. phone = [self textAtColumn:6 statement:statment];
  7418. }];
  7419. }
  7420. // count
  7421. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7422. // title
  7423. [dic setValue:@"Ship From" forKey:@"title"];
  7424. // hide
  7425. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7426. // choose
  7427. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7428. [choose_dic setValue:@"choose" forKey:@"aname"];
  7429. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7430. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7431. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7432. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7433. @"sender_contact" : @"customer_contact",
  7434. @"sender_email" : @"customer_email",
  7435. @"sender_ext" : @"customer_contact_ext",
  7436. @"sender_fax" : @"customer_fax",
  7437. @"sender_name" : @"customer_name",
  7438. @"sender_phone" : @"customer_phone"},
  7439. @"name" : @"select_cid",
  7440. @"refresh" : [NSNumber numberWithInteger:0],
  7441. @"value" : @"Sales_Order_Ship_From"};
  7442. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7443. [dic setValue:choose_dic forKey:@"item_0"];
  7444. // contact id
  7445. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7446. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7447. [contact_id_dic setValue:@"true" forKey:@"required"];
  7448. [dic setValue:contact_id_dic forKey:@"item_1"];
  7449. // company name
  7450. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7451. [dic setValue:company_name_dic forKey:@"item_2"];
  7452. // address
  7453. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7454. [dic setValue:ext_dic forKey:@"item_3"];
  7455. // contact
  7456. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7457. [dic setValue:contact_dic forKey:@"item_4"];
  7458. // phone
  7459. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7460. [dic setValue:phone_dic forKey:@"item_5"];
  7461. // fax
  7462. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7463. [dic setValue:fax_dic forKey:@"item_6"];
  7464. // email
  7465. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7466. [dic setValue:email_dic forKey:@"item_7"];
  7467. }];
  7468. return dic;
  7469. }
  7470. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7471. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7472. // setting
  7473. NSDictionary *setting = params[@"setting"];
  7474. NSNumber *hide = setting[@"FreightBillToHide"];
  7475. [dic setValue:hide forKey:@"hide"];
  7476. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7477. orderCode = [self translateSingleQuote:orderCode];
  7478. 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];
  7479. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7480. NSString *cid = [self textAtColumn:0 statement:stmt];
  7481. NSString *name = [self textAtColumn:1 statement:stmt];
  7482. NSString *ext = [self textAtColumn:2 statement:stmt];
  7483. NSString *contact = [self textAtColumn:3 statement:stmt];
  7484. NSString *email = [self textAtColumn:4 statement:stmt];
  7485. NSString *fax = [self textAtColumn:5 statement:stmt];
  7486. NSString *phone = [self textAtColumn:6 statement:stmt];
  7487. // count
  7488. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7489. // title
  7490. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7491. // hide
  7492. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7493. // choose
  7494. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7495. [choose_dic setValue:@"choose" forKey:@"aname"];
  7496. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7497. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7498. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7499. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7500. @"shipping_billto_contact" : @"receive_contact",
  7501. @"shipping_billto_email" : @"receive_email",
  7502. @"shipping_billto_ext" : @"receive_ext",
  7503. @"shipping_billto_fax" : @"receive_fax",
  7504. @"shipping_billto_name" : @"receive_name",
  7505. @"shipping_billto_phone" : @"receive_phone"},
  7506. @"type" : @"pull"};
  7507. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7508. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7509. @"type" : @"pull",
  7510. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7511. @"shipping_billto_contact" : @"customer_contact",
  7512. @"shipping_billto_email" : @"customer_email",
  7513. @"shipping_billto_ext" : @"customer_contact_ext",
  7514. @"shipping_billto_fax" : @"customer_fax",
  7515. @"shipping_billto_name" : @"customer_name",
  7516. @"shipping_billto_phone" : @"customer_phone"}
  7517. };
  7518. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7519. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7520. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7521. @"shipping_billto_contact" : @"sender_contact",
  7522. @"shipping_billto_email" : @"sender_email",
  7523. @"shipping_billto_ext" : @"sender_ext",
  7524. @"shipping_billto_fax" : @"sender_fax",
  7525. @"shipping_billto_name" : @"sender_name",
  7526. @"shipping_billto_phone" : @"sender_phone"},
  7527. @"type" : @"pull"
  7528. };
  7529. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7530. NSDictionary *select_freight_bill_to_dic = @{
  7531. @"aname" : @"Select freight bill to",
  7532. @"name" : @"select_cid",
  7533. @"refresh" : [NSNumber numberWithInteger:0],
  7534. @"value" : @"Sales_Order_Freight_Bill_To",
  7535. @"key_map" : @{
  7536. @"shipping_billto_cid" : @"customer_cid",
  7537. @"shipping_billto_contact" : @"customer_contact",
  7538. @"shipping_billto_email" : @"customer_email",
  7539. @"shipping_billto_ext" : @"customer_contact_ext",
  7540. @"shipping_billto_fax" : @"customer_fax",
  7541. @"shipping_billto_name" : @"customer_name",
  7542. @"shipping_billto_phone" : @"customer_phone"
  7543. }
  7544. };
  7545. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7546. [dic setValue:choose_dic forKey:@"item_0"];
  7547. // contact id
  7548. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7549. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7550. [contact_id_dic setValue:@"true" forKey:@"required"];
  7551. [dic setValue:contact_id_dic forKey:@"item_1"];
  7552. // company name
  7553. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7554. [dic setValue:company_name_dic forKey:@"item_2"];
  7555. // address
  7556. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7557. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7558. if ([ext_required integerValue]) {
  7559. [ext_dic setValue:@"true" forKey:@"required"];
  7560. }
  7561. [dic setValue:ext_dic forKey:@"item_3"];
  7562. // contact
  7563. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7564. [dic setValue:contact_dic forKey:@"item_4"];
  7565. // phone
  7566. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7567. [dic setValue:phone_dic forKey:@"item_5"];
  7568. // fax
  7569. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7570. [dic setValue:fax_dic forKey:@"item_6"];
  7571. // email
  7572. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7573. [dic setValue:email_dic forKey:@"item_7"];
  7574. }];
  7575. return dic;
  7576. }
  7577. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7578. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7579. // setting
  7580. NSDictionary *setting = params[@"setting"];
  7581. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7582. [dic setValue:hide forKey:@"hide"];
  7583. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7584. orderCode = [self translateSingleQuote:orderCode];
  7585. 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];
  7586. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7587. NSString *cid = [self textAtColumn:0 statement:stmt];
  7588. NSString *name = [self textAtColumn:1 statement:stmt];
  7589. NSString *ext = [self textAtColumn:2 statement:stmt];
  7590. NSString *contact = [self textAtColumn:3 statement:stmt];
  7591. NSString *email = [self textAtColumn:4 statement:stmt];
  7592. NSString *fax = [self textAtColumn:5 statement:stmt];
  7593. NSString *phone = [self textAtColumn:6 statement:stmt];
  7594. // count
  7595. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7596. // title
  7597. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7598. // hide
  7599. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7600. // choose
  7601. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7602. [choose_dic setValue:@"choose" forKey:@"aname"];
  7603. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7604. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7605. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7606. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7607. @"billing_contact" : @"receive_contact",
  7608. @"billing_email" : @"receive_email",
  7609. @"billing_ext" : @"receive_ext",
  7610. @"billing_fax" : @"receive_fax",
  7611. @"billing_name" : @"receive_name",
  7612. @"billing_phone" : @"receive_phone"},
  7613. @"type" : @"pull"};
  7614. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7615. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7616. @"type" : @"pull",
  7617. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7618. @"billing_contact" : @"customer_contact",
  7619. @"billing_email" : @"customer_email",
  7620. @"billing_ext" : @"customer_contact_ext",
  7621. @"billing_fax" : @"customer_fax",
  7622. @"billing_name" : @"customer_name",
  7623. @"billing_phone" : @"customer_phone"}
  7624. };
  7625. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7626. NSDictionary *select_bill_to_dic = @{
  7627. @"aname" : @"Select bill to",
  7628. @"name" : @"select_cid",
  7629. @"refresh" : [NSNumber numberWithInteger:0],
  7630. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7631. @"key_map" : @{
  7632. @"billing_cid" : @"customer_cid",
  7633. @"billing_contact" : @"customer_contact",
  7634. @"billing_email" : @"customer_email",
  7635. @"billing_ext" : @"customer_contact_ext",
  7636. @"billing_fax" : @"customer_fax",
  7637. @"billing_name" : @"customer_name",
  7638. @"billing_phone" : @"customer_phone"
  7639. }
  7640. };
  7641. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7642. [dic setValue:choose_dic forKey:@"item_0"];
  7643. // contact id
  7644. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7645. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7646. [contact_id_dic setValue:@"true" forKey:@"required"];
  7647. [dic setValue:contact_id_dic forKey:@"item_1"];
  7648. // company name
  7649. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7650. [dic setValue:company_name_dic forKey:@"item_2"];
  7651. // address
  7652. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7653. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7654. if ([ext_required integerValue]) {
  7655. [ext_dic setValue:@"true" forKey:@"required"];
  7656. }
  7657. [dic setValue:ext_dic forKey:@"item_3"];
  7658. // contact
  7659. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7660. [dic setValue:contact_dic forKey:@"item_4"];
  7661. // phone
  7662. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7663. [dic setValue:phone_dic forKey:@"item_5"];
  7664. // fax
  7665. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7666. [dic setValue:fax_dic forKey:@"item_6"];
  7667. // email
  7668. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7669. [dic setValue:email_dic forKey:@"item_7"];
  7670. }];
  7671. return dic;
  7672. }
  7673. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7674. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7675. // setting
  7676. NSDictionary *setting = params[@"setting"];
  7677. NSNumber *hide = setting[@"ReturnToHide"];
  7678. [dic setValue:hide forKey:@"hide"];
  7679. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7680. orderCode = [self translateSingleQuote:orderCode];
  7681. 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];
  7682. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7683. NSString *cid = [self textAtColumn:0 statement:stmt];
  7684. NSString *name = [self textAtColumn:1 statement:stmt];
  7685. NSString *ext = [self textAtColumn:2 statement:stmt];
  7686. NSString *contact = [self textAtColumn:3 statement:stmt];
  7687. NSString *email = [self textAtColumn:4 statement:stmt];
  7688. NSString *fax = [self textAtColumn:5 statement:stmt];
  7689. NSString *phone = [self textAtColumn:6 statement:stmt];
  7690. // count
  7691. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7692. // title
  7693. [dic setValue:@"Return To" forKey:@"title"];
  7694. // hide
  7695. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7696. // choose
  7697. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7698. [choose_dic setValue:@"choose" forKey:@"aname"];
  7699. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7700. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7701. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7702. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7703. @"returnto_contact" : @"sender_contact",
  7704. @"returnto_email" : @"sender_email",
  7705. @"returnto_ext" : @"sender_ext",
  7706. @"returnto_fax" : @"sender_fax",
  7707. @"returnto_name" : @"sender_name",
  7708. @"returnto_phone" : @"sender_phone"},
  7709. @"type" : @"pull"};
  7710. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7711. NSDictionary *select_return_to_dic = @{
  7712. @"aname" : @"Select return to",
  7713. @"name" : @"select_cid",
  7714. @"refresh" : [NSNumber numberWithInteger:0],
  7715. @"value" : @"Contact_Return_To",
  7716. @"key_map" : @{
  7717. @"returnto_cid" : @"customer_cid",
  7718. @"returnto_contact" : @"customer_contact",
  7719. @"returnto_email" : @"customer_email",
  7720. @"returnto_ext" : @"customer_contact_ext",
  7721. @"returnto_fax" : @"customer_fax",
  7722. @"returnto_name" : @"customer_name",
  7723. @"returnto_phone" : @"customer_phone"
  7724. }
  7725. };
  7726. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7727. [dic setValue:choose_dic forKey:@"item_0"];
  7728. // contact id
  7729. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7730. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7731. [contact_id_dic setValue:@"true" forKey:@"required"];
  7732. [dic setValue:contact_id_dic forKey:@"item_1"];
  7733. // company name
  7734. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7735. [dic setValue:company_name_dic forKey:@"item_2"];
  7736. // address
  7737. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7738. [dic setValue:ext_dic forKey:@"item_3"];
  7739. // contact
  7740. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7741. [dic setValue:contact_dic forKey:@"item_4"];
  7742. // phone
  7743. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7744. [dic setValue:phone_dic forKey:@"item_5"];
  7745. // fax
  7746. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7747. [dic setValue:fax_dic forKey:@"item_6"];
  7748. // email
  7749. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7750. [dic setValue:email_dic forKey:@"item_7"];
  7751. }];
  7752. return dic;
  7753. }
  7754. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  7755. {
  7756. assert(params[@"user"]!=nil);
  7757. assert(params[@"contact_id"]!=nil);
  7758. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7759. orderCode = [self translateSingleQuote:orderCode];
  7760. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7761. __block double TotalCuft = 0;
  7762. __block double TotalWeight = 0;
  7763. __block int TotalCarton = 0;
  7764. __block double payments = 0;
  7765. // setting
  7766. NSDictionary *setting = params[@"setting"];
  7767. NSNumber *hide = setting[@"ModelInformationHide"];
  7768. [dic setValue:hide forKey:@"hide"];
  7769. 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];
  7770. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7771. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7772. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7773. // item id
  7774. int item_id = sqlite3_column_int(stmt, 0);
  7775. // count
  7776. int item_count = sqlite3_column_int(stmt, 1);
  7777. // stockUom
  7778. int stockUom = sqlite3_column_int(stmt, 2);
  7779. // unit price
  7780. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7781. NSString* Price=nil;
  7782. if([str_price isEqualToString:@""])
  7783. {
  7784. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  7785. if(price==nil)
  7786. Price=@"No Price.";
  7787. else
  7788. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7789. }
  7790. else
  7791. {
  7792. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7793. }
  7794. // discount
  7795. double discount = sqlite3_column_double(stmt, 4);
  7796. // name
  7797. NSString *name = [self textAtColumn:5 statement:stmt];
  7798. // description
  7799. NSString *description = [self textAtColumn:6 statement:stmt];
  7800. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7801. // line note
  7802. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7803. int avaulability = sqlite3_column_int(stmt, 8);
  7804. // img
  7805. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7806. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  7807. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7808. if(combine != nil)
  7809. {
  7810. // int citem=0;
  7811. int bcount=[[combine valueForKey:@"count"] intValue];
  7812. for(int bc=0;bc<bcount;bc++)
  7813. {
  7814. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7815. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7816. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7817. subTotal += uprice * modulus * item_count;
  7818. }
  7819. }
  7820. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7821. [model_dic setValue:@"model" forKey:@"control"];
  7822. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7823. [model_dic setValue:description forKey:@"description"];
  7824. [model_dic setValue:line_note forKey:@"note"];
  7825. [model_dic setValue:img forKey:@"img_url"];
  7826. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7827. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7828. [model_dic setValue:Price forKey:@"unit_price"];
  7829. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7830. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7831. if (combine) {
  7832. [model_dic setValue:combine forKey:@"combine"];
  7833. }
  7834. // well,what under the row is the info for total
  7835. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7836. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7837. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7838. int carton=[bsubtotaljson[@"carton"] intValue];
  7839. TotalCuft += cuft;
  7840. TotalWeight += weight;
  7841. TotalCarton += carton;
  7842. payments += subTotal;
  7843. //---------------------------
  7844. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7845. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7846. }];
  7847. [dic setValue:@"Model Information" forKey:@"title"];
  7848. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7849. return dic;
  7850. }
  7851. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7852. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7853. [dic setValue:@"Remarks Content" forKey:@"title"];
  7854. // setting
  7855. NSDictionary *setting = params[@"setting"];
  7856. NSNumber *hide = setting[@"RemarksContentHide"];
  7857. [dic setValue:hide forKey:@"hide"];
  7858. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7859. orderCode = [self translateSingleQuote:orderCode];
  7860. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7861. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7862. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7863. int mustCall = sqlite3_column_int(stmt, 1);
  7864. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7865. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7866. NSDictionary *po_dic = @{
  7867. @"aname" : @"PO#",
  7868. @"control" : @"edit",
  7869. @"keyboard" : @"text",
  7870. @"name" : @"poNumber",
  7871. @"value" : poNumber
  7872. };
  7873. NSDictionary *must_call_dic = @{
  7874. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7875. @"control" : @"switch",
  7876. @"name" : @"must_call",
  7877. @"value" : mustCall ? @"true" : @"false"
  7878. };
  7879. NSDictionary *general_notes_dic = @{
  7880. @"aname" : @"General notes",
  7881. @"control" : @"text_view",
  7882. @"keyboard" : @"text",
  7883. @"name" : @"comments",
  7884. @"value" : generalNotes
  7885. };
  7886. // NSDictionary *internal_notes_dic = @{
  7887. // @"aname" : @"Internal notes",
  7888. // @"control" : @"text_view",
  7889. // @"keyboard" : @"text",
  7890. // @"name" : @"internal_notes",
  7891. // @"value" : internalNotes
  7892. // };
  7893. [dic setValue:po_dic forKey:@"item_0"];
  7894. [dic setValue:must_call_dic forKey:@"item_1"];
  7895. [dic setValue:general_notes_dic forKey:@"item_2"];
  7896. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  7897. }];
  7898. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7899. return dic;
  7900. }
  7901. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7902. // params
  7903. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7904. orderCode = [self translateSingleQuote:orderCode];
  7905. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7906. // setting
  7907. NSDictionary *setting = params[@"setting"];
  7908. NSNumber *hide = setting[@"OrderTotalHide"];
  7909. [dic setValue:hide forKey:@"hide"];
  7910. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7911. __block double lift_gate_value = 0;
  7912. __block double handling_fee = 0;
  7913. __block double shipping = 0;
  7914. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7915. int lift_gate = sqlite3_column_int(stmt, 0);
  7916. lift_gate_value = sqlite3_column_double(stmt, 1);
  7917. shipping = sqlite3_column_double(stmt, 2);
  7918. handling_fee = sqlite3_column_double(stmt, 3);
  7919. if (!lift_gate) {
  7920. lift_gate_value = 0;
  7921. }
  7922. }];
  7923. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7924. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7925. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7926. double payments = [[total valueForKey:@"payments"] doubleValue];
  7927. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7928. double totalPrice = payments;
  7929. [dic setValue:@"Order Total" forKey:@"title"];
  7930. NSDictionary *payments_dic = @{
  7931. @"align" : @"right",
  7932. @"aname" : @"Payments/Credits",
  7933. @"control" : @"text",
  7934. @"name" : @"paymentsAndCredits",
  7935. @"type" : @"price",
  7936. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7937. };
  7938. [dic setValue:payments_dic forKey:@"item_0"];
  7939. // version 1.71 remove
  7940. // NSDictionary *handling_fee_dic = @{
  7941. // @"align" : @"right",
  7942. // @"aname" : @"Handling Fee",
  7943. // @"control" : @"text",
  7944. // @"name" : @"handling_fee_value",
  7945. // @"required" : @"true",
  7946. // @"type" : @"price",
  7947. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7948. // };
  7949. NSDictionary *shipping_dic = @{
  7950. @"align" : @"right",
  7951. @"aname" : @"Shipping*",
  7952. @"control" : @"text",
  7953. @"name" : @"shipping",
  7954. @"required" : @"true",
  7955. @"type" : @"price",
  7956. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7957. };
  7958. NSDictionary *lift_gate_dic = @{
  7959. @"align" : @"right",
  7960. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7961. @"control" : @"text",
  7962. @"name" : @"lift_gate_value",
  7963. @"required" : @"true",
  7964. @"type" : @"price",
  7965. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7966. };
  7967. int item_count = 1;
  7968. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7969. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7970. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7971. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7972. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7973. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7974. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7975. } else {
  7976. }
  7977. }
  7978. // version 1.71 remove
  7979. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7980. NSDictionary *total_price_dic = @{
  7981. @"align" : @"right",
  7982. @"aname" : @"Total",
  7983. @"control" : @"text",
  7984. @"name" : @"totalPrice",
  7985. @"type" : @"price",
  7986. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7987. };
  7988. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7989. NSDictionary *total_cuft_dic = @{
  7990. @"align" : @"right",
  7991. @"aname" : @"Total Cuft",
  7992. @"control" : @"text",
  7993. @"name" : @"",
  7994. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7995. };
  7996. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7997. NSDictionary *total_weight_dic = @{
  7998. @"align" : @"right",
  7999. @"aname" : @"Total Weight",
  8000. @"control" : @"text",
  8001. @"name" : @"",
  8002. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8003. };
  8004. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8005. NSDictionary *total_carton_dic = @{
  8006. @"align" : @"right",
  8007. @"aname" : @"Total Carton",
  8008. @"control" : @"text",
  8009. @"name" : @"",
  8010. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8011. };
  8012. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8013. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8014. return dic;
  8015. }
  8016. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8017. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8018. orderCode = [self translateSingleQuote:orderCode];
  8019. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8020. // setting
  8021. NSDictionary *setting = params[@"setting"];
  8022. NSNumber *hide = setting[@"SignatureHide"];
  8023. [dic setValue:hide forKey:@"hide"];
  8024. [dic setValue:@"Signature" forKey:@"title"];
  8025. __block NSString *pic_path = @"";
  8026. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8027. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8028. pic_path = [self textAtColumn:0 statement:stmt];
  8029. }];
  8030. NSDictionary *pic_dic = @{
  8031. @"aname" : @"Signature",
  8032. @"avalue" :pic_path,
  8033. @"control" : @"signature",
  8034. @"name" : @"sign_picpath",
  8035. @"value" : pic_path
  8036. };
  8037. [dic setValue:pic_dic forKey:@"item_0"];
  8038. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8039. return dic;
  8040. }
  8041. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8042. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8043. // setting
  8044. NSDictionary *setting = params[@"setting"];
  8045. NSNumber *hide = setting[@"ShippingMethodHide"];
  8046. [dic setValue:hide forKey:@"hide"];
  8047. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8048. orderCode = [self translateSingleQuote:orderCode];
  8049. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8050. __block NSString *logist = @"";
  8051. __block NSString *lift_gate = @"";
  8052. __block int lift_gate_integer = 0;
  8053. __block NSString *logistic_note = @"";
  8054. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8055. logist = [self textAtColumn:0 statement:stmt];
  8056. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8057. logistic_note = [self textAtColumn:2 statement:stmt];
  8058. }];
  8059. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8060. [dic setValue:@"Shipping Method" forKey:@"title"];
  8061. // val_0
  8062. int PERSONAL_PICK_UP_check = 0;
  8063. int USE_MY_CARRIER_check = 0;
  8064. NSString *logistic_note_text = @"";
  8065. int will_call_check = 0;
  8066. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8067. will_call_check = 1;
  8068. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8069. PERSONAL_PICK_UP_check = 1;
  8070. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8071. USE_MY_CARRIER_check = 1;
  8072. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8073. logistic_note = [logistic_note_array firstObject];
  8074. if (logistic_note_array.count > 1) {
  8075. logistic_note_text = [logistic_note_array lastObject];
  8076. }
  8077. }
  8078. }
  8079. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8080. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8081. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8082. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8083. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8084. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8085. NSDictionary *val0_subItem_item0 = @{
  8086. @"aname" : @"Option",
  8087. @"cadedate" : @{
  8088. @"count" : [NSNumber numberWithInteger:2],
  8089. @"val_0" : @{
  8090. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8091. @"refresh" : [NSNumber numberWithInteger:0],
  8092. @"value" : @"PERSONAL PICK UP",
  8093. @"value_id" : @"PERSONAL PICK UP"
  8094. },
  8095. @"val_1" : @{
  8096. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8097. @"refresh" : [NSNumber numberWithInteger:0],
  8098. @"sub_item" : @{
  8099. @"count" : [NSNumber numberWithInteger:1],
  8100. @"item_0" : @{
  8101. @"aname" : @"BOL",
  8102. @"control" : @"edit",
  8103. @"keyboard" : @"text",
  8104. @"name" : @"logist_note_text",
  8105. @"refresh" : [NSNumber numberWithInteger:0],
  8106. @"required" : @"false",
  8107. @"value" : logistic_note_text
  8108. }
  8109. },
  8110. @"value" : @"USE MY CARRIER",
  8111. @"value_id" : @"USE MY CARRIER"
  8112. }
  8113. },
  8114. @"control" : @"enum",
  8115. @"name" : @"logistic_note",
  8116. @"required" : @"true",
  8117. @"single_select" : @"true"
  8118. };
  8119. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8120. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8121. // val_1
  8122. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8123. if([lift_gate isEqualToString:@""]) {
  8124. if (lift_gate_integer == 1) {
  8125. lift_gate = @"true";
  8126. } else {
  8127. lift_gate = @"false";
  8128. }
  8129. }
  8130. int common_carrier_check = 0;
  8131. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8132. common_carrier_check = 1;
  8133. [params setValue:lift_gate forKey:@"lift_gate"];
  8134. }
  8135. NSDictionary *val_1 = @{
  8136. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8137. @"sub_item" : @{
  8138. @"count" : [NSNumber numberWithInteger:1],
  8139. @"item_0" : @{
  8140. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8141. @"control" : @"switch",
  8142. @"name" : @"lift_gate",
  8143. @"refresh" : [NSNumber numberWithInteger:1],
  8144. @"required" : @"true",
  8145. @"value" : lift_gate
  8146. }
  8147. },
  8148. @"value" : @"COMMON CARRIER",
  8149. @"value_id" : @"COMMON CARRIER"
  8150. };
  8151. // cadedate
  8152. NSDictionary *cadedate = @{
  8153. @"count" : [NSNumber numberWithInteger:2],
  8154. @"val_0" : val_0,
  8155. @"val_1" : val_1
  8156. };
  8157. // item_0
  8158. NSMutableDictionary *item_0 = @{
  8159. @"aname" : @"Shipping",
  8160. @"cadedate" : cadedate,
  8161. @"control" : @"enum",
  8162. @"name" : @"logist",
  8163. @"refresh" : [NSNumber numberWithInteger:1],
  8164. @"single_select" : @"true",
  8165. }.mutableCopy;
  8166. NSNumber *required = setting[@"ShippingMethodRequire"];
  8167. if ([required integerValue]) {
  8168. [item_0 setValue:@"true" forKey:@"required"];
  8169. }
  8170. [dic setValue:item_0 forKey:@"item_0"];
  8171. if ([logist isEqualToString:@"WILL CALL"]) {
  8172. [dic removeObjectForKey:@"lift_gate"];
  8173. }
  8174. return dic;
  8175. }
  8176. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8177. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8178. orderCode = [self translateSingleQuote:orderCode];
  8179. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8180. __block int submit_as_integer = 0;
  8181. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8182. submit_as_integer = sqlite3_column_int(stmt, 0);
  8183. }];
  8184. int check11 = 0;
  8185. int check10 = 0;
  8186. if (submit_as_integer == 11) {
  8187. check11 = 1;
  8188. }
  8189. if (submit_as_integer == 10) {
  8190. check10 = 1;
  8191. }
  8192. // section_0
  8193. NSMutableDictionary *dic = @{
  8194. @"count" : @(1),
  8195. @"item_0" : @{
  8196. @"aname" : @"Submit Order As",
  8197. @"cadedate" : @{
  8198. @"count" : @(2),
  8199. @"val_0" : @{
  8200. @"check" : [NSNumber numberWithInteger:check11],
  8201. @"value" : @"Sales Order",
  8202. @"value_id" : @(11)
  8203. },
  8204. @"val_1" : @{
  8205. @"check" : [NSNumber numberWithInteger:check10],
  8206. @"value" : @"Quote",
  8207. @"value_id" : @(10)
  8208. }
  8209. },
  8210. @"control" : @"enum",
  8211. @"name" : @"erpOrderStatus",
  8212. @"required" : @"true",
  8213. @"single_select" : @"true"
  8214. },
  8215. @"title" : @"Order Type"
  8216. }.mutableCopy;
  8217. // setting
  8218. NSDictionary *setting = params[@"setting"];
  8219. NSNumber *hide = setting[@"OrderTypeHide"];
  8220. [dic setValue:hide forKey:@"hide"];
  8221. return dic;
  8222. }
  8223. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8224. countryCode = [self translateSingleQuote:countryCode];
  8225. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8226. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8227. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8228. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8229. if (name == NULL) {
  8230. name = "";
  8231. }
  8232. if (code == NULL) {
  8233. code = "";
  8234. }
  8235. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8236. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8237. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8238. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8239. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8240. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8241. }
  8242. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8243. [container setValue:stateDic forKey:key];
  8244. }] mutableCopy];
  8245. [ret removeObjectForKey:@"result"];
  8246. // failure 可以不用了,一样的
  8247. if (ret.allKeys.count == 0) {
  8248. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8249. [stateDic setValue:@"Other" forKey:@"value"];
  8250. [stateDic setValue:@"" forKey:@"value_id"];
  8251. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8252. if (state_code && [@"" isEqualToString:state_code]) {
  8253. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8254. }
  8255. NSString *key = [NSString stringWithFormat:@"val_0"];
  8256. [ret setValue:stateDic forKey:key];
  8257. }
  8258. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8259. return ret;
  8260. }
  8261. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8262. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8263. orderCode = [self translateSingleQuote:orderCode];
  8264. 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];
  8265. __block NSString *payType = @"";
  8266. __block NSString *firstName = @"";
  8267. __block NSString *lastName = @"";
  8268. __block NSString *addr1 = @"";
  8269. __block NSString *addr2 = @"";
  8270. __block NSString *zipcode = @"";
  8271. __block NSString *cardType = @"";
  8272. __block NSString *cardNumber = @"";
  8273. __block NSString *securityCode = @"";
  8274. __block NSString *month = @"";
  8275. __block NSString *year = @"";
  8276. __block NSString *city = @"";
  8277. __block NSString *state = @"";
  8278. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8279. payType = [self textAtColumn:0 statement:stmt];
  8280. firstName = [self textAtColumn:1 statement:stmt];
  8281. lastName = [self textAtColumn:2 statement:stmt];
  8282. addr1 = [self textAtColumn:3 statement:stmt];
  8283. addr2 = [self textAtColumn:4 statement:stmt];
  8284. zipcode = [self textAtColumn:5 statement:stmt];
  8285. cardType = [self textAtColumn:6 statement:stmt];
  8286. cardNumber = [self textAtColumn:7 statement:stmt];
  8287. securityCode = [self textAtColumn:8 statement:stmt];
  8288. month = [self textAtColumn:9 statement:stmt];
  8289. year = [self textAtColumn:10 statement:stmt];
  8290. city = [self textAtColumn:11 statement:stmt];
  8291. state = [self textAtColumn:12 statement:stmt];
  8292. }];
  8293. NSString *required = @"true";
  8294. // setting
  8295. NSDictionary *setting = params[@"setting"];
  8296. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8297. if ([requiredNumber integerValue]) {
  8298. required = @"true";
  8299. } else {
  8300. required = @"false";
  8301. }
  8302. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8303. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8304. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8305. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8306. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8307. NSString *type = [self textAtColumn:1 statement:stmt];
  8308. NSMutableDictionary *val = @{
  8309. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8310. @"value" : type,
  8311. @"value_id" : type_id
  8312. }.mutableCopy;
  8313. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  8314. NSDictionary *sub_item = @{
  8315. @"count" : @(3),
  8316. @"item_0" : @{
  8317. @"aname" : @"choose",
  8318. @"control" : @"multi_action",
  8319. @"count" : @(1),
  8320. @"item_0" : @{
  8321. @"aname" : @"Same as customer",
  8322. @"key_map" : @{
  8323. @"credit_card_address1" : @"customer_address1",
  8324. @"credit_card_address2" : @"customer_address2",
  8325. @"credit_card_city" : @"customer_city",
  8326. @"credit_card_first_name" : @"customer_first_name",
  8327. @"credit_card_last_name" : @"customer_last_name",
  8328. @"credit_card_state" : @"customer_state",
  8329. @"credit_card_zipcode" : @"customer_zipcode"
  8330. },
  8331. @"type" : @"pull"
  8332. }
  8333. },
  8334. @"item_1" : @{
  8335. @"aname" : @"",
  8336. @"color" : @"red",
  8337. @"control" : @"text",
  8338. @"name" : @"",
  8339. @"value" : @"USA Credit cards only"
  8340. },
  8341. @"item_2" : @{
  8342. @"aname" : @"Fill",
  8343. @"cadedate" : @{
  8344. @"count" : @(2),
  8345. @"val_0" : @{
  8346. @"check" : @(1),
  8347. @"sub_item" : @{
  8348. @"count" : @(11),
  8349. @"item_0" : @{
  8350. @"aname" : @"Type",
  8351. @"cadedate" : @{
  8352. @"count" : @(2),
  8353. @"val_0" : @{
  8354. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8355. @"value" : @"VISA",
  8356. @"value_id" : @"VISA"/*@(0)*/
  8357. },
  8358. @"val_1" : @{
  8359. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8360. @"value" : @"MASTER CARD",
  8361. @"value_id" : @"MASTER CARD"/*@(1)*/
  8362. }
  8363. },
  8364. @"control" : @"enum",
  8365. @"name" : @"credit_card_type",
  8366. @"required" : @"true",
  8367. @"single_select" : @"true"
  8368. },
  8369. @"item_1" : @{
  8370. @"aname" : @"Number",
  8371. @"control" : @"edit",
  8372. @"keyboard" : @"int",
  8373. @"length" : @"16",
  8374. @"name" : @"credit_card_number",
  8375. @"required" : @"true",
  8376. @"value" : cardNumber
  8377. },
  8378. @"item_10" : @{
  8379. @"aname" : @"State",
  8380. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8381. @"control" : @"enum",
  8382. @"enum" : @"true",
  8383. @"name" : @"credit_card_state",
  8384. @"required" : @"true",
  8385. @"single_select" : @"true"
  8386. },
  8387. @"item_2" : @{
  8388. @"aname" : @"Expiration Date",
  8389. @"control" : @"monthpicker",
  8390. @"name" : @"credit_card_expiration",
  8391. @"required" : @"true",
  8392. @"type" : @"date",
  8393. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8394. },
  8395. @"item_3" : @{
  8396. @"aname" : @"Security Code",
  8397. @"control" : @"edit",
  8398. @"keyboard" : @"int",
  8399. @"length" : @"3",
  8400. @"name" : @"credit_card_security_code",
  8401. @"required" : @"true",
  8402. @"value" : securityCode
  8403. },
  8404. @"item_4" : @{
  8405. @"aname" : @"First Name",
  8406. @"control" : @"edit",
  8407. @"keyboard" : @"text",
  8408. @"name" : @"credit_card_first_name",
  8409. @"required" : @"true",
  8410. @"value" : firstName
  8411. },
  8412. @"item_5" : @{
  8413. @"aname" : @"Last Name",
  8414. @"control" : @"edit",
  8415. @"keyboard" : @"text",
  8416. @"name" : @"credit_card_last_name",
  8417. @"required" : @"true",
  8418. @"value" : lastName
  8419. },
  8420. @"item_6" : @{
  8421. @"aname" : @"Address 1",
  8422. @"control" : @"edit",
  8423. @"keyboard" : @"text",
  8424. @"name" : @"credit_card_address1",
  8425. @"required" : @"true",
  8426. @"value" : addr1
  8427. },
  8428. @"item_7" : @{
  8429. @"aname" : @"Address 2",
  8430. @"control" : @"edit",
  8431. @"keyboard" : @"text",
  8432. @"name" : @"credit_card_address2",
  8433. @"value" : addr2
  8434. },
  8435. @"item_8" : @{
  8436. @"aname" : @"zip code",
  8437. @"control" : @"edit",
  8438. @"keyboard" : @"text",
  8439. @"name" : @"credit_card_zipcode",
  8440. @"required" : @"true",
  8441. @"value" : zipcode
  8442. },
  8443. @"item_9" : @{
  8444. @"aname" : @"City",
  8445. @"control" : @"edit",
  8446. @"keyboard" : @"text",
  8447. @"name" : @"credit_card_city",
  8448. @"required" : @"true",
  8449. @"value" : city
  8450. }
  8451. },
  8452. @"value" : @"Fill Now",
  8453. @"value_id" : @""
  8454. },
  8455. @"val_1" : @{
  8456. @"check" : @(0),
  8457. @"value" : @"Fill Later",
  8458. @"value_id" : @""
  8459. }
  8460. },
  8461. @"control" : @"enum",
  8462. @"name" : @"",
  8463. @"single_select" : @"true"
  8464. }
  8465. };
  8466. [val setObject:sub_item forKey:@"sub_item"];
  8467. }
  8468. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8469. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8470. }];
  8471. // "section_2"
  8472. NSMutableDictionary *dic = @{
  8473. @"count" : @(1),
  8474. @"item_0" : @{
  8475. @"aname" : @"Payment",
  8476. @"required" : required,
  8477. @"cadedate" : cadedate,
  8478. // @{
  8479. // @"count" : @(6),
  8480. // @"val_3" : @{
  8481. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8482. // @"value" : @"Check",
  8483. // @"value_id" : @"Check"
  8484. // },
  8485. // @"val_2" : @{
  8486. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8487. // @"value" : @"Cash",
  8488. // @"value_id" : @"Cash"
  8489. // },
  8490. // @"val_1" : @{
  8491. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8492. // @"value" : @"NET 60",
  8493. // @"value_id" : @"NET 30"
  8494. // },
  8495. // @"val_4" : @{
  8496. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8497. // @"value" : @"Wire Transfer",
  8498. // @"value_id" : @"Wire Transfer"
  8499. // },
  8500. // @"val_0" : @{
  8501. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8502. // @"sub_item" : @{
  8503. // @"count" : @(3),
  8504. // @"item_0" : @{
  8505. // @"aname" : @"choose",
  8506. // @"control" : @"multi_action",
  8507. // @"count" : @(1),
  8508. // @"item_0" : @{
  8509. // @"aname" : @"Same as customer",
  8510. // @"key_map" : @{
  8511. // @"credit_card_address1" : @"customer_address1",
  8512. // @"credit_card_address2" : @"customer_address2",
  8513. // @"credit_card_city" : @"customer_city",
  8514. // @"credit_card_first_name" : @"customer_first_name",
  8515. // @"credit_card_last_name" : @"customer_last_name",
  8516. // @"credit_card_state" : @"customer_state",
  8517. // @"credit_card_zipcode" : @"customer_zipcode"
  8518. // },
  8519. // @"type" : @"pull"
  8520. // }
  8521. // },
  8522. // @"item_1" : @{
  8523. // @"aname" : @"",
  8524. // @"color" : @"red",
  8525. // @"control" : @"text",
  8526. // @"name" : @"",
  8527. // @"value" : @"USA Credit cards only"
  8528. // },
  8529. // @"item_2" : @{
  8530. // @"aname" : @"Fill",
  8531. // @"cadedate" : @{
  8532. // @"count" : @(2),
  8533. // @"val_0" : @{
  8534. // @"check" : @(1),
  8535. // @"sub_item" : @{
  8536. // @"count" : @(11),
  8537. // @"item_0" : @{
  8538. // @"aname" : @"Type",
  8539. // @"cadedate" : @{
  8540. // @"count" : @(2),
  8541. // @"val_0" : @{
  8542. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8543. // @"value" : @"VISA",
  8544. // @"value_id" : @(0)
  8545. // },
  8546. // @"val_1" : @{
  8547. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8548. // @"value" : @"MASTER CARD",
  8549. // @"value_id" : @(1)
  8550. // }
  8551. // },
  8552. // @"control" : @"enum",
  8553. // @"name" : @"credit_card_type",
  8554. // @"required" : @"true",
  8555. // @"single_select" : @"true"
  8556. // },
  8557. // @"item_1" : @{
  8558. // @"aname" : @"Number",
  8559. // @"control" : @"edit",
  8560. // @"keyboard" : @"int",
  8561. // @"length" : @"16",
  8562. // @"name" : @"credit_card_number",
  8563. // @"required" : @"true",
  8564. // @"value" : cardNumber
  8565. // },
  8566. // @"item_10" : @{
  8567. // @"aname" : @"State",
  8568. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8569. // @"control" : @"enum",
  8570. // @"enum" : @"true",
  8571. // @"name" : @"credit_card_state",
  8572. // @"required" : @"true",
  8573. // @"single_select" : @"true"
  8574. // },
  8575. // @"item_2" : @{
  8576. // @"aname" : @"Expiration Date",
  8577. // @"control" : @"monthpicker",
  8578. // @"name" : @"credit_card_expiration",
  8579. // @"required" : @"true",
  8580. // @"type" : @"date",
  8581. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8582. // },
  8583. // @"item_3" : @{
  8584. // @"aname" : @"Security Code",
  8585. // @"control" : @"edit",
  8586. // @"keyboard" : @"int",
  8587. // @"length" : @"3",
  8588. // @"name" : @"credit_card_security_code",
  8589. // @"required" : @"true",
  8590. // @"value" : securityCode
  8591. // },
  8592. // @"item_4" : @{
  8593. // @"aname" : @"First Name",
  8594. // @"control" : @"edit",
  8595. // @"keyboard" : @"text",
  8596. // @"name" : @"credit_card_first_name",
  8597. // @"required" : @"true",
  8598. // @"value" : firstName
  8599. // },
  8600. // @"item_5" : @{
  8601. // @"aname" : @"Last Name",
  8602. // @"control" : @"edit",
  8603. // @"keyboard" : @"text",
  8604. // @"name" : @"credit_card_last_name",
  8605. // @"required" : @"true",
  8606. // @"value" : lastName
  8607. // },
  8608. // @"item_6" : @{
  8609. // @"aname" : @"Address 1",
  8610. // @"control" : @"edit",
  8611. // @"keyboard" : @"text",
  8612. // @"name" : @"credit_card_address1",
  8613. // @"required" : @"true",
  8614. // @"value" : addr1
  8615. // },
  8616. // @"item_7" : @{
  8617. // @"aname" : @"Address 2",
  8618. // @"control" : @"edit",
  8619. // @"keyboard" : @"text",
  8620. // @"name" : @"credit_card_address2",
  8621. // @"value" : addr2
  8622. // },
  8623. // @"item_8" : @{
  8624. // @"aname" : @"zip code",
  8625. // @"control" : @"edit",
  8626. // @"keyboard" : @"text",
  8627. // @"name" : @"credit_card_zipcode",
  8628. // @"required" : @"true",
  8629. // @"value" : zipcode
  8630. // },
  8631. // @"item_9" : @{
  8632. // @"aname" : @"City",
  8633. // @"control" : @"edit",
  8634. // @"keyboard" : @"text",
  8635. // @"name" : @"credit_card_city",
  8636. // @"required" : @"true",
  8637. // @"value" : city
  8638. // }
  8639. // },
  8640. // @"value" : @"Fill Now",
  8641. // @"value_id" : @""
  8642. // },
  8643. // @"val_1" : @{
  8644. // @"check" : @(0),
  8645. // @"value" : @"Fill Later",
  8646. // @"value_id" : @""
  8647. // }
  8648. // },
  8649. // @"control" : @"enum",
  8650. // @"name" : @"",
  8651. // @"single_select" : @"true"
  8652. // }
  8653. // },
  8654. // @"value" : @"Visa/Master",
  8655. // @"value_id" : @"Credit Card"
  8656. // },
  8657. // @"val_5" : @{
  8658. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8659. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8660. // @"value_id" : @"FOLLOW EXISTING"
  8661. // }
  8662. // },
  8663. @"control" : @"enum",
  8664. @"name" : @"paymentType",
  8665. @"single_select" : @"true"
  8666. },
  8667. @"title" : @"Payment Information"
  8668. }.mutableCopy;
  8669. NSNumber *hide = setting[@"PaymentInformationHide"];
  8670. [dic setValue:hide forKey:@"hide"];
  8671. return dic;
  8672. }
  8673. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8674. // params
  8675. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8676. orderCode = [self translateSingleQuote:orderCode];
  8677. // 缺货检查
  8678. 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];
  8679. __block BOOL outOfStock = NO;
  8680. sqlite3 *database = db;
  8681. if (!db) {
  8682. database = [iSalesDB get_db];
  8683. }
  8684. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8685. int availability = sqlite3_column_int(stmt, 0);
  8686. int item_qty = sqlite3_column_int(stmt, 1);
  8687. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8688. }];
  8689. if (!db) {
  8690. [iSalesDB close_db:database];
  8691. }
  8692. return outOfStock;
  8693. }
  8694. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8695. {
  8696. assert(params[@"user"]!=nil);
  8697. assert(params[@"contact_id"]!=nil);
  8698. assert(params[@"can_create_backorder"]!=nil);
  8699. sqlite3 *db = [iSalesDB get_db];
  8700. // params
  8701. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8702. orderCode = [self translateSingleQuote:orderCode];
  8703. // 缺货检查
  8704. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8705. if (![params[@"can_create_backorder"] boolValue]) {
  8706. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8707. if (out_of_stock) {
  8708. [iSalesDB close_db:db];
  8709. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8710. [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"];
  8711. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8712. return [RAConvertor dict2data:resultDic];
  8713. }
  8714. }
  8715. // UISetting
  8716. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8717. NSString *cachefolder = [paths objectAtIndex:0];
  8718. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8719. NSData* json =nil;
  8720. json=[NSData dataWithContentsOfFile:img_cache];
  8721. NSError *error=nil;
  8722. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8723. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8724. [params setObject:setting forKey:@"setting"];
  8725. int section_count = 0;
  8726. // 0 Order Type 1 Shipping Method 2 Payment Information
  8727. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8728. // 0 Order Type
  8729. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8730. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8731. [ret setValue:order_type_dic forKey:key0];
  8732. // 1 Shipping Method
  8733. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8734. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8735. [ret setValue:shipping_method_dic forKey:key1];
  8736. // 2 Payment Information
  8737. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8738. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8739. [ret setValue:payment_info_dic forKey:key2];
  8740. // 3 Customer
  8741. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8742. NSDictionary *customer_dic = [self customerDic:params db:db];
  8743. [ret setValue:customer_dic forKey:key3];
  8744. // 4 Ship To
  8745. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8746. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8747. [ret setValue:ship_to_dic forKey:key4];
  8748. // 5 Ship From
  8749. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8750. if (![shipFromDisable integerValue]) {
  8751. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8752. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8753. [ret setValue:ship_from_dic forKey:key5];
  8754. }
  8755. // 6 Freight Bill To
  8756. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8757. if (![freightBillToDisable integerValue]) {
  8758. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8759. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8760. [ret setValue:freight_bill_to forKey:key6];
  8761. }
  8762. // 7 Merchandise Bill To
  8763. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8764. if (![merchandiseBillToDisable integerValue]) {
  8765. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8766. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8767. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8768. }
  8769. // 8 Return To
  8770. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8771. if (![returnToDisable integerValue]) {
  8772. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8773. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8774. [ret setValue:return_to_dic forKey:key8];
  8775. }
  8776. // 9 Model Information
  8777. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8778. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8779. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8780. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8781. [ret setValue:model_info_dic forKey:key9];
  8782. // 10 Remarks Content
  8783. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8784. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8785. [ret setValue:remarks_content_dic forKey:key10];
  8786. // 11 Order Total
  8787. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8788. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8789. [ret setValue:order_total_dic forKey:key11];
  8790. // 12 Signature
  8791. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8792. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8793. [ret setValue:sign_dic forKey:key12];
  8794. [ret setValue:@(section_count) forKey:@"section_count"];
  8795. [iSalesDB close_db:db];
  8796. return [RAConvertor dict2data:ret];
  8797. // return nil;
  8798. }
  8799. #pragma mark addr editor
  8800. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8801. NSMutableDictionary *new_item = [item mutableCopy];
  8802. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8803. return new_item;
  8804. }
  8805. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8806. {
  8807. // "is_subaction" = true;
  8808. // orderCode = MOB1608240002;
  8809. // password = 123456;
  8810. // "subaction_tag" = 1;
  8811. // user = EvanK;
  8812. // {
  8813. // "is_subaction" = true;
  8814. // orderCode = MOB1608240002;
  8815. // password = 123456;
  8816. // "refresh_trigger" = zipcode;
  8817. // "subaction_tag" = 1;
  8818. // user = EvanK;
  8819. // }
  8820. NSString *country_code = nil;
  8821. NSString *zipCode = nil;
  8822. NSString *stateCode = nil;
  8823. NSString *city = nil;
  8824. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8825. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8826. NSString *code_id = params[@"country"];
  8827. country_code = [self countryCodeByid:code_id];
  8828. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8829. NSString *zip_code = params[@"zipcode"];
  8830. // 剔除全部为空格
  8831. int spaceCount = 0;
  8832. for (int i = 0; i < zip_code.length; i++) {
  8833. if ([zip_code characterAtIndex:i] == ' ') {
  8834. spaceCount++;
  8835. }
  8836. }
  8837. if (spaceCount == zip_code.length) {
  8838. zip_code = @"";
  8839. }
  8840. zipCode = zip_code;
  8841. if (zipCode.length > 0) {
  8842. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8843. country_code = [dic valueForKey:@"country_code"];
  8844. if (!country_code) {
  8845. // country_code = @"US";
  8846. NSString *code_id = params[@"country"];
  8847. country_code = [self countryCodeByid:code_id];
  8848. }
  8849. stateCode = [dic valueForKey:@"state_code"];
  8850. if(!stateCode.length) {
  8851. stateCode = params[@"state"];
  8852. }
  8853. city = [dic valueForKey:@"city"];
  8854. if (!city.length) {
  8855. city = params[@"city"];
  8856. }
  8857. // zip code
  8858. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8859. // [zipDic setValue:zipCode forKey:@"value"];
  8860. // [section_0 setValue:zipDic forKey:@"item_11"];
  8861. } else {
  8862. NSString *code_id = params[@"country"];
  8863. country_code = [self countryCodeByid:code_id];
  8864. stateCode = params[@"state"];
  8865. city = params[@"city"];
  8866. }
  8867. }
  8868. }
  8869. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8870. // [ret removeObjectForKey:@"up_params"];
  8871. [ret setObject:@"New Address" forKey:@"title"];
  8872. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8873. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8874. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8875. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8876. // [country_dic removeObjectForKey:@"refresh"];
  8877. // [country_dic removeObjectForKey:@"restore"];
  8878. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8879. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8880. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8881. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8882. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8883. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8884. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8885. // [zip_code_dic removeObjectForKey:@"refresh"];
  8886. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8887. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8888. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8889. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8890. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8891. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8892. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8893. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8894. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8895. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8896. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8897. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8898. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8899. // country
  8900. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8901. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8902. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8903. // state
  8904. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8905. // NSDictionary *tmpDic = @{
  8906. // @"value" : @"Other",
  8907. // @"value_id" : @"",
  8908. // @"check" : [NSNumber numberWithInteger:0]
  8909. // };
  8910. //
  8911. // for (int i = 0; i < allState.allKeys.count; i++) {
  8912. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8913. //
  8914. // NSDictionary *tmp = allState[key];
  8915. // [allState setValue:tmpDic forKey:key];
  8916. // tmpDic = tmp;
  8917. // }
  8918. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8919. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8920. [ret setValue:new_section_0 forKey:@"section_0"];
  8921. return [RAConvertor dict2data:ret];
  8922. }
  8923. #pragma mark save addr
  8924. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8925. {
  8926. // NSString *companyName = [self valueInParams:params key:@"company"];
  8927. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8928. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8929. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8930. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8931. // NSString *countryId = [self valueInParams:params key:@"country"];
  8932. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8933. // NSString *city = [self valueInParams:params key:@"city"];
  8934. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8935. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8936. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8937. // NSString *phone = [self valueInParams:params key:@"phone"];
  8938. // NSString *fax = [self valueInParams:params key:@"fax"];
  8939. // NSString *email = [self valueInParams:params key:@"email"];
  8940. return [self offline_saveContact:params update:NO isCustomer:NO];
  8941. }
  8942. #pragma mark cancel order
  8943. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8944. {
  8945. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8946. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8947. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8948. if (order_id.length) {
  8949. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8950. }
  8951. int ret = [iSalesDB execSql:sql];
  8952. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8953. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8954. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8955. // [dic setValue:@"160409" forKey:@"min_ver"];
  8956. return [RAConvertor dict2data:dic];
  8957. }
  8958. #pragma mark sign order
  8959. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8960. {
  8961. //参考 offline_saveBusinesscard
  8962. DebugLog(@"sign order params: %@",params);
  8963. // orderCode = MOB1608240002;
  8964. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8965. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8966. orderCode = [self translateSingleQuote:orderCode];
  8967. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8968. picPath = [self translateSingleQuote:picPath];
  8969. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8970. int ret = [iSalesDB execSql:sql];
  8971. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8972. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8973. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8974. // [dic setValue:@"160409" forKey:@"min_ver"];
  8975. return [RAConvertor dict2data:dic];
  8976. }
  8977. #pragma mark save order
  8978. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8979. NSString *ret = [self valueInParams:params key:key];
  8980. if (translate) {
  8981. ret = [self translateSingleQuote:ret];
  8982. }
  8983. return ret;
  8984. }
  8985. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8986. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8987. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8988. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8989. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8990. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8991. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8992. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8993. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8994. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8995. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8996. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8997. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8998. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8999. if (![total_price isEqualToString:@""]) {
  9000. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9001. } else {
  9002. total_price = @"";
  9003. }
  9004. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9005. if ([paymentsAndCredits isEqualToString:@""]) {
  9006. paymentsAndCredits = @"";
  9007. } else {
  9008. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9009. }
  9010. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9011. if ([handling_fee_value isEqualToString:@""]) {
  9012. handling_fee_value = @"";
  9013. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9014. handling_fee_value = @"";
  9015. } else {
  9016. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9017. }
  9018. NSString *handling_fee_placeholder = handling_fee_value;
  9019. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9020. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9021. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9022. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9023. NSString *lift_gate_placeholder = @"";
  9024. if ([lift_gate_value isEqualToString:@""]) {
  9025. lift_gate_placeholder = @"";
  9026. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9027. lift_gate_placeholder = @"";
  9028. } else {
  9029. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9030. }
  9031. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9032. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9033. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9034. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9035. 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];
  9036. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9037. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9038. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9039. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9040. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9041. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9042. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9043. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9044. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9045. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9046. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9047. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9048. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9049. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9050. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9051. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9052. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9053. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9054. if (!number_nil) {
  9055. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9056. }
  9057. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9058. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9059. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9060. if (!security_code_nil) {
  9061. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9062. }
  9063. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9064. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9065. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9066. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9067. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9068. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9069. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9070. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9071. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9072. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9073. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9074. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9075. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9076. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9077. NSString *contact_id = customer_cid;
  9078. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9079. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9080. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9081. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9082. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9083. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9084. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9085. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9086. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9087. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9088. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9089. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9090. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9091. //
  9092. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9093. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9094. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9095. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9096. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9097. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9098. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9099. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9100. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9101. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9102. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9103. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9104. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9105. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9106. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9107. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9108. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9109. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9110. 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];
  9111. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9112. if (receive_cid.length) {
  9113. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9114. }
  9115. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9116. if (receive_name.length) {
  9117. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9118. }
  9119. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9120. if (receive_ext.length) {
  9121. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9122. }
  9123. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9124. if (receive_contact.length) {
  9125. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9126. }
  9127. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9128. if (receive_phone.length) {
  9129. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9130. }
  9131. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9132. if (receive_email.length) {
  9133. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9134. }
  9135. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9136. if (receive_fax.length) {
  9137. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9138. }
  9139. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9140. if (sender_cid.length) {
  9141. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9142. }
  9143. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9144. if (sender_name.length) {
  9145. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9146. }
  9147. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9148. if (sender_ext.length) {
  9149. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9150. }
  9151. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9152. if(sender_contact.length) {
  9153. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9154. }
  9155. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9156. if (sender_phone.length) {
  9157. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9158. }
  9159. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9160. if (sender_fax.length) {
  9161. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9162. }
  9163. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9164. if (sender_email.length) {
  9165. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9166. }
  9167. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9168. if (shipping_billto_cid.length) {
  9169. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9170. }
  9171. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9172. if (shipping_billto_name.length) {
  9173. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9174. }
  9175. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9176. if (shipping_billto_ext.length) {
  9177. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9178. }
  9179. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9180. if (shipping_billto_contact.length) {
  9181. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9182. }
  9183. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9184. if (shipping_billto_phone.length) {
  9185. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9186. }
  9187. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9188. if (shipping_billto_fax.length) {
  9189. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9190. }
  9191. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9192. if (shipping_billto_email.length) {
  9193. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9194. }
  9195. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9196. if (billing_cid.length) {
  9197. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9198. }
  9199. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9200. if (billing_name.length) {
  9201. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9202. }
  9203. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9204. if (billing_ext.length) {
  9205. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9206. }
  9207. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9208. if (billing_contact.length) {
  9209. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9210. }
  9211. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9212. if (billing_phone.length) {
  9213. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9214. }
  9215. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9216. if (billing_fax.length) {
  9217. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9218. }
  9219. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9220. if (billing_email.length) {
  9221. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9222. }
  9223. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9224. if (returnto_cid.length) {
  9225. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9226. }
  9227. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9228. if (returnto_name.length) {
  9229. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9230. }
  9231. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9232. if (returnto_ext.length) {
  9233. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9234. }
  9235. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9236. if (returnto_contact.length) {
  9237. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9238. }
  9239. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9240. if (returnto_phone.length) {
  9241. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9242. }
  9243. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9244. if (returnto_fax.length) {
  9245. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9246. }
  9247. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9248. if (returnto_email.length) {
  9249. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9250. }
  9251. NSString *order_status = @"status = 1,";
  9252. if (submit) {
  9253. order_status = @"status = -11,";
  9254. }
  9255. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9256. NSString *sync_sql = @"";
  9257. if (submit) {
  9258. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9259. [param setValue:sales_rep forKey:@"sales_rep"];
  9260. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9261. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9262. }
  9263. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  9264. DebugLog(@"save order contactSql: %@",contactSql);
  9265. DebugLog(@"save order orderSql: %@",orderSql);
  9266. // int contact_ret = [iSalesDB execSql:contactSql];
  9267. int order_ret = [iSalesDB execSql:orderSql];
  9268. int ret = order_ret;
  9269. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9270. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9271. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9272. // [dic setValue:@"160409" forKey:@"min_ver"];
  9273. [dic setObject:so_id forKey:@"so#"];
  9274. return [RAConvertor dict2data:dic];
  9275. }
  9276. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9277. {
  9278. // id foo = nil;
  9279. // NSMutableArray *a = @[].mutableCopy;
  9280. // [a addObject:foo];
  9281. return [self saveorder:param submit:NO];
  9282. }
  9283. #pragma mark add to cart by name
  9284. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  9285. {
  9286. NSString *orderCode = [params objectForKey:@"orderCode"];
  9287. NSDictionary *newParams = @{
  9288. @"product_id" : params[@"product_id_string"],
  9289. @"orderCode" : orderCode,
  9290. @"can_create_backorder":params[@"can_create_backorder"]
  9291. };
  9292. return [self offline_add2cart:[newParams mutableCopy]];
  9293. }
  9294. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  9295. {
  9296. NSString *orderCode = [params objectForKey:@"orderCode"];
  9297. NSString *upccode = [params objectForKey:@"upc_code"];
  9298. // product_name = [self translateSingleQuote:product_name];
  9299. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9300. // bool search_upc = [params[@"search_upc"] boolValue];
  9301. sqlite3 *db = [iSalesDB get_db];
  9302. __block NSMutableString *product_id_string = [NSMutableString string];
  9303. // NSString *name = [product_name_array objectAtIndex:i];
  9304. NSString *sql =nil;
  9305. 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];
  9306. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9307. int product_id = sqlite3_column_int(stmt, 0);
  9308. [product_id_string appendFormat:@"%d",product_id];
  9309. }];
  9310. if (!orderCode) {
  9311. orderCode = @"";
  9312. }
  9313. NSDictionary *newParams = @{
  9314. @"product_id" : product_id_string,
  9315. @"orderCode" : orderCode,
  9316. @"can_create_backorder":params[@"can_create_backorder"]
  9317. };
  9318. [iSalesDB close_db:db];
  9319. return [self offline_add2cart:[newParams mutableCopy]];
  9320. }
  9321. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9322. {
  9323. NSString *orderCode = [params objectForKey:@"orderCode"];
  9324. NSString *product_name = [params objectForKey:@"product_name"];
  9325. product_name = [self translateSingleQuote:product_name];
  9326. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9327. bool search_upc = [params[@"search_upc"] boolValue];
  9328. sqlite3 *db = [iSalesDB get_db];
  9329. __block NSMutableString *product_id_string = [NSMutableString string];
  9330. for (int i = 0; i < product_name_array.count; i++) {
  9331. NSString *name = [product_name_array objectAtIndex:i];
  9332. NSString *sql =nil;
  9333. if(search_upc)
  9334. 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];
  9335. else
  9336. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9337. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9338. int product_id = sqlite3_column_int(stmt, 0);
  9339. if (i == product_name_array.count - 1) {
  9340. [product_id_string appendFormat:@"%d",product_id];
  9341. } else {
  9342. [product_id_string appendFormat:@"%d,",product_id];
  9343. }
  9344. }];
  9345. }
  9346. if (!orderCode) {
  9347. orderCode = @"";
  9348. }
  9349. NSDictionary *newParams = @{
  9350. @"product_id" : product_id_string,
  9351. @"orderCode" : orderCode,
  9352. @"can_create_backorder":params[@"can_create_backorder"]
  9353. };
  9354. [iSalesDB close_db:db];
  9355. return [self offline_add2cart:[newParams mutableCopy]];
  9356. }
  9357. #pragma mark reset order
  9358. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9359. {
  9360. // UIApplication * app = [UIApplication sharedApplication];
  9361. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9362. [iSalesDB disable_trigger];
  9363. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9364. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9365. [iSalesDB enable_trigger];
  9366. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9367. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9368. return [RAConvertor dict2data:dic];
  9369. }
  9370. #pragma mark submit order
  9371. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9372. {
  9373. return [self saveorder:params submit:YES];
  9374. }
  9375. #pragma mark copy order
  9376. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9377. {
  9378. NSMutableDictionary *ret = @{}.mutableCopy;
  9379. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9380. sqlite3 *db = [iSalesDB get_db];
  9381. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9382. // 首先查看联系人是否active
  9383. 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];
  9384. __block int customer_is_active = 1;
  9385. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9386. customer_is_active = sqlite3_column_int(stmt, 0);
  9387. }];
  9388. if (!customer_is_active) {
  9389. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9390. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9391. [iSalesDB close_db:db];
  9392. return [RAConvertor dict2data:ret];
  9393. }
  9394. // new order
  9395. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9396. NSString *new_order_code = [self get_offline_soid:db];
  9397. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9398. user = [self translateSingleQuote:user];
  9399. 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
  9400. __block int result = 1;
  9401. result = [iSalesDB execSql:insert_order_sql db:db];
  9402. if (!result) {
  9403. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9404. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9405. [iSalesDB close_db:db];
  9406. return [RAConvertor dict2data:ret];
  9407. }
  9408. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9409. // __block NSString *product_id = @"";
  9410. __weak typeof(self) weakSelf = self;
  9411. 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];
  9412. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9413. int is_active = sqlite3_column_int(stmt, 1);
  9414. if (is_active) {
  9415. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9416. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9417. 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];
  9418. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9419. }
  9420. }];
  9421. // product_id = [product_id substringFromIndex:1];
  9422. //
  9423. // [self offline_add2cart:@{}.mutableCopy];
  9424. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9425. [iSalesDB close_db:db];
  9426. if (result) {
  9427. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9428. } else {
  9429. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9430. }
  9431. [ret setObject:new_order_code forKey:@"so_id"];
  9432. return [RAConvertor dict2data:ret];
  9433. }
  9434. #pragma mark move wish list to cart
  9435. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9436. assert(params[@"can_create_backorder"]!=nil);
  9437. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9438. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9439. NSString *collectId = params[@"collectId"];
  9440. 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];
  9441. __block NSString *product_id = @"";
  9442. __block NSString *qty = @"";
  9443. __block int number_of_outOfStock = 0;
  9444. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9445. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9446. int productId = sqlite3_column_int(stmt, 0);
  9447. int item_qty = sqlite3_column_int(stmt, 1);
  9448. int availability = sqlite3_column_int(stmt, 2);
  9449. int _id = sqlite3_column_int(stmt, 3);
  9450. if (![params[@"can_create_backorder"] boolValue]) {
  9451. // 库存小于购买量为缺货
  9452. if (availability >= item_qty) {
  9453. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9454. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9455. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9456. } else {
  9457. number_of_outOfStock++;
  9458. }
  9459. } else {
  9460. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9461. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9462. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9463. }
  9464. }];
  9465. NSMutableDictionary *retDic = nil;
  9466. if (![params[@"can_create_backorder"] boolValue]) {
  9467. if (delete_collectId.count == 0) {
  9468. retDic = [NSMutableDictionary dictionary];
  9469. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9470. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9471. return [RAConvertor dict2data:retDic];
  9472. }
  9473. }
  9474. if (product_id.length > 1) {
  9475. product_id = [product_id substringFromIndex:1];
  9476. }
  9477. if (qty.length > 1) {
  9478. qty = [qty substringFromIndex:1];
  9479. }
  9480. NSString *orderCode = params[@"orderCode"];
  9481. if (!orderCode) {
  9482. orderCode = @"";
  9483. }
  9484. NSDictionary *newParams = @{
  9485. @"product_id" : product_id,
  9486. @"orderCode" : orderCode,
  9487. @"qty" : qty,
  9488. @"can_create_backorder":params[@"can_create_backorder"]
  9489. };
  9490. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9491. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9492. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9493. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9494. NSInteger ret = [wish_return[@"result"] intValue];
  9495. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9496. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9497. }
  9498. if (![params[@"can_create_backorder"] boolValue]) {
  9499. if (number_of_outOfStock > 0) {
  9500. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9501. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9502. }
  9503. }
  9504. return [RAConvertor dict2data:retDic];
  9505. }
  9506. #pragma mark - portfolio
  9507. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9508. // assert(params[@"contact_id"]!=nil);
  9509. assert(params[@"user"]!=nil);
  9510. assert(params[@"can_see_price"]!=nil);
  9511. int sort = [[params valueForKey:@"sort"] intValue];
  9512. int offset = [[params valueForKey:@"offset"] intValue];
  9513. int limit = [[params valueForKey:@"limit"] intValue];
  9514. NSString *orderBy = @"";
  9515. switch (sort) {
  9516. case 0:{
  9517. orderBy = @"p.modify_time desc";
  9518. }
  9519. break;
  9520. case 1:{
  9521. orderBy = @"modify_time asc";
  9522. }
  9523. break;
  9524. case 2:{
  9525. orderBy = @"p.name asc";
  9526. }
  9527. break;
  9528. case 3:{
  9529. orderBy = @"p.name desc";
  9530. }
  9531. break;
  9532. case 4:{
  9533. orderBy = @"p.description asc";
  9534. }
  9535. break;
  9536. case 5: {
  9537. orderBy = @"m.default_category asc";
  9538. }
  9539. default:
  9540. break;
  9541. }
  9542. // 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];
  9543. 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];
  9544. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9545. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9546. sqlite3 *db = [iSalesDB get_db];
  9547. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9548. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9549. int product_id = sqlite3_column_int(stmt, 0);
  9550. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9551. NSString *name = [self textAtColumn:1 statement:stmt];
  9552. NSString *description = [self textAtColumn:2 statement:stmt];
  9553. double price = sqlite3_column_double(stmt, 3);
  9554. double discount = sqlite3_column_double(stmt,4);
  9555. int qty = sqlite3_column_int(stmt, 5);
  9556. int percentage = sqlite3_column_int(stmt, 6);
  9557. int item_id = sqlite3_column_int(stmt, 7);
  9558. // int fashion_id = sqlite3_column_int(stmt, 8);
  9559. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9560. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9561. double percent = sqlite3_column_double(stmt, 11);
  9562. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9563. if ([price_null isEqualToString:@"null"]) {
  9564. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9565. }
  9566. NSMutableDictionary *item = @{
  9567. @"linenotes": line_note,
  9568. @"check": @(1),
  9569. @"product_id": product_id_string,
  9570. @"available_qty": @(qty),
  9571. @"available_percent" : @(percent),
  9572. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9573. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9574. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9575. @"img": img_path,
  9576. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9577. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9578. }.mutableCopy;
  9579. if (percentage) {
  9580. [item removeObjectForKey:@"available_qty"];
  9581. } else {
  9582. [item removeObjectForKey:@"available_percent"];
  9583. }
  9584. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9585. if ([qty_null isEqualToString:@"null"]) {
  9586. [item removeObjectForKey:@"available_qty"];
  9587. }
  9588. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9589. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9590. }];
  9591. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9592. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9593. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9594. }
  9595. [iSalesDB close_db:db];
  9596. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9597. [dic setValue:@"" forKey:@"email_content"];
  9598. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9599. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9600. return [RAConvertor dict2data:dic];
  9601. }
  9602. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9603. __block int qty = 0;
  9604. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9605. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9606. qty = sqlite3_column_int(stmt, 0);
  9607. }];
  9608. return qty;
  9609. }
  9610. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9611. {
  9612. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9613. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9614. // NSMutableDictionary * values = params[@"replaceValue"];
  9615. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9616. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9617. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9618. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9619. NSString *pdf_path = @"";
  9620. if (direct) {
  9621. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9622. } else {
  9623. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9624. }
  9625. NSString *create_user = [self valueInParams:params key:@"user"];
  9626. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9627. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9628. // model info
  9629. // 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];
  9630. // V1.90 more color
  9631. 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];
  9632. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9633. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9634. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9635. sqlite3 *db = [iSalesDB get_db];
  9636. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9637. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9638. int product_id = sqlite3_column_int(stmt, 0);
  9639. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9640. double price = sqlite3_column_double(stmt, 1);
  9641. double discount = sqlite3_column_double(stmt,2);
  9642. int qty = sqlite3_column_int(stmt, 3);
  9643. int percentage = sqlite3_column_int(stmt, 4);
  9644. int item_id = sqlite3_column_int(stmt, 5);
  9645. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9646. double percent = sqlite3_column_double(stmt, 7);
  9647. int more_color = sqlite3_column_int(stmt, 8);
  9648. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9649. /* if ([price_null isEqualToString:@"null"]) {
  9650. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9651. }
  9652. */
  9653. [product_ids_string appendFormat:@"%@,",product_id_string];
  9654. // Regular Price
  9655. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9656. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9657. // QTY
  9658. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9659. if ([qty_null isEqualToString:@"null"]) {
  9660. // 查available
  9661. qty = [self model_QTY:product_id_string db:db];
  9662. }
  9663. if (percentage) {
  9664. qty = qty * percent / 100;
  9665. }
  9666. // Special Price
  9667. if ([price_null isEqualToString:@"null"]) {
  9668. // price = regular price
  9669. price = [regular_price_str doubleValue];
  9670. }
  9671. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9672. if (![discount_null isEqualToString:@"null"]) {
  9673. price = price * (1 - discount / 100.0);
  9674. }
  9675. NSMutableDictionary *item = @{
  9676. @"line_note": line_note,
  9677. @"product_id": product_id_string,
  9678. @"available_qty": @(qty),
  9679. @"more_color":@(more_color),
  9680. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9681. @"regular_price" : regular_price_str,
  9682. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9683. }.mutableCopy;
  9684. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9685. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9686. }];
  9687. [iSalesDB close_db:db];
  9688. if (product_ids_string.length > 0) {
  9689. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9690. }
  9691. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9692. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9693. return [RAConvertor dict2data:resultDictionary];
  9694. }
  9695. NSString *model_info = [RAConvertor dict2string:dic];
  9696. // 创建PDF
  9697. // 在preview情况下保存,则不需要新建了
  9698. if (direct) {
  9699. NSMutableDictionary *tear_sheet_params = params;
  9700. // if (tear_sheet_id) {
  9701. // tear_sheet_params = values;
  9702. // }
  9703. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9704. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9705. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9706. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9707. resultDictionary = pdfInfo.mutableCopy;
  9708. } else { // 创建PDF失败
  9709. return pdfData;
  9710. }
  9711. } else {
  9712. // pdf_path 就是本地路径
  9713. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9714. }
  9715. // 将文件移动到PDF Cache文件夹
  9716. NSString *newPath = [pdf_path lastPathComponent];
  9717. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9718. NSString *cachefolder = [paths objectAtIndex:0];
  9719. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9720. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9721. NSFileManager *fileManager = [NSFileManager defaultManager];
  9722. NSError *error = nil;
  9723. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9724. if (error) { // 移动文件失败
  9725. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9726. return [RAConvertor dict2data:resultDictionary];
  9727. }
  9728. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9729. pdf_path = [newPath lastPathComponent];
  9730. // 保存信息
  9731. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9732. NSString *off_params = [RAConvertor dict2string:params];
  9733. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9734. pdf_path = [self translateSingleQuote:pdf_path];
  9735. create_user = [self translateSingleQuote:create_user];
  9736. tear_note = [self translateSingleQuote:tear_note];
  9737. tear_name = [self translateSingleQuote:tear_name];
  9738. model_info = [self translateSingleQuote:model_info];
  9739. configureParams = [self translateSingleQuote:configureParams];
  9740. off_params = [self translateSingleQuote:off_params];
  9741. 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];
  9742. if (tear_sheet_id) {
  9743. int _id = [tear_sheet_id intValue];
  9744. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9745. }
  9746. int result = [iSalesDB execSql:save_pdf_sql];
  9747. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9748. //
  9749. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9750. if (remove_Item) {
  9751. // portfolioId
  9752. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9753. }
  9754. return [RAConvertor dict2data:resultDictionary];
  9755. }
  9756. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9757. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  9758. }
  9759. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9760. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  9761. }
  9762. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9763. int offset = [[params valueForKey:@"offset"] intValue];
  9764. int limit = [[params valueForKey:@"limit"] intValue];
  9765. NSString *keyword = [params valueForKey:@"keyWord"];
  9766. NSString *where = @"where is_delete is null or is_delete = 0";
  9767. if (keyword.length) {
  9768. keyword = [self translateSingleQuote:keyword];
  9769. 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];
  9770. }
  9771. 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
  9772. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9773. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9774. NSString *cachefolder = [paths objectAtIndex:0];
  9775. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9776. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9777. NSString *name = [self textAtColumn:0 statement:stmt];
  9778. NSString *note = [self textAtColumn:1 statement:stmt];
  9779. NSString *time = [self textAtColumn:2 statement:stmt];
  9780. NSString *user = [self textAtColumn:3 statement:stmt];
  9781. NSString *path = [self textAtColumn:4 statement:stmt];
  9782. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9783. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9784. path = [pdfFolder stringByAppendingPathComponent:path];
  9785. BOOL bdir=NO;
  9786. NSFileManager* fileManager = [NSFileManager defaultManager];
  9787. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9788. {
  9789. //pdf文件不存在
  9790. path=nil;
  9791. }
  9792. time = [self changeDateTimeFormate:time];
  9793. time = [time stringByAppendingString:@" PST"];
  9794. int sheet_id = sqlite3_column_int(stmt, 5);
  9795. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9796. item[@"tearsheetsId"]=@(sheet_id);
  9797. item[@"pdf_path"]=path;
  9798. item[@"create_time"]=time;
  9799. item[@"create_user"]=user;
  9800. item[@"tear_note"]=note;
  9801. item[@"tear_name"]=name;
  9802. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9803. item[@"model_info"]=model_info;
  9804. item[@"off_params"]=off_params;
  9805. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9806. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9807. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9808. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9809. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9810. }];
  9811. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9812. return [RAConvertor dict2data:dic];
  9813. }
  9814. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9815. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9816. // NSString *user = [params objectForKey:@"user"];
  9817. // 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];
  9818. 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];
  9819. int result = [iSalesDB execSql:sql];
  9820. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9821. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9822. if (result == RESULT_TRUE) {
  9823. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9824. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9825. sqlite3 *db = [iSalesDB get_db];
  9826. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9827. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9828. [iSalesDB close_db:db];
  9829. }
  9830. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9831. return [RAConvertor dict2data:dic];
  9832. }
  9833. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9834. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9835. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9836. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9837. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9838. NSDictionary *company_item = @{
  9839. @"control": @"text",
  9840. @"keyboard": @"text",
  9841. @"name": @"company_name",
  9842. @"value": COMPANY_FULL_NAME,
  9843. @"aname": @"Company Name"
  9844. };
  9845. [section_0 setObject:company_item forKey:@"item_0"];
  9846. [dic setObject:section_0 forKey:@"section_0"];
  9847. // Regurlar Price
  9848. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9849. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9850. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9851. __block long val_count = 0;
  9852. NSString *sql = @"select name,type,order_by from price order by order_by";
  9853. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9854. NSString *name = [self textAtColumn:0 statement:stmt];
  9855. int type = sqlite3_column_int(stmt, 1);
  9856. int order_by = sqlite3_column_int(stmt, 2);
  9857. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9858. NSDictionary *price_dic = @{
  9859. @"value" : name,
  9860. @"value_id" : [NSNumber numberWithInteger:type],
  9861. @"check" : order_by == 0 ? @(1) : @(0)
  9862. };
  9863. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9864. val_count = ++(*count);
  9865. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9866. val_count = 0;
  9867. }];
  9868. [cadedate setObject:@{@"value" : @"None",
  9869. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9870. val_count++;
  9871. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9872. [price setObject:cadedate forKey:@"cadedate"];
  9873. [section1 setObject:price forKey:@"item_2"];
  9874. [dic setObject:section1 forKey:@"section_1"];
  9875. return [RAConvertor dict2data:dic];
  9876. }
  9877. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9878. NSString *product_id = [params objectForKey:@"fashionId"];
  9879. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9880. __block int result = RESULT_TRUE;
  9881. __block int qty = 0;
  9882. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9883. qty = sqlite3_column_int(stmt, 0);
  9884. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9885. result = RESULT_FALSE;
  9886. }];
  9887. NSMutableDictionary *dic = @{
  9888. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9889. @"mode" : @"Regular Mode",
  9890. @"quantity_available" : @(qty),
  9891. @"result" : @(result),
  9892. }.mutableCopy;
  9893. return [RAConvertor dict2data:dic];
  9894. }
  9895. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9896. NSString *item_ids = [params objectForKey:@"item_id"];
  9897. NSString *line_notes = [params objectForKey:@"notes"];
  9898. NSString *price_str = [params objectForKey:@"price"];
  9899. NSString *discount_str = [params objectForKey:@"discount"];
  9900. NSString *percent = [params objectForKey:@"available_percent"];
  9901. NSString *qty = [params objectForKey:@"available_qty"];
  9902. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9903. int dot = 0;
  9904. if (line_notes) {
  9905. line_notes = [self translateSingleQuote:line_notes];
  9906. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9907. sql = [sql stringByAppendingString:line_notes];
  9908. dot = 1;
  9909. } else {
  9910. line_notes = @"";
  9911. }
  9912. if (price_str) {
  9913. if (dot) {
  9914. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9915. } else {
  9916. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9917. dot = 1;
  9918. }
  9919. sql = [sql stringByAppendingString:price_str];
  9920. } else {
  9921. price_str = @"";
  9922. }
  9923. if (discount_str) {
  9924. if (dot) {
  9925. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9926. } else {
  9927. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9928. dot = 1;
  9929. }
  9930. sql = [sql stringByAppendingString:discount_str];
  9931. } else {
  9932. discount_str = @"";
  9933. }
  9934. if (percent) {
  9935. if (dot) {
  9936. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9937. } else {
  9938. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9939. dot = 1;
  9940. }
  9941. sql = [sql stringByAppendingString:percent];
  9942. } else {
  9943. percent = @"";
  9944. }
  9945. if (qty) {
  9946. if (dot) {
  9947. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9948. } else {
  9949. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9950. dot = 1;
  9951. }
  9952. sql = [sql stringByAppendingString:qty];
  9953. } else {
  9954. qty = @"";
  9955. }
  9956. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9957. sql = [sql stringByAppendingString:where];
  9958. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9959. int result = [iSalesDB execSql:sql];
  9960. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9961. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9962. return [RAConvertor dict2data:dic];
  9963. }
  9964. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9965. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9966. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9967. sqlite3 *db = [iSalesDB get_db];
  9968. 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];
  9969. int result = [iSalesDB execSql:sql db:db];
  9970. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9971. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9972. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9973. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9974. [iSalesDB close_db:db];
  9975. return [RAConvertor dict2data:dic];
  9976. }
  9977. + (void)offline_removePDFWithName:(NSString *)name {
  9978. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9979. NSString *cachefolder = [paths objectAtIndex:0];
  9980. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9981. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9982. NSFileManager *fileManager = [NSFileManager defaultManager];
  9983. [fileManager removeItemAtPath:path error:nil];
  9984. }
  9985. + (void)offline_clear_PDFCache {
  9986. NSFileManager *fileManager = [NSFileManager defaultManager];
  9987. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9988. NSString *cachefolder = [paths objectAtIndex:0];
  9989. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9990. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9991. for (NSString *path in pdf_files) {
  9992. [self offline_removePDFWithName:[path lastPathComponent]];
  9993. }
  9994. }
  9995. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9996. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9997. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9998. NSString *user = [params objectForKey:@"user"];
  9999. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10000. if (![create_user isEqualToString:user]) {
  10001. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10002. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10003. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10004. return [RAConvertor dict2data:dic];
  10005. }
  10006. 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]];
  10007. __block int is_local = 0;
  10008. __block NSString *path = @"";
  10009. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10010. is_local = sqlite3_column_int(stmt, 0);
  10011. path = [self textAtColumn:1 statement:stmt];
  10012. }];
  10013. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10014. if (is_local == 1) {
  10015. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10016. // 删除文件
  10017. [self offline_removePDFWithName:path];
  10018. }
  10019. int result = [iSalesDB execSql:sql];
  10020. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10021. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10022. return [RAConvertor dict2data:dic];
  10023. }
  10024. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10025. {
  10026. // assert(add_params[@"contact_id"]!=nil);
  10027. assert(add_params[@"user"]!=nil);
  10028. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10029. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10030. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10031. NSString* where=@"1=1";
  10032. if (ver!=nil) {
  10033. where=@"is_dirty=1";
  10034. }
  10035. 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];
  10036. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10037. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10038. sqlite3 *db = [iSalesDB get_db];
  10039. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10040. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10041. NSInteger _id = sqlite3_column_int(stmt, 0);
  10042. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10043. NSString *name = [self textAtColumn:2 statement:stmt];
  10044. NSString *desc = [self textAtColumn:3 statement:stmt];
  10045. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10046. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10047. NSInteger qty = sqlite3_column_int(stmt, 6);
  10048. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10049. double percent = sqlite3_column_double(stmt, 8);
  10050. double price = sqlite3_column_double(stmt, 9);
  10051. double discount = sqlite3_column_double(stmt, 10);
  10052. NSString *img = [self textAtColumn:11 statement:stmt];
  10053. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10054. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10055. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10056. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10057. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10058. if ([price_null isEqualToString:@"null"]) {
  10059. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10060. }
  10061. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10062. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10063. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10064. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10065. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10066. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10067. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10068. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10069. if ([qty_null isEqualToString:@"null"]) {
  10070. // [item setValue:@"null" forKey:@"available_qty"];
  10071. } else {
  10072. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10073. }
  10074. if ([is_percent_null isEqualToString:@"null"]) {
  10075. // [item setValue:@"null" forKey:@"percentage"];
  10076. } else {
  10077. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10078. }
  10079. if ([percent_null isEqualToString:@"null"]) {
  10080. // [item setValue:@"null" forKey:@"percent"];
  10081. } else {
  10082. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10083. }
  10084. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10085. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10086. [item setValue:name forKey:@"name"];
  10087. [item setValue:desc forKey:@"description"];
  10088. [item setValue:img forKey:@"img"];
  10089. [item setValue:line_note forKey:@"line_note"];
  10090. [item setValue:create_time forKey:@"createtime"];
  10091. [item setValue:modify_time forKey:@"modifytime"];
  10092. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10093. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10094. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10095. }];
  10096. [iSalesDB close_db:db];
  10097. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10098. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10099. }
  10100. return ret;
  10101. }
  10102. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10103. {
  10104. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10105. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10106. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10107. NSString* where=@"1=1";
  10108. if (ver!=nil) {
  10109. where=@"is_dirty=1";
  10110. }
  10111. 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];
  10112. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10113. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10114. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10115. NSInteger _id = sqlite3_column_int(stmt, 0);
  10116. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10117. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10118. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10119. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10120. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10121. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10122. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10123. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10124. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10125. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10126. NSString *uuid = [NSUUID UUID].UUIDString;
  10127. int is_delete = sqlite3_column_int(stmt, 11);
  10128. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10129. int is_local = sqlite3_column_int(stmt, 12);
  10130. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10131. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10132. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10133. }
  10134. [item setObject:pdf_path forKey:@"pdf_path"];
  10135. [item setObject:create_user forKey:@"create_user"];
  10136. [item setObject:tear_note forKey:@"tear_note"];
  10137. [item setObject:tear_name forKey:@"tear_name"];
  10138. [item setObject:model_info forKey:@"model_info"];
  10139. [item setObject:createtime forKey:@"createtime"];
  10140. [item setObject:modifytime forKey:@"modifytime"];
  10141. [item setObject:urlParams forKey:@"urlParams"];
  10142. [item setObject:off_params forKey:@"off_params"];
  10143. [item setObject:uuid forKey:@"pdf_token"];
  10144. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10145. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10146. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10147. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10148. } else {
  10149. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10150. }
  10151. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10152. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10153. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10154. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10155. }];
  10156. return ret;
  10157. }
  10158. #pragma mark 2020
  10159. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10160. {
  10161. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10162. sqlite3 *db = [iSalesDB get_db];
  10163. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10164. if (contactType) {
  10165. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10166. } else {
  10167. contactType = @"1 = 1";
  10168. }
  10169. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10170. DebugLog(@"offline contact list keyword: %@",keyword);
  10171. // advanced search
  10172. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10173. if (contact_name) {
  10174. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10175. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10176. } else {
  10177. contact_name = @"";
  10178. }
  10179. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10180. if (customer_phone) {
  10181. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10182. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10183. } else {
  10184. customer_phone = @"";
  10185. }
  10186. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10187. if (customer_fax) {
  10188. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10189. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10190. } else {
  10191. customer_fax = @"";
  10192. }
  10193. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10194. if (customer_zipcode) {
  10195. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10196. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10197. } else {
  10198. customer_zipcode = @"";
  10199. }
  10200. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10201. if (customer_sales_rep) {
  10202. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10203. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10204. } else {
  10205. customer_sales_rep = @"";
  10206. }
  10207. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10208. if (customer_state) {
  10209. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10210. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10211. } else {
  10212. customer_state = @"";
  10213. }
  10214. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10215. if (customer_name) {
  10216. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10217. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10218. } else {
  10219. customer_name = @"";
  10220. }
  10221. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10222. if (customer_country) {
  10223. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10224. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10225. } else {
  10226. customer_country = @"";
  10227. }
  10228. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10229. if (customer_cid) {
  10230. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10231. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10232. } else {
  10233. customer_cid = @"";
  10234. }
  10235. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10236. if (customer_city) {
  10237. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10238. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  10239. } else {
  10240. customer_city = @"";
  10241. }
  10242. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  10243. if (customer_address) {
  10244. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10245. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  10246. } else {
  10247. customer_address = @"";
  10248. }
  10249. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  10250. if (customer_email) {
  10251. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10252. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  10253. } else {
  10254. customer_email = @"";
  10255. }
  10256. NSString *price_name = [params valueForKey:@"price_name"];
  10257. if (price_name) {
  10258. if ([price_name containsString:@","]) {
  10259. // 首先从 price表中查处name
  10260. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  10261. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  10262. for (int i = 1;i < pArray.count;i++) {
  10263. NSString *p = pArray[i];
  10264. [mutablePStr appendFormat:@" or type = %@ ",p];
  10265. }
  10266. [mutablePStr appendString:@";"];
  10267. __block NSMutableArray *price_name_array = [NSMutableArray array];
  10268. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10269. char *name = (char *)sqlite3_column_text(stmt, 0);
  10270. if (!name)
  10271. name = "";
  10272. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  10273. }];
  10274. // 再根据name 拼sql
  10275. NSMutableString *mutable_price_name = [NSMutableString string];
  10276. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  10277. for (int i = 1; i < price_name_array.count; i++) {
  10278. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  10279. }
  10280. [mutable_price_name appendString:@")"];
  10281. price_name = mutable_price_name;
  10282. } else {
  10283. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  10284. if ([price_name isEqualToString:@""]) {
  10285. price_name = @"";
  10286. } else {
  10287. __block NSString *price;
  10288. price_name = [self translateSingleQuote:price_name];
  10289. [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) {
  10290. char *p = (char *)sqlite3_column_text(stmt, 0);
  10291. if (p == NULL) {
  10292. p = "";
  10293. }
  10294. price = [NSString stringWithUTF8String:p];
  10295. }];
  10296. if ([price isEqualToString:@""]) {
  10297. price_name = @"";
  10298. } else {
  10299. price = [self translateSingleQuote:price];
  10300. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  10301. }
  10302. }
  10303. }
  10304. } else {
  10305. price_name = @"";
  10306. }
  10307. int limit = [[params valueForKey:@"limit"] intValue];
  10308. int offset = [[params valueForKey:@"offset"] intValue];
  10309. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10310. 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];
  10311. 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];
  10312. // int result= [iSalesDB AddExFunction:db];
  10313. int count =0;
  10314. NSString *sqlQuery = nil;
  10315. if(keyword.length==0)
  10316. {
  10317. // 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];
  10318. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  10319. sqlQuery = sql;
  10320. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  10321. }
  10322. else
  10323. {
  10324. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  10325. keyword = keyword.lowercaseString;
  10326. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10327. 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];
  10328. 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]];
  10329. }
  10330. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10331. sqlite3_stmt * statement;
  10332. [ret setValue:@"2" forKey:@"result"];
  10333. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  10334. // 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";
  10335. int i = 0;
  10336. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10337. {
  10338. while (sqlite3_step(statement) == SQLITE_ROW)
  10339. {
  10340. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10341. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10342. int editable = sqlite3_column_int(statement, 0);
  10343. char *company_name = (char*)sqlite3_column_text(statement, 1);
  10344. NSString *nscompany_name =nil;
  10345. if(company_name==nil)
  10346. nscompany_name=@"";
  10347. else
  10348. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  10349. char *country = (char*)sqlite3_column_text(statement, 2);
  10350. if(country==nil)
  10351. country="";
  10352. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10353. // char *addr = (char*)sqlite3_column_text(statement, 3);
  10354. // if(addr==nil)
  10355. // addr="";
  10356. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10357. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10358. if(zipcode==nil)
  10359. zipcode="";
  10360. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10361. char *state = (char*)sqlite3_column_text(statement, 5);
  10362. if(state==nil)
  10363. state="";
  10364. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10365. char *city = (char*)sqlite3_column_text(statement, 6);
  10366. if(city==nil)
  10367. city="";
  10368. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10369. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10370. // NSString *nscontact_name = nil;
  10371. // if(contact_name==nil)
  10372. // nscontact_name=@"";
  10373. // else
  10374. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10375. char *phone = (char*)sqlite3_column_text(statement, 8);
  10376. NSString *nsphone = nil;
  10377. if(phone==nil)
  10378. nsphone=@"";
  10379. else
  10380. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10381. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10382. if(contact_id==nil)
  10383. contact_id="";
  10384. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10385. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10386. if(addr_1==nil)
  10387. addr_1="";
  10388. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10389. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10390. if(addr_2==nil)
  10391. addr_2="";
  10392. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10393. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10394. if(addr_3==nil)
  10395. addr_3="";
  10396. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10397. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10398. if(addr_4==nil)
  10399. addr_4="";
  10400. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10401. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10402. if(first_name==nil)
  10403. first_name="";
  10404. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10405. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10406. if(last_name==nil)
  10407. last_name="";
  10408. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10409. char *fax = (char*)sqlite3_column_text(statement, 16);
  10410. NSString *nsfax = nil;
  10411. if(fax==nil)
  10412. nsfax=@"";
  10413. else
  10414. {
  10415. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10416. if(nsfax.length>0)
  10417. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10418. }
  10419. char *email = (char*)sqlite3_column_text(statement, 17);
  10420. NSString *nsemail = nil;
  10421. if(email==nil)
  10422. nsemail=@"";
  10423. else
  10424. {
  10425. nsemail= [[NSString alloc]initWithUTF8String:email];
  10426. if(nsemail.length>0)
  10427. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10428. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10429. }
  10430. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10431. [arr_name addObject:nsfirst_name];
  10432. [arr_name addObject:nslast_name];
  10433. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10434. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10435. {
  10436. // decrypt
  10437. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10438. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10439. nsphone=[AESCrypt fastdecrypt:nsphone];
  10440. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10441. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10442. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10443. }
  10444. [arr_addr addObject:nscompany_name];
  10445. [arr_addr addObject:nscontact_name];
  10446. [arr_addr addObject:@"<br>"];
  10447. [arr_addr addObject:nsaddr_1];
  10448. [arr_addr addObject:nsaddr_2];
  10449. [arr_addr addObject:nsaddr_3];
  10450. [arr_addr addObject:nsaddr_4];
  10451. //[arr_addr addObject:nsaddr];
  10452. [arr_addr addObject:nszipcode];
  10453. [arr_addr addObject:nscity];
  10454. [arr_addr addObject:nsstate];
  10455. [arr_addr addObject:nscountry];
  10456. [arr_addr addObject:@"<br>"];
  10457. [arr_addr addObject:nsphone];
  10458. [arr_addr addObject:nsfax];
  10459. [arr_addr addObject:nsemail];
  10460. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10461. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10462. [item setValue:name forKey:@"name"];
  10463. [item setValue:nscontact_id forKey:@"contact_id"];
  10464. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10465. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10466. i++;
  10467. }
  10468. sqlite3_finalize(statement);
  10469. }
  10470. [iSalesDB close_db:db];
  10471. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10472. dispatch_async(dispatch_get_main_queue(), ^{
  10473. UIApplication * app = [UIApplication sharedApplication];
  10474. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10475. [ret setValue:appDelegate.mode forKey:@"mode"];
  10476. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10477. result(ret);
  10478. });
  10479. return ;
  10480. });
  10481. }
  10482. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10483. //{
  10484. //
  10485. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10486. //
  10487. // NSString* contactId = [params valueForKey:@"contactId"];
  10488. //
  10489. //
  10490. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10491. //
  10492. //
  10493. //
  10494. //
  10495. //
  10496. //
  10497. // sqlite3 *db = [iSalesDB get_db];
  10498. //
  10499. //
  10500. //
  10501. //
  10502. //
  10503. // NSString *sqlQuery = nil;
  10504. //
  10505. //
  10506. // {
  10507. // 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];
  10508. //
  10509. // }
  10510. //
  10511. //
  10512. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10513. // sqlite3_stmt * statement;
  10514. //
  10515. //
  10516. // [ret setValue:@"2" forKey:@"result"];
  10517. //
  10518. //
  10519. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10520. // {
  10521. //
  10522. // //int i = 0;
  10523. // if (sqlite3_step(statement) == SQLITE_ROW)
  10524. // {
  10525. //
  10526. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10527. //
  10528. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10529. //
  10530. //
  10531. // // int editable = sqlite3_column_int(statement, 0);
  10532. //
  10533. //
  10534. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10535. // NSString *nscompany_name =nil;
  10536. // if(company_name==nil)
  10537. // nscompany_name=@"";
  10538. // else
  10539. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10540. //
  10541. //
  10542. // char *country = (char*)sqlite3_column_text(statement, 2);
  10543. // if(country==nil)
  10544. // country="";
  10545. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10546. //
  10547. //
  10548. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10549. // // if(addr==nil)
  10550. // // addr="";
  10551. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10552. //
  10553. //
  10554. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10555. // if(zipcode==nil)
  10556. // zipcode="";
  10557. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10558. //
  10559. //
  10560. // char *state = (char*)sqlite3_column_text(statement, 5);
  10561. // if(state==nil)
  10562. // state="";
  10563. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10564. //
  10565. // char *city = (char*)sqlite3_column_text(statement, 6);
  10566. // if(city==nil)
  10567. // city="";
  10568. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10569. //
  10570. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10571. // // NSString *nscontact_name = nil;
  10572. // // if(contact_name==nil)
  10573. // // nscontact_name=@"";
  10574. // // else
  10575. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10576. //
  10577. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10578. // NSString *nsphone = nil;
  10579. // if(phone==nil)
  10580. // nsphone=@"";
  10581. // else
  10582. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10583. //
  10584. //
  10585. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10586. // // if(contact_id==nil)
  10587. // // contact_id="";
  10588. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10589. //
  10590. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10591. // if(addr_1==nil)
  10592. // addr_1="";
  10593. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10594. //
  10595. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10596. // if(addr_2==nil)
  10597. // addr_2="";
  10598. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10599. //
  10600. //
  10601. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10602. // if(addr_3==nil)
  10603. // addr_3="";
  10604. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10605. //
  10606. //
  10607. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10608. // if(addr_4==nil)
  10609. // addr_4="";
  10610. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10611. //
  10612. //
  10613. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10614. // if(first_name==nil)
  10615. // first_name="";
  10616. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10617. //
  10618. //
  10619. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10620. // if(last_name==nil)
  10621. // last_name="";
  10622. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10623. //
  10624. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10625. // NSString *nsfax = nil;
  10626. // if(fax==nil)
  10627. // nsfax=@"";
  10628. // else
  10629. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10630. //
  10631. // char *email = (char*)sqlite3_column_text(statement, 17);
  10632. // NSString *nsemail = nil;
  10633. // if(email==nil)
  10634. // nsemail=@"";
  10635. // else
  10636. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10637. //
  10638. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10639. // NSString *nsimg_0 = nil;
  10640. // if(img_0==nil)
  10641. // nsimg_0=@"";
  10642. // else
  10643. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10644. //
  10645. // [self copy_bcardImg:nsimg_0];
  10646. //
  10647. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10648. // NSString *nsimg_1 = nil;
  10649. // if(img_1==nil)
  10650. // nsimg_1=@"";
  10651. // else
  10652. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10653. // [self copy_bcardImg:nsimg_1];
  10654. //
  10655. //
  10656. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10657. // NSString *nsimg_2 = nil;
  10658. // if(img_2==nil)
  10659. // nsimg_2=@"";
  10660. // else
  10661. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10662. // [self copy_bcardImg:nsimg_2];
  10663. //
  10664. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10665. // NSString *nsprice_type = nil;
  10666. // if(price_type==nil)
  10667. // nsprice_type=@"";
  10668. // else
  10669. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10670. //
  10671. //
  10672. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10673. // NSString *nsnotes = nil;
  10674. // if(notes==nil)
  10675. // nsnotes=@"";
  10676. // else
  10677. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10678. //
  10679. //
  10680. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10681. // NSString *nssalesrep = nil;
  10682. // if(salesrep==nil)
  10683. // nssalesrep=@"";
  10684. // else
  10685. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10686. //
  10687. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10688. //
  10689. //
  10690. // {
  10691. // // decrypt
  10692. //
  10693. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10694. //
  10695. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10696. //
  10697. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10698. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10699. //
  10700. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10701. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10702. //
  10703. // }
  10704. //
  10705. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10706. //
  10707. // [arr_name addObject:nsfirst_name];
  10708. // [arr_name addObject:nslast_name];
  10709. //
  10710. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10711. //
  10712. //
  10713. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10714. //
  10715. // [arr_ext addObject:nsaddr_1];
  10716. // [arr_ext addObject:nsaddr_2];
  10717. // [arr_ext addObject:nsaddr_3];
  10718. // [arr_ext addObject:nsaddr_4];
  10719. // [arr_ext addObject:@"\r\n"];
  10720. //
  10721. // [arr_ext addObject:nscity];
  10722. // [arr_ext addObject:nsstate];
  10723. // [arr_ext addObject:nszipcode];
  10724. // [arr_ext addObject:nscountry];
  10725. //
  10726. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10727. //
  10728. //
  10729. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10730. //
  10731. //
  10732. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10733. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10734. // [item setValue:nscountry forKey:@"customer_country"];
  10735. // [item setValue:nsphone forKey:@"customer_phone"];
  10736. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10737. // [item setValue:nscompany_name forKey:@"customer_name"];
  10738. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10739. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10740. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10741. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10742. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10743. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10744. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10745. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  10746. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  10747. // [item setValue:nslast_name forKey:@"customer_last_name"];
  10748. // [item setValue:nscity forKey:@"customer_city"];
  10749. // [item setValue:nsstate forKey:@"customer_state"];
  10750. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  10751. // [item setValue:contactId forKey:@"customer_cid"];
  10752. // [item setValue:nscontact_name forKey:@"customer_contact"];
  10753. // [item setValue:nsfax forKey:@"customer_fax"];
  10754. // [item setValue:nsemail forKey:@"customer_email"];
  10755. // [item setValue:contact_type forKey:@"customer_contact_type"];
  10756. //
  10757. //
  10758. // [ret setObject:item forKey:@"customerInfo"];
  10759. // // i++;
  10760. //
  10761. //
  10762. //
  10763. // }
  10764. //
  10765. //
  10766. //
  10767. //
  10768. // sqlite3_finalize(statement);
  10769. // }
  10770. //
  10771. //
  10772. //
  10773. // [iSalesDB close_db:db];
  10774. //
  10775. //
  10776. //
  10777. //
  10778. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10779. // dispatch_async(dispatch_get_main_queue(), ^{
  10780. // UIApplication * app = [UIApplication sharedApplication];
  10781. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10782. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10783. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10784. // result(ret);
  10785. // });
  10786. //
  10787. // return ;
  10788. // });
  10789. //}
  10790. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10791. {
  10792. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10793. NSString* order_code= appDelegate.order_code;
  10794. NSString* user = appDelegate.user;
  10795. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10796. NSString* category = [params valueForKey:@"category"];
  10797. if (!category || [category isEqualToString:@""]) {
  10798. category = @"%";
  10799. }
  10800. category = [self translateSingleQuote:category];
  10801. int limit = [[params valueForKey:@"limit"] intValue];
  10802. int offset = [[params valueForKey:@"offset"] intValue];
  10803. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10804. NSString *limit_str = @"";
  10805. if (limited) {
  10806. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10807. }
  10808. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10809. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10810. sqlite3 *db = [iSalesDB get_db];
  10811. // [iSalesDB AddExFunction:db];
  10812. int count;
  10813. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  10814. 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];
  10815. double price_min = 0;
  10816. double price_max = 0;
  10817. if ([params.allKeys containsObject:@"alert"]) {
  10818. // alert
  10819. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  10820. NSString *alert = params[@"alert"];
  10821. if ([alert isEqualToString:@"Display All"]) {
  10822. alert = [NSString stringWithFormat:@""];
  10823. } else {
  10824. alert = [self translateSingleQuote:alert];
  10825. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  10826. }
  10827. // available
  10828. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  10829. NSString *available = params[@"available"];
  10830. NSString *available_condition;
  10831. if ([available isEqualToString:@"Display All"]) {
  10832. available_condition = @"";
  10833. } else if ([available isEqualToString:@"Available Now"]) {
  10834. available_condition = @"and availability > 0";
  10835. } else {
  10836. available_condition = @"and availability == 0";
  10837. }
  10838. // best seller
  10839. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  10840. NSString *best_seller = @"";
  10841. NSString *order_best_seller = @"m.name asc";
  10842. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  10843. best_seller = @"and best_seller > 0";
  10844. order_best_seller = @"m.best_seller desc,m.name asc";
  10845. }
  10846. // price
  10847. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  10848. NSString *price = params[@"price"];
  10849. price_min = 0;
  10850. price_max = MAXFLOAT;
  10851. if (user && price != nil) {
  10852. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  10853. NSMutableString *priceName = [NSMutableString string];
  10854. for (int i = 0; i < priceTypeArray.count; i++) {
  10855. NSString *pricetype = priceTypeArray[i];
  10856. pricetype = [self translateSingleQuote:pricetype];
  10857. if (i == 0) {
  10858. [priceName appendFormat:@"'%@'",pricetype];
  10859. } else {
  10860. [priceName appendFormat:@",'%@'",pricetype];
  10861. }
  10862. }
  10863. if ([price isEqualToString:@"Display All"]) {
  10864. price = [NSString stringWithFormat:@""];
  10865. } else if([price containsString:@"+"]){
  10866. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  10867. price_min = [price doubleValue];
  10868. 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];
  10869. } else {
  10870. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  10871. price_min = [[priceArray objectAtIndex:0] doubleValue];
  10872. price_max = [[priceArray objectAtIndex:1] doubleValue];
  10873. 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];
  10874. }
  10875. } else {
  10876. price = @"";
  10877. }
  10878. // sold_by_qty : Sold in quantities of %@
  10879. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  10880. NSString *qty = params[@"sold_by_qty"];
  10881. if ([qty isEqualToString:@"Display All"]) {
  10882. qty = @"";
  10883. } else {
  10884. qty = [self translateSingleQuote:qty];
  10885. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  10886. }
  10887. // cate
  10888. // category = [self translateSingleQuote:category];
  10889. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  10890. // cate mutiple selection
  10891. NSString *category_id = params[@"category"];
  10892. NSMutableArray *cate_id_array = nil;
  10893. NSMutableString *cateWhere = [NSMutableString string];
  10894. if ([category_id isEqualToString:@""] || !category_id) {
  10895. [cateWhere appendString:@"1 = 1"];
  10896. } else {
  10897. if ([category_id containsString:@","]) {
  10898. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  10899. } else {
  10900. cate_id_array = [@[category_id] mutableCopy];
  10901. }
  10902. [cateWhere appendString:@"("];
  10903. for (int i = 0; i < cate_id_array.count; i++) {
  10904. if (i == 0) {
  10905. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10906. } else {
  10907. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10908. }
  10909. }
  10910. [cateWhere appendString:@")"];
  10911. }
  10912. // where bestseller > 0 order by bestseller desc
  10913. // sql query: alert availability(int) best_seller(int) price qty
  10914. 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];
  10915. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  10916. }
  10917. DebugLog(@"offline category where: %@",where);
  10918. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  10919. if (!user) {
  10920. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  10921. }
  10922. [ret setValue:filter forKey:@"filter"];
  10923. DebugLog(@"offline_category sql:%@",sqlQuery);
  10924. sqlite3_stmt * statement;
  10925. [ret setValue:@"2" forKey:@"result"];
  10926. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10927. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10928. // int count=0;
  10929. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10930. {
  10931. int i=0;
  10932. while (sqlite3_step(statement) == SQLITE_ROW)
  10933. {
  10934. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10935. char *name = (char*)sqlite3_column_text(statement, 0);
  10936. if(name==nil)
  10937. name="";
  10938. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10939. char *description = (char*)sqlite3_column_text(statement, 1);
  10940. if(description==nil)
  10941. description="";
  10942. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10943. int product_id = sqlite3_column_int(statement, 2);
  10944. int wid = sqlite3_column_int(statement, 3);
  10945. int closeout = sqlite3_column_int(statement, 4);
  10946. int cid = sqlite3_column_int(statement, 5);
  10947. int wisdelete = sqlite3_column_int(statement, 6);
  10948. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  10949. int more_color = sqlite3_column_int(statement, 8);
  10950. // Defaul Category ID
  10951. __block NSString *categoryID = nil;
  10952. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  10953. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10954. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  10955. if(default_category==nil)
  10956. default_category="";
  10957. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  10958. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  10959. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  10960. categoryID = nsdefault_category;
  10961. }];
  10962. if (!categoryID.length) {
  10963. NSString *cateIDs = params[@"category"];
  10964. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  10965. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  10966. for (NSString *cateID in requestCategoryArr) {
  10967. BOOL needBreak = NO;
  10968. for (NSString *itemCateIDBox in itemCategoryArr) {
  10969. if (itemCateIDBox.length > 4) {
  10970. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  10971. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  10972. if ([itemCategoryID isEqualToString:cateID]) {
  10973. needBreak = YES;
  10974. categoryID = itemCategoryID;
  10975. break;
  10976. }
  10977. }
  10978. }
  10979. if (needBreak) {
  10980. break;
  10981. }
  10982. }
  10983. }
  10984. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10985. if(wid !=0 && wisdelete != 1)
  10986. [item setValue:@"true" forKey:@"wish_exists"];
  10987. else
  10988. [item setValue:@"false" forKey:@"wish_exists"];
  10989. if(closeout==0)
  10990. [item setValue:@"false" forKey:@"is_closeout"];
  10991. else
  10992. [item setValue:@"true" forKey:@"is_closeout"];
  10993. if(cid==0)
  10994. [item setValue:@"false" forKey:@"cart_exists"];
  10995. else
  10996. [item setValue:@"true" forKey:@"cart_exists"];
  10997. if (more_color == 0) {
  10998. [item setObject:@(false) forKey:@"more_color"];
  10999. } else if (more_color == 1) {
  11000. [item setObject:@(true) forKey:@"more_color"];
  11001. }
  11002. [item addEntriesFromDictionary:imgjson];
  11003. // [item setValue:nsurl forKey:@"img"];
  11004. [item setValue:nsname forKey:@"name"];
  11005. [item setValue:nsdescription forKey:@"description"];
  11006. if (categoryID) {
  11007. [item setValue:categoryID forKey:@"item_category_id"];
  11008. }
  11009. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11010. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11011. i++;
  11012. }
  11013. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11014. [ret setObject:items forKey:@"items"];
  11015. sqlite3_finalize(statement);
  11016. } else {
  11017. DebugLog(@"nothing...");
  11018. }
  11019. DebugLog(@"count:%d",count);
  11020. [iSalesDB close_db:db];
  11021. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11022. dispatch_async(dispatch_get_main_queue(), ^{
  11023. // UIApplication * app = [UIApplication sharedApplication];
  11024. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11025. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11026. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11027. result(ret);
  11028. });
  11029. });
  11030. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11031. }
  11032. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11033. {
  11034. [self categoryList:params limited:YES completionHandler:result];
  11035. }
  11036. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11037. {
  11038. UIApplication * app = [UIApplication sharedApplication];
  11039. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11040. NSString* order_code = appDelegate.order_code;
  11041. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11042. NSString* keyword = [params valueForKey:@"keyword"];
  11043. keyword=keyword.lowercaseString;
  11044. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11045. int limit = [[params valueForKey:@"limit"] intValue];
  11046. int offset = [[params valueForKey:@"offset"] intValue];
  11047. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11048. NSString *limit_str = @"";
  11049. if (limited) {
  11050. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11051. }
  11052. sqlite3 *db = [iSalesDB get_db];
  11053. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11054. NSString *sqlQuery = nil;
  11055. if(exactMatch )
  11056. 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 ;
  11057. else
  11058. 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
  11059. DebugLog(@"offline_search sql:%@",sqlQuery);
  11060. sqlite3_stmt * statement;
  11061. [ret setValue:@"2" forKey:@"result"];
  11062. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11063. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11064. // int count=0;
  11065. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11066. {
  11067. int i=0;
  11068. while (sqlite3_step(statement) == SQLITE_ROW)
  11069. {
  11070. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11071. // char *name = (char*)sqlite3_column_text(statement, 1);
  11072. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11073. char *name = (char*)sqlite3_column_text(statement, 0);
  11074. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11075. char *description = (char*)sqlite3_column_text(statement, 1);
  11076. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11077. int product_id = sqlite3_column_int(statement, 2);
  11078. // char *url = (char*)sqlite3_column_text(statement, 3);
  11079. // if(url==nil)
  11080. // url="";
  11081. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11082. int wid = sqlite3_column_int(statement, 3);
  11083. int closeout = sqlite3_column_int(statement, 4);
  11084. int cid = sqlite3_column_int(statement, 5);
  11085. int wisdelete = sqlite3_column_int(statement, 6);
  11086. int more_color = sqlite3_column_int(statement, 7);
  11087. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11088. if(wid !=0 && wisdelete != 1)
  11089. [item setValue:@"true" forKey:@"wish_exists"];
  11090. else
  11091. [item setValue:@"false" forKey:@"wish_exists"];
  11092. if(closeout==0)
  11093. [item setValue:@"false" forKey:@"is_closeout"];
  11094. else
  11095. [item setValue:@"true" forKey:@"is_closeout"];
  11096. if(cid==0)
  11097. [item setValue:@"false" forKey:@"cart_exists"];
  11098. else
  11099. [item setValue:@"true" forKey:@"cart_exists"];
  11100. if (more_color == 0) {
  11101. [item setObject:@(false) forKey:@"more_color"];
  11102. } else if (more_color == 1) {
  11103. [item setObject:@(true) forKey:@"more_color"];
  11104. }
  11105. [item addEntriesFromDictionary:imgjson];
  11106. // [item setValue:nsurl forKey:@"img"];
  11107. [item setValue:nsname forKey:@"fash_name"];
  11108. [item setValue:nsdescription forKey:@"description"];
  11109. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11110. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11111. i++;
  11112. }
  11113. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11114. [ret setObject:items forKey:@"items"];
  11115. sqlite3_finalize(statement);
  11116. }
  11117. DebugLog(@"count:%d",count);
  11118. [iSalesDB close_db:db];
  11119. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11120. dispatch_async(dispatch_get_main_queue(), ^{
  11121. result(ret);
  11122. });
  11123. });
  11124. }
  11125. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11126. {
  11127. [self search:params limited:YES completionHandler:result];
  11128. }
  11129. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11130. //{
  11131. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11132. // NSString* order_code = appDelegate.order_code;
  11133. // NSString* user = appDelegate.user;
  11134. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11135. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11136. //
  11137. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11138. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11139. //
  11140. // // category
  11141. // NSDictionary *category_menu = [self offline_category_menu];
  11142. // [filter setValue:category_menu forKey:@"category"];
  11143. //
  11144. // NSString* where= nil;
  11145. // NSString* orderby= @"m.name";
  11146. // if (!filterSearch) {
  11147. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11148. //
  11149. // switch (covertype) {
  11150. // case 0:
  11151. // {
  11152. // where=@"m.category like'%%#005#%%'";
  11153. // break;
  11154. // }
  11155. // case 1:
  11156. // {
  11157. // where=@"m.alert like '%QS%'";
  11158. // break;
  11159. // }
  11160. // case 2:
  11161. // {
  11162. // where=@"m.availability>0";
  11163. // break;
  11164. // }
  11165. // case 3:
  11166. // {
  11167. // where=@"m.best_seller>0";
  11168. // orderby=@"m.best_seller desc,m.name asc";
  11169. // break;
  11170. // }
  11171. // default:
  11172. // where=@"1=1";
  11173. // break;
  11174. // }
  11175. //
  11176. // }
  11177. //
  11178. //
  11179. // int limit = [[params valueForKey:@"limit"] intValue];
  11180. // int offset = [[params valueForKey:@"offset"] intValue];
  11181. //
  11182. // NSString *limit_str = @"";
  11183. // if (limited) {
  11184. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11185. // }
  11186. //
  11187. //
  11188. // sqlite3 *db = [iSalesDB get_db];
  11189. // // [iSalesDB AddExFunction:db];
  11190. //
  11191. // int count;
  11192. //
  11193. // NSString *sqlQuery = nil;
  11194. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11195. // 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];
  11196. //
  11197. //
  11198. // double price_min = 0;
  11199. // double price_max = 0;
  11200. // if (filterSearch) {
  11201. // // alert
  11202. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11203. // NSString *alert = params[@"alert"];
  11204. // if ([alert isEqualToString:@"Display All"]) {
  11205. // alert = [NSString stringWithFormat:@""];
  11206. // } else {
  11207. // alert = [self translateSingleQuote:alert];
  11208. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11209. // }
  11210. //
  11211. // // available
  11212. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11213. // NSString *available = params[@"available"];
  11214. // NSString *available_condition;
  11215. // if ([available isEqualToString:@"Display All"]) {
  11216. // available_condition = @"";
  11217. // } else if ([available isEqualToString:@"Available Now"]) {
  11218. // available_condition = @"and availability > 0";
  11219. // } else {
  11220. // available_condition = @"and availability == 0";
  11221. // }
  11222. //
  11223. // // best seller
  11224. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11225. // NSString *best_seller = @"";
  11226. // NSString *order_best_seller = @"m.name asc";
  11227. //
  11228. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11229. // best_seller = @"and best_seller > 0";
  11230. // order_best_seller = @"m.best_seller desc,m.name asc";
  11231. // }
  11232. //
  11233. // // price
  11234. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11235. // NSString *price = params[@"price"];
  11236. // price_min = 0;
  11237. // price_max = MAXFLOAT;
  11238. // if (user) {
  11239. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11240. // NSMutableString *priceName = [NSMutableString string];
  11241. // for (int i = 0; i < priceTypeArray.count; i++) {
  11242. // NSString *pricetype = priceTypeArray[i];
  11243. // pricetype = [self translateSingleQuote:pricetype];
  11244. // if (i == 0) {
  11245. // [priceName appendFormat:@"'%@'",pricetype];
  11246. // } else {
  11247. // [priceName appendFormat:@",'%@'",pricetype];
  11248. // }
  11249. // }
  11250. //
  11251. // if ([price isEqualToString:@"Display All"]) {
  11252. // price = [NSString stringWithFormat:@""];
  11253. // } else if([price containsString:@"+"]){
  11254. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11255. // price_min = [price doubleValue];
  11256. // 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];
  11257. // } else {
  11258. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11259. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  11260. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  11261. // 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];
  11262. // }
  11263. // } else {
  11264. // price = @"";
  11265. // }
  11266. //
  11267. // // sold_by_qty : Sold in quantities of %@
  11268. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11269. // NSString *qty = params[@"sold_by_qty"];
  11270. // if ([qty isEqualToString:@"Display All"]) {
  11271. // qty = @"";
  11272. // } else {
  11273. // qty = [self translateSingleQuote:qty];
  11274. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11275. // }
  11276. //
  11277. // // category
  11278. // NSString *category_id = params[@"ctgId"];
  11279. // NSMutableArray *cate_id_array = nil;
  11280. // NSMutableString *cateWhere = [NSMutableString string];
  11281. //
  11282. // if ([category_id isEqualToString:@""] || !category_id) {
  11283. // [cateWhere appendString:@"1 = 1"];
  11284. // } else {
  11285. //
  11286. // if ([category_id containsString:@","]) {
  11287. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11288. // } else {
  11289. // cate_id_array = [@[category_id] mutableCopy];
  11290. // }
  11291. // /*-----------*/
  11292. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  11293. //
  11294. // [cateWhere appendString:@"("];
  11295. // for (int i = 0; i < cate_id_array.count; i++) {
  11296. //
  11297. // for (NSString *key0 in cateDic.allKeys) {
  11298. //
  11299. // if ([key0 containsString:@"category_"]) {
  11300. //
  11301. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  11302. //
  11303. // for (NSString *key1 in category0.allKeys) {
  11304. //
  11305. // if ([key1 containsString:@"category_"]) {
  11306. //
  11307. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  11308. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  11309. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11310. //
  11311. // cate_id_array[i] = [category1 objectForKey:@"id"];
  11312. // if (i == 0) {
  11313. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11314. // } else {
  11315. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11316. // }
  11317. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11318. // [category0 setValue:category1 forKey:key1];
  11319. // [cateDic setValue:category0 forKey:key0];
  11320. //
  11321. // }
  11322. //
  11323. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  11324. //
  11325. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11326. //
  11327. // cate_id_array[i] = [category0 objectForKey:@"id"];
  11328. // if (i == 0) {
  11329. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11330. // } else {
  11331. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11332. // }
  11333. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11334. // [cateDic setValue:category0 forKey:key0];
  11335. //
  11336. // }
  11337. //
  11338. // }
  11339. //
  11340. // }
  11341. //
  11342. // }
  11343. //
  11344. // }
  11345. //
  11346. // }
  11347. // [cateWhere appendString:@")"];
  11348. //
  11349. // [filter setValue:cateDic forKey:@"category"];
  11350. // }
  11351. //
  11352. // // where bestseller > 0 order by bestseller desc
  11353. // // sql query: alert availability(int) best_seller(int) price qty
  11354. //
  11355. // 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];
  11356. //
  11357. //
  11358. // // count
  11359. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11360. //
  11361. // }
  11362. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11363. //
  11364. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11365. //
  11366. //
  11367. //
  11368. //
  11369. // if (!user) {
  11370. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11371. // }
  11372. //
  11373. // [ret setValue:filter forKey:@"filter"];
  11374. //
  11375. //
  11376. //
  11377. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11378. //
  11379. // sqlite3_stmt * statement;
  11380. // [ret setValue:@"2" forKey:@"result"];
  11381. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11382. //
  11383. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11384. // // int count=0;
  11385. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11386. // {
  11387. //
  11388. // int i=0;
  11389. // while (sqlite3_step(statement) == SQLITE_ROW)
  11390. // {
  11391. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11392. //
  11393. //
  11394. // char *name = (char*)sqlite3_column_text(statement, 0);
  11395. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11396. //
  11397. // char *description = (char*)sqlite3_column_text(statement, 1);
  11398. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11399. //
  11400. //
  11401. //
  11402. // int product_id = sqlite3_column_int(statement, 2);
  11403. //
  11404. //
  11405. // int wid = sqlite3_column_int(statement, 3);
  11406. // int closeout = sqlite3_column_int(statement, 4);
  11407. // int cid = sqlite3_column_int(statement, 5);
  11408. // int wisdelete = sqlite3_column_int(statement, 6);
  11409. // int more_color = sqlite3_column_int(statement, 7);
  11410. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11411. //
  11412. // if(wid !=0 && wisdelete != 1)
  11413. // [item setValue:@"true" forKey:@"wish_exists"];
  11414. // else
  11415. // [item setValue:@"false" forKey:@"wish_exists"];
  11416. //
  11417. // if(closeout==0)
  11418. // [item setValue:@"false" forKey:@"is_closeout"];
  11419. // else
  11420. // [item setValue:@"true" forKey:@"is_closeout"];
  11421. //
  11422. // if(cid==0)
  11423. // [item setValue:@"false" forKey:@"cart_exists"];
  11424. // else
  11425. // [item setValue:@"true" forKey:@"cart_exists"];
  11426. //
  11427. // if (more_color == 0) {
  11428. // [item setObject:@(false) forKey:@"more_color"];
  11429. // } else if (more_color == 1) {
  11430. // [item setObject:@(true) forKey:@"more_color"];
  11431. // }
  11432. //
  11433. // [item addEntriesFromDictionary:imgjson];
  11434. //
  11435. //
  11436. // // [item setValue:nsurl forKey:@"img"];
  11437. // [item setValue:nsname forKey:@"fash_name"];
  11438. // [item setValue:nsdescription forKey:@"description"];
  11439. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11440. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11441. // i++;
  11442. // }
  11443. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11444. // [ret setObject:items forKey:@"items"];
  11445. // sqlite3_finalize(statement);
  11446. // }
  11447. //
  11448. // [iSalesDB close_db:db];
  11449. //
  11450. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11451. //
  11452. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11453. // dispatch_async(dispatch_get_main_queue(), ^{
  11454. //
  11455. // result(ret);
  11456. // });
  11457. //
  11458. // });
  11459. //
  11460. //
  11461. //
  11462. //}
  11463. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11464. //{
  11465. // [self itemsearch:params limited:YES completionHandler:result];
  11466. //}
  11467. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11468. {
  11469. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11470. NSString* offline_command=params[@"offline_Command"];
  11471. NSMutableDictionary* ret=nil;
  11472. if([offline_command isEqualToString:@"model_NIYMAL"])
  11473. {
  11474. NSString* category = params[@"category"];
  11475. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11476. }
  11477. dispatch_async(dispatch_get_main_queue(), ^{
  11478. result(ret);
  11479. });
  11480. });
  11481. }
  11482. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11483. {
  11484. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11485. int sort = [[params objectForKey:@"sort"] intValue];
  11486. NSString *sort_str = @"";
  11487. switch (sort) {
  11488. case 0:{
  11489. sort_str = @"order by w.modify_time desc";
  11490. }
  11491. break;
  11492. case 1:{
  11493. sort_str = @"order by w.modify_time asc";
  11494. }
  11495. break;
  11496. case 2:{
  11497. sort_str = @"order by m.name asc";
  11498. }
  11499. break;
  11500. case 3:{
  11501. sort_str = @"order by m.name desc";
  11502. }
  11503. break;
  11504. case 4:{
  11505. sort_str = @"order by m.description asc";
  11506. }
  11507. break;
  11508. default:
  11509. break;
  11510. }
  11511. // NSString* user = appDelegate.user;
  11512. sqlite3 *db = [iSalesDB get_db];
  11513. // order by w.create_time
  11514. 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];
  11515. // 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];
  11516. sqlite3_stmt * statement;
  11517. NSDate *date1 = [NSDate date];
  11518. // NSDate *date2 = nil;
  11519. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11520. int count=0;
  11521. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11522. {
  11523. while (sqlite3_step(statement) == SQLITE_ROW)
  11524. {
  11525. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11526. int product_id = sqlite3_column_double(statement, 0);
  11527. char *description = (char*)sqlite3_column_text(statement, 1);
  11528. if(description==nil)
  11529. description= "";
  11530. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11531. int item_id = sqlite3_column_double(statement, 2);
  11532. NSDate *date_image = [NSDate date];
  11533. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11534. printf("image : ");
  11535. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11536. // char *url = (char*)sqlite3_column_text(statement, 3);
  11537. // if(url==nil)
  11538. // url="";
  11539. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11540. int qty = sqlite3_column_int(statement, 3);
  11541. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11542. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11543. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11544. item[@"description"]= nsdescription;
  11545. item[@"img"]= nsurl;
  11546. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11547. count++;
  11548. }
  11549. printf("total time:");
  11550. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11551. ret[@"count"]= [NSNumber numberWithInt:count];
  11552. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11553. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11554. ret[@"result"]= [NSNumber numberWithInt:2];
  11555. sqlite3_finalize(statement);
  11556. }
  11557. [iSalesDB close_db:db];
  11558. dispatch_async(dispatch_get_main_queue(), ^{
  11559. UIApplication * app = [UIApplication sharedApplication];
  11560. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11561. appDelegate.wish_count =count;
  11562. [appDelegate update_count_mark];
  11563. result(ret);
  11564. });
  11565. });
  11566. }
  11567. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11568. {
  11569. // UIApplication * app = [UIApplication sharedApplication];
  11570. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11571. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11572. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11573. sqlite3 *db = [iSalesDB get_db];
  11574. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11575. NSString* product_id=params[@"product_id"];
  11576. NSString *item_count_str = params[@"item_count"];
  11577. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11578. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11579. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11580. // NSString *sql = @"";
  11581. for(int i=0;i<arr.count;i++)
  11582. {
  11583. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11584. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11585. __block int cart_count = 0;
  11586. if (!item_count_str) {
  11587. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11588. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11589. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11590. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11591. cart_count = [[model_set_components lastObject] intValue];
  11592. }];
  11593. }
  11594. if(count==0)
  11595. {
  11596. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11597. sqlite3_stmt *stmt;
  11598. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11599. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11600. if (item_count_arr) {
  11601. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11602. } else {
  11603. sqlite3_bind_int(stmt,2,cart_count);
  11604. }
  11605. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11606. [iSalesDB execSql:@"ROLLBACK" db:db];
  11607. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11608. [iSalesDB close_db:db];
  11609. DebugLog(@"add to wishlist error");
  11610. dispatch_async(dispatch_get_main_queue(), ^{
  11611. result(ret);
  11612. });
  11613. }
  11614. } else {
  11615. int qty = 0;
  11616. if (item_count_arr) {
  11617. qty = [item_count_arr[i] intValue];
  11618. } else {
  11619. qty = cart_count;
  11620. }
  11621. 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]];
  11622. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11623. [iSalesDB execSql:@"ROLLBACK" db:db];
  11624. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11625. [iSalesDB close_db:db];
  11626. DebugLog(@"add to wishlist error");
  11627. dispatch_async(dispatch_get_main_queue(), ^{
  11628. result(ret);
  11629. });
  11630. }
  11631. }
  11632. }
  11633. // [iSalesDB execSql:sql db:db];
  11634. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11635. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11636. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11637. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11638. [iSalesDB close_db:db];
  11639. ret[@"result"]= [NSNumber numberWithInt:2];
  11640. dispatch_async(dispatch_get_main_queue(), ^{
  11641. UIApplication * app = [UIApplication sharedApplication];
  11642. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11643. appDelegate.wish_count =count;
  11644. [appDelegate update_count_mark];
  11645. result(ret);
  11646. });
  11647. });
  11648. }
  11649. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11650. {
  11651. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11652. bool bLogin = appDelegate.bLogin;
  11653. __block NSString* contact_id = appDelegate.contact_id;
  11654. __block NSString* user = appDelegate.user;
  11655. __block NSString* order_code = appDelegate.order_code;
  11656. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11657. NSString* model_name = [params valueForKey:@"product_name"];
  11658. NSString* product_id = [params valueForKey:@"product_id"];
  11659. NSString* upc_code = [params valueForKey:@"upc_code"];
  11660. NSString* category = [params valueForKey:@"category"];
  11661. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11662. if(category==nil) {
  11663. category = default_category_id;
  11664. } else {
  11665. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11666. // 参数重有多个category id
  11667. if (arr_0.count > 1) {
  11668. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11669. // 取交集
  11670. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11671. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11672. [set_0 intersectSet:set_1];
  11673. if (set_0.count == 1) {
  11674. category = (NSString *)[set_0 anyObject];
  11675. } else {
  11676. if ([set_0 containsObject:default_category_id]) {
  11677. category = default_category_id;
  11678. } else {
  11679. category = (NSString *)[set_0 anyObject];
  11680. }
  11681. }
  11682. }
  11683. }
  11684. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11685. sqlite3 *db = [iSalesDB get_db];
  11686. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11687. NSString *sqlQuery = nil;
  11688. if(product_id==nil)
  11689. if(model_name==nil)
  11690. {
  11691. 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='%@';
  11692. }
  11693. else
  11694. {
  11695. 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='%@';
  11696. }
  11697. else
  11698. 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=%@;
  11699. sqlite3_stmt * statement;
  11700. [ret setValue:@"2" forKey:@"result"];
  11701. [ret setValue:@"3" forKey:@"detail_section_count"];
  11702. // int count=0;
  11703. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11704. {
  11705. if (sqlite3_step(statement) == SQLITE_ROW)
  11706. {
  11707. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11708. char *name = (char*)sqlite3_column_text(statement, 0);
  11709. if(name==nil)
  11710. name="";
  11711. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11712. char *description = (char*)sqlite3_column_text(statement, 1);
  11713. if(description==nil)
  11714. description="";
  11715. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11716. int product_id = sqlite3_column_int(statement, 2);
  11717. char *color = (char*)sqlite3_column_text(statement, 3);
  11718. if(color==nil)
  11719. color="";
  11720. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11721. //
  11722. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11723. // if(legcolor==nil)
  11724. // legcolor="";
  11725. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11726. //
  11727. //
  11728. int availability = sqlite3_column_int(statement, 5);
  11729. //
  11730. int incoming_stock = sqlite3_column_int(statement, 6);
  11731. char *demension = (char*)sqlite3_column_text(statement, 7);
  11732. if(demension==nil)
  11733. demension="";
  11734. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11735. // ,,,,,,,,,
  11736. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11737. if(seat_height==nil)
  11738. seat_height="";
  11739. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11740. char *material = (char*)sqlite3_column_text(statement, 9);
  11741. if(material==nil)
  11742. material="";
  11743. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11744. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11745. if(box_dim==nil)
  11746. box_dim="";
  11747. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11748. char *volume = (char*)sqlite3_column_text(statement, 11);
  11749. if(volume==nil)
  11750. volume="";
  11751. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11752. double weight = sqlite3_column_double(statement, 12);
  11753. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11754. if(model_set==nil)
  11755. model_set="";
  11756. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11757. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11758. if(load_ability==nil)
  11759. load_ability="";
  11760. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11761. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11762. // if(default_category==nil)
  11763. // default_category="";
  11764. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11765. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11766. if(fabric_content==nil)
  11767. fabric_content="";
  11768. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11769. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11770. if(assembling==nil)
  11771. assembling="";
  11772. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11773. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11774. if(made_in==nil)
  11775. made_in="";
  11776. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11777. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11778. if(special_remarks==nil)
  11779. special_remarks="";
  11780. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11781. int stockUcom = sqlite3_column_double(statement, 20);
  11782. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11783. if(product_group==nil)
  11784. product_group="";
  11785. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11786. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11787. // if(fashion_selector==nil)
  11788. // fashion_selector="";
  11789. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11790. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11791. if(selector_field==nil)
  11792. selector_field="";
  11793. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11794. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11795. if(property_field==nil)
  11796. property_field="";
  11797. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11798. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11799. if(packaging==nil)
  11800. packaging="";
  11801. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11802. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11803. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11804. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11805. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11806. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11807. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11808. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11809. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11810. NSString* Availability=nil;
  11811. if(availability>0)
  11812. Availability=[NSString stringWithFormat:@"%d",availability];
  11813. else
  11814. Availability = @"Out of Stock";
  11815. [img_section setValue:Availability forKey:@"Availability"];
  11816. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11817. char *eta = (char*)sqlite3_column_text(statement, 25);
  11818. if(eta==nil)
  11819. eta="";
  11820. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11821. if ([nseta isEqualToString:@"null"]) {
  11822. nseta = @"";
  11823. }
  11824. if (availability <= 0) {
  11825. [img_section setValue:nseta forKey:@"ETA"];
  11826. }
  11827. int item_id = sqlite3_column_int(statement, 26);
  11828. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11829. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11830. NSString* Price=nil;
  11831. if(bLogin==false)
  11832. Price=@"Must Sign in.";
  11833. else
  11834. {
  11835. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11836. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11837. if(price==nil)
  11838. Price=@"No Price.";
  11839. else
  11840. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11841. }
  11842. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11843. [img_section setValue:Price forKey:@"price"];
  11844. [img_section setValue:nsname forKey:@"model_name"];
  11845. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11846. if (order_code) { // 离线order code即so#
  11847. 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];
  11848. __block int cartQTY = 0;
  11849. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11850. cartQTY = sqlite3_column_int(stmt, 0);
  11851. }];
  11852. if (cartQTY > 0) {
  11853. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11854. }
  11855. }
  11856. [ret setObject:img_section forKey:@"img_section"];
  11857. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11858. int detail0_item_count=0;
  11859. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11860. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11861. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11862. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11863. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11864. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11865. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11866. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11867. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11868. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11869. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11870. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11871. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11872. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11873. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  11874. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  11875. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  11876. {
  11877. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  11878. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11879. }
  11880. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  11881. [detail0_section setValue:@"kv" forKey:@"type"];
  11882. [detail0_section setValue:@"Product Information" forKey:@"title"];
  11883. [ret setObject:detail0_section forKey:@"detail_0"];
  11884. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  11885. // [detail1_section setValue:@"detail" forKey:@"target"];
  11886. // [detail1_section setValue:@"popup" forKey:@"action"];
  11887. // [detail1_section setValue:@"content" forKey:@"type"];
  11888. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  11889. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  11890. // [detail1_section setValue:@"true" forKey:@"single_row"];
  11891. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  11892. [ret setObject:detail1_section forKey:@"detail_1"];
  11893. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  11894. [detail2_section setValue:@"detail" forKey:@"target"];
  11895. [detail2_section setValue:@"popup" forKey:@"action"];
  11896. [detail2_section setValue:@"content" forKey:@"type"];
  11897. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  11898. [detail2_section setValue:@"true" forKey:@"single_row"];
  11899. [detail2_section setValue:@"local" forKey:@"data"];
  11900. [ret setObject:detail2_section forKey:@"detail_2"];
  11901. }
  11902. sqlite3_finalize(statement);
  11903. }
  11904. else
  11905. {
  11906. [ret setValue:@"8" forKey:@"result"];
  11907. }
  11908. // DebugLog(@"count:%d",count);
  11909. [iSalesDB close_db:db];
  11910. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11911. dispatch_async(dispatch_get_main_queue(), ^{
  11912. result(ret);
  11913. });
  11914. });
  11915. }
  11916. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11917. //{
  11918. //
  11919. // UIApplication * app = [UIApplication sharedApplication];
  11920. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11921. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11922. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11923. // sqlite3 *db = [iSalesDB get_db];
  11924. // NSString* collectId=params[@"collectId"];
  11925. //
  11926. //
  11927. //
  11928. //
  11929. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  11930. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  11931. // [iSalesDB execSql:sqlQuery db:db];
  11932. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11933. // [iSalesDB close_db:db];
  11934. //
  11935. //
  11936. // ret[@"result"]= [NSNumber numberWithInt:2];
  11937. // dispatch_async(dispatch_get_main_queue(), ^{
  11938. //
  11939. // appDelegate.wish_count =count;
  11940. //
  11941. // [appDelegate update_count_mark];
  11942. // result(ret);
  11943. // });
  11944. //
  11945. // });
  11946. //}
  11947. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11948. {
  11949. UIApplication * app = [UIApplication sharedApplication];
  11950. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11951. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11952. [iSalesDB disable_trigger];
  11953. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11954. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11955. [iSalesDB enable_trigger];
  11956. dispatch_async(dispatch_get_main_queue(), ^{
  11957. //
  11958. // NSString* user = [params valueForKey:@"user"];
  11959. //
  11960. // NSString* password = [params valueForKey:@"password"];
  11961. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11962. ret[@"result"]=[NSNumber numberWithInt:2 ];
  11963. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  11964. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  11965. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  11966. [appDelegate update_count_mark];
  11967. appDelegate.can_show_price =false;
  11968. appDelegate.can_see_price =false;
  11969. appDelegate.can_create_portfolio =false;
  11970. appDelegate.can_create_order =false;
  11971. appDelegate.can_cancel_order =false;
  11972. appDelegate.can_set_cart_price =false;
  11973. appDelegate.can_delete_order =false;
  11974. appDelegate.can_submit_order =false;
  11975. appDelegate.can_set_tearsheet_price =false;
  11976. appDelegate.can_update_contact_info = false;
  11977. appDelegate.save_order_logout = false;
  11978. appDelegate.submit_order_logout = false;
  11979. appDelegate.alert_sold_in_quantities = false;
  11980. appDelegate.ipad_perm =nil ;
  11981. appDelegate.user_type = USER_ROLE_UNKNOWN;
  11982. appDelegate.OrderFilter= nil;
  11983. [appDelegate SetSo:nil];
  11984. [appDelegate set_main_button_panel];
  11985. result(ret);
  11986. });
  11987. });
  11988. }
  11989. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11990. {
  11991. // iSalesDB.
  11992. // UIApplication * app = [UIApplication sharedApplication];
  11993. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11994. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11995. [iSalesDB disable_trigger];
  11996. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11997. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11998. [iSalesDB enable_trigger];
  11999. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12000. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12001. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12002. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12003. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12004. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12005. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12006. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12007. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12008. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12009. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12010. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12011. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12012. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12013. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12014. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12015. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12016. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12017. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12018. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12019. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12020. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12021. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12022. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12023. [arr_name addObject:customer_first_name];
  12024. [arr_name addObject:customer_last_name];
  12025. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12026. // default ship from
  12027. 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';";
  12028. __block NSString *cid = @"";
  12029. __block NSString *name = @"";
  12030. __block NSString *ext = @"";
  12031. __block NSString *contact = @"";
  12032. __block NSString *email = @"";
  12033. __block NSString *fax = @"";
  12034. __block NSString *phone = @"";
  12035. sqlite3 *db = [iSalesDB get_db];
  12036. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12037. cid = [self textAtColumn:0 statement:statment];
  12038. name = [self textAtColumn:1 statement:statment];
  12039. ext = [self textAtColumn:2 statement:statment];
  12040. contact = [self textAtColumn:3 statement:statment];
  12041. email = [self textAtColumn:4 statement:statment];
  12042. fax = [self textAtColumn:5 statement:statment];
  12043. phone = [self textAtColumn:6 statement:statment];
  12044. }];
  12045. NSString* so_id = [self get_offline_soid:db];
  12046. if(so_id==nil)
  12047. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12048. 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];
  12049. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12050. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12051. //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'
  12052. //soId
  12053. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12054. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12055. sqlite3_stmt * statement;
  12056. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12057. {
  12058. if (sqlite3_step(statement) == SQLITE_ROW)
  12059. {
  12060. // char *name = (char*)sqlite3_column_text(statement, 1);
  12061. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12062. //ret = sqlite3_column_int(statement, 0);
  12063. char *soId = (char*)sqlite3_column_text(statement, 0);
  12064. if(soId==nil)
  12065. soId="";
  12066. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12067. [ret setValue:nssoId forKey:@"soId"];
  12068. [ret setValue:nssoId forKey:@"orderCode"];
  12069. }
  12070. sqlite3_finalize(statement);
  12071. }
  12072. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12073. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12074. [iSalesDB close_db:db];
  12075. dispatch_async(dispatch_get_main_queue(), ^{
  12076. result(ret);
  12077. });
  12078. });
  12079. }
  12080. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12081. {
  12082. UIApplication * app = [UIApplication sharedApplication];
  12083. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12084. bool can_create_backorder= appDelegate.can_create_backorder;
  12085. NSString* user = appDelegate.user;
  12086. NSString* contact_id = appDelegate.contact_id;
  12087. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12088. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12089. sqlite3 *db = [iSalesDB get_db];
  12090. NSString* orderCode=params[@"orderCode"];
  12091. int sort = [[params objectForKey:@"sort"] intValue];
  12092. NSString *sort_str = @"";
  12093. switch (sort) {
  12094. case 0:{
  12095. sort_str = @"order by c.modify_time desc";
  12096. }
  12097. break;
  12098. case 1:{
  12099. sort_str = @"order by c.modify_time asc";
  12100. }
  12101. break;
  12102. case 2:{
  12103. sort_str = @"order by m.name asc";
  12104. }
  12105. break;
  12106. case 3:{
  12107. sort_str = @"order by m.name desc";
  12108. }
  12109. break;
  12110. case 4:{
  12111. sort_str = @"order by m.description asc";
  12112. }
  12113. break;
  12114. default:
  12115. break;
  12116. }
  12117. 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 ];
  12118. // 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 ];
  12119. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12120. DebugLog(@"offline_login sql:%@",sqlQuery);
  12121. sqlite3_stmt * statement;
  12122. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12123. NSDate *date1 = [NSDate date];
  12124. int count=0;
  12125. int cart_count=0;
  12126. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12127. if ( dbresult== SQLITE_OK)
  12128. {
  12129. while (sqlite3_step(statement) == SQLITE_ROW)
  12130. {
  12131. // NSDate *row_date = [NSDate date];
  12132. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12133. int product_id = sqlite3_column_int(statement, 0);
  12134. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12135. int item_id = sqlite3_column_int(statement, 7);
  12136. NSString* Price=nil;
  12137. if(str_price==nil)
  12138. {
  12139. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12140. // NSDate *price_date = [NSDate date];
  12141. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12142. // DebugLog(@"price time interval");
  12143. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12144. if(price==nil)
  12145. Price=@"No Price.";
  12146. else
  12147. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12148. }
  12149. else
  12150. {
  12151. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12152. }
  12153. double discount = sqlite3_column_double(statement, 2);
  12154. int item_count = sqlite3_column_int(statement, 3);
  12155. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12156. NSString *nsline_note=nil;
  12157. if(line_note!=nil)
  12158. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12159. char *name = (char*)sqlite3_column_text(statement, 5);
  12160. NSString *nsname=nil;
  12161. if(name!=nil)
  12162. nsname= [[NSString alloc]initWithUTF8String:name];
  12163. char *description = (char*)sqlite3_column_text(statement, 6);
  12164. NSString *nsdescription=nil;
  12165. if(description!=nil)
  12166. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12167. int stockUom = sqlite3_column_int(statement, 8);
  12168. int _id = sqlite3_column_int(statement, 9);
  12169. int availability = sqlite3_column_int(statement, 10);
  12170. // NSDate *subtotal_date = [NSDate date];
  12171. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12172. // DebugLog(@"subtotal_date time interval");
  12173. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12174. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12175. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12176. int carton=[bsubtotaljson[@"carton"] intValue];
  12177. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12178. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12179. // NSDate *img_date = [NSDate date];
  12180. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12181. // DebugLog(@"img_date time interval");
  12182. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12183. itemjson[@"model"]=nsname;
  12184. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12185. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12186. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12187. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12188. itemjson[@"check"]=@"true";
  12189. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12190. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12191. itemjson[@"unit_price"]=Price;
  12192. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12193. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12194. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12195. itemjson[@"note"]=nsline_note;
  12196. if (!can_create_backorder) {
  12197. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12198. }
  12199. // NSDate *date2 = [NSDate date];
  12200. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12201. // DebugLog(@"model_bundle time interval");
  12202. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12203. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12204. count++;
  12205. // DebugLog(@"row time interval");
  12206. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12207. }
  12208. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12209. sqlite3_finalize(statement);
  12210. }
  12211. DebugLog(@"request cart total time interval");
  12212. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12213. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12214. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12215. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12216. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12217. ret[@"count"]=[NSNumber numberWithInt:count ];
  12218. ret[@"mode"]=@"Regular Mode";
  12219. [iSalesDB close_db:db];
  12220. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12221. DebugLog(@"general notes :%@",general_note);
  12222. ret[@"general_note"]= general_note;
  12223. dispatch_async(dispatch_get_main_queue(), ^{
  12224. result(ret);
  12225. });
  12226. });
  12227. }
  12228. @end