OLDataProvider.m 682 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818
  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. NSMutableDictionary* orderlist = nil;
  1552. NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1553. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1554. // order list 为空,不用维护
  1555. #ifdef RA_NOTIFICATION
  1556. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1557. #else
  1558. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1559. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1560. #endif
  1561. return;
  1562. // [orderlist addObject:appDelegate.order_code];
  1563. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1564. }
  1565. else
  1566. {
  1567. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1568. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1569. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1570. }
  1571. int count = [orderlist[@"count"] intValue];
  1572. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1573. int newcount = 0;
  1574. for(int i=0;i<count;i++)
  1575. {
  1576. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1577. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1578. continue;
  1579. else
  1580. {
  1581. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1582. newcount++;
  1583. }
  1584. }
  1585. newlist[@"count"] = @(newcount);
  1586. [RAUtils dicttofile:orderlistPath dict:newlist];
  1587. #ifdef RA_NOTIFICATION
  1588. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1589. #else
  1590. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1591. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1592. #endif
  1593. }
  1594. +(NSString*) scanTemplatePath:(NSString*)file
  1595. {
  1596. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1597. NSString *documents = [paths objectAtIndex:0];
  1598. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1599. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1600. return templatefile;
  1601. }
  1602. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1603. {
  1604. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1605. NSString *documents = [paths objectAtIndex:0];
  1606. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1607. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1608. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1609. return [dict mutableCopy];
  1610. }
  1611. +(void) deleteScanOrder:(NSString*) order_code
  1612. {
  1613. if(order_code.length==0)
  1614. return;
  1615. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1616. NSString *documents = [paths objectAtIndex:0];
  1617. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1618. if([appDelegate.order_code isEqualToString:order_code])
  1619. {
  1620. //要删除的订单是当前打开的订单;
  1621. appDelegate.order_code = nil;
  1622. appDelegate.customerInfo = nil;
  1623. RASingleton.sharedInstance.scan_cart = nil;
  1624. appDelegate.contact_id = nil;
  1625. [appDelegate updateScanButton:false];
  1626. [appDelegate update_count_mark];
  1627. }
  1628. //删除订单目录
  1629. [[NSFileManager defaultManager] removeItemAtPath:[documents stringByAppendingPathComponent:order_code] error:nil];
  1630. //维护订单列表
  1631. NSMutableDictionary* orderlist = nil;
  1632. NSString *orderlistPath = [documents stringByAppendingPathComponent:@"orderlist.json"];
  1633. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1634. // order list 为空,不用维护
  1635. #ifdef RA_NOTIFICATION
  1636. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1637. #else
  1638. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1639. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1640. #endif
  1641. return;
  1642. // [orderlist addObject:appDelegate.order_code];
  1643. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1644. }
  1645. else
  1646. {
  1647. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1648. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1649. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1650. }
  1651. int count = [orderlist[@"count"] intValue];
  1652. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1653. int newcount = 0;
  1654. for(int i=0;i<count;i++)
  1655. {
  1656. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1657. if([item[@"order_code"] isEqualToString: order_code ])
  1658. continue;
  1659. else
  1660. {
  1661. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1662. newcount++;
  1663. }
  1664. }
  1665. newlist[@"count"] = @(newcount);
  1666. [RAUtils dicttofile:orderlistPath dict:newlist];
  1667. #ifdef RA_NOTIFICATION
  1668. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1669. #else
  1670. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1671. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1672. #endif
  1673. }
  1674. +(void) saveScanCart:(NSMutableDictionary*) cart
  1675. {
  1676. if(cart==nil)
  1677. return;
  1678. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1679. NSString *documents = [paths objectAtIndex:0];
  1680. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1681. NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1682. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  1683. [RAUtils dicttofile:cartpath dict:cart];
  1684. }
  1685. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  1686. {
  1687. assert(add_params[@"user"]!=nil);
  1688. // assert(add_params[@"contact_id"]!=nil);
  1689. // assert(add_params[@"password"]!=nil);
  1690. NSString* serial= [[NSUUID UUID] UUIDString];
  1691. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1692. NSString *cachefolder = [paths objectAtIndex:0];
  1693. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1694. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1695. NSFileManager* fileManager = [NSFileManager defaultManager];
  1696. BOOL bdir=YES;
  1697. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1698. {
  1699. NSError *error = nil;
  1700. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1701. if(!bsuccess)
  1702. DebugLog(@"Create UPLOAD folder failed");
  1703. }
  1704. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1705. ret[@"contact"]=[self prepareContact:serial];
  1706. ret[@"wishlist"]=[self prepareWishlist:serial];
  1707. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  1708. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  1709. ret[@"view_portfolio"] = [self preparePDF:serial];
  1710. NSString* str= [RAConvertor dict2string:ret];
  1711. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1712. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1713. NSError *error=nil;
  1714. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1715. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1716. ZipArchive* zip = [[ZipArchive alloc] init];
  1717. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1718. for(int i=0;i<arr_files.count;i++)
  1719. {
  1720. NSString* file=arr_files[i];
  1721. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1722. }
  1723. if( ![zip CloseZipFile2] )
  1724. {
  1725. zippath = @"";
  1726. }
  1727. ret[@"file"]=zippath;
  1728. return ret;
  1729. }
  1730. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1731. {
  1732. if(filename.length==0)
  1733. return false;
  1734. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1735. bool ret=false;
  1736. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1737. NSString *cachefolder = [paths objectAtIndex:0];
  1738. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1739. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1740. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1741. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1742. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1743. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1744. NSFileManager* fileManager = [NSFileManager defaultManager];
  1745. BOOL bdir=NO;
  1746. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1747. {
  1748. NSError *error = nil;
  1749. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1750. {
  1751. ret=false;
  1752. }
  1753. else
  1754. {
  1755. ret=true;
  1756. }
  1757. }
  1758. return ret;
  1759. }
  1760. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1761. {
  1762. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1763. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1764. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1765. NSString* where=@" 1=1";
  1766. if (ver!=nil) {
  1767. where=@"is_dirty=1";
  1768. }
  1769. sqlite3 *db = [iSalesDB get_db];
  1770. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1771. sqlite3_stmt * statement;
  1772. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1773. int count=0;
  1774. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1775. if ( dbresult== SQLITE_OK)
  1776. {
  1777. while (sqlite3_step(statement) == SQLITE_ROW)
  1778. {
  1779. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1780. int _id = sqlite3_column_int(statement, 0);
  1781. int product_id = sqlite3_column_int(statement, 1);
  1782. int item_id = sqlite3_column_int(statement, 2);
  1783. int qty = sqlite3_column_int(statement, 3);
  1784. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1785. NSString* nscreate_time=nil;
  1786. if(create_time!=nil)
  1787. {
  1788. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1789. }
  1790. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1791. NSString* nsmodify_time=nil;
  1792. if(modify_time!=nil)
  1793. {
  1794. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1795. }
  1796. int is_delete = sqlite3_column_int(statement, 6);
  1797. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1798. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1799. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1800. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1801. itemjson[@"create_time"]=nscreate_time;
  1802. itemjson[@"modify_time"]=nsmodify_time;
  1803. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1804. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1805. count++;
  1806. }
  1807. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1808. sqlite3_finalize(statement);
  1809. }
  1810. ret[@"count"]=[NSNumber numberWithInt:count ];
  1811. [iSalesDB close_db:db];
  1812. return ret;
  1813. }
  1814. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  1815. {
  1816. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1817. // UIApplication * app = [UIApplication sharedApplication];
  1818. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1819. assert(add_params[@"user"]!=nil);
  1820. // assert(add_params[@"password"]!=nil);
  1821. 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 ];
  1822. // 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 ];
  1823. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1824. DebugLog(@"offline_login sql:%@",sqlQuery);
  1825. sqlite3_stmt * statement;
  1826. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1827. int count=0;
  1828. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1829. if ( dbresult== SQLITE_OK)
  1830. {
  1831. while (sqlite3_step(statement) == SQLITE_ROW)
  1832. {
  1833. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1834. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1835. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1836. int product_id = sqlite3_column_int(statement, 0);
  1837. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1838. int item_id = sqlite3_column_int(statement, 7);
  1839. NSString* Price=nil;
  1840. if(str_price==nil)
  1841. {
  1842. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1843. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  1844. if(price==nil)
  1845. Price=@"No Price.";
  1846. else
  1847. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1848. }
  1849. else
  1850. {
  1851. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1852. }
  1853. double discount = sqlite3_column_double(statement, 2);
  1854. int item_count = sqlite3_column_int(statement, 3);
  1855. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1856. NSString *nsline_note=nil;
  1857. if(line_note!=nil)
  1858. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1859. char *name = (char*)sqlite3_column_text(statement, 5);
  1860. NSString *nsname=nil;
  1861. if(name!=nil)
  1862. nsname= [[NSString alloc]initWithUTF8String:name];
  1863. char *description = (char*)sqlite3_column_text(statement, 6);
  1864. NSString *nsdescription=nil;
  1865. if(description!=nil)
  1866. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1867. // int stockUom = sqlite3_column_int(statement, 8);
  1868. // int _id = sqlite3_column_int(statement, 9);
  1869. //
  1870. //
  1871. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1872. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1873. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1874. // int carton=[bsubtotaljson[@"carton"] intValue];
  1875. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1876. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1877. itemjson[@"model"]=nsname;
  1878. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1879. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1880. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1881. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1882. // itemjson[@"check"]=@"true";
  1883. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1884. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1885. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1886. itemjson[@"unit_price"]=Price;
  1887. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1888. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1889. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1890. itemjson[@"note"]=nsline_note;
  1891. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  1892. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1893. count++;
  1894. }
  1895. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1896. sqlite3_finalize(statement);
  1897. }
  1898. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1899. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1900. return ret;
  1901. }
  1902. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  1903. {
  1904. assert(add_params[@"user"]!=nil);
  1905. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1906. sqlite3 *db = [iSalesDB get_db];
  1907. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1908. // for(int i=0;i<arr_soid.count;i++)
  1909. sqlite3_stmt * statement;
  1910. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1911. //int count=0;
  1912. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1913. if ( dbresult== SQLITE_OK)
  1914. {
  1915. int count=0;
  1916. while (sqlite3_step(statement) == SQLITE_ROW)
  1917. {
  1918. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1919. int _id = sqlite3_column_int(statement, 0);
  1920. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1921. NSString* nssync_data=nil;
  1922. if(sync_data!=nil)
  1923. {
  1924. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1925. }
  1926. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1927. NSString* nsimg_1=nil;
  1928. if(img_1!=nil)
  1929. {
  1930. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1931. [self copy_upImg:serial file:nsimg_1];
  1932. }
  1933. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1934. NSString* nsso_no=nil;
  1935. if(so_no!=nil)
  1936. {
  1937. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1938. }
  1939. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1940. itemjson[@"order_type"]=@"submit order";
  1941. else
  1942. itemjson[@"order_type"]=@"archive order";
  1943. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1944. itemjson[@"json_data"]= nssync_data;
  1945. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  1946. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1947. count++;
  1948. }
  1949. ret[@"count"]=[NSNumber numberWithInt:count ];
  1950. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1951. }
  1952. sqlite3_finalize(statement);
  1953. [iSalesDB close_db:db];
  1954. return ret;
  1955. }
  1956. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1957. {
  1958. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1959. sqlite3 *db = [iSalesDB get_db];
  1960. // UIApplication * app = [UIApplication sharedApplication];
  1961. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1962. 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";
  1963. sqlite3_stmt * statement;
  1964. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1965. int count=0;
  1966. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1967. if ( dbresult== SQLITE_OK)
  1968. {
  1969. while (sqlite3_step(statement) == SQLITE_ROW)
  1970. {
  1971. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1972. int _id = sqlite3_column_int(statement, 0);
  1973. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1974. NSString* nsimg_0=nil;
  1975. if(img_0!=nil)
  1976. {
  1977. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1978. [self copy_upImg:serial file:nsimg_0];
  1979. }
  1980. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1981. NSString* nsimg_1=nil;
  1982. if(img_1!=nil)
  1983. {
  1984. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1985. [self copy_upImg:serial file:nsimg_1];
  1986. }
  1987. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1988. NSString* nsimg_2=nil;
  1989. if(img_2!=nil)
  1990. {
  1991. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1992. [self copy_upImg:serial file:nsimg_2];
  1993. }
  1994. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1995. NSString* nssync_data=nil;
  1996. if(sync_data!=nil)
  1997. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1998. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1999. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2000. itemjson[@"json_data"]= nssync_data;
  2001. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2002. count++;
  2003. }
  2004. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2005. sqlite3_finalize(statement);
  2006. }
  2007. ret[@"count"]=[NSNumber numberWithInt:count ];
  2008. [iSalesDB close_db:db];
  2009. return ret;
  2010. }
  2011. +(bool) check_offlinedata
  2012. {
  2013. // UIApplication * app = [UIApplication sharedApplication];
  2014. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2015. sqlite3 *db = [iSalesDB get_db];
  2016. NSString * where=@"1=1";
  2017. // if(appDelegate.user!=nil)
  2018. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2019. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2020. [iSalesDB close_db:db];
  2021. if(count==0)
  2022. {
  2023. return false;
  2024. }
  2025. return true;
  2026. //
  2027. //[iSalesDB close_db:db];
  2028. }
  2029. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2030. {
  2031. // UIApplication * app = [UIApplication sharedApplication];
  2032. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2033. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2034. sqlite3 *db = [iSalesDB get_db];
  2035. NSString* collectId=params[@"collectId"];
  2036. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2037. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2038. [iSalesDB execSql:sqlQuery db:db];
  2039. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2040. [iSalesDB close_db:db];
  2041. // appDelegate.wish_count =count;
  2042. //
  2043. // [appDelegate update_count_mark];
  2044. ret[@"result"]= [NSNumber numberWithInt:2];
  2045. ret[@"wish_count"]=@(count);
  2046. return ret;
  2047. }
  2048. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2049. //{
  2050. //
  2051. // UIApplication * app = [UIApplication sharedApplication];
  2052. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2053. //
  2054. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2055. // sqlite3 *db = [iSalesDB get_db];
  2056. // NSString* product_id=params[@"product_id"];
  2057. //
  2058. //
  2059. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2060. //
  2061. //
  2062. // for(int i=0;i<arr.count;i++)
  2063. // {
  2064. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2065. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2066. // if(count==0)
  2067. // {
  2068. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2069. // [iSalesDB execSql:sqlQuery db:db];
  2070. // }
  2071. // }
  2072. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2073. // [iSalesDB close_db:db];
  2074. //
  2075. // appDelegate.wish_count =count;
  2076. //
  2077. // [appDelegate update_count_mark];
  2078. // ret[@"result"]= [NSNumber numberWithInt:2];
  2079. // return ret;
  2080. // //
  2081. // //return ret;
  2082. //}
  2083. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2084. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2085. DebugLog(@"time interval: %lf",interval);
  2086. }
  2087. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2088. //{
  2089. // UIApplication * app = [UIApplication sharedApplication];
  2090. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2091. //
  2092. // int sort = [[params objectForKey:@"sort"] intValue];
  2093. // NSString *sort_str = @"";
  2094. // switch (sort) {
  2095. // case 0:{
  2096. // sort_str = @"order by w.modify_time desc";
  2097. // }
  2098. // break;
  2099. // case 1:{
  2100. // sort_str = @"order by w.modify_time asc";
  2101. // }
  2102. // break;
  2103. // case 2:{
  2104. // sort_str = @"order by m.name asc";
  2105. // }
  2106. // break;
  2107. // case 3:{
  2108. // sort_str = @"order by m.name desc";
  2109. // }
  2110. // break;
  2111. // case 4:{
  2112. // sort_str = @"order by m.description asc";
  2113. // }
  2114. // break;
  2115. //
  2116. // default:
  2117. // break;
  2118. //
  2119. // }
  2120. //
  2121. //
  2122. //// NSString* user = appDelegate.user;
  2123. //
  2124. // sqlite3 *db = [iSalesDB get_db];
  2125. //
  2126. // // order by w.create_time
  2127. // 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];
  2128. //
  2129. //// 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];
  2130. //
  2131. //
  2132. // sqlite3_stmt * statement;
  2133. //
  2134. // NSDate *date1 = [NSDate date];
  2135. //// NSDate *date2 = nil;
  2136. //
  2137. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2138. // int count=0;
  2139. //
  2140. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2141. // {
  2142. //
  2143. //
  2144. // while (sqlite3_step(statement) == SQLITE_ROW)
  2145. // {
  2146. //
  2147. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2148. //
  2149. // int product_id = sqlite3_column_double(statement, 0);
  2150. //
  2151. //
  2152. //
  2153. //
  2154. //
  2155. // char *description = (char*)sqlite3_column_text(statement, 1);
  2156. // if(description==nil)
  2157. // description= "";
  2158. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2159. //
  2160. // int item_id = sqlite3_column_double(statement, 2);
  2161. //
  2162. // NSDate *date_image = [NSDate date];
  2163. //
  2164. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2165. //
  2166. // printf("image : ");
  2167. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2168. //
  2169. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2170. // // if(url==nil)
  2171. // // url="";
  2172. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2173. //
  2174. // int qty = sqlite3_column_int(statement, 3);
  2175. //
  2176. //
  2177. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2178. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2179. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2180. // item[@"description"]= nsdescription;
  2181. // item[@"img"]= nsurl;
  2182. //
  2183. //
  2184. //
  2185. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2186. // count++;
  2187. //
  2188. // }
  2189. // printf("total time:");
  2190. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2191. //
  2192. // ret[@"count"]= [NSNumber numberWithInt:count];
  2193. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2194. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2195. // ret[@"result"]= [NSNumber numberWithInt:2];
  2196. //
  2197. //
  2198. // appDelegate.wish_count =count;
  2199. //
  2200. // [appDelegate update_count_mark];
  2201. // sqlite3_finalize(statement);
  2202. //
  2203. //
  2204. //
  2205. //
  2206. // }
  2207. //
  2208. // [iSalesDB close_db:db];
  2209. //
  2210. // return ret;
  2211. //}
  2212. +(NSDictionary*) offline_notimpl
  2213. {
  2214. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2215. ret[@"result"]=@"8";
  2216. ret[@"err_msg"]=@"offline mode does not support this function.";
  2217. return ret;
  2218. }
  2219. +(NSDictionary*) offline_home
  2220. {
  2221. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2222. NSString *cachefolder = [paths objectAtIndex:0];
  2223. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2224. NSData* json =nil;
  2225. json=[NSData dataWithContentsOfFile:img_cache];
  2226. if(json==nil)
  2227. return nil;
  2228. NSError *error=nil;
  2229. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2230. return menu;
  2231. }
  2232. +(NSDictionary*) offline_category_menu
  2233. {
  2234. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2235. NSString *cachefolder = [paths objectAtIndex:0];
  2236. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2237. NSData* json =nil;
  2238. json=[NSData dataWithContentsOfFile:img_cache];
  2239. if(json==nil)
  2240. return nil;
  2241. NSError *error=nil;
  2242. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2243. return menu;
  2244. }
  2245. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2246. {
  2247. NSString* offline_command=params[@"offline_Command"];
  2248. NSDictionary* ret=nil;
  2249. if([offline_command isEqualToString:@"model_NIYMAL"])
  2250. {
  2251. NSString* category = params[@"category"];
  2252. ret = [self refresh_model_NIYMAL:category];
  2253. }
  2254. return ret;
  2255. }
  2256. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2257. {
  2258. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2259. [ret setValue:@"2" forKey:@"result"];
  2260. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2261. sqlite3* db= [iSalesDB get_db];
  2262. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2263. [iSalesDB close_db:db];
  2264. [ret setObject:detail1_section forKey:@"detail_1"];
  2265. return ret;
  2266. }
  2267. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2268. {
  2269. //assert(user!=nil);
  2270. // UIApplication * app = [UIApplication sharedApplication];
  2271. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2272. // NSArray* arr1 = [self get_user_all_price_type];
  2273. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2274. // NSSet *set1 = [NSSet setWithArray:arr1];
  2275. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2276. // if(appDelegate.contact_id==nil)
  2277. // set2=[set1 mutableCopy];
  2278. // else
  2279. // [set2 intersectsSet:set1];
  2280. // NSArray *retarr = [set2 allObjects];
  2281. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2282. // sqlite3 *db = [iSalesDB get_db];
  2283. NSString* sqlQuery = nil;
  2284. if(contact_id==nil)
  2285. {
  2286. if(!blogin)
  2287. return nil;
  2288. 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];
  2289. }
  2290. else
  2291. 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];
  2292. sqlite3_stmt * statement;
  2293. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2294. int count=0;
  2295. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2296. {
  2297. while (sqlite3_step(statement) == SQLITE_ROW)
  2298. {
  2299. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2300. char *name = (char*)sqlite3_column_text(statement, 0);
  2301. if(name==nil)
  2302. name="";
  2303. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2304. // double price = sqlite3_column_double(statement, 1);
  2305. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2306. // if(isnull==nil)
  2307. // item[nsname]= @"No Price";
  2308. // else
  2309. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2310. char *price = (char*)sqlite3_column_text(statement, 1);
  2311. if(price!=nil)
  2312. {
  2313. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2314. nsprice=[AESCrypt fastdecrypt:nsprice];
  2315. if(nsprice.length>0)
  2316. {
  2317. double dp= [nsprice doubleValue];
  2318. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2319. }
  2320. }
  2321. else
  2322. {
  2323. item[nsname]= @"No Price";
  2324. }
  2325. // int type= sqlite3_column_int(statement, 2);
  2326. //item[@"type"]=@"price";
  2327. // item[nsname]= nsprice;
  2328. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2329. count++;
  2330. }
  2331. ret[@"count"]= [NSNumber numberWithInt:count];
  2332. sqlite3_finalize(statement);
  2333. }
  2334. // [iSalesDB close_db:db];
  2335. return ret;
  2336. }
  2337. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2338. //{
  2339. // NSArray* arr1 = [self get_user_all_price_type:db];
  2340. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2341. //
  2342. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2343. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2344. // // [set2 intersectsSet:set1];
  2345. // //
  2346. // //
  2347. // // NSArray *retarr = [set2 allObjects];
  2348. //
  2349. // NSString* whereprice=nil;
  2350. // if(contact_id==nil)
  2351. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2352. // else
  2353. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2354. //
  2355. //
  2356. // // sqlite3 *db = [iSalesDB get_db];
  2357. //
  2358. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2359. // sqlite3_stmt * statement;
  2360. //
  2361. //
  2362. // NSNumber* ret = nil;
  2363. // double dprice=DBL_MAX;
  2364. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2365. // {
  2366. //
  2367. //
  2368. // while (sqlite3_step(statement) == SQLITE_ROW)
  2369. // {
  2370. //
  2371. // // double val = sqlite3_column_double(statement, 0);
  2372. // char *price = (char*)sqlite3_column_text(statement, 0);
  2373. // if(price!=nil)
  2374. // {
  2375. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2376. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2377. // if(nsprice.length>0)
  2378. // {
  2379. // double dp= [nsprice doubleValue];
  2380. // if(dp<dprice)
  2381. // dprice=dp;
  2382. // }
  2383. // }
  2384. // }
  2385. //
  2386. //
  2387. //
  2388. //
  2389. // sqlite3_finalize(statement);
  2390. //
  2391. //
  2392. //
  2393. //
  2394. // }
  2395. //
  2396. // // [iSalesDB close_db:db];
  2397. //
  2398. // if(dprice==DBL_MAX)
  2399. // ret= nil;
  2400. // else
  2401. // ret= [NSNumber numberWithDouble:dprice];
  2402. // return ret;
  2403. //}
  2404. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2405. {
  2406. assert(user!=nil);
  2407. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2408. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2409. NSString* whereprice=nil;
  2410. if(contact_id==nil)
  2411. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2412. else
  2413. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2414. // sqlite3 *db = [iSalesDB get_db];
  2415. NSString *productIdCondition = @"1 = 1";
  2416. if (product_id) {
  2417. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2418. }
  2419. NSString *itemIdCondition = @"";
  2420. if (item_id) {
  2421. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2422. }
  2423. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2424. sqlite3_stmt * statement;
  2425. NSNumber* ret = nil;
  2426. double dprice=DBL_MAX;
  2427. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2428. {
  2429. while (sqlite3_step(statement) == SQLITE_ROW)
  2430. {
  2431. // double val = sqlite3_column_double(statement, 0);
  2432. char *price = (char*)sqlite3_column_text(statement, 0);
  2433. if(price!=nil)
  2434. {
  2435. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2436. nsprice=[AESCrypt fastdecrypt:nsprice];
  2437. if(nsprice.length>0)
  2438. {
  2439. double dp= [nsprice doubleValue];
  2440. if(dp<dprice)
  2441. dprice=dp;
  2442. }
  2443. }
  2444. }
  2445. sqlite3_finalize(statement);
  2446. }
  2447. // [iSalesDB close_db:db];
  2448. if(dprice==DBL_MAX)
  2449. ret= nil;
  2450. else
  2451. ret= [NSNumber numberWithDouble:dprice];
  2452. return ret;
  2453. }
  2454. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2455. {
  2456. assert(user!=nil);
  2457. NSArray* ret=nil;
  2458. // sqlite3 *db = [iSalesDB get_db];
  2459. // no customer assigned , use login user contact_id
  2460. // UIApplication * app = [UIApplication sharedApplication];
  2461. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2462. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2463. sqlite3_stmt * statement;
  2464. // int count=0;
  2465. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2466. {
  2467. if (sqlite3_step(statement) == SQLITE_ROW)
  2468. {
  2469. char *val = (char*)sqlite3_column_text(statement, 0);
  2470. if(val==nil)
  2471. val="";
  2472. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2473. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2474. }
  2475. sqlite3_finalize(statement);
  2476. }
  2477. // [iSalesDB close_db:db];
  2478. return ret;
  2479. }
  2480. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2481. {
  2482. // sqlite3 *db = [iSalesDB get_db];
  2483. if(contact_id==nil)
  2484. {
  2485. // no customer assigned , use login user contact_id
  2486. // UIApplication * app = [UIApplication sharedApplication];
  2487. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2488. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2489. sqlite3_stmt * statement;
  2490. // int count=0;
  2491. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2492. {
  2493. if (sqlite3_step(statement) == SQLITE_ROW)
  2494. {
  2495. char *val = (char*)sqlite3_column_text(statement, 0);
  2496. if(val==nil)
  2497. val="";
  2498. contact_id = [[NSString alloc]initWithUTF8String:val];
  2499. }
  2500. sqlite3_finalize(statement);
  2501. }
  2502. if(contact_id.length<=0)
  2503. {
  2504. // [iSalesDB close_db:db];
  2505. return nil;
  2506. }
  2507. }
  2508. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2509. sqlite3_stmt * statement;
  2510. NSArray* ret=nil;
  2511. // int count=0;
  2512. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2513. {
  2514. if (sqlite3_step(statement) == SQLITE_ROW)
  2515. {
  2516. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2517. if(price_type==nil)
  2518. price_type="";
  2519. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2520. if(nsprice_type.length>0)
  2521. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2522. else
  2523. ret=nil;
  2524. }
  2525. sqlite3_finalize(statement);
  2526. }
  2527. // [iSalesDB close_db:db];
  2528. return ret;
  2529. }
  2530. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2531. {
  2532. NSString* ret= nil;
  2533. // sqlite3 *db = [iSalesDB get_db];
  2534. NSString *sqlQuery = nil;
  2535. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2536. // 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;
  2537. // 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
  2538. if(product_id==nil && model_name)
  2539. 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;
  2540. else if (product_id)
  2541. 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
  2542. sqlite3_stmt * statement;
  2543. // int count=0;
  2544. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2545. {
  2546. if (sqlite3_step(statement) == SQLITE_ROW)
  2547. {
  2548. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2549. if(imgurl==nil)
  2550. imgurl="";
  2551. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2552. ret=nsimgurl;
  2553. }
  2554. sqlite3_finalize(statement);
  2555. }
  2556. else
  2557. {
  2558. [ret setValue:@"8" forKey:@"result"];
  2559. }
  2560. // [iSalesDB close_db:db];
  2561. DebugLog(@"data string: %@",ret );
  2562. return ret;
  2563. }
  2564. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2565. {
  2566. NSString* ret= nil;
  2567. sqlite3 *db = [iSalesDB get_db];
  2568. NSString *sqlQuery = nil;
  2569. if(product_id==nil)
  2570. if(upc_code==nil)
  2571. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2572. else
  2573. 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='%@';
  2574. else
  2575. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2576. sqlite3_stmt * statement;
  2577. // int count=0;
  2578. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2579. {
  2580. if (sqlite3_step(statement) == SQLITE_ROW)
  2581. {
  2582. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2583. if(default_category==nil)
  2584. default_category="";
  2585. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2586. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2587. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2588. ret=nsdefault_category;
  2589. }
  2590. sqlite3_finalize(statement);
  2591. }
  2592. else
  2593. {
  2594. [ret setValue:@"8" forKey:@"result"];
  2595. }
  2596. [iSalesDB close_db:db];
  2597. DebugLog(@"data string: %@",ret );
  2598. return ret;
  2599. }
  2600. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2601. NSString *sql = nil;
  2602. if (product_id != nil) {
  2603. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2604. } else {
  2605. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2606. }
  2607. NSString *result_set = [iSalesDB jk_queryText:sql];
  2608. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2609. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2610. return result_set;
  2611. }
  2612. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2613. //{
  2614. //
  2615. //
  2616. // assert(params[@"user"]!=nil);
  2617. //
  2618. //
  2619. //
  2620. // NSString* model_name = [params valueForKey:@"product_name"];
  2621. //
  2622. // NSString* product_id = [params valueForKey:@"product_id"];
  2623. //
  2624. // NSString* category = [params valueForKey:@"category"];
  2625. //
  2626. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2627. // if(category==nil) {
  2628. // category = default_category_id;
  2629. // } else {
  2630. //
  2631. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2632. // // 参数重有多个category id
  2633. // if (arr_0.count > 1) {
  2634. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2635. //
  2636. // // 取交集
  2637. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2638. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2639. // [set_0 intersectSet:set_1];
  2640. //
  2641. // if (set_0.count == 1) {
  2642. // category = (NSString *)[set_0 anyObject];
  2643. // } else {
  2644. // if ([set_0 containsObject:default_category_id]) {
  2645. // category = default_category_id;
  2646. // } else {
  2647. // category = (NSString *)[set_0 anyObject];
  2648. // }
  2649. // }
  2650. // }
  2651. // }
  2652. //
  2653. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2654. //
  2655. //
  2656. //
  2657. //
  2658. //
  2659. //
  2660. // sqlite3 *db = [iSalesDB get_db];
  2661. //
  2662. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2663. //
  2664. //
  2665. // NSString *sqlQuery = nil;
  2666. //
  2667. // if(product_id==nil)
  2668. // 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='%@';
  2669. // else
  2670. //
  2671. // 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=%@;
  2672. //
  2673. //
  2674. // sqlite3_stmt * statement;
  2675. // [ret setValue:@"2" forKey:@"result"];
  2676. // [ret setValue:@"3" forKey:@"detail_section_count"];
  2677. //
  2678. // // int count=0;
  2679. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2680. // {
  2681. //
  2682. //
  2683. // if (sqlite3_step(statement) == SQLITE_ROW)
  2684. // {
  2685. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2686. //
  2687. //
  2688. // char *name = (char*)sqlite3_column_text(statement, 0);
  2689. // if(name==nil)
  2690. // name="";
  2691. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2692. //
  2693. // char *description = (char*)sqlite3_column_text(statement, 1);
  2694. // if(description==nil)
  2695. // description="";
  2696. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2697. //
  2698. //
  2699. // int product_id = sqlite3_column_int(statement, 2);
  2700. //
  2701. //
  2702. // char *color = (char*)sqlite3_column_text(statement, 3);
  2703. // if(color==nil)
  2704. // color="";
  2705. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2706. // //
  2707. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2708. // // if(legcolor==nil)
  2709. // // legcolor="";
  2710. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2711. // //
  2712. // //
  2713. // int availability = sqlite3_column_int(statement, 5);
  2714. // //
  2715. // int incoming_stock = sqlite3_column_int(statement, 6);
  2716. //
  2717. //
  2718. // char *demension = (char*)sqlite3_column_text(statement, 7);
  2719. // if(demension==nil)
  2720. // demension="";
  2721. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2722. //
  2723. //
  2724. //
  2725. // // ,,,,,,,,,
  2726. //
  2727. //
  2728. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2729. // if(seat_height==nil)
  2730. // seat_height="";
  2731. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2732. //
  2733. //
  2734. // char *material = (char*)sqlite3_column_text(statement, 9);
  2735. // if(material==nil)
  2736. // material="";
  2737. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2738. //
  2739. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2740. // if(box_dim==nil)
  2741. // box_dim="";
  2742. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2743. //
  2744. // char *volume = (char*)sqlite3_column_text(statement, 11);
  2745. // if(volume==nil)
  2746. // volume="";
  2747. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2748. //
  2749. // double weight = sqlite3_column_double(statement, 12);
  2750. //
  2751. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  2752. // if(model_set==nil)
  2753. // model_set="";
  2754. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2755. //
  2756. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2757. // if(load_ability==nil)
  2758. // load_ability="";
  2759. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2760. //
  2761. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  2762. //// if(default_category==nil)
  2763. //// default_category="";
  2764. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2765. //
  2766. //
  2767. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2768. // if(fabric_content==nil)
  2769. // fabric_content="";
  2770. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2771. //
  2772. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  2773. // if(assembling==nil)
  2774. // assembling="";
  2775. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2776. //
  2777. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  2778. // if(made_in==nil)
  2779. // made_in="";
  2780. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2781. //
  2782. //
  2783. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2784. // if(special_remarks==nil)
  2785. // special_remarks="";
  2786. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2787. //
  2788. //
  2789. // int stockUcom = sqlite3_column_double(statement, 20);
  2790. //
  2791. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  2792. // if(product_group==nil)
  2793. // product_group="";
  2794. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2795. //
  2796. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2797. // // if(fashion_selector==nil)
  2798. // // fashion_selector="";
  2799. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2800. //
  2801. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2802. // if(selector_field==nil)
  2803. // selector_field="";
  2804. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2805. //
  2806. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  2807. // if(property_field==nil)
  2808. // property_field="";
  2809. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2810. //
  2811. //
  2812. //
  2813. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  2814. // if(packaging==nil)
  2815. // packaging="";
  2816. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2817. //
  2818. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2819. //
  2820. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2821. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2822. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2823. //
  2824. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  2825. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2826. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2827. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2828. //
  2829. //
  2830. //
  2831. //
  2832. //
  2833. // NSString* Availability=nil;
  2834. // if(availability>0)
  2835. // Availability=[NSString stringWithFormat:@"%d",availability];
  2836. // else
  2837. // Availability = @"Out of Stock";
  2838. //
  2839. // [img_section setValue:Availability forKey:@"Availability"];
  2840. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2841. //
  2842. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2843. //
  2844. //
  2845. // char *eta = (char*)sqlite3_column_text(statement, 25);
  2846. // if(eta==nil)
  2847. // eta="";
  2848. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2849. // if ([nseta isEqualToString:@"null"]) {
  2850. // nseta = @"";
  2851. // }
  2852. // if (availability <= 0) {
  2853. // [img_section setValue:nseta forKey:@"ETA"];
  2854. // }
  2855. //
  2856. //
  2857. // int item_id = sqlite3_column_int(statement, 26);
  2858. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2859. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  2860. //
  2861. // NSString* Price=nil;
  2862. // if(appDelegate.bLogin==false)
  2863. // Price=@"Must Sign in.";
  2864. // else
  2865. // {
  2866. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2867. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2868. // if(price==nil)
  2869. // Price=@"No Price.";
  2870. // else
  2871. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2872. // }
  2873. //
  2874. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  2875. // [img_section setValue:Price forKey:@"price"];
  2876. // [img_section setValue:nsname forKey:@"model_name"];
  2877. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  2878. //
  2879. // if (appDelegate.order_code) { // 离线order code即so#
  2880. //
  2881. // 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];
  2882. // __block int cartQTY = 0;
  2883. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2884. //
  2885. // cartQTY = sqlite3_column_int(stmt, 0);
  2886. //
  2887. // }];
  2888. //
  2889. // if (cartQTY > 0) {
  2890. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2891. // }
  2892. // }
  2893. //
  2894. // [ret setObject:img_section forKey:@"img_section"];
  2895. //
  2896. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2897. // int detail0_item_count=0;
  2898. //
  2899. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2900. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2901. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2902. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2903. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2904. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2905. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2906. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2907. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2908. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2909. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2910. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2911. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2912. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2913. //
  2914. //
  2915. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2916. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2917. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2918. // {
  2919. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2920. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2921. // }
  2922. //
  2923. //
  2924. //
  2925. //
  2926. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2927. // [detail0_section setValue:@"kv" forKey:@"type"];
  2928. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  2929. //
  2930. // [ret setObject:detail0_section forKey:@"detail_0"];
  2931. //
  2932. //
  2933. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2934. // // [detail1_section setValue:@"detail" forKey:@"target"];
  2935. // // [detail1_section setValue:@"popup" forKey:@"action"];
  2936. // // [detail1_section setValue:@"content" forKey:@"type"];
  2937. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2938. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2939. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  2940. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2941. // [ret setObject:detail1_section forKey:@"detail_1"];
  2942. //
  2943. //
  2944. //
  2945. //
  2946. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2947. // [detail2_section setValue:@"detail" forKey:@"target"];
  2948. // [detail2_section setValue:@"popup" forKey:@"action"];
  2949. // [detail2_section setValue:@"content" forKey:@"type"];
  2950. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2951. // [detail2_section setValue:@"true" forKey:@"single_row"];
  2952. // [detail2_section setValue:@"local" forKey:@"data"];
  2953. // [ret setObject:detail2_section forKey:@"detail_2"];
  2954. // }
  2955. //
  2956. //
  2957. //
  2958. //
  2959. // sqlite3_finalize(statement);
  2960. // }
  2961. // else
  2962. // {
  2963. // [ret setValue:@"8" forKey:@"result"];
  2964. // }
  2965. //// DebugLog(@"count:%d",count);
  2966. //
  2967. //
  2968. // [iSalesDB close_db:db];
  2969. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2970. //
  2971. // return ret;
  2972. //}
  2973. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2974. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2975. NSString* keyword = [params valueForKey:@"keyword"];
  2976. keyword=keyword.lowercaseString;
  2977. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2978. int limit = [[params valueForKey:@"limit"] intValue];
  2979. int offset = [[params valueForKey:@"offset"] intValue];
  2980. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2981. NSString *limit_str = @"";
  2982. if (limited) {
  2983. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2984. }
  2985. sqlite3 *db = [iSalesDB get_db];
  2986. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2987. // UIApplication * app = [UIApplication sharedApplication];
  2988. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2989. NSString *sqlQuery = nil;
  2990. if(exactMatch )
  2991. 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 ;
  2992. else
  2993. 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
  2994. DebugLog(@"offline_search sql:%@",sqlQuery);
  2995. sqlite3_stmt * statement;
  2996. [ret setValue:@"2" forKey:@"result"];
  2997. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2998. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2999. // int count=0;
  3000. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3001. {
  3002. int i=0;
  3003. while (sqlite3_step(statement) == SQLITE_ROW)
  3004. {
  3005. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3006. // char *name = (char*)sqlite3_column_text(statement, 1);
  3007. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3008. char *name = (char*)sqlite3_column_text(statement, 0);
  3009. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3010. char *description = (char*)sqlite3_column_text(statement, 1);
  3011. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3012. int product_id = sqlite3_column_int(statement, 2);
  3013. // char *url = (char*)sqlite3_column_text(statement, 3);
  3014. // if(url==nil)
  3015. // url="";
  3016. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3017. int wid = sqlite3_column_int(statement, 3);
  3018. int closeout = sqlite3_column_int(statement, 4);
  3019. int cid = sqlite3_column_int(statement, 5);
  3020. int wisdelete = sqlite3_column_int(statement, 6);
  3021. int more_color = sqlite3_column_int(statement, 7);
  3022. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3023. if(wid !=0 && wisdelete != 1)
  3024. [item setValue:@"true" forKey:@"wish_exists"];
  3025. else
  3026. [item setValue:@"false" forKey:@"wish_exists"];
  3027. if(closeout==0)
  3028. [item setValue:@"false" forKey:@"is_closeout"];
  3029. else
  3030. [item setValue:@"true" forKey:@"is_closeout"];
  3031. if(cid==0)
  3032. [item setValue:@"false" forKey:@"cart_exists"];
  3033. else
  3034. [item setValue:@"true" forKey:@"cart_exists"];
  3035. if (more_color == 0) {
  3036. [item setObject:@(false) forKey:@"more_color"];
  3037. } else if (more_color == 1) {
  3038. [item setObject:@(true) forKey:@"more_color"];
  3039. }
  3040. [item addEntriesFromDictionary:imgjson];
  3041. // [item setValue:nsurl forKey:@"img"];
  3042. [item setValue:nsname forKey:@"fash_name"];
  3043. [item setValue:nsdescription forKey:@"description"];
  3044. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3045. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3046. i++;
  3047. }
  3048. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3049. [ret setObject:items forKey:@"items"];
  3050. sqlite3_finalize(statement);
  3051. }
  3052. DebugLog(@"count:%d",count);
  3053. [iSalesDB close_db:db];
  3054. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3055. return ret;
  3056. }
  3057. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3058. {
  3059. return [self search:params limited:YES];
  3060. }
  3061. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3062. //{
  3063. // UIApplication * app = [UIApplication sharedApplication];
  3064. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3065. //
  3066. //// [iSalesDB disable_trigger]
  3067. // [iSalesDB disable_trigger];
  3068. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3069. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3070. //
  3071. // [iSalesDB enable_trigger];
  3072. //// [iSalesDB enable_trigger]
  3073. // //
  3074. // // NSString* user = [params valueForKey:@"user"];
  3075. // //
  3076. // // NSString* password = [params valueForKey:@"password"];
  3077. //
  3078. //
  3079. //
  3080. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3081. //
  3082. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3083. //
  3084. //
  3085. //
  3086. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3087. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3088. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3089. //
  3090. // [appDelegate update_count_mark];
  3091. //
  3092. //
  3093. // appDelegate.can_show_price =false;
  3094. // appDelegate.can_see_price =false;
  3095. // appDelegate.can_create_portfolio =false;
  3096. // appDelegate.can_create_order =false;
  3097. //
  3098. //
  3099. // appDelegate.can_cancel_order =false;
  3100. // appDelegate.can_set_cart_price =false;
  3101. // appDelegate.can_delete_order =false;
  3102. // appDelegate.can_submit_order =false;
  3103. // appDelegate.can_set_tearsheet_price =false;
  3104. // appDelegate.can_update_contact_info = false;
  3105. //
  3106. // appDelegate.save_order_logout = false;
  3107. // appDelegate.submit_order_logout = false;
  3108. // appDelegate.alert_sold_in_quantities = false;
  3109. //
  3110. // appDelegate.ipad_perm =nil ;
  3111. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3112. // appDelegate.OrderFilter= nil;
  3113. // [appDelegate SetSo:nil];
  3114. // [appDelegate set_main_button_panel];
  3115. //
  3116. //
  3117. // // sqlite3 *db = [iSalesDB get_db];
  3118. // //
  3119. // //
  3120. // //
  3121. // //
  3122. // //
  3123. // // 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"]];
  3124. // //
  3125. // //
  3126. // //
  3127. // //
  3128. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3129. // // sqlite3_stmt * statement;
  3130. // //
  3131. // //
  3132. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3133. // //
  3134. // //
  3135. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3136. // // {
  3137. // //
  3138. // //
  3139. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3140. // // {
  3141. // //
  3142. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3143. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3144. // //
  3145. // //
  3146. // //
  3147. // // int can_show_price = sqlite3_column_int(statement, 0);
  3148. // // int can_see_price = sqlite3_column_int(statement, 1);
  3149. // //
  3150. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3151. // // if(contact_id==nil)
  3152. // // contact_id="";
  3153. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3154. // //
  3155. // // int user_type = sqlite3_column_int(statement, 3);
  3156. // //
  3157. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3158. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3159. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3160. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3161. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3162. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3163. // // int can_create_order = sqlite3_column_int(statement, 10);
  3164. // //
  3165. // //
  3166. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3167. // // if(mode==nil)
  3168. // // mode="";
  3169. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3170. // //
  3171. // //
  3172. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3173. // // if(username==nil)
  3174. // // username="";
  3175. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3176. // //
  3177. // //
  3178. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3179. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3180. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3181. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3182. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3183. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3184. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3185. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3186. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3187. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3188. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3189. // //
  3190. // // [header setValue:nsusername forKey:@"username"];
  3191. // //
  3192. // //
  3193. // // [ret setObject:header forKey:@"header"];
  3194. // // [ret setValue:nsmode forKey:@"mode"];
  3195. // //
  3196. // //
  3197. // // }
  3198. // //
  3199. // //
  3200. // //
  3201. // // sqlite3_finalize(statement);
  3202. // // }
  3203. // //
  3204. // //
  3205. // //
  3206. // // [iSalesDB close_db:db];
  3207. // //
  3208. // //
  3209. // //
  3210. // //
  3211. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3212. //
  3213. // return ret;
  3214. //}
  3215. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3216. //{
  3217. //
  3218. // UIApplication * app = [UIApplication sharedApplication];
  3219. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3220. //
  3221. // [iSalesDB disable_trigger];
  3222. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3223. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3224. // [iSalesDB enable_trigger];
  3225. //
  3226. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3227. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3228. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3229. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3230. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3231. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3232. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3233. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3234. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3235. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3236. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3237. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3238. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3239. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3240. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3241. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3242. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3243. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3244. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3245. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3246. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3247. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3248. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3249. //
  3250. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3251. //
  3252. // [arr_name addObject:customer_first_name];
  3253. // [arr_name addObject:customer_last_name];
  3254. //
  3255. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3256. //
  3257. // // default ship from
  3258. // 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';";
  3259. //
  3260. // __block NSString *cid = @"";
  3261. // __block NSString *name = @"";
  3262. // __block NSString *ext = @"";
  3263. // __block NSString *contact = @"";
  3264. // __block NSString *email = @"";
  3265. // __block NSString *fax = @"";
  3266. // __block NSString *phone = @"";
  3267. //
  3268. // sqlite3 *db = [iSalesDB get_db];
  3269. //
  3270. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3271. //
  3272. // cid = [self textAtColumn:0 statement:statment];
  3273. // name = [self textAtColumn:1 statement:statment];
  3274. // ext = [self textAtColumn:2 statement:statment];
  3275. // contact = [self textAtColumn:3 statement:statment];
  3276. // email = [self textAtColumn:4 statement:statment];
  3277. // fax = [self textAtColumn:5 statement:statment];
  3278. // phone = [self textAtColumn:6 statement:statment];
  3279. //
  3280. // }];
  3281. //
  3282. // NSString* so_id = [self get_offline_soid:db];
  3283. // if(so_id==nil)
  3284. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3285. //
  3286. // 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];
  3287. //
  3288. //
  3289. //
  3290. // int result =[iSalesDB execSql:sql_neworder db:db];
  3291. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3292. //
  3293. //
  3294. //
  3295. // //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'
  3296. // //soId
  3297. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3298. //
  3299. //
  3300. //
  3301. //
  3302. //
  3303. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3304. // sqlite3_stmt * statement;
  3305. //
  3306. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3307. // {
  3308. // if (sqlite3_step(statement) == SQLITE_ROW)
  3309. // {
  3310. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3311. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3312. //
  3313. // //ret = sqlite3_column_int(statement, 0);
  3314. //
  3315. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3316. // if(soId==nil)
  3317. // soId="";
  3318. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3319. // [ret setValue:nssoId forKey:@"soId"];
  3320. // [ret setValue:nssoId forKey:@"orderCode"];
  3321. //
  3322. // }
  3323. // sqlite3_finalize(statement);
  3324. // }
  3325. //
  3326. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3327. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3328. //
  3329. //
  3330. // [iSalesDB close_db:db];
  3331. //
  3332. // return [RAConvertor dict2data:ret];
  3333. //
  3334. //}
  3335. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3336. {
  3337. //assert(params[@"order_code"]);
  3338. // assert(params[@"order_code"]);
  3339. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3340. NSString* orderCode = [params valueForKey:@"orderCode"];
  3341. NSString* app_order_code= params[@"appDelegate.order_code"];
  3342. // UIApplication * app = [UIApplication sharedApplication];
  3343. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3344. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3345. {
  3346. [iSalesDB disable_trigger];
  3347. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3348. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3349. [iSalesDB enable_trigger];
  3350. }
  3351. sqlite3 *db = [iSalesDB get_db];
  3352. int cart_count=[self query_ordercartcount:orderCode db:db];
  3353. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3354. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3355. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3356. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3357. [iSalesDB close_db:db];
  3358. return [RAConvertor dict2data:ret];
  3359. }
  3360. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3361. {
  3362. assert(params[@"can_create_backorder"]!=nil);
  3363. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3364. sqlite3 *db = [iSalesDB get_db];
  3365. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3366. int count =0;
  3367. if(params[@"count"]!=nil)
  3368. {
  3369. count = [params[@"count"] intValue];
  3370. }
  3371. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3372. NSString* product_id=params[@"product_id"];
  3373. NSString* orderCode=params[@"orderCode"];
  3374. NSString *qty = params[@"qty"];
  3375. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3376. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3377. __block int number_of_outOfStock = 0;
  3378. for(int i=0;i<arr_id.count;i++)
  3379. {
  3380. NSInteger item_qty= count;
  3381. if (qty) {
  3382. item_qty = [qty_arr[i] integerValue];
  3383. }
  3384. if(item_qty==0)
  3385. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3386. // 检查新加Model数量是否大于库存
  3387. if (![params[@"can_create_backorder"] boolValue]) {
  3388. __block BOOL needContinue = NO;
  3389. [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) {
  3390. int availability = sqlite3_column_int(stmt, 0);
  3391. // 库存小于购买量为缺货
  3392. if (availability < item_qty || availability <= 0) {
  3393. number_of_outOfStock++;
  3394. needContinue = YES;
  3395. }
  3396. }];
  3397. if (needContinue) {
  3398. continue;
  3399. }
  3400. }
  3401. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3402. NSString* sql=nil;
  3403. sqlite3_stmt *stmt = nil;
  3404. BOOL shouldStep = NO;
  3405. if(_id<0)
  3406. {
  3407. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3408. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3409. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3410. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3411. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3412. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3413. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3414. shouldStep = YES;
  3415. }
  3416. else
  3417. {
  3418. if (qty) { // wish list move to cart
  3419. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3420. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3421. sqlite3_bind_int(stmt, 1, _id);
  3422. shouldStep = YES;
  3423. } else {
  3424. 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];
  3425. __block BOOL update = YES;
  3426. if (![params[@"can_create_backorder"] boolValue]) {
  3427. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3428. int newQTY = sqlite3_column_int(stmt, 0);
  3429. int availability = sqlite3_column_int(stmt, 1);
  3430. if (newQTY > availability) { // 库存不够
  3431. update = NO;
  3432. number_of_outOfStock++;
  3433. }
  3434. }];
  3435. }
  3436. if (update) {
  3437. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3438. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3439. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3440. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3441. shouldStep = YES;
  3442. }
  3443. }
  3444. }
  3445. if (shouldStep) {
  3446. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3447. [iSalesDB execSql:@"ROLLBACK" db:db];
  3448. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3449. [iSalesDB close_db:db];
  3450. DebugLog(@"add to cart error");
  3451. return [RAConvertor dict2data:ret];
  3452. }
  3453. }
  3454. }
  3455. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3456. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3457. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3458. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3459. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3460. if (![params[@"can_create_backorder"] boolValue]) {
  3461. if (number_of_outOfStock > 0) {
  3462. ret[@"result"]=[NSNumber numberWithInt:8];
  3463. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3464. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3465. }
  3466. }
  3467. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3468. [iSalesDB close_db:db];
  3469. return [RAConvertor dict2data:ret];
  3470. }
  3471. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3472. {
  3473. // UIApplication * app = [UIApplication sharedApplication];
  3474. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3475. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3476. 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];
  3477. sqlite3_stmt * statement;
  3478. int count=0;
  3479. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3480. {
  3481. while (sqlite3_step(statement) == SQLITE_ROW)
  3482. {
  3483. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3484. int bitem_id = sqlite3_column_int(statement, 0);
  3485. int bitem_qty = sqlite3_column_int(statement, 1);
  3486. char *name = (char*)sqlite3_column_text(statement, 2);
  3487. if(name==nil)
  3488. name="";
  3489. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3490. char *description = (char*)sqlite3_column_text(statement, 3);
  3491. if(description==nil)
  3492. description="";
  3493. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3494. double unit_price = sqlite3_column_double(statement, 4);
  3495. int use_unitprice = sqlite3_column_int(statement, 5);
  3496. if(use_unitprice!=1)
  3497. {
  3498. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3499. }
  3500. itemjson[@"model"]=nsname;
  3501. itemjson[@"description"]=nsdescription;
  3502. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3503. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3504. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3505. if(compute)
  3506. {
  3507. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3508. }
  3509. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3510. count++;
  3511. }
  3512. sqlite3_finalize(statement);
  3513. }
  3514. ret[@"count"]=@(count);
  3515. if(count==0)
  3516. return nil;
  3517. else
  3518. return ret;
  3519. }
  3520. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3521. {
  3522. //compute: add part to subtotal;
  3523. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3524. dict_item[@(item_id)]=@"1";
  3525. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3526. double cuft=0;
  3527. double weight=0;
  3528. int carton=0;
  3529. 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];
  3530. sqlite3_stmt * statement;
  3531. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3532. {
  3533. if (sqlite3_step(statement) == SQLITE_ROW)
  3534. {
  3535. double ulength = sqlite3_column_double(statement, 0);
  3536. double uwidth = sqlite3_column_double(statement, 1);
  3537. double uheight = sqlite3_column_double(statement, 2);
  3538. double uweight = sqlite3_column_double(statement, 3);
  3539. double mlength = sqlite3_column_double(statement, 4);
  3540. double mwidth = sqlite3_column_double(statement, 5);
  3541. double mheight = sqlite3_column_double(statement, 6);
  3542. double mweight = sqlite3_column_double(statement, 7);
  3543. double ilength = sqlite3_column_double(statement, 8);
  3544. double iwidth = sqlite3_column_double(statement, 9);
  3545. double iheight = sqlite3_column_double(statement, 10);
  3546. double iweight = sqlite3_column_double(statement, 11);
  3547. // int pcs = sqlite3_column_int(statement,12);
  3548. int mpack = sqlite3_column_int(statement, 13);
  3549. int ipack = sqlite3_column_int(statement, 14);
  3550. double ucbf = sqlite3_column_double(statement, 15);
  3551. // double icbf = sqlite3_column_double(statement, 16);
  3552. // double mcbf = sqlite3_column_double(statement, 17);
  3553. if(ipack==0)
  3554. {
  3555. carton= count/mpack ;
  3556. weight = mweight*carton;
  3557. cuft= carton*(mlength*mwidth*mheight);
  3558. int remain=count%mpack;
  3559. if(remain==0)
  3560. {
  3561. //do nothing;
  3562. }
  3563. else
  3564. {
  3565. carton++;
  3566. weight += uweight*remain;
  3567. cuft += (ulength*uwidth*uheight)*remain;
  3568. }
  3569. }
  3570. else
  3571. {
  3572. carton = count/(mpack*ipack);
  3573. weight = mweight*carton;
  3574. cuft= carton*(mlength*mwidth*mheight);
  3575. int remain=count%(mpack*ipack);
  3576. if(remain==0)
  3577. {
  3578. // do nothing;
  3579. }
  3580. else
  3581. {
  3582. carton++;
  3583. int icarton =remain/ipack;
  3584. int iremain=remain%ipack;
  3585. weight += iweight*icarton;
  3586. cuft += (ilength*iwidth*iheight)*icarton;
  3587. if(iremain==0)
  3588. {
  3589. //do nothing;
  3590. }
  3591. else
  3592. {
  3593. weight += uweight*iremain;
  3594. cuft += (ulength*uwidth*uheight)*iremain;
  3595. }
  3596. }
  3597. }
  3598. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3599. cuft=ucbf*count;
  3600. weight= uweight*count;
  3601. #endif
  3602. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3603. cuft=ucbf*count;
  3604. weight= uweight*count;
  3605. #endif
  3606. }
  3607. sqlite3_finalize(statement);
  3608. }
  3609. if(compute)
  3610. {
  3611. NSArray * arr_count=nil;
  3612. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3613. for(int i=0;i<arr_bundle.count;i++)
  3614. {
  3615. dict_item[arr_bundle[i]]=@"1";
  3616. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3617. cuft+=[bundlejson[@"cuft"] doubleValue];
  3618. weight+=[bundlejson[@"weight"] doubleValue];
  3619. carton+=[bundlejson[@"carton"] intValue];
  3620. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3621. }
  3622. }
  3623. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3624. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3625. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3626. ret[@"items"]=dict_item;
  3627. return ret;
  3628. }
  3629. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3630. {
  3631. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3632. // 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 ];
  3633. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3634. DebugLog(@"offline_login sql:%@",sqlQuery);
  3635. sqlite3_stmt * statement;
  3636. int cart_count=0;
  3637. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3638. if ( dbresult== SQLITE_OK)
  3639. {
  3640. while (sqlite3_step(statement) == SQLITE_ROW)
  3641. {
  3642. int item_id = sqlite3_column_int(statement, 0);
  3643. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3644. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3645. }
  3646. sqlite3_finalize(statement);
  3647. }
  3648. return cart_count;
  3649. }
  3650. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3651. //{
  3652. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3653. // sqlite3 *db = [iSalesDB get_db];
  3654. // UIApplication * app = [UIApplication sharedApplication];
  3655. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3656. //
  3657. // NSString* orderCode=params[@"orderCode"];
  3658. //
  3659. // int sort = [[params objectForKey:@"sort"] intValue];
  3660. // NSString *sort_str = @"";
  3661. // switch (sort) {
  3662. // case 0:{
  3663. // sort_str = @"order by c.modify_time desc";
  3664. // }
  3665. // break;
  3666. // case 1:{
  3667. // sort_str = @"order by c.modify_time asc";
  3668. // }
  3669. // break;
  3670. // case 2:{
  3671. // sort_str = @"order by m.name asc";
  3672. // }
  3673. // break;
  3674. // case 3:{
  3675. // sort_str = @"order by m.name desc";
  3676. // }
  3677. // break;
  3678. // case 4:{
  3679. // sort_str = @"order by m.description asc";
  3680. // }
  3681. // break;
  3682. //
  3683. // default:
  3684. // break;
  3685. //
  3686. // }
  3687. //
  3688. //
  3689. //
  3690. // 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 ];
  3691. //
  3692. //
  3693. //// 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 ];
  3694. //
  3695. //
  3696. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3697. //
  3698. // DebugLog(@"offline_login sql:%@",sqlQuery);
  3699. // sqlite3_stmt * statement;
  3700. //
  3701. //
  3702. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3703. //
  3704. // NSDate *date1 = [NSDate date];
  3705. //
  3706. // int count=0;
  3707. // int cart_count=0;
  3708. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3709. // if ( dbresult== SQLITE_OK)
  3710. // {
  3711. //
  3712. //
  3713. // while (sqlite3_step(statement) == SQLITE_ROW)
  3714. // {
  3715. //// NSDate *row_date = [NSDate date];
  3716. //
  3717. //
  3718. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3719. //
  3720. //
  3721. //
  3722. //
  3723. // int product_id = sqlite3_column_int(statement, 0);
  3724. //
  3725. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  3726. //
  3727. // int item_id = sqlite3_column_int(statement, 7);
  3728. //
  3729. // NSString* Price=nil;
  3730. // if(str_price==nil)
  3731. // {
  3732. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3733. //// NSDate *price_date = [NSDate date];
  3734. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3735. //// DebugLog(@"price time interval");
  3736. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3737. //
  3738. // if(price==nil)
  3739. // Price=@"No Price.";
  3740. // else
  3741. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3742. // }
  3743. // else
  3744. // {
  3745. //
  3746. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3747. // }
  3748. //
  3749. //
  3750. // double discount = sqlite3_column_double(statement, 2);
  3751. // int item_count = sqlite3_column_int(statement, 3);
  3752. //
  3753. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  3754. // NSString *nsline_note=nil;
  3755. // if(line_note!=nil)
  3756. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3757. //
  3758. //
  3759. // char *name = (char*)sqlite3_column_text(statement, 5);
  3760. // NSString *nsname=nil;
  3761. // if(name!=nil)
  3762. // nsname= [[NSString alloc]initWithUTF8String:name];
  3763. //
  3764. // char *description = (char*)sqlite3_column_text(statement, 6);
  3765. // NSString *nsdescription=nil;
  3766. // if(description!=nil)
  3767. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  3768. //
  3769. //
  3770. //
  3771. // int stockUom = sqlite3_column_int(statement, 8);
  3772. // int _id = sqlite3_column_int(statement, 9);
  3773. // int availability = sqlite3_column_int(statement, 10);
  3774. //
  3775. //// NSDate *subtotal_date = [NSDate date];
  3776. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3777. //// DebugLog(@"subtotal_date time interval");
  3778. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3779. //
  3780. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3781. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  3782. // int carton=[bsubtotaljson[@"carton"] intValue];
  3783. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3784. //
  3785. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3786. //// NSDate *img_date = [NSDate date];
  3787. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3788. //// DebugLog(@"img_date time interval");
  3789. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3790. //
  3791. // itemjson[@"model"]=nsname;
  3792. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3793. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3794. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3795. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3796. // itemjson[@"check"]=@"true";
  3797. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3798. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3799. // itemjson[@"unit_price"]=Price;
  3800. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3801. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3802. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3803. // itemjson[@"note"]=nsline_note;
  3804. // if (!appDelegate.can_create_backorder) {
  3805. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3806. // }
  3807. //// NSDate *date2 = [NSDate date];
  3808. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3809. //// DebugLog(@"model_bundle time interval");
  3810. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3811. //
  3812. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3813. // count++;
  3814. //
  3815. //// DebugLog(@"row time interval");
  3816. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3817. // }
  3818. //
  3819. //
  3820. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3821. // sqlite3_finalize(statement);
  3822. // }
  3823. //
  3824. //
  3825. // DebugLog(@"request cart total time interval");
  3826. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3827. //
  3828. //
  3829. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3830. //
  3831. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3832. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3833. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3834. // ret[@"count"]=[NSNumber numberWithInt:count ];
  3835. //
  3836. // ret[@"mode"]=@"Regular Mode";
  3837. //
  3838. // [iSalesDB close_db:db];
  3839. //
  3840. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3841. //
  3842. // DebugLog(@"general notes :%@",general_note);
  3843. //
  3844. // ret[@"general_note"]= general_note;
  3845. //
  3846. // return [RAConvertor dict2data:ret];
  3847. //}
  3848. +(NSData*) offline_login :(NSMutableDictionary *) params
  3849. {
  3850. NSString* user = [params valueForKey:@"user"];
  3851. NSString* password = [params valueForKey:@"password"];
  3852. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3853. sqlite3 *db = [iSalesDB get_db];
  3854. 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"]];
  3855. DebugLog(@"offline_login sql:%@",sqlQuery);
  3856. sqlite3_stmt * statement;
  3857. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3858. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3859. {
  3860. if (sqlite3_step(statement) == SQLITE_ROW)
  3861. {
  3862. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3863. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3864. int can_show_price = sqlite3_column_int(statement, 0);
  3865. int can_see_price = sqlite3_column_int(statement, 1);
  3866. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3867. if(contact_id==nil)
  3868. contact_id="";
  3869. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3870. int user_type = sqlite3_column_int(statement, 3);
  3871. int can_cancel_order = sqlite3_column_int(statement, 4);
  3872. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3873. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3874. int can_delete_order = sqlite3_column_int(statement, 7);
  3875. int can_submit_order = sqlite3_column_int(statement, 8);
  3876. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3877. int can_create_order = sqlite3_column_int(statement, 10);
  3878. char *mode = (char*)sqlite3_column_text(statement, 11);
  3879. if(mode==nil)
  3880. mode="";
  3881. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3882. char *username = (char*)sqlite3_column_text(statement, 12);
  3883. if(username==nil)
  3884. username="";
  3885. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3886. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3887. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3888. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3889. [header setValue:nscontact_id forKey:@"contact_id"];
  3890. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3891. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3892. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3893. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3894. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3895. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3896. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3897. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3898. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3899. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3900. [header setValue:nsusername forKey:@"username"];
  3901. NSError* error=nil;
  3902. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3903. [header setValue:statusFilter forKey:@"statusFilter"];
  3904. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3905. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3906. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3907. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3908. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3909. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3910. [ret setObject:header forKey:@"header"];
  3911. [ret setValue:nsmode forKey:@"mode"];
  3912. }
  3913. sqlite3_finalize(statement);
  3914. }
  3915. [iSalesDB close_db:db];
  3916. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3917. return [RAConvertor dict2data:ret];
  3918. }
  3919. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3920. {
  3921. assert(params[@"mode"]!=nil);
  3922. NSString* contactId = [params valueForKey:@"contactId"];
  3923. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3924. sqlite3 *db = [iSalesDB get_db];
  3925. NSString *sqlQuery = nil;
  3926. {
  3927. 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];
  3928. }
  3929. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3930. sqlite3_stmt * statement;
  3931. [ret setValue:@"2" forKey:@"result"];
  3932. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3933. {
  3934. //int i = 0;
  3935. if (sqlite3_step(statement) == SQLITE_ROW)
  3936. {
  3937. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3938. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3939. // int editable = sqlite3_column_int(statement, 0);
  3940. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3941. NSString *nscompany_name =nil;
  3942. if(company_name==nil)
  3943. nscompany_name=@"";
  3944. else
  3945. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3946. char *country = (char*)sqlite3_column_text(statement, 2);
  3947. if(country==nil)
  3948. country="";
  3949. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3950. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3951. // if(addr==nil)
  3952. // addr="";
  3953. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3954. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3955. if(zipcode==nil)
  3956. zipcode="";
  3957. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3958. char *state = (char*)sqlite3_column_text(statement, 5);
  3959. if(state==nil)
  3960. state="";
  3961. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3962. char *city = (char*)sqlite3_column_text(statement, 6);
  3963. if(city==nil)
  3964. city="";
  3965. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3966. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3967. // NSString *nscontact_name = nil;
  3968. // if(contact_name==nil)
  3969. // nscontact_name=@"";
  3970. // else
  3971. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3972. char *phone = (char*)sqlite3_column_text(statement, 8);
  3973. NSString *nsphone = nil;
  3974. if(phone==nil)
  3975. nsphone=@"";
  3976. else
  3977. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3978. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3979. // if(contact_id==nil)
  3980. // contact_id="";
  3981. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3982. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3983. if(addr_1==nil)
  3984. addr_1="";
  3985. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3986. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3987. if(addr_2==nil)
  3988. addr_2="";
  3989. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3990. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3991. if(addr_3==nil)
  3992. addr_3="";
  3993. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3994. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3995. if(addr_4==nil)
  3996. addr_4="";
  3997. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3998. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3999. if(first_name==nil)
  4000. first_name="";
  4001. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4002. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4003. if(last_name==nil)
  4004. last_name="";
  4005. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4006. char *fax = (char*)sqlite3_column_text(statement, 16);
  4007. NSString *nsfax = nil;
  4008. if(fax==nil)
  4009. nsfax=@"";
  4010. else
  4011. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4012. char *email = (char*)sqlite3_column_text(statement, 17);
  4013. NSString *nsemail = nil;
  4014. if(email==nil)
  4015. nsemail=@"";
  4016. else
  4017. nsemail= [[NSString alloc]initWithUTF8String:email];
  4018. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4019. NSString *nsimg_0 = nil;
  4020. if(img_0==nil)
  4021. nsimg_0=@"";
  4022. else
  4023. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4024. [self copy_bcardImg:nsimg_0];
  4025. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4026. NSString *nsimg_1 = nil;
  4027. if(img_1==nil)
  4028. nsimg_1=@"";
  4029. else
  4030. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4031. [self copy_bcardImg:nsimg_1];
  4032. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4033. NSString *nsimg_2 = nil;
  4034. if(img_2==nil)
  4035. nsimg_2=@"";
  4036. else
  4037. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4038. [self copy_bcardImg:nsimg_2];
  4039. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4040. NSString *nsprice_type = nil;
  4041. if(price_type==nil)
  4042. nsprice_type=@"";
  4043. else
  4044. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4045. char *notes = (char*)sqlite3_column_text(statement, 22);
  4046. NSString *nsnotes = nil;
  4047. if(notes==nil)
  4048. nsnotes=@"";
  4049. else
  4050. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4051. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4052. NSString *nssalesrep = nil;
  4053. if(salesrep==nil)
  4054. nssalesrep=@"";
  4055. else
  4056. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4057. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4058. {
  4059. // decrypt
  4060. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4061. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4062. nsphone=[AESCrypt fastdecrypt:nsphone];
  4063. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4064. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4065. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4066. }
  4067. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4068. [arr_name addObject:nsfirst_name];
  4069. [arr_name addObject:nslast_name];
  4070. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4071. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4072. [arr_ext addObject:nsaddr_1];
  4073. [arr_ext addObject:nsaddr_2];
  4074. [arr_ext addObject:nsaddr_3];
  4075. [arr_ext addObject:nsaddr_4];
  4076. [arr_ext addObject:@"\r\n"];
  4077. [arr_ext addObject:nscity];
  4078. [arr_ext addObject:nsstate];
  4079. [arr_ext addObject:nszipcode];
  4080. [arr_ext addObject:nscountry];
  4081. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4082. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4083. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4084. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4085. [item setValue:nscountry forKey:@"customer_country"];
  4086. [item setValue:nsphone forKey:@"customer_phone"];
  4087. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4088. [item setValue:nscompany_name forKey:@"customer_name"];
  4089. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4090. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4091. [item setValue:nsext forKey:@"customer_contact_ext"];
  4092. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4093. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4094. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4095. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4096. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4097. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4098. [item setValue:nslast_name forKey:@"customer_last_name"];
  4099. [item setValue:nscity forKey:@"customer_city"];
  4100. [item setValue:nsstate forKey:@"customer_state"];
  4101. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4102. [item setValue:contactId forKey:@"customer_cid"];
  4103. [item setValue:nscontact_name forKey:@"customer_contact"];
  4104. [item setValue:nsfax forKey:@"customer_fax"];
  4105. [item setValue:nsemail forKey:@"customer_email"];
  4106. [item setValue:contact_type forKey:@"customer_contact_type"];
  4107. [ret setObject:item forKey:@"customerInfo"];
  4108. // i++;
  4109. }
  4110. // UIApplication * app = [UIApplication sharedApplication];
  4111. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4112. [ret setValue:params[@"mode"] forKey:@"mode"];
  4113. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4114. sqlite3_finalize(statement);
  4115. }
  4116. [iSalesDB close_db:db];
  4117. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4118. return ret;
  4119. }
  4120. + (bool) copy_bcardImg:(NSString*) filename
  4121. {
  4122. if(filename.length==0)
  4123. return false;
  4124. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4125. bool ret=false;
  4126. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4127. NSString *cachefolder = [paths objectAtIndex:0];
  4128. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4129. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4130. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4131. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4132. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4133. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4134. //
  4135. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4136. NSFileManager* fileManager = [NSFileManager defaultManager];
  4137. BOOL bdir=NO;
  4138. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4139. {
  4140. NSError *error = nil;
  4141. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4142. {
  4143. ret=false;
  4144. }
  4145. else
  4146. {
  4147. ret=true;
  4148. }
  4149. // NSError *error = nil;
  4150. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4151. //
  4152. // if(!bsuccess)
  4153. // {
  4154. // DebugLog(@"Create offline_createimg folder failed");
  4155. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4156. // return [RAConvertor dict2data:ret];
  4157. // }
  4158. // if(bsuccess)
  4159. // {
  4160. // sqlite3 *db = [self get_db];
  4161. //
  4162. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4163. // [iSalesDB close_db:db];
  4164. // }
  4165. }
  4166. return ret;
  4167. //
  4168. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4169. // if(bsuccess)
  4170. // {
  4171. // NSError *error = nil;
  4172. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4173. // {
  4174. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4175. // }
  4176. // else
  4177. // {
  4178. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4179. //
  4180. // ret[@"img_url_aname"]=filename;
  4181. // ret[@"img_url"]=savedImagePath;
  4182. // }
  4183. // }
  4184. }
  4185. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4186. {
  4187. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4188. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4189. NSString *cachefolder = [paths objectAtIndex:0];
  4190. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4191. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4192. NSFileManager* fileManager = [NSFileManager defaultManager];
  4193. BOOL bdir=YES;
  4194. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4195. {
  4196. NSError *error = nil;
  4197. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4198. if(!bsuccess)
  4199. {
  4200. DebugLog(@"Create offline_createimg folder failed");
  4201. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4202. return [RAConvertor dict2data:ret];
  4203. }
  4204. // if(bsuccess)
  4205. // {
  4206. // sqlite3 *db = [self get_db];
  4207. //
  4208. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4209. // [iSalesDB close_db:db];
  4210. // }
  4211. }
  4212. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4213. {
  4214. NSError *error = nil;
  4215. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4216. if(!bsuccess)
  4217. {
  4218. DebugLog(@"Create img_cache folder failed");
  4219. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4220. return [RAConvertor dict2data:ret];
  4221. }
  4222. // if(bsuccess)
  4223. // {
  4224. // sqlite3 *db = [self get_db];
  4225. //
  4226. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4227. // [iSalesDB close_db:db];
  4228. // }
  4229. }
  4230. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4231. //JEPG格式
  4232. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4233. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  4234. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4235. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4236. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4237. if(bsuccess)
  4238. {
  4239. NSError *error = nil;
  4240. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4241. {
  4242. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4243. }
  4244. else
  4245. {
  4246. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4247. ret[@"img_url_aname"]=filename;
  4248. ret[@"img_url"]=filename;
  4249. }
  4250. }
  4251. else
  4252. {
  4253. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4254. }
  4255. return [RAConvertor dict2data:ret];
  4256. }
  4257. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4258. {
  4259. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4260. category = [category substringToIndex:3];
  4261. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4262. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4263. params[@"category"]= category;
  4264. ret[@"params"]= params;
  4265. [ret setValue:@"detail" forKey:@"target"];
  4266. [ret setValue:@"popup" forKey:@"action"];
  4267. [ret setValue:@"content" forKey:@"type"];
  4268. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4269. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4270. [ret setValue:@"true" forKey:@"single_row"];
  4271. [ret setValue:@"true" forKey:@"partial_refresh"];
  4272. // sqlite3 *db = [iSalesDB get_db];
  4273. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4274. NSString *sqlQuery =nil;
  4275. #ifdef BUILD_NPD
  4276. 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 ;
  4277. #else
  4278. 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];
  4279. #endif
  4280. sqlite3_stmt * statement;
  4281. int count = 0;
  4282. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4283. // int count=0;
  4284. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4285. {
  4286. int i=0;
  4287. while (sqlite3_step(statement) == SQLITE_ROW)
  4288. {
  4289. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4290. // char *name = (char*)sqlite3_column_text(statement, 1);
  4291. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4292. char *name = (char*)sqlite3_column_text(statement, 0);
  4293. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4294. int product_id = sqlite3_column_int(statement, 1);
  4295. char *url = (char*)sqlite3_column_text(statement, 2);
  4296. if(url==nil)
  4297. url="";
  4298. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4299. [item setValue:nsurl forKey:@"picture_path"];
  4300. [item setValue:nsname forKey:@"fash_name"];
  4301. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4302. [item setValue:category forKey:@"category"];
  4303. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4304. i++;
  4305. }
  4306. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4307. sqlite3_finalize(statement);
  4308. }
  4309. DebugLog(@"count:%d",count);
  4310. // [iSalesDB close_db:db];
  4311. return ret;
  4312. }
  4313. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4314. {
  4315. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4316. [ret setValue:key forKey:@"key"];
  4317. [ret setValue:value forKey:@"val"];
  4318. [ret setValue:@"price" forKey:@"type"];
  4319. return ret;
  4320. }
  4321. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4322. {
  4323. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4324. [ret setValue:key forKey:@"key"];
  4325. [ret setValue:value forKey:@"val"];
  4326. return ret;
  4327. }
  4328. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4329. {
  4330. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4331. [ret setValue:@"0" forKey:@"img_count"];
  4332. // sqlite3 *db = [iSalesDB get_db];
  4333. 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 ;
  4334. sqlite3_stmt * statement;
  4335. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4336. {
  4337. int i=0;
  4338. if (sqlite3_step(statement) == SQLITE_ROW)
  4339. {
  4340. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4341. // char *name = (char*)sqlite3_column_text(statement, 1);
  4342. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4343. char *value = (char*)sqlite3_column_text(statement, 0);
  4344. if(value==nil)
  4345. value="";
  4346. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4347. char *key = (char*)sqlite3_column_text(statement, 1);
  4348. if(key==nil)
  4349. key="";
  4350. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4351. [item setValue:nsvalue forKey:@"val"];
  4352. [item setValue:nskey forKey:@"key"];
  4353. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4354. [ret setValue:@"1" forKey:@"count"];
  4355. i++;
  4356. }
  4357. sqlite3_finalize(statement);
  4358. }
  4359. // [iSalesDB close_db:db];
  4360. return ret;
  4361. }
  4362. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4363. {
  4364. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4365. [ret setValue:@"0" forKey:@"count"];
  4366. // sqlite3 *db = [iSalesDB get_db];
  4367. 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;
  4368. sqlite3_stmt * statement;
  4369. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4370. {
  4371. int i=0;
  4372. while (sqlite3_step(statement) == SQLITE_ROW)
  4373. {
  4374. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4375. // char *name = (char*)sqlite3_column_text(statement, 1);
  4376. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4377. char *value = (char*)sqlite3_column_text(statement, 0);
  4378. if(value==nil)
  4379. value="";
  4380. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4381. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4382. if(selector_display==nil)
  4383. selector_display="";
  4384. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4385. int product_id = sqlite3_column_int(statement, 2);
  4386. char *category = (char*)sqlite3_column_text(statement, 3);
  4387. if(category==nil)
  4388. category="";
  4389. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4390. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4391. [item setValue:nsvalue forKey:@"title"];
  4392. [item setValue:url forKey:@"pic_url"];
  4393. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4394. [params setValue:@"2" forKey:@"count"];
  4395. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4396. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4397. [param0 setValue:@"product_id" forKey:@"name"];
  4398. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4399. [param1 setValue:nscategory forKey:@"val"];
  4400. [param1 setValue:@"category" forKey:@"name"];
  4401. [params setObject:param0 forKey:@"param_0"];
  4402. [params setObject:param1 forKey:@"param_1"];
  4403. [item setObject:params forKey:@"params"];
  4404. [ret setValue:nsselector_display forKey:@"name"];
  4405. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4406. i++;
  4407. }
  4408. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4409. [ret setValue:@"switch" forKey:@"action"];
  4410. sqlite3_finalize(statement);
  4411. }
  4412. // [iSalesDB close_db:db];
  4413. return ret;
  4414. }
  4415. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4416. {
  4417. // model 在 category search 显示的图片。
  4418. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4419. // sqlite3 *db = [iSalesDB get_db];
  4420. 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];
  4421. sqlite3_stmt * statement;
  4422. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4423. {
  4424. while (sqlite3_step(statement) == SQLITE_ROW)
  4425. {
  4426. char *url = (char*)sqlite3_column_text(statement, 0);
  4427. if(url==nil)
  4428. url="";
  4429. int type = sqlite3_column_int(statement, 1);
  4430. if(type==0)
  4431. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4432. else
  4433. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4434. }
  4435. sqlite3_finalize(statement);
  4436. }
  4437. // [iSalesDB close_db:db];
  4438. return ret;
  4439. }
  4440. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4441. {
  4442. int item_id=-1;
  4443. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4444. sqlite3_stmt * statement;
  4445. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4446. {
  4447. if (sqlite3_step(statement) == SQLITE_ROW)
  4448. {
  4449. item_id = sqlite3_column_int(statement, 0);
  4450. }
  4451. sqlite3_finalize(statement);
  4452. }
  4453. return item_id;
  4454. }
  4455. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4456. {
  4457. // NSString* ret = @"";
  4458. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4459. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4460. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4461. sqlite3_stmt * statement;
  4462. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4463. {
  4464. while (sqlite3_step(statement) == SQLITE_ROW)
  4465. {
  4466. int bitem_id = sqlite3_column_int(statement, 0);
  4467. int bitem_qty = sqlite3_column_int(statement, 1);
  4468. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4469. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4470. }
  4471. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4472. sqlite3_finalize(statement);
  4473. }
  4474. // if(ret==nil)
  4475. // ret=@"";
  4476. *count=arr_count;
  4477. return arr_bundle;
  4478. }
  4479. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4480. {
  4481. // get default sold qty, return -1 if model not found;
  4482. int ret = -1;
  4483. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4484. sqlite3_stmt * statement;
  4485. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4486. {
  4487. if (sqlite3_step(statement) == SQLITE_ROW)
  4488. {
  4489. ret = sqlite3_column_int(statement, 0);
  4490. }
  4491. sqlite3_finalize(statement);
  4492. }
  4493. return ret;
  4494. }
  4495. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4496. {
  4497. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4498. [ret setValue:@"0" forKey:@"img_count"];
  4499. // sqlite3 *db = [iSalesDB get_db];
  4500. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4501. sqlite3_stmt * statement;
  4502. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4503. {
  4504. int i=0;
  4505. while (sqlite3_step(statement) == SQLITE_ROW)
  4506. {
  4507. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4508. // char *name = (char*)sqlite3_column_text(statement, 1);
  4509. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4510. char *url = (char*)sqlite3_column_text(statement, 0);
  4511. if(url==nil)
  4512. url="";
  4513. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4514. [item setValue:nsurl forKey:@"s"];
  4515. [item setValue:nsurl forKey:@"l"];
  4516. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4517. i++;
  4518. }
  4519. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4520. sqlite3_finalize(statement);
  4521. }
  4522. // [iSalesDB close_db:db];
  4523. return ret;
  4524. }
  4525. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4526. // UIApplication * app = [UIApplication sharedApplication];
  4527. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4528. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4529. sqlite3 *db = [iSalesDB get_db];
  4530. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4531. NSString* product_id=params[@"product_id"];
  4532. NSString *item_count_str = params[@"item_count"];
  4533. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4534. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4535. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4536. // NSString *sql = @"";
  4537. for(int i=0;i<arr.count;i++)
  4538. {
  4539. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4540. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4541. __block int cart_count = 0;
  4542. if (!item_count_str) {
  4543. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4544. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4545. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4546. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4547. cart_count = [[model_set_components lastObject] intValue];
  4548. }];
  4549. }
  4550. if(count==0)
  4551. {
  4552. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4553. sqlite3_stmt *stmt;
  4554. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4555. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4556. if (item_count_arr) {
  4557. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4558. } else {
  4559. sqlite3_bind_int(stmt,2,cart_count);
  4560. }
  4561. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4562. [iSalesDB execSql:@"ROLLBACK" db:db];
  4563. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4564. [iSalesDB close_db:db];
  4565. DebugLog(@"add to wishlist error");
  4566. return ret;
  4567. }
  4568. } else {
  4569. int qty = 0;
  4570. if (item_count_arr) {
  4571. qty = [item_count_arr[i] intValue];
  4572. } else {
  4573. qty = cart_count;
  4574. }
  4575. 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]];
  4576. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4577. [iSalesDB execSql:@"ROLLBACK" db:db];
  4578. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4579. [iSalesDB close_db:db];
  4580. DebugLog(@"add to wishlist error");
  4581. return ret;
  4582. }
  4583. }
  4584. }
  4585. // [iSalesDB execSql:sql db:db];
  4586. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4587. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4588. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4589. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4590. [iSalesDB close_db:db];
  4591. ret[@"wish_count"]=@(count);
  4592. ret[@"result"]= [NSNumber numberWithInt:2];
  4593. return ret;
  4594. }
  4595. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4596. // 0 category
  4597. // 1 search
  4598. // 2 itemsearch
  4599. NSData *ret = nil;
  4600. NSDictionary *items = nil;
  4601. switch (from) {
  4602. case 0:{
  4603. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4604. }
  4605. break;
  4606. case 1:{
  4607. items = [[self search:params limited:NO] objectForKey:@"items"];
  4608. }
  4609. break;
  4610. case 2:{
  4611. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4612. }
  4613. break;
  4614. default:
  4615. break;
  4616. }
  4617. if (!items) {
  4618. return ret;
  4619. }
  4620. int count = [[items objectForKey:@"count"] intValue];
  4621. NSMutableString *product_id_str = [@"" mutableCopy];
  4622. for (int i = 0; i < count; i++) {
  4623. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4624. NSDictionary *item = [items objectForKey:key];
  4625. NSString *product_id = [item objectForKey:@"product_id"];
  4626. if (i == 0) {
  4627. [product_id_str appendString:product_id];
  4628. } else {
  4629. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4630. }
  4631. }
  4632. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4633. if ([add_to isEqualToString:@"cart"]) {
  4634. NSString *order_code = [params objectForKey:@"orderCode"];
  4635. if (order_code.length) {
  4636. NSDictionary *newParams = @{
  4637. @"product_id" : product_id_str,
  4638. @"orderCode" : order_code,
  4639. @"can_create_backorder":params[@"can_create_backorder"]
  4640. };
  4641. ret = [self offline_add2cart:newParams.mutableCopy];
  4642. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4643. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4644. ret = [RAConvertor dict2data:retDic];
  4645. }
  4646. } else if([add_to isEqualToString:@"wishlist"]) {
  4647. NSDictionary *newParams = @{
  4648. @"product_id" : product_id_str
  4649. };
  4650. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4651. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4652. ret = [RAConvertor dict2data:retDic];
  4653. } else if([add_to isEqualToString:@"portfolio"]) {
  4654. NSDictionary *newParams = @{
  4655. @"product_id" : product_id_str
  4656. };
  4657. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4658. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4659. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4660. ret = [RAConvertor dict2data:retDic];
  4661. }
  4662. return ret;
  4663. }
  4664. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4665. {
  4666. return [self addAll:params from:0];
  4667. }
  4668. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4669. {
  4670. return [self addAll:params from:1];
  4671. }
  4672. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4673. {
  4674. return [self addAll:params from:2];
  4675. }
  4676. #pragma mark - Jack
  4677. #warning 做SQL操作时转义!!
  4678. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4679. // "val_227" : {
  4680. // "check" : 1,
  4681. // "value" : "US United States",
  4682. // "value_id" : "228"
  4683. // },
  4684. if (!countryCode) {
  4685. countryCode = @"US";
  4686. }
  4687. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4688. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4689. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4690. int code_id = sqlite3_column_int(stmt, 3); // id
  4691. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4692. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4693. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4694. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4695. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4696. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4697. }
  4698. long n = *count;
  4699. *count = n + 1;
  4700. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4701. [container setValue:countryDic forKey:key];
  4702. }] mutableCopy];
  4703. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4704. return ret;
  4705. }
  4706. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4707. countryCode = [self translateSingleQuote:countryCode];
  4708. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4709. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4710. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4711. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4712. if (name == NULL) {
  4713. name = "";
  4714. }
  4715. if (code == NULL) {
  4716. code = "";
  4717. }
  4718. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4719. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4720. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4721. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4722. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4723. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4724. }
  4725. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4726. [container setValue:stateDic forKey:key];
  4727. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4728. DebugLog(@"query all state error: %@",err_msg);
  4729. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4730. // [stateDic setValue:@"Other" forKey:@"value"];
  4731. // [stateDic setValue:@"" forKey:@"value_id"];
  4732. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4733. //
  4734. // if (state_code && [@"" isEqualToString:state_code]) {
  4735. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4736. // }
  4737. //
  4738. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4739. // [container setValue:stateDic forKey:key];
  4740. }] mutableCopy];
  4741. [ret removeObjectForKey:@"result"];
  4742. // failure 可以不用了,一样的
  4743. if (ret.allKeys.count == 0) {
  4744. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4745. [stateDic setValue:@"Other" forKey:@"value"];
  4746. [stateDic setValue:@"" forKey:@"value_id"];
  4747. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4748. if (state_code && [@"" isEqualToString:state_code]) {
  4749. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4750. }
  4751. NSString *key = [NSString stringWithFormat:@"val_0"];
  4752. [ret setValue:stateDic forKey:key];
  4753. }
  4754. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4755. return ret;
  4756. }
  4757. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4758. codeId = [self translateSingleQuote:codeId];
  4759. 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];
  4760. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4761. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4762. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4763. if (name == NULL) {
  4764. name = "";
  4765. }
  4766. if (code == NULL) {
  4767. code = "";
  4768. }
  4769. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4770. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4771. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4772. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4773. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4774. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4775. }
  4776. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4777. [container setValue:stateDic forKey:key];
  4778. }] mutableCopy];
  4779. [ret removeObjectForKey:@"result"];
  4780. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4781. return ret;
  4782. }
  4783. + (NSDictionary *)offline_getPrice {
  4784. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4785. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4786. char *name = (char *) sqlite3_column_text(stmt, 1);
  4787. int type = sqlite3_column_int(stmt, 2);
  4788. int orderBy = sqlite3_column_int(stmt, 3);
  4789. if (name == NULL) {
  4790. name = "";
  4791. }
  4792. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4793. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4794. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4795. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4796. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4797. if (orderBy == 0) {
  4798. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4799. }
  4800. [container setValue:priceDic forKey:key];
  4801. }] mutableCopy];
  4802. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4803. return ret;
  4804. }
  4805. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4806. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4807. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4808. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4809. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4810. // int _id = sqlite3_column_int(stmt, 0);
  4811. NSString *name = [self textAtColumn:1 statement:stmt];
  4812. NSDictionary *typeDic = @{
  4813. @"value_id" : name,
  4814. @"value" : name,
  4815. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4816. };
  4817. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4818. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  4819. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4820. }];
  4821. return ret;
  4822. }
  4823. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  4824. // 首先从offline_login表中取出sales_code
  4825. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4826. // NSString *user = app.user;
  4827. user = [self translateSingleQuote:user];
  4828. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4829. __block NSString *user_code = @"";
  4830. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4831. char *code = (char *)sqlite3_column_text(stmt, 0);
  4832. if (code == NULL) {
  4833. code = "";
  4834. }
  4835. user_code = [NSString stringWithUTF8String:code];
  4836. }];
  4837. // 再取所有salesRep
  4838. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4839. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4840. // 1 name 2 code 3 salesrep_id
  4841. char *name = (char *)sqlite3_column_text(stmt, 1);
  4842. char *code = (char *)sqlite3_column_text(stmt, 2);
  4843. int salesrep_id = sqlite3_column_int(stmt, 3);
  4844. if (name == NULL) {
  4845. name = "";
  4846. }
  4847. if (code == NULL) {
  4848. code = "";
  4849. }
  4850. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4851. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4852. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4853. // 比较code 相等则check
  4854. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4855. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4856. }
  4857. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4858. }] mutableCopy];
  4859. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4860. return ret;
  4861. }
  4862. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4863. zipcode = [self translateSingleQuote:zipcode];
  4864. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4865. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4866. char *country = (char *)sqlite3_column_text(stmt, 0);
  4867. char *state = (char *)sqlite3_column_text(stmt, 1);
  4868. char *city = (char *)sqlite3_column_text(stmt, 2);
  4869. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4870. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4871. if (country == NULL) {
  4872. country = "";
  4873. }
  4874. if (state == NULL) {
  4875. state = "";
  4876. }
  4877. if (city == NULL) {
  4878. city = "";
  4879. }
  4880. if (country_code == NULL) {
  4881. country_code = "";
  4882. }
  4883. if (state_code == NULL) {
  4884. state_code = "";
  4885. }
  4886. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4887. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4888. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4889. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4890. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4891. }] mutableCopy];
  4892. return ret;
  4893. }
  4894. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4895. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4896. [item setValue:value forKey:valueKey];
  4897. [dic setValue:item forKey:itemKey];
  4898. }
  4899. + (NSString *)countryCodeByid:(NSString *)code_id {
  4900. NSString *ret = nil;
  4901. code_id = [self translateSingleQuote:code_id];
  4902. sqlite3 *db = [iSalesDB get_db];
  4903. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4904. sqlite3_stmt * statement;
  4905. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4906. while (sqlite3_step(statement) == SQLITE_ROW) {
  4907. char *code = (char *)sqlite3_column_text(statement, 0);
  4908. if (code == NULL) {
  4909. code = "";
  4910. }
  4911. ret = [NSString stringWithUTF8String:code];
  4912. }
  4913. sqlite3_finalize(statement);
  4914. }
  4915. [iSalesDB close_db:db];
  4916. return ret;
  4917. }
  4918. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4919. NSString *ret = nil;
  4920. code = [self translateSingleQuote:code];
  4921. sqlite3 *db = [iSalesDB get_db];
  4922. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4923. sqlite3_stmt * statement;
  4924. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4925. while (sqlite3_step(statement) == SQLITE_ROW) {
  4926. char *_id = (char *)sqlite3_column_text(statement, 0);
  4927. if (_id == NULL) {
  4928. _id = "";
  4929. }
  4930. ret = [NSString stringWithFormat:@"%s",_id];
  4931. }
  4932. sqlite3_finalize(statement);
  4933. }
  4934. [iSalesDB close_db:db];
  4935. return ret;
  4936. }
  4937. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4938. NSString *name = nil;
  4939. codeId = [self translateSingleQuote:codeId];
  4940. sqlite3 *db = [iSalesDB get_db];
  4941. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4942. sqlite3_stmt * statement;
  4943. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4944. while (sqlite3_step(statement) == SQLITE_ROW) {
  4945. char *value = (char *)sqlite3_column_text(statement, 0);
  4946. if (value == NULL) {
  4947. value = "";
  4948. }
  4949. name = [NSString stringWithUTF8String:value];
  4950. }
  4951. sqlite3_finalize(statement);
  4952. }
  4953. [iSalesDB close_db:db];
  4954. return name;
  4955. }
  4956. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4957. NSString *ret = nil;
  4958. sqlite3 *db = [iSalesDB get_db];
  4959. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4960. sqlite3_stmt * statement;
  4961. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4962. while (sqlite3_step(statement) == SQLITE_ROW) {
  4963. char *name = (char *)sqlite3_column_text(statement, 0);
  4964. if (name == NULL) {
  4965. name = "";
  4966. }
  4967. ret = [NSString stringWithUTF8String:name];
  4968. }
  4969. sqlite3_finalize(statement);
  4970. }
  4971. [iSalesDB close_db:db];
  4972. return ret;
  4973. }
  4974. + (NSString *)salesRepCodeById:(NSString *)_id {
  4975. NSString *ret = nil;
  4976. _id = [self translateSingleQuote:_id];
  4977. sqlite3 *db = [iSalesDB get_db];
  4978. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4979. sqlite3_stmt * statement;
  4980. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4981. while (sqlite3_step(statement) == SQLITE_ROW) {
  4982. char *rep = (char *)sqlite3_column_text(statement, 0);
  4983. if (rep == NULL) {
  4984. rep = "";
  4985. }
  4986. ret = [NSString stringWithUTF8String:rep];
  4987. }
  4988. sqlite3_finalize(statement);
  4989. }
  4990. [iSalesDB close_db:db];
  4991. return ret;
  4992. }
  4993. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4994. char *tx = (char *)sqlite3_column_text(stmt, col);
  4995. if (tx == NULL) {
  4996. tx = "";
  4997. }
  4998. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4999. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5000. if (!text) {
  5001. text = @"";
  5002. }
  5003. // 将字符全部为' '的字符串干掉
  5004. int spaceCount = 0;
  5005. for (int i = 0; i < text.length; i++) {
  5006. if ([text characterAtIndex:i] == ' ') {
  5007. spaceCount++;
  5008. }
  5009. }
  5010. if (spaceCount == text.length) {
  5011. text = @"";
  5012. }
  5013. return text;
  5014. }
  5015. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5016. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5017. NSData *data = [NSData dataWithContentsOfFile:path];
  5018. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5019. return ret;
  5020. }
  5021. + (NSString *)textFileName:(NSString *)name {
  5022. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5023. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5024. if (!text) {
  5025. text = @"";
  5026. }
  5027. return text;
  5028. }
  5029. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5030. return [[dic objectForKey:key] mutableCopy];
  5031. }
  5032. + (id)translateSingleQuote:(NSString *)string {
  5033. if ([string isKindOfClass:[NSString class]])
  5034. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5035. return string;
  5036. }
  5037. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5038. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5039. }
  5040. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5041. NSString* ret= nil;
  5042. NSString *sqlQuery = nil;
  5043. // 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
  5044. 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];
  5045. sqlite3_stmt * statement;
  5046. // int count=0;
  5047. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5048. {
  5049. if (sqlite3_step(statement) == SQLITE_ROW)
  5050. {
  5051. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5052. if(imgurl==nil)
  5053. imgurl="";
  5054. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5055. ret=nsimgurl;
  5056. }
  5057. sqlite3_finalize(statement);
  5058. }
  5059. else
  5060. {
  5061. [ret setValue:@"8" forKey:@"result"];
  5062. }
  5063. // [iSalesDB close_db:db];
  5064. // DebugLog(@"data string: %@",ret );
  5065. return ret;
  5066. }
  5067. #pragma mark contact Advanced search
  5068. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5069. {
  5070. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5071. return [RAConvertor dict2data:contactAdvanceDic];
  5072. }
  5073. #pragma mark create new contact
  5074. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5075. assert(params[@"user"]!=nil);
  5076. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5077. NSData *data = [NSData dataWithContentsOfFile:path];
  5078. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5079. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5080. NSString *countryCode = nil;
  5081. NSString *countryCode_id = nil;
  5082. NSString *stateCode = nil;
  5083. NSString *city = nil;
  5084. NSString *zipCode = nil;
  5085. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5086. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5087. NSString *code_id = params[@"country"];
  5088. countryCode_id = code_id;
  5089. countryCode = [self countryCodeByid:code_id];
  5090. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5091. NSString *zip_code = params[@"zipcode"];
  5092. // 剔除全部为空格
  5093. int spaceCount = 0;
  5094. for (int i = 0; i < zip_code.length; i++) {
  5095. if ([zip_code characterAtIndex:i] == ' ') {
  5096. spaceCount++;
  5097. }
  5098. }
  5099. if (spaceCount == zip_code.length) {
  5100. zip_code = @"";
  5101. }
  5102. zipCode = zip_code;
  5103. if (zipCode.length > 0) {
  5104. countryCode_id = params[@"country"];
  5105. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5106. countryCode = [dic valueForKey:@"country_code"];
  5107. if (!countryCode) {
  5108. // countryCode = @"US";
  5109. NSString *code_id = params[@"country"];
  5110. countryCode = [self countryCodeByid:code_id];
  5111. }
  5112. stateCode = [dic valueForKey:@"state_code"];
  5113. if (!stateCode.length) {
  5114. stateCode = params[@"state"];
  5115. }
  5116. city = [dic valueForKey:@"city"];
  5117. if (!city.length) {
  5118. city = params[@"city"];
  5119. }
  5120. // zip code
  5121. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5122. [zipDic setValue:zipCode forKey:@"value"];
  5123. [section_0 setValue:zipDic forKey:@"item_11"];
  5124. } else {
  5125. NSString *code_id = params[@"country"];
  5126. countryCode = [self countryCodeByid:code_id];
  5127. stateCode = params[@"state"];
  5128. city = params[@"city"];
  5129. }
  5130. }
  5131. } else {
  5132. // default: US United States
  5133. countryCode = @"US";
  5134. countryCode_id = @"228";
  5135. }
  5136. // country
  5137. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5138. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5139. // state
  5140. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5141. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5142. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5143. // city
  5144. if (city) {
  5145. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5146. [cityDic setValue:city forKey:@"value"];
  5147. [section_0 setValue:cityDic forKey:@"item_13"];
  5148. }
  5149. // price type
  5150. NSDictionary *priceDic = [self offline_getPrice];
  5151. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5152. // contact type
  5153. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5154. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5155. // Sales Rep
  5156. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5157. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5158. [ret setValue:section_0 forKey:@"section_0"];
  5159. return [RAConvertor dict2data:ret];
  5160. }
  5161. #pragma mark save
  5162. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5163. NSString *addr = nil;
  5164. NSString *contact_name = nil;
  5165. NSString *companyName = [params objectForKey:@"company"];
  5166. if (companyName) {
  5167. companyName = [AESCrypt fastencrypt:companyName];
  5168. } else {
  5169. companyName = @"";
  5170. }
  5171. DebugLog(@"company");
  5172. companyName = [self translateSingleQuote:companyName];
  5173. NSString *addr1 = [params objectForKey:@"address"];
  5174. NSString *addr2 = [params objectForKey:@"address2"];
  5175. NSString *addr3 = [params objectForKey:@"address_3"];
  5176. NSString *addr4 = [params objectForKey:@"address_4"];
  5177. if (!addr2) {
  5178. addr2 = @"";
  5179. }
  5180. if (!addr3) {
  5181. addr3 = @"";
  5182. }
  5183. if (!addr4) {
  5184. addr4 = @"";
  5185. }
  5186. if (!addr1) {
  5187. addr1 = @"";
  5188. }
  5189. DebugLog(@"addr");
  5190. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5191. addr = [AESCrypt fastencrypt:addr];
  5192. addr = [self translateSingleQuote:addr];
  5193. if (addr1 && ![addr1 isEqualToString:@""]) {
  5194. addr1 = [AESCrypt fastencrypt:addr1];
  5195. }
  5196. addr1 = [self translateSingleQuote:addr1];
  5197. addr2 = [self translateSingleQuote:addr2];
  5198. addr3 = [self translateSingleQuote:addr3];
  5199. addr4 = [self translateSingleQuote:addr4];
  5200. NSString *country = [params objectForKey:@"country"];
  5201. if (country) {
  5202. country = [self countryNameByCountryCodeId:country];
  5203. } else {
  5204. country = @"";
  5205. }
  5206. DebugLog(@"country");
  5207. country = [self translateSingleQuote:country];
  5208. NSString *state = [params objectForKey:@"state"];
  5209. if (!state) {
  5210. state = @"";
  5211. }
  5212. DebugLog(@"state");
  5213. state = [self translateSingleQuote:state];
  5214. NSString *city = [params objectForKey:@"city"];
  5215. if (!city) {
  5216. city = @"";
  5217. }
  5218. city = [self translateSingleQuote:city];
  5219. NSString *zipcode = [params objectForKey:@"zipcode"];
  5220. if (!zipcode) {
  5221. zipcode = @"";
  5222. }
  5223. DebugLog(@"zip");
  5224. zipcode = [self translateSingleQuote:zipcode];
  5225. NSString *fistName = [params objectForKey:@"firstname"];
  5226. if (!fistName) {
  5227. fistName = @"";
  5228. }
  5229. NSString *lastName = [params objectForKey:@"lastname"];
  5230. if (!lastName) {
  5231. lastName = @"";
  5232. }
  5233. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5234. DebugLog(@"contact_name");
  5235. contact_name = [self translateSingleQuote:contact_name];
  5236. fistName = [self translateSingleQuote:fistName];
  5237. lastName = [self translateSingleQuote:lastName];
  5238. NSString *phone = [params objectForKey:@"phone"];
  5239. if (phone) {
  5240. phone = [AESCrypt fastencrypt:phone];
  5241. } else {
  5242. phone = @"";
  5243. }
  5244. DebugLog(@"PHONE");
  5245. phone = [self translateSingleQuote:phone];
  5246. NSString *fax = [params objectForKey:@"fax"];
  5247. if (!fax) {
  5248. fax = @"";
  5249. }
  5250. DebugLog(@"FAX");
  5251. fax = [self translateSingleQuote:fax];
  5252. NSString *email = [params objectForKey:@"email"];
  5253. if (!email) {
  5254. email = @"";
  5255. }
  5256. DebugLog(@"EMAIL:%@",email);
  5257. email = [self translateSingleQuote:email];
  5258. NSString *notes = [params objectForKey:@"contact_notes"];
  5259. if (!notes) {
  5260. notes = @"";
  5261. }
  5262. DebugLog(@"NOTE:%@",notes);
  5263. notes = [self translateSingleQuote:notes];
  5264. NSString *price = [params objectForKey:@"price_name"];
  5265. if (price) {
  5266. price = [self priceNameByPriceId:price];
  5267. } else {
  5268. price = @"";
  5269. }
  5270. DebugLog(@"PRICE");
  5271. price = [self translateSingleQuote:price];
  5272. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5273. if (salesRep) {
  5274. salesRep = [self salesRepCodeById:salesRep];
  5275. } else {
  5276. salesRep = @"";
  5277. }
  5278. salesRep = [self translateSingleQuote:salesRep];
  5279. NSString *img = [params objectForKey:@"business_card"];
  5280. NSArray *array = [img componentsSeparatedByString:@","];
  5281. NSString *img_0 = array[0];
  5282. if (!img_0) {
  5283. img_0 = @"";
  5284. }
  5285. img_0 = [self translateSingleQuote:img_0];
  5286. NSString *img_1 = array[1];
  5287. if (!img_1) {
  5288. img_1 = @"";
  5289. }
  5290. img_1 = [self translateSingleQuote:img_1];
  5291. NSString *img_2 = array[2];
  5292. if (!img_2) {
  5293. img_2 = @"";
  5294. }
  5295. img_2 = [self translateSingleQuote:img_2];
  5296. NSString *contact_id = [NSUUID UUID].UUIDString;
  5297. NSString *contact_type = [params objectForKey:@"type_name"];
  5298. if (!contact_type) {
  5299. contact_type = @"";
  5300. }
  5301. contact_type = [self translateSingleQuote:contact_type];
  5302. // 判断更新时是否为customer
  5303. if (update) {
  5304. contact_id = [params objectForKey:@"contact_id"];
  5305. if (!contact_id) {
  5306. contact_id = @"";
  5307. }
  5308. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5309. __block int customer = 0;
  5310. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5311. customer = sqlite3_column_int(stmt, 0);
  5312. }];
  5313. isCustomer = customer ? YES : NO;
  5314. }
  5315. NSMutableDictionary *sync_dic = [params mutableCopy];
  5316. if (isCustomer) {
  5317. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5318. } else {
  5319. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5320. }
  5321. NSString *sync_data = nil;
  5322. NSString *sql = nil;
  5323. if (update){
  5324. contact_id = [params objectForKey:@"contact_id"];
  5325. if (!contact_id) {
  5326. contact_id = @"";
  5327. }
  5328. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5329. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5330. sync_data = [RAConvertor dict2string:sync_dic];
  5331. sync_data = [self translateSingleQuote:sync_data];
  5332. 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];
  5333. } else {
  5334. contact_id = [self translateSingleQuote:contact_id];
  5335. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5336. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5337. sync_data = [RAConvertor dict2string:sync_dic];
  5338. sync_data = [self translateSingleQuote:sync_data];
  5339. 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];
  5340. }
  5341. int result = [iSalesDB execSql:sql];
  5342. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5343. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5344. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5345. }
  5346. #pragma mark save new contact
  5347. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5348. {
  5349. return [self offline_saveContact:params update:NO isCustomer:YES];
  5350. }
  5351. #pragma mark edit contact
  5352. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5353. {
  5354. assert(params[@"user"]!=nil);
  5355. // {
  5356. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5357. // password = 123456;
  5358. // user = EvanK;
  5359. // }
  5360. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5361. NSData *data = [NSData dataWithContentsOfFile:path];
  5362. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5363. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5364. NSString *countryCode = nil;
  5365. NSString *countryCode_id = nil;
  5366. NSString *stateCode = nil;
  5367. /*------contact infor------*/
  5368. __block NSString *country = nil;
  5369. __block NSString *company_name = nil;
  5370. __block NSString *contact_id = params[@"contact_id"];
  5371. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5372. __block NSString *zipcode = nil;
  5373. __block NSString *state = nil; // state_code
  5374. __block NSString *city = nil; //
  5375. __block NSString *firt_name,*last_name;
  5376. __block NSString *phone,*fax,*email;
  5377. __block NSString *notes,*price_type,*sales_rep;
  5378. __block NSString *img_0,*img_1,*img_2;
  5379. __block NSString *contact_type;
  5380. contact_id = [self translateSingleQuote:contact_id];
  5381. 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];
  5382. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5383. country = [self textAtColumn:0 statement:stmt]; // country name
  5384. company_name = [self textAtColumn:1 statement:stmt];
  5385. addr_1 = [self textAtColumn:2 statement:stmt];
  5386. addr_2 = [self textAtColumn:3 statement:stmt];
  5387. addr_3 = [self textAtColumn:4 statement:stmt];
  5388. addr_4 = [self textAtColumn:5 statement:stmt];
  5389. zipcode = [self textAtColumn:6 statement:stmt];
  5390. state = [self textAtColumn:7 statement:stmt]; // state code
  5391. city = [self textAtColumn:8 statement:stmt];
  5392. firt_name = [self textAtColumn:9 statement:stmt];
  5393. last_name = [self textAtColumn:10 statement:stmt];
  5394. phone = [self textAtColumn:11 statement:stmt];
  5395. fax = [self textAtColumn:12 statement:stmt];
  5396. email = [self textAtColumn:13 statement:stmt];
  5397. notes = [self textAtColumn:14 statement:stmt];
  5398. price_type = [self textAtColumn:15 statement:stmt]; // name
  5399. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5400. img_0 = [self textAtColumn:17 statement:stmt];
  5401. img_1 = [self textAtColumn:18 statement:stmt];
  5402. img_2 = [self textAtColumn:19 statement:stmt];
  5403. contact_type = [self textAtColumn:20 statement:stmt];
  5404. }];
  5405. // decrypt
  5406. if (company_name) {
  5407. company_name = [AESCrypt fastdecrypt:company_name];
  5408. }
  5409. if (addr_1) {
  5410. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5411. }
  5412. if (phone) {
  5413. phone = [AESCrypt fastdecrypt:phone];
  5414. }
  5415. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5416. countryCode = [iSalesDB jk_queryText:countrySql];
  5417. stateCode = state;
  5418. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5419. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5420. NSString *code_id = params[@"country"];
  5421. countryCode_id = code_id;
  5422. countryCode = [self countryCodeByid:code_id];
  5423. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5424. NSString *zip_code = params[@"zipcode"];
  5425. // 剔除全部为空格
  5426. int spaceCount = 0;
  5427. for (int i = 0; i < zip_code.length; i++) {
  5428. if ([zip_code characterAtIndex:i] == ' ') {
  5429. spaceCount++;
  5430. }
  5431. }
  5432. if (spaceCount == zip_code.length) {
  5433. zip_code = @"";
  5434. }
  5435. if (zipcode.length > 0) {
  5436. zipcode = zip_code;
  5437. countryCode_id = params[@"country"];
  5438. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5439. countryCode = [dic valueForKey:@"country_code"];
  5440. if (!countryCode) {
  5441. NSString *code_id = params[@"country"];
  5442. countryCode = [self countryCodeByid:code_id];
  5443. }
  5444. stateCode = [dic valueForKey:@"state_code"];
  5445. if (!stateCode.length) {
  5446. stateCode = params[@"state"];
  5447. }
  5448. city = [dic valueForKey:@"city"];
  5449. if (!city.length) {
  5450. city = params[@"city"];
  5451. }
  5452. // zip code
  5453. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5454. [zipDic setValue:zipcode forKey:@"value"];
  5455. [section_0 setValue:zipDic forKey:@"item_8"];
  5456. } else {
  5457. NSString *code_id = params[@"country"];
  5458. countryCode = [self countryCodeByid:code_id];
  5459. stateCode = params[@"state"];
  5460. city = params[@"city"];
  5461. }
  5462. }
  5463. }
  5464. // 0 Country
  5465. // 1 Company Name
  5466. // 2 Contact ID
  5467. // 3 Picture
  5468. // 4 Address 1
  5469. // 5 Address 2
  5470. // 6 Address 3
  5471. // 7 Address 4
  5472. // 8 Zip Code
  5473. // 9 State/Province
  5474. // 10 City
  5475. // 11 Contact First Name
  5476. // 12 Contact Last Name
  5477. // 13 Phone
  5478. // 14 Fax
  5479. // 15 Email
  5480. // 16 Contact Notes
  5481. // 17 Price Type
  5482. // 18 Contact Type
  5483. // 19 Sales Rep
  5484. // country
  5485. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5486. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5487. // company
  5488. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5489. // contact_id
  5490. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5491. // picture
  5492. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5493. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5494. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5495. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5496. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5497. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5498. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5499. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5500. // addr 1 2 3 4
  5501. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5502. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5503. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5504. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5505. // zip code
  5506. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5507. // state
  5508. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5509. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5510. // city
  5511. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5512. // first last
  5513. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5514. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5515. // phone fax email
  5516. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5517. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5518. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5519. // notes
  5520. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5521. // price
  5522. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5523. for (NSString *key in priceDic.allKeys) {
  5524. if ([key containsString:@"val_"]) {
  5525. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5526. if ([dic[@"value"] isEqualToString:price_type]) {
  5527. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5528. [priceDic setValue:dic forKey:key];
  5529. }
  5530. }
  5531. }
  5532. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5533. // Contact Rep
  5534. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5535. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5536. // Sales Rep
  5537. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5538. for (NSString *key in repDic.allKeys) {
  5539. if ([key containsString:@"val_"]) {
  5540. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5541. NSString *value = dic[@"value"];
  5542. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5543. if (code && [code isEqualToString:sales_rep]) {
  5544. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5545. [repDic setValue:dic forKey:key];
  5546. }
  5547. }
  5548. }
  5549. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5550. [ret setValue:section_0 forKey:@"section_0"];
  5551. return [RAConvertor dict2data:ret];
  5552. }
  5553. #pragma mark save contact
  5554. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5555. {
  5556. return [self offline_saveContact:params update:YES isCustomer:YES];
  5557. }
  5558. #pragma mark category
  5559. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5560. if (ck) {
  5561. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5562. for (NSString *key in res.allKeys) {
  5563. if (![key isEqualToString:@"count"]) {
  5564. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5565. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5566. if ([val[@"value"] isEqualToString:ck]) {
  5567. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5568. }
  5569. [res setValue:val forKey:key];
  5570. }
  5571. }
  5572. [dic setValue:res forKey:valueKey];
  5573. }
  5574. }
  5575. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5576. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5577. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5578. NSString* category = [params valueForKey:@"category"];
  5579. if (!category || [category isEqualToString:@""]) {
  5580. category = @"%";
  5581. }
  5582. category = [self translateSingleQuote:category];
  5583. int limit = [[params valueForKey:@"limit"] intValue];
  5584. int offset = [[params valueForKey:@"offset"] intValue];
  5585. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5586. NSString *limit_str = @"";
  5587. if (limited) {
  5588. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5589. }
  5590. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5591. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5592. sqlite3 *db = [iSalesDB get_db];
  5593. // [iSalesDB AddExFunction:db];
  5594. int count;
  5595. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5596. 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];
  5597. double price_min = 0;
  5598. double price_max = 0;
  5599. if ([params.allKeys containsObject:@"alert"]) {
  5600. // alert
  5601. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5602. NSString *alert = params[@"alert"];
  5603. if ([alert isEqualToString:@"Display All"]) {
  5604. alert = [NSString stringWithFormat:@""];
  5605. } else {
  5606. alert = [self translateSingleQuote:alert];
  5607. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5608. }
  5609. // available
  5610. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5611. NSString *available = params[@"available"];
  5612. NSString *available_condition;
  5613. if ([available isEqualToString:@"Display All"]) {
  5614. available_condition = @"";
  5615. } else if ([available isEqualToString:@"Available Now"]) {
  5616. available_condition = @"and availability > 0";
  5617. } else {
  5618. available_condition = @"and availability == 0";
  5619. }
  5620. // best seller
  5621. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5622. NSString *best_seller = @"";
  5623. NSString *order_best_seller = @"m.name asc";
  5624. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5625. best_seller = @"and best_seller > 0";
  5626. order_best_seller = @"m.best_seller desc,m.name asc";
  5627. }
  5628. // price
  5629. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5630. NSString *price = params[@"price"];
  5631. price_min = 0;
  5632. price_max = MAXFLOAT;
  5633. if (params[@"user"] && price != nil) {
  5634. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5635. NSMutableString *priceName = [NSMutableString string];
  5636. for (int i = 0; i < priceTypeArray.count; i++) {
  5637. NSString *pricetype = priceTypeArray[i];
  5638. pricetype = [self translateSingleQuote:pricetype];
  5639. if (i == 0) {
  5640. [priceName appendFormat:@"'%@'",pricetype];
  5641. } else {
  5642. [priceName appendFormat:@",'%@'",pricetype];
  5643. }
  5644. }
  5645. if ([price isEqualToString:@"Display All"]) {
  5646. price = [NSString stringWithFormat:@""];
  5647. } else if([price containsString:@"+"]){
  5648. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5649. price_min = [price doubleValue];
  5650. 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];
  5651. } else {
  5652. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5653. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5654. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5655. 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];
  5656. }
  5657. } else {
  5658. price = @"";
  5659. }
  5660. // sold_by_qty : Sold in quantities of %@
  5661. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5662. NSString *qty = params[@"sold_by_qty"];
  5663. if ([qty isEqualToString:@"Display All"]) {
  5664. qty = @"";
  5665. } else {
  5666. qty = [self translateSingleQuote:qty];
  5667. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5668. }
  5669. // model name;
  5670. NSString* modelname =params[@"modelName"];
  5671. if(modelname.length==0)
  5672. {
  5673. modelname=@"";
  5674. }
  5675. else
  5676. {
  5677. modelname = modelname.lowercaseString;
  5678. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5679. }
  5680. //modelDescription
  5681. NSString* modelDescription =params[@"modelDescription"];
  5682. if(modelDescription.length==0)
  5683. {
  5684. modelDescription=@"";
  5685. }
  5686. else
  5687. {
  5688. modelDescription = modelDescription.lowercaseString;
  5689. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5690. }
  5691. // cate
  5692. // category = [self translateSingleQuote:category];
  5693. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5694. // cate mutiple selection
  5695. NSString *category_id = params[@"category"];
  5696. NSMutableArray *cate_id_array = nil;
  5697. NSMutableString *cateWhere = [NSMutableString string];
  5698. if ([category_id isEqualToString:@""] || !category_id) {
  5699. [cateWhere appendString:@"1 = 1"];
  5700. } else {
  5701. if ([category_id containsString:@","]) {
  5702. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5703. } else {
  5704. cate_id_array = [@[category_id] mutableCopy];
  5705. }
  5706. [cateWhere appendString:@"("];
  5707. for (int i = 0; i < cate_id_array.count; i++) {
  5708. if (i == 0) {
  5709. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5710. } else {
  5711. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5712. }
  5713. }
  5714. [cateWhere appendString:@")"];
  5715. }
  5716. // where bestseller > 0 order by bestseller desc
  5717. // sql query: alert availability(int) best_seller(int) price qty
  5718. 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];
  5719. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5720. }
  5721. DebugLog(@"offline category where: %@",where);
  5722. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5723. if (!params[@"user"]) {
  5724. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5725. }
  5726. [ret setValue:filter forKey:@"filter"];
  5727. DebugLog(@"offline_category sql:%@",sqlQuery);
  5728. sqlite3_stmt * statement;
  5729. [ret setValue:@"2" forKey:@"result"];
  5730. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5731. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5732. // int count=0;
  5733. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5734. {
  5735. int i=0;
  5736. while (sqlite3_step(statement) == SQLITE_ROW)
  5737. {
  5738. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5739. char *name = (char*)sqlite3_column_text(statement, 0);
  5740. if(name==nil)
  5741. name="";
  5742. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5743. char *description = (char*)sqlite3_column_text(statement, 1);
  5744. if(description==nil)
  5745. description="";
  5746. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5747. int product_id = sqlite3_column_int(statement, 2);
  5748. int wid = sqlite3_column_int(statement, 3);
  5749. int closeout = sqlite3_column_int(statement, 4);
  5750. int cid = sqlite3_column_int(statement, 5);
  5751. int wisdelete = sqlite3_column_int(statement, 6);
  5752. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5753. int more_color = sqlite3_column_int(statement, 8);
  5754. // Defaul Category ID
  5755. __block NSString *categoryID = nil;
  5756. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5757. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5758. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5759. if(default_category==nil)
  5760. default_category="";
  5761. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5762. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5763. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5764. categoryID = nsdefault_category;
  5765. }];
  5766. if (!categoryID.length) {
  5767. NSString *cateIDs = params[@"category"];
  5768. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5769. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5770. for (NSString *cateID in requestCategoryArr) {
  5771. BOOL needBreak = NO;
  5772. for (NSString *itemCateIDBox in itemCategoryArr) {
  5773. if (itemCateIDBox.length > 4) {
  5774. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5775. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5776. if ([itemCategoryID isEqualToString:cateID]) {
  5777. needBreak = YES;
  5778. categoryID = itemCategoryID;
  5779. break;
  5780. }
  5781. }
  5782. }
  5783. if (needBreak) {
  5784. break;
  5785. }
  5786. }
  5787. }
  5788. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5789. if(wid !=0 && wisdelete != 1)
  5790. [item setValue:@"true" forKey:@"wish_exists"];
  5791. else
  5792. [item setValue:@"false" forKey:@"wish_exists"];
  5793. if(closeout==0)
  5794. [item setValue:@"false" forKey:@"is_closeout"];
  5795. else
  5796. [item setValue:@"true" forKey:@"is_closeout"];
  5797. if(cid==0)
  5798. [item setValue:@"false" forKey:@"cart_exists"];
  5799. else
  5800. [item setValue:@"true" forKey:@"cart_exists"];
  5801. if (more_color == 0) {
  5802. [item setObject:@(false) forKey:@"more_color"];
  5803. } else if (more_color == 1) {
  5804. [item setObject:@(true) forKey:@"more_color"];
  5805. }
  5806. [item addEntriesFromDictionary:imgjson];
  5807. // [item setValue:nsurl forKey:@"img"];
  5808. [item setValue:nsname forKey:@"name"];
  5809. [item setValue:nsdescription forKey:@"description"];
  5810. if (categoryID) {
  5811. [item setValue:categoryID forKey:@"item_category_id"];
  5812. }
  5813. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5814. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5815. i++;
  5816. }
  5817. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5818. [ret setObject:items forKey:@"items"];
  5819. sqlite3_finalize(statement);
  5820. } else {
  5821. DebugLog(@"nothing...");
  5822. }
  5823. DebugLog(@"count:%d",count);
  5824. [iSalesDB close_db:db];
  5825. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5826. return ret;
  5827. }
  5828. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5829. {
  5830. return [self categoryList:params limited:YES];
  5831. }
  5832. # pragma mark item search
  5833. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5834. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5835. // assert(params[@"order_code"]);
  5836. // params[@"user"]
  5837. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5838. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5839. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5840. // category
  5841. NSDictionary *category_menu = [self offline_category_menu];
  5842. [filter setValue:category_menu forKey:@"category"];
  5843. NSString* where= nil;
  5844. NSString* orderby= @"m.name";
  5845. if (!filterSearch) {
  5846. int covertype = [[params valueForKey:@"covertype"] intValue];
  5847. switch (covertype) {
  5848. case 0:
  5849. {
  5850. where=@"m.category like'%%#005#%%'";
  5851. break;
  5852. }
  5853. case 1:
  5854. {
  5855. where=@"m.alert like '%QS%'";
  5856. break;
  5857. }
  5858. case 2:
  5859. {
  5860. where=@"m.availability>0";
  5861. break;
  5862. }
  5863. case 3:
  5864. {
  5865. where=@"m.best_seller>0";
  5866. orderby=@"m.best_seller desc,m.name asc";
  5867. break;
  5868. }
  5869. default:
  5870. where=@"1=1";
  5871. break;
  5872. }
  5873. }
  5874. int limit = [[params valueForKey:@"limit"] intValue];
  5875. int offset = [[params valueForKey:@"offset"] intValue];
  5876. NSString *limit_str = @"";
  5877. if (limited) {
  5878. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5879. }
  5880. sqlite3 *db = [iSalesDB get_db];
  5881. // [iSalesDB AddExFunction:db];
  5882. int count;
  5883. NSString *sqlQuery = nil;
  5884. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5885. 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];
  5886. double price_min = 0;
  5887. double price_max = 0;
  5888. if (filterSearch) {
  5889. // alert
  5890. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5891. NSString *alert = params[@"alert"];
  5892. if ([alert isEqualToString:@"Display All"]) {
  5893. alert = [NSString stringWithFormat:@""];
  5894. } else {
  5895. alert = [self translateSingleQuote:alert];
  5896. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5897. }
  5898. // available
  5899. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5900. NSString *available = params[@"available"];
  5901. NSString *available_condition;
  5902. if ([available isEqualToString:@"Display All"]) {
  5903. available_condition = @"";
  5904. } else if ([available isEqualToString:@"Available Now"]) {
  5905. available_condition = @"and availability > 0";
  5906. } else {
  5907. available_condition = @"and availability == 0";
  5908. }
  5909. // best seller
  5910. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5911. NSString *best_seller = @"";
  5912. NSString *order_best_seller = @"m.name asc";
  5913. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5914. best_seller = @"and best_seller > 0";
  5915. order_best_seller = @"m.best_seller desc,m.name asc";
  5916. }
  5917. // price
  5918. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5919. NSString *price = params[@"price"];
  5920. price_min = 0;
  5921. price_max = MAXFLOAT;
  5922. if (params[@"user"]) {
  5923. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5924. NSMutableString *priceName = [NSMutableString string];
  5925. for (int i = 0; i < priceTypeArray.count; i++) {
  5926. NSString *pricetype = priceTypeArray[i];
  5927. pricetype = [self translateSingleQuote:pricetype];
  5928. if (i == 0) {
  5929. [priceName appendFormat:@"'%@'",pricetype];
  5930. } else {
  5931. [priceName appendFormat:@",'%@'",pricetype];
  5932. }
  5933. }
  5934. if ([price isEqualToString:@"Display All"]) {
  5935. price = [NSString stringWithFormat:@""];
  5936. } else if([price containsString:@"+"]){
  5937. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5938. price_min = [price doubleValue];
  5939. 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];
  5940. } else {
  5941. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5942. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5943. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5944. 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];
  5945. }
  5946. } else {
  5947. price = @"";
  5948. }
  5949. // sold_by_qty : Sold in quantities of %@
  5950. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5951. NSString *qty = params[@"sold_by_qty"];
  5952. if ([qty isEqualToString:@"Display All"]) {
  5953. qty = @"";
  5954. } else {
  5955. qty = [self translateSingleQuote:qty];
  5956. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5957. }
  5958. // model name;
  5959. NSString* modelname =params[@"modelName"];
  5960. if(modelname.length==0)
  5961. {
  5962. modelname=@"";
  5963. }
  5964. else
  5965. {
  5966. modelname = modelname.lowercaseString;
  5967. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5968. }
  5969. //modelDescription
  5970. NSString* modelDescription =params[@"modelDescription"];
  5971. if(modelDescription.length==0)
  5972. {
  5973. modelDescription=@"";
  5974. }
  5975. else
  5976. {
  5977. modelDescription = modelDescription.lowercaseString;
  5978. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5979. }
  5980. //lower(description) like'%%%@%%'
  5981. // category
  5982. NSString *category_id = params[@"ctgId"];
  5983. NSMutableArray *cate_id_array = nil;
  5984. NSMutableString *cateWhere = [NSMutableString string];
  5985. if ([category_id isEqualToString:@""] || !category_id) {
  5986. [cateWhere appendString:@"1 = 1"];
  5987. } else {
  5988. if ([category_id containsString:@","]) {
  5989. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5990. } else {
  5991. cate_id_array = [@[category_id] mutableCopy];
  5992. }
  5993. /*-----------*/
  5994. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5995. [cateWhere appendString:@"("];
  5996. for (int i = 0; i < cate_id_array.count; i++) {
  5997. for (NSString *key0 in cateDic.allKeys) {
  5998. if ([key0 containsString:@"category_"]) {
  5999. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6000. for (NSString *key1 in category0.allKeys) {
  6001. if ([key1 containsString:@"category_"]) {
  6002. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6003. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6004. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6005. cate_id_array[i] = [category1 objectForKey:@"id"];
  6006. if (i == 0) {
  6007. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6008. } else {
  6009. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6010. }
  6011. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6012. [category0 setValue:category1 forKey:key1];
  6013. [cateDic setValue:category0 forKey:key0];
  6014. }
  6015. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6016. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6017. cate_id_array[i] = [category0 objectForKey:@"id"];
  6018. if (i == 0) {
  6019. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6020. } else {
  6021. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6022. }
  6023. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6024. [cateDic setValue:category0 forKey:key0];
  6025. }
  6026. }
  6027. }
  6028. }
  6029. }
  6030. }
  6031. [cateWhere appendString:@")"];
  6032. [filter setValue:cateDic forKey:@"category"];
  6033. }
  6034. // where bestseller > 0 order by bestseller desc
  6035. // sql query: alert availability(int) best_seller(int) price qty
  6036. 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];
  6037. // count
  6038. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6039. }
  6040. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6041. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6042. if (!params[@"user"]) {
  6043. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6044. }
  6045. [ret setValue:filter forKey:@"filter"];
  6046. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6047. sqlite3_stmt * statement;
  6048. [ret setValue:@"2" forKey:@"result"];
  6049. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6050. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6051. // int count=0;
  6052. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6053. {
  6054. int i=0;
  6055. while (sqlite3_step(statement) == SQLITE_ROW)
  6056. {
  6057. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6058. char *name = (char*)sqlite3_column_text(statement, 0);
  6059. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6060. char *description = (char*)sqlite3_column_text(statement, 1);
  6061. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6062. int product_id = sqlite3_column_int(statement, 2);
  6063. int wid = sqlite3_column_int(statement, 3);
  6064. int closeout = sqlite3_column_int(statement, 4);
  6065. int cid = sqlite3_column_int(statement, 5);
  6066. int wisdelete = sqlite3_column_int(statement, 6);
  6067. int more_color = sqlite3_column_int(statement, 7);
  6068. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6069. if(wid !=0 && wisdelete != 1)
  6070. [item setValue:@"true" forKey:@"wish_exists"];
  6071. else
  6072. [item setValue:@"false" forKey:@"wish_exists"];
  6073. if(closeout==0)
  6074. [item setValue:@"false" forKey:@"is_closeout"];
  6075. else
  6076. [item setValue:@"true" forKey:@"is_closeout"];
  6077. if(cid==0)
  6078. [item setValue:@"false" forKey:@"cart_exists"];
  6079. else
  6080. [item setValue:@"true" forKey:@"cart_exists"];
  6081. if (more_color == 0) {
  6082. [item setObject:@(false) forKey:@"more_color"];
  6083. } else if (more_color == 1) {
  6084. [item setObject:@(true) forKey:@"more_color"];
  6085. }
  6086. [item addEntriesFromDictionary:imgjson];
  6087. // [item setValue:nsurl forKey:@"img"];
  6088. [item setValue:nsname forKey:@"fash_name"];
  6089. [item setValue:nsdescription forKey:@"description"];
  6090. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6091. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6092. i++;
  6093. }
  6094. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6095. [ret setObject:items forKey:@"items"];
  6096. sqlite3_finalize(statement);
  6097. }
  6098. [iSalesDB close_db:db];
  6099. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6100. return ret;
  6101. }
  6102. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6103. {
  6104. return [self itemsearch:params limited:YES];
  6105. }
  6106. #pragma mark order detail
  6107. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6108. if (str1.length == 0) {
  6109. str1 = @"&nbsp";
  6110. }
  6111. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6112. return str;
  6113. }
  6114. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6115. {
  6116. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6117. [fromformatter setDateFormat:from];
  6118. NSDate *date = [fromformatter dateFromString:datetime];
  6119. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6120. [toformatter setDateFormat:to];
  6121. NSString * ret = [toformatter stringFromDate:date];
  6122. return ret;
  6123. }
  6124. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6125. // 把毫秒去掉
  6126. if ([dateTime containsString:@"."]) {
  6127. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6128. }
  6129. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6130. formatter.dateFormat = formate;
  6131. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6132. NSDate *date = [formatter dateFromString:dateTime];
  6133. formatter.dateFormat = newFormate;
  6134. NSString *result = [formatter stringFromDate:date];
  6135. return result ? result : @"";
  6136. }
  6137. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6138. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6139. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6140. }
  6141. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6142. {
  6143. assert(params[@"mode"]!=nil);
  6144. assert(params[@"user"]!=nil);
  6145. DebugLog(@"offline oderdetail params: %@",params);
  6146. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6147. int orderId = [params[@"orderId"] intValue];
  6148. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6149. // decrypt card number and card security code
  6150. // 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 ];
  6151. 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];
  6152. sqlite3 *db = [iSalesDB get_db];
  6153. sqlite3_stmt * statement;
  6154. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6155. NSString *nssoid = @"";
  6156. NSString* orderinfo = @"";
  6157. NSString *moreInfo = @"";
  6158. double handlingFee = 0;
  6159. double payments_and_credist = 0;
  6160. double totalPrice = 0;
  6161. double shippingFee = 0;
  6162. double lift_gate = 0;
  6163. NSString *customer_contact = @"";
  6164. NSString *customer_email = @"";
  6165. NSString *customer_fax = @"";
  6166. NSString *customer_phone = @"";
  6167. NSString *customer_city = @"";
  6168. NSString *customer_state = @"";
  6169. NSString *customer_zipcode = @"";
  6170. NSString *customer_country = @"";
  6171. BOOL must_call = NO;
  6172. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6173. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6174. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6175. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6176. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6177. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6178. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6179. {
  6180. if (sqlite3_step(statement) == SQLITE_ROW)
  6181. {
  6182. // int order_id = sqlite3_column_int(statement, 0);
  6183. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6184. // ret[@"sign_url"] = sign_url;
  6185. section_1 = @{
  6186. @"data":sign_url,
  6187. @"title":@"Signature",
  6188. @"type":@"sign_url"
  6189. }.mutableCopy;
  6190. [ret setObject:section_1 forKey:@"section_1"];
  6191. customer_contact = [self textAtColumn:52 statement:statement];
  6192. customer_email = [self textAtColumn:53 statement:statement];
  6193. customer_phone = [self textAtColumn:54 statement:statement];
  6194. customer_fax = [self textAtColumn:55 statement:statement];
  6195. customer_city = [self textAtColumn:60 statement:statement];
  6196. customer_state = [self textAtColumn:61 statement:statement];
  6197. customer_zipcode = [self textAtColumn:62 statement:statement];
  6198. customer_country = [self textAtColumn:63 statement:statement];
  6199. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6200. int offline_edit=sqlite3_column_int(statement, 56);
  6201. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6202. char *soid = (char*)sqlite3_column_text(statement, 1);
  6203. if(soid==nil)
  6204. soid= "";
  6205. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6206. // so#
  6207. ret[@"so#"] = nssoid;
  6208. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6209. if(poNumber==nil)
  6210. poNumber= "";
  6211. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6212. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6213. if(create_time==nil)
  6214. create_time= "";
  6215. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6216. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6217. int status = sqlite3_column_int(statement, 4);
  6218. int erpStatus = sqlite3_column_int(statement, 49);
  6219. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6220. // status
  6221. if (status > 1 && status != 3) {
  6222. status = erpStatus;
  6223. } else if (status == 3) {
  6224. status = 15;
  6225. }
  6226. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6227. ret[@"order_status"] = nsstatus;
  6228. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6229. if(company_name==nil)
  6230. company_name= "";
  6231. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6232. // company name
  6233. ret[@"company_name"] = nscompany_name;
  6234. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6235. if(customer_contact==nil)
  6236. customer_contact= "";
  6237. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6238. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6239. if(addr_1==nil)
  6240. addr_1="";
  6241. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6242. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6243. if(addr_2==nil)
  6244. addr_2="";
  6245. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6246. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6247. if(addr_3==nil)
  6248. addr_3="";
  6249. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6250. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6251. if(addr_4==nil)
  6252. addr_4="";
  6253. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6254. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6255. [arr_addr addObject:nsaddr_1];
  6256. [arr_addr addObject:nsaddr_2];
  6257. [arr_addr addObject:nsaddr_3];
  6258. [arr_addr addObject:nsaddr_4];
  6259. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6260. [arr_addr addObject:customer_state];
  6261. [arr_addr addObject:customer_zipcode];
  6262. [arr_addr addObject:customer_country];
  6263. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6264. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6265. char *logist = (char*)sqlite3_column_text(statement, 11);
  6266. if(logist==nil)
  6267. logist= "";
  6268. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6269. if (/*status == -11 || */status == 10 || status == 11) {
  6270. nslogist = [self textAtColumn:59 statement:statement];
  6271. };
  6272. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6273. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6274. shipping = @"Shipping To Be Quoted";
  6275. } else {
  6276. shippingFee = sqlite3_column_double(statement, 12);
  6277. }
  6278. // Shipping
  6279. // ret[@"Shipping"] = shipping;
  6280. NSDictionary *shipping_item = @{
  6281. @"title":@"Shipping",
  6282. @"value":shipping
  6283. };
  6284. [price_data setObject:shipping_item forKey:@"item_1"];
  6285. int have_lift_gate = sqlite3_column_int(statement, 17);
  6286. lift_gate = sqlite3_column_double(statement, 13);
  6287. // Liftgate Fee(No loading dock)
  6288. if (!have_lift_gate) {
  6289. lift_gate = 0;
  6290. }
  6291. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6292. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6293. if (sqlite3_column_int(statement, 57)) {
  6294. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6295. liftgate_value = @"Shipping To Be Quoted";
  6296. }
  6297. NSDictionary *liftgate_item = @{
  6298. @"title":@"Liftgate Fee(No loading dock)",
  6299. @"value":liftgate_value
  6300. };
  6301. [price_data setObject:liftgate_item forKey:@"item_2"];
  6302. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6303. // [ret removeObjectForKey:@"Shipping"];
  6304. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6305. // }
  6306. //
  6307. // if (have_lift_gate) {
  6308. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6309. // }
  6310. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6311. if(general_notes==nil)
  6312. general_notes= "";
  6313. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6314. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6315. if(internal_notes==nil)
  6316. internal_notes= "";
  6317. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6318. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6319. if(payment_type==nil)
  6320. payment_type= "";
  6321. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6322. // order info
  6323. orderinfo = [self textFileName:@"order_info.html"];
  6324. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6325. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6326. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6327. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6328. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6329. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6330. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6331. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6332. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6333. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6334. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6335. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6336. NSString *payment = nil;
  6337. // id -> show
  6338. __block NSString *show_pay_type = nspayment_type;
  6339. [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) {
  6340. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6341. if (show_typ_ch != NULL) {
  6342. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6343. }
  6344. }];
  6345. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6346. {
  6347. payment = [self textFileName:@"creditcardpayment.html"];
  6348. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6349. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6350. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6351. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6352. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6353. NSString *card_type = [self textAtColumn:42 statement:statement];
  6354. if (card_type.length > 0) { // 显示星号
  6355. card_type = @"****";
  6356. }
  6357. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6358. if (card_number.length > 0 && card_number.length > 4) {
  6359. for (int i = 0; i < card_number.length - 4; i++) {
  6360. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6361. }
  6362. } else {
  6363. card_number = @"";
  6364. }
  6365. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6366. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6367. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6368. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6369. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6370. card_expiration = @"****";
  6371. }
  6372. NSString *card_city = [self textAtColumn:46 statement:statement];
  6373. NSString *card_state = [self textAtColumn:47 statement:statement];
  6374. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6375. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6376. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6377. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6378. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6379. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6380. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6381. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6382. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6383. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6384. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6385. }
  6386. else
  6387. {
  6388. payment=[self textFileName:@"normalpayment.html"];
  6389. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6390. }
  6391. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6392. ret[@"result"]= [NSNumber numberWithInt:2];
  6393. // more info
  6394. moreInfo = [self textFileName:@"more_info.html"];
  6395. /*****ship to******/
  6396. // ShipToCompany_or_&nbsp
  6397. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6398. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6399. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6400. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6401. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6402. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6403. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6404. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6405. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6406. /*****ship from******/
  6407. // ShipFromCompany_or_&nbsp
  6408. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6409. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6410. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6411. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6412. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6413. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6414. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6415. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6416. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6417. /*****freight to******/
  6418. // FreightBillToCompany_or_&nbsp
  6419. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6420. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6421. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6422. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6423. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6424. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6425. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6426. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6427. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6428. /*****merchandise to******/
  6429. // MerchandiseBillToCompany_or_&nbsp
  6430. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6431. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6432. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6433. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6434. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6435. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6436. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6437. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6438. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6439. /*****return to******/
  6440. // ReturnToCompany_or_&nbsp
  6441. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6442. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6443. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6444. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6445. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6446. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6447. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6448. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6449. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6450. //
  6451. // DebugLog(@"more info : %@",moreInfo);
  6452. // handling fee
  6453. handlingFee = sqlite3_column_double(statement, 33);
  6454. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6455. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6456. if (sqlite3_column_int(statement, 58)) {
  6457. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6458. handling_fee_value = @"Shipping To Be Quoted";
  6459. }
  6460. NSDictionary *handling_fee_item = @{
  6461. @"title":@"Handling Fee",
  6462. @"value":handling_fee_value
  6463. };
  6464. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6465. //
  6466. // customer info
  6467. customerID = [self textAtColumn:36 statement:statement];
  6468. // mode
  6469. ret[@"mode"] = params[@"mode"];
  6470. // model_count
  6471. ret[@"model_count"] = @(0);
  6472. }
  6473. sqlite3_finalize(statement);
  6474. }
  6475. [iSalesDB close_db:db];
  6476. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6477. // "customer_email" = "Shui Hu";
  6478. // "customer_fax" = "";
  6479. // "customer_first_name" = F;
  6480. // "customer_last_name" = L;
  6481. // "customer_name" = ",da He Xiang Dong Liu A";
  6482. // "customer_phone" = "Hey Xuan Feng";
  6483. contactInfo[@"customer_phone"] = customer_phone;
  6484. contactInfo[@"customer_fax"] = customer_fax;
  6485. contactInfo[@"customer_email"] = customer_email;
  6486. NSString *first_name = @"";
  6487. NSString *last_name = @"";
  6488. if ([customer_contact isEqualToString:@""]) {
  6489. } else if ([customer_contact containsString:@" "]) {
  6490. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6491. first_name = [customer_contact substringToIndex:first_space_index];
  6492. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6493. }
  6494. contactInfo[@"customer_first_name"] = first_name;
  6495. contactInfo[@"customer_last_name"] = last_name;
  6496. ret[@"customerInfo"] = contactInfo;
  6497. // models
  6498. if (nssoid) {
  6499. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6500. __block double TotalCuft = 0;
  6501. __block double TotalWeight = 0;
  6502. __block int TotalCarton = 0;
  6503. __block double allItemPrice = 0;
  6504. 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];
  6505. sqlite3 *db1 = [iSalesDB get_db];
  6506. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6507. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6508. int product_id = sqlite3_column_int(stmt, 0);
  6509. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6510. int item_id = sqlite3_column_int(stmt, 7);
  6511. NSString* Price=nil;
  6512. if(str_price==nil)
  6513. {
  6514. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6515. if(price==nil)
  6516. Price=@"No Price.";
  6517. else
  6518. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6519. }
  6520. else
  6521. {
  6522. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6523. }
  6524. double discount = sqlite3_column_double(stmt, 2);
  6525. int item_count = sqlite3_column_int(stmt, 3);
  6526. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6527. NSString *nsline_note=nil;
  6528. if(line_note!=nil)
  6529. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6530. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6531. // NSString *nsname = nil;
  6532. // if(name!=nil)
  6533. // nsname= [[NSString alloc]initWithUTF8String:name];
  6534. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6535. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6536. // NSString *nsdescription=nil;
  6537. // if(description!=nil)
  6538. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6539. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6540. // int stockUom = sqlite3_column_int(stmt, 8);
  6541. // int _id = sqlite3_column_int(stmt, 9);
  6542. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6543. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6544. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6545. int carton=[bsubtotaljson[@"carton"] intValue];
  6546. TotalCuft += cuft;
  6547. TotalWeight += weight;
  6548. TotalCarton += carton;
  6549. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6550. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6551. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6552. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6553. itemjson[@"note"]=nsline_note;
  6554. itemjson[@"origin_price"] = Price;
  6555. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6556. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6557. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6558. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6559. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6560. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6561. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6562. if(itemjson[@"combine"] != nil)
  6563. {
  6564. // int citem=0;
  6565. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6566. for(int bc=0;bc<bcount;bc++)
  6567. {
  6568. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6569. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6570. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6571. subTotal += uprice * modulus * item_count;
  6572. }
  6573. }
  6574. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6575. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6576. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6577. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6578. itemjson[@"type"] = @"order_item";
  6579. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6580. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6581. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6582. allItemPrice += subTotal;
  6583. }];
  6584. section_3[@"data"] = model_data;
  6585. section_3[@"type"] = @"sub_order";
  6586. section_3[@"title"] = @"Models";
  6587. section_3[@"switch"] = @(false);
  6588. ret[@"section_3"] = section_3;
  6589. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6590. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6591. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6592. // payments/Credits
  6593. // payments_and_credist = sqlite3_column_double(statement, 34);
  6594. payments_and_credist = allItemPrice;
  6595. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6596. NSDictionary *sub_total_item = @{
  6597. @"title":@"Sub-Total",
  6598. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6599. };
  6600. [price_data setObject:sub_total_item forKey:@"item_0"];
  6601. // // total
  6602. // totalPrice = sqlite3_column_double(statement, 35);
  6603. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6604. } else {
  6605. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6606. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6607. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6608. // payments/Credits
  6609. payments_and_credist = 0;
  6610. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6611. NSDictionary *sub_total_item = @{
  6612. @"title":@"Sub-Total",
  6613. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6614. };
  6615. [price_data setObject:sub_total_item forKey:@"item_0"];
  6616. }
  6617. // total
  6618. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6619. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6620. NSDictionary *total_item = @{
  6621. @"title":@"Total",
  6622. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6623. };
  6624. [price_data setObject:total_item forKey:@"item_4"];
  6625. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6626. section_4 = @{
  6627. @"data":price_data,
  6628. @"title":@"Price Info",
  6629. @"type":@"price_info"
  6630. }.mutableCopy;
  6631. ret[@"section_4"] = section_4;
  6632. // ret[@"order_info"]= orderinfo;
  6633. section_0 = @{
  6634. @"data":orderinfo,
  6635. @"title":@"Order Info",
  6636. @"type":@"order_info"
  6637. }.mutableCopy;
  6638. ret[@"section_0"] = section_0;
  6639. // ret[@"more_order_info"] = moreInfo;
  6640. section_2 = @{
  6641. @"data":moreInfo,
  6642. @"title":@"More Info",
  6643. @"type":@"more_order_info"
  6644. }.mutableCopy;
  6645. ret[@"section_2"] = section_2;
  6646. ret[@"count"] = @(5);
  6647. return [RAConvertor dict2data:ret];
  6648. }
  6649. #pragma mark order list
  6650. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6651. NSString *nsstatus = @"";
  6652. switch (status) {
  6653. case 0:
  6654. {
  6655. nsstatus=@"Temp Order";
  6656. break;
  6657. }
  6658. case 1:
  6659. {
  6660. nsstatus=@"Saved Order";
  6661. break;
  6662. }
  6663. case 2: {
  6664. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6665. break;
  6666. }
  6667. case 3:
  6668. case 15:
  6669. {
  6670. nsstatus=@"Cancelled";
  6671. break;
  6672. }
  6673. case 10:
  6674. {
  6675. nsstatus=@"Quote Submitted";
  6676. break;
  6677. }
  6678. case 11:
  6679. {
  6680. nsstatus=@"Sales Order Submitted";
  6681. break;
  6682. }
  6683. case 12:
  6684. {
  6685. nsstatus=@"Processing";
  6686. break;
  6687. }
  6688. case 13:
  6689. {
  6690. nsstatus=@"Shipped";
  6691. break;
  6692. }
  6693. case 14:
  6694. {
  6695. nsstatus=@"Closed";
  6696. break;
  6697. }
  6698. case -11:
  6699. {
  6700. nsstatus = @"Ready For Submit";
  6701. break;
  6702. }
  6703. default:
  6704. break;
  6705. }
  6706. return nsstatus;
  6707. }
  6708. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  6709. {
  6710. double total = 0;
  6711. __block double payments_and_credist = 0;
  6712. __block double allItemPrice = 0;
  6713. // sqlite3 *db1 = [iSalesDB get_db];
  6714. if (so_id) {
  6715. // 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];
  6716. 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];
  6717. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6718. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6719. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6720. int product_id = sqlite3_column_int(stmt, 0);
  6721. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6722. int discount = sqlite3_column_int(stmt, 2);
  6723. int item_count = sqlite3_column_int(stmt, 3);
  6724. // int item_id = sqlite3_column_int(stmt, 7);
  6725. int item_id = sqlite3_column_int(stmt, 4);
  6726. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6727. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6728. NSString* Price=nil;
  6729. if(str_price==nil)
  6730. {
  6731. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  6732. if(price==nil)
  6733. Price=@"No Price.";
  6734. else
  6735. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6736. }
  6737. else
  6738. {
  6739. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6740. }
  6741. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  6742. itemjson[@"The unit price"]=Price;
  6743. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6744. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6745. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6746. if(itemjson[@"combine"] != nil)
  6747. {
  6748. // int citem=0;
  6749. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6750. for(int bc=0;bc<bcount;bc++)
  6751. {
  6752. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6753. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6754. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6755. subTotal += uprice * modulus * item_count;
  6756. }
  6757. }
  6758. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6759. allItemPrice += subTotal;
  6760. }];
  6761. payments_and_credist = allItemPrice;
  6762. } else {
  6763. // payments/Credits
  6764. payments_and_credist = 0;
  6765. }
  6766. // lift_gate handlingFee shippingFee
  6767. __block double lift_gate = 0;
  6768. __block double handlingFee = 0;
  6769. __block double shippingFee = 0;
  6770. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6771. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6772. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6773. if (have_lift_gate) {
  6774. lift_gate = sqlite3_column_double(stmt, 1);
  6775. }
  6776. handlingFee = sqlite3_column_double(stmt, 2);
  6777. shippingFee = sqlite3_column_double(stmt, 3);
  6778. }];
  6779. // total
  6780. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6781. if (close) {
  6782. [iSalesDB close_db:db1];
  6783. }
  6784. return total;
  6785. }
  6786. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6787. {
  6788. assert(params[@"user"]!=nil);
  6789. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6790. int limit = [[params valueForKey:@"limit"] intValue];
  6791. int offset = [[params valueForKey:@"offset"] intValue];
  6792. NSString* keyword = [params valueForKey:@"keyWord"];
  6793. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6794. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6795. NSString* where=@"1 = 1";
  6796. if(keyword.length>0)
  6797. 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]];
  6798. if (orderStatus.length > 0) {
  6799. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6800. if (order_status_array.count == 1) {
  6801. int status_value = [[order_status_array firstObject] intValue];
  6802. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6803. if (status_value == 15 || status_value == 3) {
  6804. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6805. } else {
  6806. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6807. }
  6808. } else {
  6809. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6810. }
  6811. } else if (order_status_array.count > 1) {
  6812. for (int i = 0; i < order_status_array.count;i++) {
  6813. NSString *status = order_status_array[i];
  6814. NSString *condition = @" or";
  6815. if (i == 0) {
  6816. condition = @" and (";
  6817. }
  6818. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6819. int status_value = [status intValue];
  6820. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6821. if (status_value == 15 || status_value == 3) {
  6822. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6823. } else {
  6824. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6825. }
  6826. } else {
  6827. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6828. }
  6829. }
  6830. where = [where stringByAppendingString:@" )"];
  6831. }
  6832. }
  6833. 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];
  6834. // DebugLog(@"order list sql: %@",sqlQuery);
  6835. sqlite3 *db = [iSalesDB get_db];
  6836. sqlite3_stmt * statement;
  6837. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6838. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6839. {
  6840. int count=0;
  6841. while (sqlite3_step(statement) == SQLITE_ROW)
  6842. {
  6843. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6844. int order_id = sqlite3_column_double(statement, 0);
  6845. char *soid = (char*)sqlite3_column_text(statement, 1);
  6846. if(soid==nil)
  6847. soid= "";
  6848. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6849. int status = sqlite3_column_double(statement, 2);
  6850. int erpStatus = sqlite3_column_double(statement, 9);
  6851. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6852. if(sales_rep==nil)
  6853. sales_rep= "";
  6854. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6855. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6856. if(create_by==nil)
  6857. create_by= "";
  6858. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6859. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6860. if(company_name==nil)
  6861. company_name= "";
  6862. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6863. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6864. if(create_time==nil)
  6865. create_time= "";
  6866. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6867. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6868. // double total_price = sqlite3_column_double(statement, 7);
  6869. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  6870. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6871. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6872. int offline_edit = sqlite3_column_int(statement, 10);
  6873. // ": "JH",
  6874. // "": "$8307.00",
  6875. // "": "MOB1608050001",
  6876. // "": "ArpithaT",
  6877. // "": "1st Stage Property Transformations",
  6878. // "": "JANICE SUTTON",
  6879. // "": 2255,
  6880. // "": "08/02/2016 09:49:18",
  6881. // "": 1,
  6882. // "": "Saved Order"
  6883. // "": "1470384050483",
  6884. // "model_count": "6 / 28"
  6885. item[@"sales_rep"]= nssales_rep;
  6886. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6887. item[@"so#"]= nssoid;
  6888. item[@"create_by"]= nscreate_by;
  6889. item[@"customer_name"]= nscompany_name;
  6890. item[@"customer_contact"] = customer_contact;
  6891. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6892. item[@"purchase_time"]= nscreate_time;
  6893. int statusCode = status;
  6894. if (statusCode == 2) {
  6895. statusCode = erpStatus;
  6896. } else if (statusCode == 3) {
  6897. statusCode = 15;
  6898. }
  6899. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6900. item[@"order_status"]= nsstatus;
  6901. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6902. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6903. // item[@"model_count"]
  6904. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6905. count++;
  6906. }
  6907. ret[@"count"]= [NSNumber numberWithInt:count];
  6908. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6909. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6910. ret[@"result"]= [NSNumber numberWithInt:2];
  6911. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6912. ret[@"time_zone"] = @"PST";
  6913. sqlite3_finalize(statement);
  6914. }
  6915. 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];
  6916. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6917. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6918. [iSalesDB close_db:db];
  6919. return [RAConvertor dict2data:ret];
  6920. }
  6921. #pragma mark update gnotes
  6922. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6923. {
  6924. DebugLog(@"params: %@",params);
  6925. // comments = Meyoyoyoyoyoyoy;
  6926. // orderCode = MOB1608110001;
  6927. // password = 123456;
  6928. // user = EvanK;
  6929. 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]];
  6930. int ret = [iSalesDB execSql:sql];
  6931. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6932. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6933. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6934. // [dic setValue:@"160409" forKey:@"min_ver"];
  6935. return [RAConvertor dict2data:dic];
  6936. }
  6937. #pragma mark move to wishlist
  6938. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6939. {
  6940. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6941. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6942. _id = [NSString stringWithFormat:@"(%@)",_id];
  6943. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6944. sqlite3 *db = [iSalesDB get_db];
  6945. __block NSString *product_id = @"";
  6946. __block NSString *item_count_str = @"";
  6947. // __block NSString *item_id = nil;
  6948. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6949. // product_id = [self textAtColumn:0 statement:stmt];
  6950. int item_count = sqlite3_column_int(stmt, 1);
  6951. // item_id = [self textAtColumn:2 statement:stmt];
  6952. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6953. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6954. if (p_id.length) {
  6955. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6956. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6957. }
  6958. }];
  6959. [iSalesDB close_db:db];
  6960. // 去除第一个,
  6961. if (product_id.length > 1) {
  6962. product_id = [product_id substringFromIndex:1];
  6963. }
  6964. if (item_count_str.length > 1) {
  6965. item_count_str = [item_count_str substringFromIndex:1];
  6966. }
  6967. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6968. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6969. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6970. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6971. sqlite3 *db1 = [iSalesDB get_db];
  6972. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6973. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6974. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6975. // 删除
  6976. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6977. int ret = [iSalesDB execSql:deleteSql db:db1];
  6978. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6979. [iSalesDB close_db:db1];
  6980. return [RAConvertor dict2data:dic];
  6981. }
  6982. #pragma mark cart delete
  6983. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6984. {
  6985. // cartItemId = 548;
  6986. // orderCode = MOB1608110001;
  6987. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6988. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6989. _id = [NSString stringWithFormat:@"(%@)",_id];
  6990. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6991. int ret = [iSalesDB execSql:sql];
  6992. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6993. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6994. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6995. // [dic setValue:@"160409" forKey:@"min_ver"];
  6996. return [RAConvertor dict2data:dic];
  6997. }
  6998. #pragma mark set price
  6999. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7000. {
  7001. DebugLog(@"cart set price params: %@",params);
  7002. // "cartitem_id" = 1;
  7003. // discount = "0.000000";
  7004. // "item_note" = "";
  7005. // price = "269.000000";
  7006. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7007. NSString *notes = [self valueInParams:params key:@"item_note"];
  7008. NSString *discount = [self valueInParams:params key:@"discount"];
  7009. NSString *price = [self valueInParams:params key:@"price"];
  7010. // bool badd_price_changed=false;
  7011. // sqlite3* db=[iSalesDB get_db];
  7012. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7013. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7014. // NSRange range;
  7015. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7016. //
  7017. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7018. // badd_price_changed=true;
  7019. // [iSalesDB close_db:db];
  7020. //
  7021. // if(badd_price_changed)
  7022. // {
  7023. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7024. // }
  7025. //
  7026. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7027. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  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 set line notes
  7036. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7037. {
  7038. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7039. NSString *notes = [self valueInParams:params key:@"notes"];
  7040. notes = [self translateSingleQuote:notes];
  7041. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7042. int ret = [iSalesDB execSql:sql];
  7043. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7044. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7045. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7046. // [dic setValue:@"160409" forKey:@"min_ver"];
  7047. return [RAConvertor dict2data:dic];
  7048. }
  7049. #pragma mark set qty
  7050. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7051. {
  7052. assert(params[@"can_create_backorder"]!=nil);
  7053. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7054. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7055. int item_count = [params[@"inputInt"] intValue];
  7056. // 购买检查数量大于库存
  7057. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7058. if (!params[@"can_create_backorder"]) {
  7059. 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];
  7060. __block BOOL out_of_stock = NO;
  7061. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7062. int availability = sqlite3_column_int(stmt, 0);
  7063. if (availability < item_count) {
  7064. out_of_stock = YES;
  7065. }
  7066. }];
  7067. if (out_of_stock) { // 缺货
  7068. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7069. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7070. return [RAConvertor dict2data:dic];
  7071. }
  7072. }
  7073. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7074. int ret = [iSalesDB execSql:sql];
  7075. __block int item_id = 0;
  7076. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7077. item_id = sqlite3_column_int(stmt, 0);
  7078. }];
  7079. sqlite3 *db = [iSalesDB get_db];
  7080. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7081. [iSalesDB close_db:db];
  7082. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7083. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7084. // [dic setValue:@"160409" forKey:@"min_ver"];
  7085. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7086. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7087. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7088. return [RAConvertor dict2data:dic];
  7089. }
  7090. #pragma mark place order
  7091. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7092. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7093. if (aname) {
  7094. [dic setValue:aname forKey:@"aname"];
  7095. }
  7096. if (control) {
  7097. [dic setValue:control forKey:@"control"];
  7098. }
  7099. if (keyboard) {
  7100. [dic setValue:keyboard forKey:@"keyboard"];
  7101. }
  7102. if (name) {
  7103. [dic setValue:name forKey:@"name"];
  7104. }
  7105. if (value) {
  7106. [dic setValue:value forKey:@"value"];
  7107. }
  7108. return dic;
  7109. }
  7110. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7111. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7112. orderCode = [self translateSingleQuote:orderCode];
  7113. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7114. 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];
  7115. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7116. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7117. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7118. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7119. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7120. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7121. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7122. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7123. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7124. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7125. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7126. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7127. NSString *email = [self textAtColumn:11 statement:stmt];
  7128. NSString *phone = [self textAtColumn:12 statement:stmt];
  7129. NSString *fax = [self textAtColumn:13 statement:stmt];
  7130. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7131. NSString *city = [self textAtColumn:15 statement:stmt];
  7132. NSString *state = [self textAtColumn:16 statement:stmt];
  7133. NSString *country = [self textAtColumn:17 statement:stmt];
  7134. NSString *name = [self textAtColumn:18 statement:stmt];
  7135. // contact id
  7136. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7137. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7138. [contact_id_dic setValue:@"text" forKey:@"control"];
  7139. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7140. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7141. [contact_id_dic setValue:@"true" forKey:@"required"];
  7142. [contact_id_dic setValue:contact_id forKey:@"value"];
  7143. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7144. // business card
  7145. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7146. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7147. [business_card_dic setValue:@"img" forKey:@"control"];
  7148. [business_card_dic setValue:@"1" forKey:@"disable"];
  7149. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7150. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7151. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7152. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7153. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7154. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7155. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7156. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7157. // fax
  7158. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7159. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7160. // zipcode
  7161. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7162. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7163. // city
  7164. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7165. [customer_dic setValue:city_dic forKey:@"item_12"];
  7166. // state
  7167. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7168. [customer_dic setValue:state_dic forKey:@"item_13"];
  7169. // country
  7170. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7171. [customer_dic setValue:country_dic forKey:@"item_14"];
  7172. // company name
  7173. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7174. [customer_dic setValue:company_dic forKey:@"item_2"];
  7175. // addr_1
  7176. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7177. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7178. // addr_2
  7179. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7180. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7181. // addr_3
  7182. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7183. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7184. // addr_4
  7185. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7186. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7187. // Contact
  7188. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7189. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7190. // email
  7191. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7192. [customer_dic setValue:email_dic forKey:@"item_8"];
  7193. // phone
  7194. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7195. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7196. // title
  7197. [customer_dic setValue:@"Customer" forKey:@"title"];
  7198. // count
  7199. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7200. }];
  7201. // setting
  7202. NSDictionary *setting = params[@"setting"];
  7203. NSNumber *hide = setting[@"CustomerHide"];
  7204. [customer_dic setValue:hide forKey:@"hide"];
  7205. return customer_dic;
  7206. }
  7207. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7208. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7209. // setting
  7210. NSDictionary *setting = params[@"setting"];
  7211. NSNumber *hide = setting[@"ShipToHide"];
  7212. [dic setValue:hide forKey:@"hide"];
  7213. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7214. orderCode = [self translateSingleQuote:orderCode];
  7215. 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];
  7216. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7217. NSString *cid = [self textAtColumn:0 statement:stmt];
  7218. NSString *name = [self textAtColumn:1 statement:stmt];
  7219. NSString *ext = [self textAtColumn:2 statement:stmt];
  7220. NSString *contact = [self textAtColumn:3 statement:stmt];
  7221. NSString *email = [self textAtColumn:4 statement:stmt];
  7222. NSString *fax = [self textAtColumn:5 statement:stmt];
  7223. NSString *phone = [self textAtColumn:6 statement:stmt];
  7224. // count
  7225. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7226. // title
  7227. [dic setValue:@"Ship To" forKey:@"title"];
  7228. // choose
  7229. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7230. [choose_dic setValue:@"choose" forKey:@"aname"];
  7231. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7232. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7233. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7234. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7235. @"receive_contact" : @"customer_contact",
  7236. @"receive_email" : @"customer_email",
  7237. @"receive_ext" : @"customer_contact_ext",
  7238. @"receive_fax" : @"customer_fax",
  7239. @"receive_name" : @"customer_name",
  7240. @"receive_phone" : @"customer_phone"},
  7241. @"refresh" : [NSNumber numberWithInteger:1],
  7242. @"type" : @"pull"};
  7243. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7244. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7245. @"name" : @"Add new address",
  7246. @"refresh" : [NSNumber numberWithInteger:1],
  7247. @"value" : @"new_addr"
  7248. };
  7249. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7250. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7251. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7252. @"receive_contact" : @"customer_contact",
  7253. @"receive_email" : @"customer_email",
  7254. @"receive_ext" : @"customer_contact_ext",
  7255. @"receive_fax" : @"customer_fax",
  7256. @"receive_name" : @"customer_name",
  7257. @"receive_phone" : @"customer_phone"},
  7258. @"name" : @"select_ship_to",
  7259. @"refresh" : [NSNumber numberWithInteger:1],
  7260. @"value" : @"Sales_Order_Ship_To"};
  7261. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7262. [dic setValue:choose_dic forKey:@"item_0"];
  7263. // contact id
  7264. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7265. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7266. [contact_id_dic setValue:@"true" forKey:@"required"];
  7267. [dic setValue:contact_id_dic forKey:@"item_1"];
  7268. // company name
  7269. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7270. [dic setValue:company_name_dic forKey:@"item_2"];
  7271. // address
  7272. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7273. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7274. if ([required integerValue]) {
  7275. [ext_dic setValue:@"true" forKey:@"required"];
  7276. }
  7277. [dic setValue:ext_dic forKey:@"item_3"];
  7278. // contact
  7279. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7280. [dic setValue:contact_dic forKey:@"item_4"];
  7281. // phone
  7282. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7283. [dic setValue:phone_dic forKey:@"item_5"];
  7284. // fax
  7285. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7286. [dic setValue:fax_dic forKey:@"item_6"];
  7287. // email
  7288. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7289. [dic setValue:email_dic forKey:@"item_7"];
  7290. }];
  7291. return dic;
  7292. }
  7293. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7294. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7295. // setting
  7296. NSDictionary *setting = params[@"setting"];
  7297. NSNumber *hide = setting[@"ShipFromHide"];
  7298. [dic setValue:hide forKey:@"hide"];
  7299. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7300. orderCode = [self translateSingleQuote:orderCode];
  7301. 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];
  7302. 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';";
  7303. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7304. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7305. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7306. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7307. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7308. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7309. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7310. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7311. if (!cid.length) {
  7312. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7313. cid = [self textAtColumn:0 statement:statment];
  7314. name = [self textAtColumn:1 statement:statment];
  7315. ext = [self textAtColumn:2 statement:statment];
  7316. contact = [self textAtColumn:3 statement:statment];
  7317. email = [self textAtColumn:4 statement:statment];
  7318. fax = [self textAtColumn:5 statement:statment];
  7319. phone = [self textAtColumn:6 statement:statment];
  7320. }];
  7321. }
  7322. // count
  7323. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7324. // title
  7325. [dic setValue:@"Ship From" forKey:@"title"];
  7326. // hide
  7327. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7328. // choose
  7329. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7330. [choose_dic setValue:@"choose" forKey:@"aname"];
  7331. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7332. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7333. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7334. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7335. @"sender_contact" : @"customer_contact",
  7336. @"sender_email" : @"customer_email",
  7337. @"sender_ext" : @"customer_contact_ext",
  7338. @"sender_fax" : @"customer_fax",
  7339. @"sender_name" : @"customer_name",
  7340. @"sender_phone" : @"customer_phone"},
  7341. @"name" : @"select_cid",
  7342. @"refresh" : [NSNumber numberWithInteger:0],
  7343. @"value" : @"Sales_Order_Ship_From"};
  7344. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7345. [dic setValue:choose_dic forKey:@"item_0"];
  7346. // contact id
  7347. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7348. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7349. [contact_id_dic setValue:@"true" forKey:@"required"];
  7350. [dic setValue:contact_id_dic forKey:@"item_1"];
  7351. // company name
  7352. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7353. [dic setValue:company_name_dic forKey:@"item_2"];
  7354. // address
  7355. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7356. [dic setValue:ext_dic forKey:@"item_3"];
  7357. // contact
  7358. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7359. [dic setValue:contact_dic forKey:@"item_4"];
  7360. // phone
  7361. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7362. [dic setValue:phone_dic forKey:@"item_5"];
  7363. // fax
  7364. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7365. [dic setValue:fax_dic forKey:@"item_6"];
  7366. // email
  7367. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7368. [dic setValue:email_dic forKey:@"item_7"];
  7369. }];
  7370. return dic;
  7371. }
  7372. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7373. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7374. // setting
  7375. NSDictionary *setting = params[@"setting"];
  7376. NSNumber *hide = setting[@"FreightBillToHide"];
  7377. [dic setValue:hide forKey:@"hide"];
  7378. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7379. orderCode = [self translateSingleQuote:orderCode];
  7380. 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];
  7381. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7382. NSString *cid = [self textAtColumn:0 statement:stmt];
  7383. NSString *name = [self textAtColumn:1 statement:stmt];
  7384. NSString *ext = [self textAtColumn:2 statement:stmt];
  7385. NSString *contact = [self textAtColumn:3 statement:stmt];
  7386. NSString *email = [self textAtColumn:4 statement:stmt];
  7387. NSString *fax = [self textAtColumn:5 statement:stmt];
  7388. NSString *phone = [self textAtColumn:6 statement:stmt];
  7389. // count
  7390. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7391. // title
  7392. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7393. // hide
  7394. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7395. // choose
  7396. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7397. [choose_dic setValue:@"choose" forKey:@"aname"];
  7398. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7399. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7400. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7401. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7402. @"shipping_billto_contact" : @"receive_contact",
  7403. @"shipping_billto_email" : @"receive_email",
  7404. @"shipping_billto_ext" : @"receive_ext",
  7405. @"shipping_billto_fax" : @"receive_fax",
  7406. @"shipping_billto_name" : @"receive_name",
  7407. @"shipping_billto_phone" : @"receive_phone"},
  7408. @"type" : @"pull"};
  7409. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7410. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7411. @"type" : @"pull",
  7412. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7413. @"shipping_billto_contact" : @"customer_contact",
  7414. @"shipping_billto_email" : @"customer_email",
  7415. @"shipping_billto_ext" : @"customer_contact_ext",
  7416. @"shipping_billto_fax" : @"customer_fax",
  7417. @"shipping_billto_name" : @"customer_name",
  7418. @"shipping_billto_phone" : @"customer_phone"}
  7419. };
  7420. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7421. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7422. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7423. @"shipping_billto_contact" : @"sender_contact",
  7424. @"shipping_billto_email" : @"sender_email",
  7425. @"shipping_billto_ext" : @"sender_ext",
  7426. @"shipping_billto_fax" : @"sender_fax",
  7427. @"shipping_billto_name" : @"sender_name",
  7428. @"shipping_billto_phone" : @"sender_phone"},
  7429. @"type" : @"pull"
  7430. };
  7431. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7432. NSDictionary *select_freight_bill_to_dic = @{
  7433. @"aname" : @"Select freight bill to",
  7434. @"name" : @"select_cid",
  7435. @"refresh" : [NSNumber numberWithInteger:0],
  7436. @"value" : @"Sales_Order_Freight_Bill_To",
  7437. @"key_map" : @{
  7438. @"shipping_billto_cid" : @"customer_cid",
  7439. @"shipping_billto_contact" : @"customer_contact",
  7440. @"shipping_billto_email" : @"customer_email",
  7441. @"shipping_billto_ext" : @"customer_contact_ext",
  7442. @"shipping_billto_fax" : @"customer_fax",
  7443. @"shipping_billto_name" : @"customer_name",
  7444. @"shipping_billto_phone" : @"customer_phone"
  7445. }
  7446. };
  7447. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7448. [dic setValue:choose_dic forKey:@"item_0"];
  7449. // contact id
  7450. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7451. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7452. [contact_id_dic setValue:@"true" forKey:@"required"];
  7453. [dic setValue:contact_id_dic forKey:@"item_1"];
  7454. // company name
  7455. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7456. [dic setValue:company_name_dic forKey:@"item_2"];
  7457. // address
  7458. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7459. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7460. if ([ext_required integerValue]) {
  7461. [ext_dic setValue:@"true" forKey:@"required"];
  7462. }
  7463. [dic setValue:ext_dic forKey:@"item_3"];
  7464. // contact
  7465. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7466. [dic setValue:contact_dic forKey:@"item_4"];
  7467. // phone
  7468. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7469. [dic setValue:phone_dic forKey:@"item_5"];
  7470. // fax
  7471. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7472. [dic setValue:fax_dic forKey:@"item_6"];
  7473. // email
  7474. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7475. [dic setValue:email_dic forKey:@"item_7"];
  7476. }];
  7477. return dic;
  7478. }
  7479. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7480. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7481. // setting
  7482. NSDictionary *setting = params[@"setting"];
  7483. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7484. [dic setValue:hide forKey:@"hide"];
  7485. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7486. orderCode = [self translateSingleQuote:orderCode];
  7487. 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];
  7488. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7489. NSString *cid = [self textAtColumn:0 statement:stmt];
  7490. NSString *name = [self textAtColumn:1 statement:stmt];
  7491. NSString *ext = [self textAtColumn:2 statement:stmt];
  7492. NSString *contact = [self textAtColumn:3 statement:stmt];
  7493. NSString *email = [self textAtColumn:4 statement:stmt];
  7494. NSString *fax = [self textAtColumn:5 statement:stmt];
  7495. NSString *phone = [self textAtColumn:6 statement:stmt];
  7496. // count
  7497. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7498. // title
  7499. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7500. // hide
  7501. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7502. // choose
  7503. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7504. [choose_dic setValue:@"choose" forKey:@"aname"];
  7505. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7506. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7507. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7508. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7509. @"billing_contact" : @"receive_contact",
  7510. @"billing_email" : @"receive_email",
  7511. @"billing_ext" : @"receive_ext",
  7512. @"billing_fax" : @"receive_fax",
  7513. @"billing_name" : @"receive_name",
  7514. @"billing_phone" : @"receive_phone"},
  7515. @"type" : @"pull"};
  7516. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7517. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7518. @"type" : @"pull",
  7519. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7520. @"billing_contact" : @"customer_contact",
  7521. @"billing_email" : @"customer_email",
  7522. @"billing_ext" : @"customer_contact_ext",
  7523. @"billing_fax" : @"customer_fax",
  7524. @"billing_name" : @"customer_name",
  7525. @"billing_phone" : @"customer_phone"}
  7526. };
  7527. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7528. NSDictionary *select_bill_to_dic = @{
  7529. @"aname" : @"Select bill to",
  7530. @"name" : @"select_cid",
  7531. @"refresh" : [NSNumber numberWithInteger:0],
  7532. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7533. @"key_map" : @{
  7534. @"billing_cid" : @"customer_cid",
  7535. @"billing_contact" : @"customer_contact",
  7536. @"billing_email" : @"customer_email",
  7537. @"billing_ext" : @"customer_contact_ext",
  7538. @"billing_fax" : @"customer_fax",
  7539. @"billing_name" : @"customer_name",
  7540. @"billing_phone" : @"customer_phone"
  7541. }
  7542. };
  7543. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7544. [dic setValue:choose_dic forKey:@"item_0"];
  7545. // contact id
  7546. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7547. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7548. [contact_id_dic setValue:@"true" forKey:@"required"];
  7549. [dic setValue:contact_id_dic forKey:@"item_1"];
  7550. // company name
  7551. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7552. [dic setValue:company_name_dic forKey:@"item_2"];
  7553. // address
  7554. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7555. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7556. if ([ext_required integerValue]) {
  7557. [ext_dic setValue:@"true" forKey:@"required"];
  7558. }
  7559. [dic setValue:ext_dic forKey:@"item_3"];
  7560. // contact
  7561. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7562. [dic setValue:contact_dic forKey:@"item_4"];
  7563. // phone
  7564. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7565. [dic setValue:phone_dic forKey:@"item_5"];
  7566. // fax
  7567. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7568. [dic setValue:fax_dic forKey:@"item_6"];
  7569. // email
  7570. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7571. [dic setValue:email_dic forKey:@"item_7"];
  7572. }];
  7573. return dic;
  7574. }
  7575. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7576. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7577. // setting
  7578. NSDictionary *setting = params[@"setting"];
  7579. NSNumber *hide = setting[@"ReturnToHide"];
  7580. [dic setValue:hide forKey:@"hide"];
  7581. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7582. orderCode = [self translateSingleQuote:orderCode];
  7583. 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];
  7584. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7585. NSString *cid = [self textAtColumn:0 statement:stmt];
  7586. NSString *name = [self textAtColumn:1 statement:stmt];
  7587. NSString *ext = [self textAtColumn:2 statement:stmt];
  7588. NSString *contact = [self textAtColumn:3 statement:stmt];
  7589. NSString *email = [self textAtColumn:4 statement:stmt];
  7590. NSString *fax = [self textAtColumn:5 statement:stmt];
  7591. NSString *phone = [self textAtColumn:6 statement:stmt];
  7592. // count
  7593. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7594. // title
  7595. [dic setValue:@"Return To" forKey:@"title"];
  7596. // hide
  7597. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7598. // choose
  7599. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7600. [choose_dic setValue:@"choose" forKey:@"aname"];
  7601. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7602. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7603. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7604. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7605. @"returnto_contact" : @"sender_contact",
  7606. @"returnto_email" : @"sender_email",
  7607. @"returnto_ext" : @"sender_ext",
  7608. @"returnto_fax" : @"sender_fax",
  7609. @"returnto_name" : @"sender_name",
  7610. @"returnto_phone" : @"sender_phone"},
  7611. @"type" : @"pull"};
  7612. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7613. NSDictionary *select_return_to_dic = @{
  7614. @"aname" : @"Select return to",
  7615. @"name" : @"select_cid",
  7616. @"refresh" : [NSNumber numberWithInteger:0],
  7617. @"value" : @"Contact_Return_To",
  7618. @"key_map" : @{
  7619. @"returnto_cid" : @"customer_cid",
  7620. @"returnto_contact" : @"customer_contact",
  7621. @"returnto_email" : @"customer_email",
  7622. @"returnto_ext" : @"customer_contact_ext",
  7623. @"returnto_fax" : @"customer_fax",
  7624. @"returnto_name" : @"customer_name",
  7625. @"returnto_phone" : @"customer_phone"
  7626. }
  7627. };
  7628. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7629. [dic setValue:choose_dic forKey:@"item_0"];
  7630. // contact id
  7631. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7632. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7633. [contact_id_dic setValue:@"true" forKey:@"required"];
  7634. [dic setValue:contact_id_dic forKey:@"item_1"];
  7635. // company name
  7636. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7637. [dic setValue:company_name_dic forKey:@"item_2"];
  7638. // address
  7639. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7640. [dic setValue:ext_dic forKey:@"item_3"];
  7641. // contact
  7642. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7643. [dic setValue:contact_dic forKey:@"item_4"];
  7644. // phone
  7645. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7646. [dic setValue:phone_dic forKey:@"item_5"];
  7647. // fax
  7648. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7649. [dic setValue:fax_dic forKey:@"item_6"];
  7650. // email
  7651. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7652. [dic setValue:email_dic forKey:@"item_7"];
  7653. }];
  7654. return dic;
  7655. }
  7656. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  7657. {
  7658. assert(params[@"user"]!=nil);
  7659. assert(params[@"contact_id"]!=nil);
  7660. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7661. orderCode = [self translateSingleQuote:orderCode];
  7662. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7663. __block double TotalCuft = 0;
  7664. __block double TotalWeight = 0;
  7665. __block int TotalCarton = 0;
  7666. __block double payments = 0;
  7667. // setting
  7668. NSDictionary *setting = params[@"setting"];
  7669. NSNumber *hide = setting[@"ModelInformationHide"];
  7670. [dic setValue:hide forKey:@"hide"];
  7671. 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];
  7672. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7673. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7674. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7675. // item id
  7676. int item_id = sqlite3_column_int(stmt, 0);
  7677. // count
  7678. int item_count = sqlite3_column_int(stmt, 1);
  7679. // stockUom
  7680. int stockUom = sqlite3_column_int(stmt, 2);
  7681. // unit price
  7682. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7683. NSString* Price=nil;
  7684. if([str_price isEqualToString:@""])
  7685. {
  7686. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  7687. if(price==nil)
  7688. Price=@"No Price.";
  7689. else
  7690. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7691. }
  7692. else
  7693. {
  7694. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7695. }
  7696. // discount
  7697. double discount = sqlite3_column_double(stmt, 4);
  7698. // name
  7699. NSString *name = [self textAtColumn:5 statement:stmt];
  7700. // description
  7701. NSString *description = [self textAtColumn:6 statement:stmt];
  7702. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7703. // line note
  7704. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7705. int avaulability = sqlite3_column_int(stmt, 8);
  7706. // img
  7707. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7708. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  7709. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7710. if(combine != nil)
  7711. {
  7712. // int citem=0;
  7713. int bcount=[[combine valueForKey:@"count"] intValue];
  7714. for(int bc=0;bc<bcount;bc++)
  7715. {
  7716. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7717. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7718. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7719. subTotal += uprice * modulus * item_count;
  7720. }
  7721. }
  7722. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7723. [model_dic setValue:@"model" forKey:@"control"];
  7724. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7725. [model_dic setValue:description forKey:@"description"];
  7726. [model_dic setValue:line_note forKey:@"note"];
  7727. [model_dic setValue:img forKey:@"img_url"];
  7728. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7729. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7730. [model_dic setValue:Price forKey:@"unit_price"];
  7731. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7732. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7733. if (combine) {
  7734. [model_dic setValue:combine forKey:@"combine"];
  7735. }
  7736. // well,what under the row is the info for total
  7737. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7738. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7739. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7740. int carton=[bsubtotaljson[@"carton"] intValue];
  7741. TotalCuft += cuft;
  7742. TotalWeight += weight;
  7743. TotalCarton += carton;
  7744. payments += subTotal;
  7745. //---------------------------
  7746. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7747. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7748. }];
  7749. [dic setValue:@"Model Information" forKey:@"title"];
  7750. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7751. return dic;
  7752. }
  7753. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7754. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7755. [dic setValue:@"Remarks Content" forKey:@"title"];
  7756. // setting
  7757. NSDictionary *setting = params[@"setting"];
  7758. NSNumber *hide = setting[@"RemarksContentHide"];
  7759. [dic setValue:hide forKey:@"hide"];
  7760. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7761. orderCode = [self translateSingleQuote:orderCode];
  7762. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7763. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7764. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7765. int mustCall = sqlite3_column_int(stmt, 1);
  7766. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7767. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7768. NSDictionary *po_dic = @{
  7769. @"aname" : @"PO#",
  7770. @"control" : @"edit",
  7771. @"keyboard" : @"text",
  7772. @"name" : @"poNumber",
  7773. @"value" : poNumber
  7774. };
  7775. NSDictionary *must_call_dic = @{
  7776. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7777. @"control" : @"switch",
  7778. @"name" : @"must_call",
  7779. @"value" : mustCall ? @"true" : @"false"
  7780. };
  7781. NSDictionary *general_notes_dic = @{
  7782. @"aname" : @"General notes",
  7783. @"control" : @"text_view",
  7784. @"keyboard" : @"text",
  7785. @"name" : @"comments",
  7786. @"value" : generalNotes
  7787. };
  7788. // NSDictionary *internal_notes_dic = @{
  7789. // @"aname" : @"Internal notes",
  7790. // @"control" : @"text_view",
  7791. // @"keyboard" : @"text",
  7792. // @"name" : @"internal_notes",
  7793. // @"value" : internalNotes
  7794. // };
  7795. [dic setValue:po_dic forKey:@"item_0"];
  7796. [dic setValue:must_call_dic forKey:@"item_1"];
  7797. [dic setValue:general_notes_dic forKey:@"item_2"];
  7798. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  7799. }];
  7800. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7801. return dic;
  7802. }
  7803. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7804. // params
  7805. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7806. orderCode = [self translateSingleQuote:orderCode];
  7807. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7808. // setting
  7809. NSDictionary *setting = params[@"setting"];
  7810. NSNumber *hide = setting[@"OrderTotalHide"];
  7811. [dic setValue:hide forKey:@"hide"];
  7812. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7813. __block double lift_gate_value = 0;
  7814. __block double handling_fee = 0;
  7815. __block double shipping = 0;
  7816. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7817. int lift_gate = sqlite3_column_int(stmt, 0);
  7818. lift_gate_value = sqlite3_column_double(stmt, 1);
  7819. shipping = sqlite3_column_double(stmt, 2);
  7820. handling_fee = sqlite3_column_double(stmt, 3);
  7821. if (!lift_gate) {
  7822. lift_gate_value = 0;
  7823. }
  7824. }];
  7825. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7826. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7827. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7828. double payments = [[total valueForKey:@"payments"] doubleValue];
  7829. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7830. double totalPrice = payments;
  7831. [dic setValue:@"Order Total" forKey:@"title"];
  7832. NSDictionary *payments_dic = @{
  7833. @"align" : @"right",
  7834. @"aname" : @"Payments/Credits",
  7835. @"control" : @"text",
  7836. @"name" : @"paymentsAndCredits",
  7837. @"type" : @"price",
  7838. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7839. };
  7840. [dic setValue:payments_dic forKey:@"item_0"];
  7841. // version 1.71 remove
  7842. // NSDictionary *handling_fee_dic = @{
  7843. // @"align" : @"right",
  7844. // @"aname" : @"Handling Fee",
  7845. // @"control" : @"text",
  7846. // @"name" : @"handling_fee_value",
  7847. // @"required" : @"true",
  7848. // @"type" : @"price",
  7849. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7850. // };
  7851. NSDictionary *shipping_dic = @{
  7852. @"align" : @"right",
  7853. @"aname" : @"Shipping*",
  7854. @"control" : @"text",
  7855. @"name" : @"shipping",
  7856. @"required" : @"true",
  7857. @"type" : @"price",
  7858. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7859. };
  7860. NSDictionary *lift_gate_dic = @{
  7861. @"align" : @"right",
  7862. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7863. @"control" : @"text",
  7864. @"name" : @"lift_gate_value",
  7865. @"required" : @"true",
  7866. @"type" : @"price",
  7867. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7868. };
  7869. int item_count = 1;
  7870. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7871. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7872. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7873. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7874. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7875. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7876. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7877. } else {
  7878. }
  7879. }
  7880. // version 1.71 remove
  7881. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7882. NSDictionary *total_price_dic = @{
  7883. @"align" : @"right",
  7884. @"aname" : @"Total",
  7885. @"control" : @"text",
  7886. @"name" : @"totalPrice",
  7887. @"type" : @"price",
  7888. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7889. };
  7890. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7891. NSDictionary *total_cuft_dic = @{
  7892. @"align" : @"right",
  7893. @"aname" : @"Total Cuft",
  7894. @"control" : @"text",
  7895. @"name" : @"",
  7896. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7897. };
  7898. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7899. NSDictionary *total_weight_dic = @{
  7900. @"align" : @"right",
  7901. @"aname" : @"Total Weight",
  7902. @"control" : @"text",
  7903. @"name" : @"",
  7904. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7905. };
  7906. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7907. NSDictionary *total_carton_dic = @{
  7908. @"align" : @"right",
  7909. @"aname" : @"Total Carton",
  7910. @"control" : @"text",
  7911. @"name" : @"",
  7912. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7913. };
  7914. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7915. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7916. return dic;
  7917. }
  7918. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7919. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7920. orderCode = [self translateSingleQuote:orderCode];
  7921. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7922. // setting
  7923. NSDictionary *setting = params[@"setting"];
  7924. NSNumber *hide = setting[@"SignatureHide"];
  7925. [dic setValue:hide forKey:@"hide"];
  7926. [dic setValue:@"Signature" forKey:@"title"];
  7927. __block NSString *pic_path = @"";
  7928. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7929. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7930. pic_path = [self textAtColumn:0 statement:stmt];
  7931. }];
  7932. NSDictionary *pic_dic = @{
  7933. @"aname" : @"Signature",
  7934. @"avalue" :pic_path,
  7935. @"control" : @"signature",
  7936. @"name" : @"sign_picpath",
  7937. @"value" : pic_path
  7938. };
  7939. [dic setValue:pic_dic forKey:@"item_0"];
  7940. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7941. return dic;
  7942. }
  7943. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7944. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7945. // setting
  7946. NSDictionary *setting = params[@"setting"];
  7947. NSNumber *hide = setting[@"ShippingMethodHide"];
  7948. [dic setValue:hide forKey:@"hide"];
  7949. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7950. orderCode = [self translateSingleQuote:orderCode];
  7951. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7952. __block NSString *logist = @"";
  7953. __block NSString *lift_gate = @"";
  7954. __block int lift_gate_integer = 0;
  7955. __block NSString *logistic_note = @"";
  7956. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7957. logist = [self textAtColumn:0 statement:stmt];
  7958. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7959. logistic_note = [self textAtColumn:2 statement:stmt];
  7960. }];
  7961. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7962. [dic setValue:@"Shipping Method" forKey:@"title"];
  7963. // val_0
  7964. int PERSONAL_PICK_UP_check = 0;
  7965. int USE_MY_CARRIER_check = 0;
  7966. NSString *logistic_note_text = @"";
  7967. int will_call_check = 0;
  7968. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7969. will_call_check = 1;
  7970. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7971. PERSONAL_PICK_UP_check = 1;
  7972. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7973. USE_MY_CARRIER_check = 1;
  7974. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7975. logistic_note = [logistic_note_array firstObject];
  7976. if (logistic_note_array.count > 1) {
  7977. logistic_note_text = [logistic_note_array lastObject];
  7978. }
  7979. }
  7980. }
  7981. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7982. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7983. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7984. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7985. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7986. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7987. NSDictionary *val0_subItem_item0 = @{
  7988. @"aname" : @"Option",
  7989. @"cadedate" : @{
  7990. @"count" : [NSNumber numberWithInteger:2],
  7991. @"val_0" : @{
  7992. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7993. @"refresh" : [NSNumber numberWithInteger:0],
  7994. @"value" : @"PERSONAL PICK UP",
  7995. @"value_id" : @"PERSONAL PICK UP"
  7996. },
  7997. @"val_1" : @{
  7998. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7999. @"refresh" : [NSNumber numberWithInteger:0],
  8000. @"sub_item" : @{
  8001. @"count" : [NSNumber numberWithInteger:1],
  8002. @"item_0" : @{
  8003. @"aname" : @"BOL",
  8004. @"control" : @"edit",
  8005. @"keyboard" : @"text",
  8006. @"name" : @"logist_note_text",
  8007. @"refresh" : [NSNumber numberWithInteger:0],
  8008. @"required" : @"false",
  8009. @"value" : logistic_note_text
  8010. }
  8011. },
  8012. @"value" : @"USE MY CARRIER",
  8013. @"value_id" : @"USE MY CARRIER"
  8014. }
  8015. },
  8016. @"control" : @"enum",
  8017. @"name" : @"logistic_note",
  8018. @"required" : @"true",
  8019. @"single_select" : @"true"
  8020. };
  8021. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8022. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8023. // val_1
  8024. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8025. if([lift_gate isEqualToString:@""]) {
  8026. if (lift_gate_integer == 1) {
  8027. lift_gate = @"true";
  8028. } else {
  8029. lift_gate = @"false";
  8030. }
  8031. }
  8032. int common_carrier_check = 0;
  8033. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8034. common_carrier_check = 1;
  8035. [params setValue:lift_gate forKey:@"lift_gate"];
  8036. }
  8037. NSDictionary *val_1 = @{
  8038. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8039. @"sub_item" : @{
  8040. @"count" : [NSNumber numberWithInteger:1],
  8041. @"item_0" : @{
  8042. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8043. @"control" : @"switch",
  8044. @"name" : @"lift_gate",
  8045. @"refresh" : [NSNumber numberWithInteger:1],
  8046. @"required" : @"true",
  8047. @"value" : lift_gate
  8048. }
  8049. },
  8050. @"value" : @"COMMON CARRIER",
  8051. @"value_id" : @"COMMON CARRIER"
  8052. };
  8053. // cadedate
  8054. NSDictionary *cadedate = @{
  8055. @"count" : [NSNumber numberWithInteger:2],
  8056. @"val_0" : val_0,
  8057. @"val_1" : val_1
  8058. };
  8059. // item_0
  8060. NSMutableDictionary *item_0 = @{
  8061. @"aname" : @"Shipping",
  8062. @"cadedate" : cadedate,
  8063. @"control" : @"enum",
  8064. @"name" : @"logist",
  8065. @"refresh" : [NSNumber numberWithInteger:1],
  8066. @"single_select" : @"true",
  8067. }.mutableCopy;
  8068. NSNumber *required = setting[@"ShippingMethodRequire"];
  8069. if ([required integerValue]) {
  8070. [item_0 setValue:@"true" forKey:@"required"];
  8071. }
  8072. [dic setValue:item_0 forKey:@"item_0"];
  8073. if ([logist isEqualToString:@"WILL CALL"]) {
  8074. [dic removeObjectForKey:@"lift_gate"];
  8075. }
  8076. return dic;
  8077. }
  8078. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8079. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8080. orderCode = [self translateSingleQuote:orderCode];
  8081. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8082. __block int submit_as_integer = 0;
  8083. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8084. submit_as_integer = sqlite3_column_int(stmt, 0);
  8085. }];
  8086. int check11 = 0;
  8087. int check10 = 0;
  8088. if (submit_as_integer == 11) {
  8089. check11 = 1;
  8090. }
  8091. if (submit_as_integer == 10) {
  8092. check10 = 1;
  8093. }
  8094. // section_0
  8095. NSMutableDictionary *dic = @{
  8096. @"count" : @(1),
  8097. @"item_0" : @{
  8098. @"aname" : @"Submit Order As",
  8099. @"cadedate" : @{
  8100. @"count" : @(2),
  8101. @"val_0" : @{
  8102. @"check" : [NSNumber numberWithInteger:check11],
  8103. @"value" : @"Sales Order",
  8104. @"value_id" : @(11)
  8105. },
  8106. @"val_1" : @{
  8107. @"check" : [NSNumber numberWithInteger:check10],
  8108. @"value" : @"Quote",
  8109. @"value_id" : @(10)
  8110. }
  8111. },
  8112. @"control" : @"enum",
  8113. @"name" : @"erpOrderStatus",
  8114. @"required" : @"true",
  8115. @"single_select" : @"true"
  8116. },
  8117. @"title" : @"Order Type"
  8118. }.mutableCopy;
  8119. // setting
  8120. NSDictionary *setting = params[@"setting"];
  8121. NSNumber *hide = setting[@"OrderTypeHide"];
  8122. [dic setValue:hide forKey:@"hide"];
  8123. return dic;
  8124. }
  8125. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8126. countryCode = [self translateSingleQuote:countryCode];
  8127. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8128. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8129. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8130. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8131. if (name == NULL) {
  8132. name = "";
  8133. }
  8134. if (code == NULL) {
  8135. code = "";
  8136. }
  8137. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8138. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8139. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8140. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8141. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8142. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8143. }
  8144. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8145. [container setValue:stateDic forKey:key];
  8146. }] mutableCopy];
  8147. [ret removeObjectForKey:@"result"];
  8148. // failure 可以不用了,一样的
  8149. if (ret.allKeys.count == 0) {
  8150. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8151. [stateDic setValue:@"Other" forKey:@"value"];
  8152. [stateDic setValue:@"" forKey:@"value_id"];
  8153. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8154. if (state_code && [@"" isEqualToString:state_code]) {
  8155. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8156. }
  8157. NSString *key = [NSString stringWithFormat:@"val_0"];
  8158. [ret setValue:stateDic forKey:key];
  8159. }
  8160. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8161. return ret;
  8162. }
  8163. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8164. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8165. orderCode = [self translateSingleQuote:orderCode];
  8166. 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];
  8167. __block NSString *payType = @"";
  8168. __block NSString *firstName = @"";
  8169. __block NSString *lastName = @"";
  8170. __block NSString *addr1 = @"";
  8171. __block NSString *addr2 = @"";
  8172. __block NSString *zipcode = @"";
  8173. __block NSString *cardType = @"";
  8174. __block NSString *cardNumber = @"";
  8175. __block NSString *securityCode = @"";
  8176. __block NSString *month = @"";
  8177. __block NSString *year = @"";
  8178. __block NSString *city = @"";
  8179. __block NSString *state = @"";
  8180. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8181. payType = [self textAtColumn:0 statement:stmt];
  8182. firstName = [self textAtColumn:1 statement:stmt];
  8183. lastName = [self textAtColumn:2 statement:stmt];
  8184. addr1 = [self textAtColumn:3 statement:stmt];
  8185. addr2 = [self textAtColumn:4 statement:stmt];
  8186. zipcode = [self textAtColumn:5 statement:stmt];
  8187. cardType = [self textAtColumn:6 statement:stmt];
  8188. cardNumber = [self textAtColumn:7 statement:stmt];
  8189. securityCode = [self textAtColumn:8 statement:stmt];
  8190. month = [self textAtColumn:9 statement:stmt];
  8191. year = [self textAtColumn:10 statement:stmt];
  8192. city = [self textAtColumn:11 statement:stmt];
  8193. state = [self textAtColumn:12 statement:stmt];
  8194. }];
  8195. NSString *required = @"true";
  8196. // setting
  8197. NSDictionary *setting = params[@"setting"];
  8198. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8199. if ([requiredNumber integerValue]) {
  8200. required = @"true";
  8201. } else {
  8202. required = @"false";
  8203. }
  8204. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8205. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8206. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8207. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8208. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8209. NSString *type = [self textAtColumn:1 statement:stmt];
  8210. NSMutableDictionary *val = @{
  8211. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8212. @"value" : type,
  8213. @"value_id" : type_id
  8214. }.mutableCopy;
  8215. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  8216. NSDictionary *sub_item = @{
  8217. @"count" : @(3),
  8218. @"item_0" : @{
  8219. @"aname" : @"choose",
  8220. @"control" : @"multi_action",
  8221. @"count" : @(1),
  8222. @"item_0" : @{
  8223. @"aname" : @"Same as customer",
  8224. @"key_map" : @{
  8225. @"credit_card_address1" : @"customer_address1",
  8226. @"credit_card_address2" : @"customer_address2",
  8227. @"credit_card_city" : @"customer_city",
  8228. @"credit_card_first_name" : @"customer_first_name",
  8229. @"credit_card_last_name" : @"customer_last_name",
  8230. @"credit_card_state" : @"customer_state",
  8231. @"credit_card_zipcode" : @"customer_zipcode"
  8232. },
  8233. @"type" : @"pull"
  8234. }
  8235. },
  8236. @"item_1" : @{
  8237. @"aname" : @"",
  8238. @"color" : @"red",
  8239. @"control" : @"text",
  8240. @"name" : @"",
  8241. @"value" : @"USA Credit cards only"
  8242. },
  8243. @"item_2" : @{
  8244. @"aname" : @"Fill",
  8245. @"cadedate" : @{
  8246. @"count" : @(2),
  8247. @"val_0" : @{
  8248. @"check" : @(1),
  8249. @"sub_item" : @{
  8250. @"count" : @(11),
  8251. @"item_0" : @{
  8252. @"aname" : @"Type",
  8253. @"cadedate" : @{
  8254. @"count" : @(2),
  8255. @"val_0" : @{
  8256. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8257. @"value" : @"VISA",
  8258. @"value_id" : @"VISA"/*@(0)*/
  8259. },
  8260. @"val_1" : @{
  8261. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8262. @"value" : @"MASTER CARD",
  8263. @"value_id" : @"MASTER CARD"/*@(1)*/
  8264. }
  8265. },
  8266. @"control" : @"enum",
  8267. @"name" : @"credit_card_type",
  8268. @"required" : @"true",
  8269. @"single_select" : @"true"
  8270. },
  8271. @"item_1" : @{
  8272. @"aname" : @"Number",
  8273. @"control" : @"edit",
  8274. @"keyboard" : @"int",
  8275. @"length" : @"16",
  8276. @"name" : @"credit_card_number",
  8277. @"required" : @"true",
  8278. @"value" : cardNumber
  8279. },
  8280. @"item_10" : @{
  8281. @"aname" : @"State",
  8282. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8283. @"control" : @"enum",
  8284. @"enum" : @"true",
  8285. @"name" : @"credit_card_state",
  8286. @"required" : @"true",
  8287. @"single_select" : @"true"
  8288. },
  8289. @"item_2" : @{
  8290. @"aname" : @"Expiration Date",
  8291. @"control" : @"monthpicker",
  8292. @"name" : @"credit_card_expiration",
  8293. @"required" : @"true",
  8294. @"type" : @"date",
  8295. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8296. },
  8297. @"item_3" : @{
  8298. @"aname" : @"Security Code",
  8299. @"control" : @"edit",
  8300. @"keyboard" : @"int",
  8301. @"length" : @"3",
  8302. @"name" : @"credit_card_security_code",
  8303. @"required" : @"true",
  8304. @"value" : securityCode
  8305. },
  8306. @"item_4" : @{
  8307. @"aname" : @"First Name",
  8308. @"control" : @"edit",
  8309. @"keyboard" : @"text",
  8310. @"name" : @"credit_card_first_name",
  8311. @"required" : @"true",
  8312. @"value" : firstName
  8313. },
  8314. @"item_5" : @{
  8315. @"aname" : @"Last Name",
  8316. @"control" : @"edit",
  8317. @"keyboard" : @"text",
  8318. @"name" : @"credit_card_last_name",
  8319. @"required" : @"true",
  8320. @"value" : lastName
  8321. },
  8322. @"item_6" : @{
  8323. @"aname" : @"Address 1",
  8324. @"control" : @"edit",
  8325. @"keyboard" : @"text",
  8326. @"name" : @"credit_card_address1",
  8327. @"required" : @"true",
  8328. @"value" : addr1
  8329. },
  8330. @"item_7" : @{
  8331. @"aname" : @"Address 2",
  8332. @"control" : @"edit",
  8333. @"keyboard" : @"text",
  8334. @"name" : @"credit_card_address2",
  8335. @"value" : addr2
  8336. },
  8337. @"item_8" : @{
  8338. @"aname" : @"zip code",
  8339. @"control" : @"edit",
  8340. @"keyboard" : @"text",
  8341. @"name" : @"credit_card_zipcode",
  8342. @"required" : @"true",
  8343. @"value" : zipcode
  8344. },
  8345. @"item_9" : @{
  8346. @"aname" : @"City",
  8347. @"control" : @"edit",
  8348. @"keyboard" : @"text",
  8349. @"name" : @"credit_card_city",
  8350. @"required" : @"true",
  8351. @"value" : city
  8352. }
  8353. },
  8354. @"value" : @"Fill Now",
  8355. @"value_id" : @""
  8356. },
  8357. @"val_1" : @{
  8358. @"check" : @(0),
  8359. @"value" : @"Fill Later",
  8360. @"value_id" : @""
  8361. }
  8362. },
  8363. @"control" : @"enum",
  8364. @"name" : @"",
  8365. @"single_select" : @"true"
  8366. }
  8367. };
  8368. [val setObject:sub_item forKey:@"sub_item"];
  8369. }
  8370. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8371. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8372. }];
  8373. // "section_2"
  8374. NSMutableDictionary *dic = @{
  8375. @"count" : @(1),
  8376. @"item_0" : @{
  8377. @"aname" : @"Payment",
  8378. @"required" : required,
  8379. @"cadedate" : cadedate,
  8380. // @{
  8381. // @"count" : @(6),
  8382. // @"val_3" : @{
  8383. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8384. // @"value" : @"Check",
  8385. // @"value_id" : @"Check"
  8386. // },
  8387. // @"val_2" : @{
  8388. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8389. // @"value" : @"Cash",
  8390. // @"value_id" : @"Cash"
  8391. // },
  8392. // @"val_1" : @{
  8393. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8394. // @"value" : @"NET 60",
  8395. // @"value_id" : @"NET 30"
  8396. // },
  8397. // @"val_4" : @{
  8398. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8399. // @"value" : @"Wire Transfer",
  8400. // @"value_id" : @"Wire Transfer"
  8401. // },
  8402. // @"val_0" : @{
  8403. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8404. // @"sub_item" : @{
  8405. // @"count" : @(3),
  8406. // @"item_0" : @{
  8407. // @"aname" : @"choose",
  8408. // @"control" : @"multi_action",
  8409. // @"count" : @(1),
  8410. // @"item_0" : @{
  8411. // @"aname" : @"Same as customer",
  8412. // @"key_map" : @{
  8413. // @"credit_card_address1" : @"customer_address1",
  8414. // @"credit_card_address2" : @"customer_address2",
  8415. // @"credit_card_city" : @"customer_city",
  8416. // @"credit_card_first_name" : @"customer_first_name",
  8417. // @"credit_card_last_name" : @"customer_last_name",
  8418. // @"credit_card_state" : @"customer_state",
  8419. // @"credit_card_zipcode" : @"customer_zipcode"
  8420. // },
  8421. // @"type" : @"pull"
  8422. // }
  8423. // },
  8424. // @"item_1" : @{
  8425. // @"aname" : @"",
  8426. // @"color" : @"red",
  8427. // @"control" : @"text",
  8428. // @"name" : @"",
  8429. // @"value" : @"USA Credit cards only"
  8430. // },
  8431. // @"item_2" : @{
  8432. // @"aname" : @"Fill",
  8433. // @"cadedate" : @{
  8434. // @"count" : @(2),
  8435. // @"val_0" : @{
  8436. // @"check" : @(1),
  8437. // @"sub_item" : @{
  8438. // @"count" : @(11),
  8439. // @"item_0" : @{
  8440. // @"aname" : @"Type",
  8441. // @"cadedate" : @{
  8442. // @"count" : @(2),
  8443. // @"val_0" : @{
  8444. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8445. // @"value" : @"VISA",
  8446. // @"value_id" : @(0)
  8447. // },
  8448. // @"val_1" : @{
  8449. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8450. // @"value" : @"MASTER CARD",
  8451. // @"value_id" : @(1)
  8452. // }
  8453. // },
  8454. // @"control" : @"enum",
  8455. // @"name" : @"credit_card_type",
  8456. // @"required" : @"true",
  8457. // @"single_select" : @"true"
  8458. // },
  8459. // @"item_1" : @{
  8460. // @"aname" : @"Number",
  8461. // @"control" : @"edit",
  8462. // @"keyboard" : @"int",
  8463. // @"length" : @"16",
  8464. // @"name" : @"credit_card_number",
  8465. // @"required" : @"true",
  8466. // @"value" : cardNumber
  8467. // },
  8468. // @"item_10" : @{
  8469. // @"aname" : @"State",
  8470. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8471. // @"control" : @"enum",
  8472. // @"enum" : @"true",
  8473. // @"name" : @"credit_card_state",
  8474. // @"required" : @"true",
  8475. // @"single_select" : @"true"
  8476. // },
  8477. // @"item_2" : @{
  8478. // @"aname" : @"Expiration Date",
  8479. // @"control" : @"monthpicker",
  8480. // @"name" : @"credit_card_expiration",
  8481. // @"required" : @"true",
  8482. // @"type" : @"date",
  8483. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8484. // },
  8485. // @"item_3" : @{
  8486. // @"aname" : @"Security Code",
  8487. // @"control" : @"edit",
  8488. // @"keyboard" : @"int",
  8489. // @"length" : @"3",
  8490. // @"name" : @"credit_card_security_code",
  8491. // @"required" : @"true",
  8492. // @"value" : securityCode
  8493. // },
  8494. // @"item_4" : @{
  8495. // @"aname" : @"First Name",
  8496. // @"control" : @"edit",
  8497. // @"keyboard" : @"text",
  8498. // @"name" : @"credit_card_first_name",
  8499. // @"required" : @"true",
  8500. // @"value" : firstName
  8501. // },
  8502. // @"item_5" : @{
  8503. // @"aname" : @"Last Name",
  8504. // @"control" : @"edit",
  8505. // @"keyboard" : @"text",
  8506. // @"name" : @"credit_card_last_name",
  8507. // @"required" : @"true",
  8508. // @"value" : lastName
  8509. // },
  8510. // @"item_6" : @{
  8511. // @"aname" : @"Address 1",
  8512. // @"control" : @"edit",
  8513. // @"keyboard" : @"text",
  8514. // @"name" : @"credit_card_address1",
  8515. // @"required" : @"true",
  8516. // @"value" : addr1
  8517. // },
  8518. // @"item_7" : @{
  8519. // @"aname" : @"Address 2",
  8520. // @"control" : @"edit",
  8521. // @"keyboard" : @"text",
  8522. // @"name" : @"credit_card_address2",
  8523. // @"value" : addr2
  8524. // },
  8525. // @"item_8" : @{
  8526. // @"aname" : @"zip code",
  8527. // @"control" : @"edit",
  8528. // @"keyboard" : @"text",
  8529. // @"name" : @"credit_card_zipcode",
  8530. // @"required" : @"true",
  8531. // @"value" : zipcode
  8532. // },
  8533. // @"item_9" : @{
  8534. // @"aname" : @"City",
  8535. // @"control" : @"edit",
  8536. // @"keyboard" : @"text",
  8537. // @"name" : @"credit_card_city",
  8538. // @"required" : @"true",
  8539. // @"value" : city
  8540. // }
  8541. // },
  8542. // @"value" : @"Fill Now",
  8543. // @"value_id" : @""
  8544. // },
  8545. // @"val_1" : @{
  8546. // @"check" : @(0),
  8547. // @"value" : @"Fill Later",
  8548. // @"value_id" : @""
  8549. // }
  8550. // },
  8551. // @"control" : @"enum",
  8552. // @"name" : @"",
  8553. // @"single_select" : @"true"
  8554. // }
  8555. // },
  8556. // @"value" : @"Visa/Master",
  8557. // @"value_id" : @"Credit Card"
  8558. // },
  8559. // @"val_5" : @{
  8560. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8561. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8562. // @"value_id" : @"FOLLOW EXISTING"
  8563. // }
  8564. // },
  8565. @"control" : @"enum",
  8566. @"name" : @"paymentType",
  8567. @"single_select" : @"true"
  8568. },
  8569. @"title" : @"Payment Information"
  8570. }.mutableCopy;
  8571. NSNumber *hide = setting[@"PaymentInformationHide"];
  8572. [dic setValue:hide forKey:@"hide"];
  8573. return dic;
  8574. }
  8575. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8576. // params
  8577. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8578. orderCode = [self translateSingleQuote:orderCode];
  8579. // 缺货检查
  8580. 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];
  8581. __block BOOL outOfStock = NO;
  8582. sqlite3 *database = db;
  8583. if (!db) {
  8584. database = [iSalesDB get_db];
  8585. }
  8586. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8587. int availability = sqlite3_column_int(stmt, 0);
  8588. int item_qty = sqlite3_column_int(stmt, 1);
  8589. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8590. }];
  8591. if (!db) {
  8592. [iSalesDB close_db:database];
  8593. }
  8594. return outOfStock;
  8595. }
  8596. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8597. {
  8598. assert(params[@"user"]!=nil);
  8599. assert(params[@"contact_id"]!=nil);
  8600. assert(params[@"can_create_backorder"]!=nil);
  8601. sqlite3 *db = [iSalesDB get_db];
  8602. // params
  8603. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8604. orderCode = [self translateSingleQuote:orderCode];
  8605. // 缺货检查
  8606. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8607. if (![params[@"can_create_backorder"] boolValue]) {
  8608. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8609. if (out_of_stock) {
  8610. [iSalesDB close_db:db];
  8611. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8612. [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"];
  8613. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8614. return [RAConvertor dict2data:resultDic];
  8615. }
  8616. }
  8617. // UISetting
  8618. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8619. NSString *cachefolder = [paths objectAtIndex:0];
  8620. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8621. NSData* json =nil;
  8622. json=[NSData dataWithContentsOfFile:img_cache];
  8623. NSError *error=nil;
  8624. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8625. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8626. [params setObject:setting forKey:@"setting"];
  8627. int section_count = 0;
  8628. // 0 Order Type 1 Shipping Method 2 Payment Information
  8629. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8630. // 0 Order Type
  8631. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8632. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8633. [ret setValue:order_type_dic forKey:key0];
  8634. // 1 Shipping Method
  8635. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8636. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8637. [ret setValue:shipping_method_dic forKey:key1];
  8638. // 2 Payment Information
  8639. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8640. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8641. [ret setValue:payment_info_dic forKey:key2];
  8642. // 3 Customer
  8643. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8644. NSDictionary *customer_dic = [self customerDic:params db:db];
  8645. [ret setValue:customer_dic forKey:key3];
  8646. // 4 Ship To
  8647. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8648. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8649. [ret setValue:ship_to_dic forKey:key4];
  8650. // 5 Ship From
  8651. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8652. if (![shipFromDisable integerValue]) {
  8653. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8654. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8655. [ret setValue:ship_from_dic forKey:key5];
  8656. }
  8657. // 6 Freight Bill To
  8658. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8659. if (![freightBillToDisable integerValue]) {
  8660. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8661. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8662. [ret setValue:freight_bill_to forKey:key6];
  8663. }
  8664. // 7 Merchandise Bill To
  8665. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8666. if (![merchandiseBillToDisable integerValue]) {
  8667. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8668. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8669. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8670. }
  8671. // 8 Return To
  8672. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8673. if (![returnToDisable integerValue]) {
  8674. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8675. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8676. [ret setValue:return_to_dic forKey:key8];
  8677. }
  8678. // 9 Model Information
  8679. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8680. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8681. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8682. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8683. [ret setValue:model_info_dic forKey:key9];
  8684. // 10 Remarks Content
  8685. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8686. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8687. [ret setValue:remarks_content_dic forKey:key10];
  8688. // 11 Order Total
  8689. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8690. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8691. [ret setValue:order_total_dic forKey:key11];
  8692. // 12 Signature
  8693. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8694. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8695. [ret setValue:sign_dic forKey:key12];
  8696. [ret setValue:@(section_count) forKey:@"section_count"];
  8697. [iSalesDB close_db:db];
  8698. return [RAConvertor dict2data:ret];
  8699. // return nil;
  8700. }
  8701. #pragma mark addr editor
  8702. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8703. NSMutableDictionary *new_item = [item mutableCopy];
  8704. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8705. return new_item;
  8706. }
  8707. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8708. {
  8709. // "is_subaction" = true;
  8710. // orderCode = MOB1608240002;
  8711. // password = 123456;
  8712. // "subaction_tag" = 1;
  8713. // user = EvanK;
  8714. // {
  8715. // "is_subaction" = true;
  8716. // orderCode = MOB1608240002;
  8717. // password = 123456;
  8718. // "refresh_trigger" = zipcode;
  8719. // "subaction_tag" = 1;
  8720. // user = EvanK;
  8721. // }
  8722. NSString *country_code = nil;
  8723. NSString *zipCode = nil;
  8724. NSString *stateCode = nil;
  8725. NSString *city = nil;
  8726. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8727. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8728. NSString *code_id = params[@"country"];
  8729. country_code = [self countryCodeByid:code_id];
  8730. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8731. NSString *zip_code = params[@"zipcode"];
  8732. // 剔除全部为空格
  8733. int spaceCount = 0;
  8734. for (int i = 0; i < zip_code.length; i++) {
  8735. if ([zip_code characterAtIndex:i] == ' ') {
  8736. spaceCount++;
  8737. }
  8738. }
  8739. if (spaceCount == zip_code.length) {
  8740. zip_code = @"";
  8741. }
  8742. zipCode = zip_code;
  8743. if (zipCode.length > 0) {
  8744. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8745. country_code = [dic valueForKey:@"country_code"];
  8746. if (!country_code) {
  8747. // country_code = @"US";
  8748. NSString *code_id = params[@"country"];
  8749. country_code = [self countryCodeByid:code_id];
  8750. }
  8751. stateCode = [dic valueForKey:@"state_code"];
  8752. if(!stateCode.length) {
  8753. stateCode = params[@"state"];
  8754. }
  8755. city = [dic valueForKey:@"city"];
  8756. if (!city.length) {
  8757. city = params[@"city"];
  8758. }
  8759. // zip code
  8760. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8761. // [zipDic setValue:zipCode forKey:@"value"];
  8762. // [section_0 setValue:zipDic forKey:@"item_11"];
  8763. } else {
  8764. NSString *code_id = params[@"country"];
  8765. country_code = [self countryCodeByid:code_id];
  8766. stateCode = params[@"state"];
  8767. city = params[@"city"];
  8768. }
  8769. }
  8770. }
  8771. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8772. // [ret removeObjectForKey:@"up_params"];
  8773. [ret setObject:@"New Address" forKey:@"title"];
  8774. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8775. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8776. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8777. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8778. // [country_dic removeObjectForKey:@"refresh"];
  8779. // [country_dic removeObjectForKey:@"restore"];
  8780. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8781. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8782. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8783. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8784. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8785. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8786. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8787. // [zip_code_dic removeObjectForKey:@"refresh"];
  8788. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8789. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8790. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8791. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8792. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8793. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8794. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8795. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8796. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8797. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8798. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8799. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8800. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8801. // country
  8802. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8803. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8804. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8805. // state
  8806. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8807. // NSDictionary *tmpDic = @{
  8808. // @"value" : @"Other",
  8809. // @"value_id" : @"",
  8810. // @"check" : [NSNumber numberWithInteger:0]
  8811. // };
  8812. //
  8813. // for (int i = 0; i < allState.allKeys.count; i++) {
  8814. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8815. //
  8816. // NSDictionary *tmp = allState[key];
  8817. // [allState setValue:tmpDic forKey:key];
  8818. // tmpDic = tmp;
  8819. // }
  8820. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8821. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8822. [ret setValue:new_section_0 forKey:@"section_0"];
  8823. return [RAConvertor dict2data:ret];
  8824. }
  8825. #pragma mark save addr
  8826. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8827. {
  8828. // NSString *companyName = [self valueInParams:params key:@"company"];
  8829. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8830. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8831. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8832. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8833. // NSString *countryId = [self valueInParams:params key:@"country"];
  8834. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8835. // NSString *city = [self valueInParams:params key:@"city"];
  8836. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8837. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8838. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8839. // NSString *phone = [self valueInParams:params key:@"phone"];
  8840. // NSString *fax = [self valueInParams:params key:@"fax"];
  8841. // NSString *email = [self valueInParams:params key:@"email"];
  8842. return [self offline_saveContact:params update:NO isCustomer:NO];
  8843. }
  8844. #pragma mark cancel order
  8845. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8846. {
  8847. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8848. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8849. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8850. if (order_id.length) {
  8851. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8852. }
  8853. int ret = [iSalesDB execSql:sql];
  8854. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8855. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8856. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8857. // [dic setValue:@"160409" forKey:@"min_ver"];
  8858. return [RAConvertor dict2data:dic];
  8859. }
  8860. #pragma mark sign order
  8861. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8862. {
  8863. //参考 offline_saveBusinesscard
  8864. DebugLog(@"sign order params: %@",params);
  8865. // orderCode = MOB1608240002;
  8866. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8867. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8868. orderCode = [self translateSingleQuote:orderCode];
  8869. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8870. picPath = [self translateSingleQuote:picPath];
  8871. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8872. int ret = [iSalesDB execSql:sql];
  8873. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8874. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8875. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8876. // [dic setValue:@"160409" forKey:@"min_ver"];
  8877. return [RAConvertor dict2data:dic];
  8878. }
  8879. #pragma mark save order
  8880. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8881. NSString *ret = [self valueInParams:params key:key];
  8882. if (translate) {
  8883. ret = [self translateSingleQuote:ret];
  8884. }
  8885. return ret;
  8886. }
  8887. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8888. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8889. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8890. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8891. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8892. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8893. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8894. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8895. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8896. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8897. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8898. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8899. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8900. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8901. if (![total_price isEqualToString:@""]) {
  8902. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8903. } else {
  8904. total_price = @"";
  8905. }
  8906. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8907. if ([paymentsAndCredits isEqualToString:@""]) {
  8908. paymentsAndCredits = @"";
  8909. } else {
  8910. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8911. }
  8912. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8913. if ([handling_fee_value isEqualToString:@""]) {
  8914. handling_fee_value = @"";
  8915. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8916. handling_fee_value = @"";
  8917. } else {
  8918. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8919. }
  8920. NSString *handling_fee_placeholder = handling_fee_value;
  8921. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8922. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8923. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8924. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8925. NSString *lift_gate_placeholder = @"";
  8926. if ([lift_gate_value isEqualToString:@""]) {
  8927. lift_gate_placeholder = @"";
  8928. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8929. lift_gate_placeholder = @"";
  8930. } else {
  8931. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8932. }
  8933. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8934. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8935. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8936. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8937. 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];
  8938. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8939. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8940. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8941. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8942. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8943. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8944. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8945. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8946. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8947. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8948. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8949. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8950. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8951. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8952. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8953. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8954. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8955. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8956. if (!number_nil) {
  8957. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8958. }
  8959. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8960. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8961. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8962. if (!security_code_nil) {
  8963. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8964. }
  8965. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8966. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8967. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8968. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8969. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8970. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8971. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8972. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8973. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8974. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8975. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8976. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8977. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8978. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8979. NSString *contact_id = customer_cid;
  8980. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8981. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8982. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8983. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8984. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8985. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8986. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8987. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8988. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8989. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8990. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8991. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8992. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8993. //
  8994. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8995. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8996. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8997. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8998. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8999. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9000. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9001. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9002. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9003. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9004. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9005. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9006. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9007. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9008. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9009. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9010. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9011. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9012. 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];
  9013. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9014. if (receive_cid.length) {
  9015. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9016. }
  9017. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9018. if (receive_name.length) {
  9019. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9020. }
  9021. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9022. if (receive_ext.length) {
  9023. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9024. }
  9025. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9026. if (receive_contact.length) {
  9027. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9028. }
  9029. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9030. if (receive_phone.length) {
  9031. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9032. }
  9033. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9034. if (receive_email.length) {
  9035. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9036. }
  9037. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9038. if (receive_fax.length) {
  9039. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9040. }
  9041. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9042. if (sender_cid.length) {
  9043. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9044. }
  9045. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9046. if (sender_name.length) {
  9047. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9048. }
  9049. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9050. if (sender_ext.length) {
  9051. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9052. }
  9053. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9054. if(sender_contact.length) {
  9055. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9056. }
  9057. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9058. if (sender_phone.length) {
  9059. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9060. }
  9061. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9062. if (sender_fax.length) {
  9063. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9064. }
  9065. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9066. if (sender_email.length) {
  9067. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9068. }
  9069. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9070. if (shipping_billto_cid.length) {
  9071. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9072. }
  9073. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9074. if (shipping_billto_name.length) {
  9075. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9076. }
  9077. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9078. if (shipping_billto_ext.length) {
  9079. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9080. }
  9081. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9082. if (shipping_billto_contact.length) {
  9083. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9084. }
  9085. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9086. if (shipping_billto_phone.length) {
  9087. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9088. }
  9089. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9090. if (shipping_billto_fax.length) {
  9091. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9092. }
  9093. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9094. if (shipping_billto_email.length) {
  9095. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9096. }
  9097. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9098. if (billing_cid.length) {
  9099. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9100. }
  9101. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9102. if (billing_name.length) {
  9103. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9104. }
  9105. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9106. if (billing_ext.length) {
  9107. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9108. }
  9109. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9110. if (billing_contact.length) {
  9111. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9112. }
  9113. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9114. if (billing_phone.length) {
  9115. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9116. }
  9117. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9118. if (billing_fax.length) {
  9119. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9120. }
  9121. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9122. if (billing_email.length) {
  9123. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9124. }
  9125. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9126. if (returnto_cid.length) {
  9127. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9128. }
  9129. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9130. if (returnto_name.length) {
  9131. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9132. }
  9133. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9134. if (returnto_ext.length) {
  9135. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9136. }
  9137. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9138. if (returnto_contact.length) {
  9139. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9140. }
  9141. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9142. if (returnto_phone.length) {
  9143. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9144. }
  9145. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9146. if (returnto_fax.length) {
  9147. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9148. }
  9149. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9150. if (returnto_email.length) {
  9151. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9152. }
  9153. NSString *order_status = @"status = 1,";
  9154. if (submit) {
  9155. order_status = @"status = -11,";
  9156. }
  9157. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9158. NSString *sync_sql = @"";
  9159. if (submit) {
  9160. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9161. [param setValue:sales_rep forKey:@"sales_rep"];
  9162. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9163. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9164. }
  9165. 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];
  9166. DebugLog(@"save order contactSql: %@",contactSql);
  9167. DebugLog(@"save order orderSql: %@",orderSql);
  9168. // int contact_ret = [iSalesDB execSql:contactSql];
  9169. int order_ret = [iSalesDB execSql:orderSql];
  9170. int ret = order_ret;
  9171. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9172. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9173. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9174. // [dic setValue:@"160409" forKey:@"min_ver"];
  9175. [dic setObject:so_id forKey:@"so#"];
  9176. return [RAConvertor dict2data:dic];
  9177. }
  9178. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9179. {
  9180. // id foo = nil;
  9181. // NSMutableArray *a = @[].mutableCopy;
  9182. // [a addObject:foo];
  9183. return [self saveorder:param submit:NO];
  9184. }
  9185. #pragma mark add to cart by name
  9186. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  9187. {
  9188. NSString *orderCode = [params objectForKey:@"orderCode"];
  9189. NSDictionary *newParams = @{
  9190. @"product_id" : params[@"product_id_string"],
  9191. @"orderCode" : orderCode,
  9192. @"can_create_backorder":params[@"can_create_backorder"]
  9193. };
  9194. return [self offline_add2cart:[newParams mutableCopy]];
  9195. }
  9196. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  9197. {
  9198. NSString *orderCode = [params objectForKey:@"orderCode"];
  9199. NSString *upccode = [params objectForKey:@"upc_code"];
  9200. // product_name = [self translateSingleQuote:product_name];
  9201. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9202. // bool search_upc = [params[@"search_upc"] boolValue];
  9203. sqlite3 *db = [iSalesDB get_db];
  9204. __block NSMutableString *product_id_string = [NSMutableString string];
  9205. // NSString *name = [product_name_array objectAtIndex:i];
  9206. NSString *sql =nil;
  9207. 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];
  9208. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9209. int product_id = sqlite3_column_int(stmt, 0);
  9210. [product_id_string appendFormat:@"%d",product_id];
  9211. }];
  9212. if (!orderCode) {
  9213. orderCode = @"";
  9214. }
  9215. NSDictionary *newParams = @{
  9216. @"product_id" : product_id_string,
  9217. @"orderCode" : orderCode,
  9218. @"can_create_backorder":params[@"can_create_backorder"]
  9219. };
  9220. [iSalesDB close_db:db];
  9221. return [self offline_add2cart:[newParams mutableCopy]];
  9222. }
  9223. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9224. {
  9225. NSString *orderCode = [params objectForKey:@"orderCode"];
  9226. NSString *product_name = [params objectForKey:@"product_name"];
  9227. product_name = [self translateSingleQuote:product_name];
  9228. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9229. bool search_upc = [params[@"search_upc"] boolValue];
  9230. sqlite3 *db = [iSalesDB get_db];
  9231. __block NSMutableString *product_id_string = [NSMutableString string];
  9232. for (int i = 0; i < product_name_array.count; i++) {
  9233. NSString *name = [product_name_array objectAtIndex:i];
  9234. NSString *sql =nil;
  9235. if(search_upc)
  9236. 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];
  9237. else
  9238. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9239. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9240. int product_id = sqlite3_column_int(stmt, 0);
  9241. if (i == product_name_array.count - 1) {
  9242. [product_id_string appendFormat:@"%d",product_id];
  9243. } else {
  9244. [product_id_string appendFormat:@"%d,",product_id];
  9245. }
  9246. }];
  9247. }
  9248. if (!orderCode) {
  9249. orderCode = @"";
  9250. }
  9251. NSDictionary *newParams = @{
  9252. @"product_id" : product_id_string,
  9253. @"orderCode" : orderCode,
  9254. @"can_create_backorder":params[@"can_create_backorder"]
  9255. };
  9256. [iSalesDB close_db:db];
  9257. return [self offline_add2cart:[newParams mutableCopy]];
  9258. }
  9259. #pragma mark reset order
  9260. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9261. {
  9262. // UIApplication * app = [UIApplication sharedApplication];
  9263. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9264. [iSalesDB disable_trigger];
  9265. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9266. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9267. [iSalesDB enable_trigger];
  9268. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9269. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9270. return [RAConvertor dict2data:dic];
  9271. }
  9272. #pragma mark submit order
  9273. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9274. {
  9275. return [self saveorder:params submit:YES];
  9276. }
  9277. #pragma mark copy order
  9278. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9279. {
  9280. NSMutableDictionary *ret = @{}.mutableCopy;
  9281. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9282. sqlite3 *db = [iSalesDB get_db];
  9283. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9284. // 首先查看联系人是否active
  9285. 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];
  9286. __block int customer_is_active = 1;
  9287. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9288. customer_is_active = sqlite3_column_int(stmt, 0);
  9289. }];
  9290. if (!customer_is_active) {
  9291. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9292. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9293. [iSalesDB close_db:db];
  9294. return [RAConvertor dict2data:ret];
  9295. }
  9296. // new order
  9297. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9298. NSString *new_order_code = [self get_offline_soid:db];
  9299. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9300. user = [self translateSingleQuote:user];
  9301. 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
  9302. __block int result = 1;
  9303. result = [iSalesDB execSql:insert_order_sql db:db];
  9304. if (!result) {
  9305. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9306. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9307. [iSalesDB close_db:db];
  9308. return [RAConvertor dict2data:ret];
  9309. }
  9310. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9311. // __block NSString *product_id = @"";
  9312. __weak typeof(self) weakSelf = self;
  9313. 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];
  9314. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9315. int is_active = sqlite3_column_int(stmt, 1);
  9316. if (is_active) {
  9317. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9318. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9319. 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];
  9320. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9321. }
  9322. }];
  9323. // product_id = [product_id substringFromIndex:1];
  9324. //
  9325. // [self offline_add2cart:@{}.mutableCopy];
  9326. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9327. [iSalesDB close_db:db];
  9328. if (result) {
  9329. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9330. } else {
  9331. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9332. }
  9333. [ret setObject:new_order_code forKey:@"so_id"];
  9334. return [RAConvertor dict2data:ret];
  9335. }
  9336. #pragma mark move wish list to cart
  9337. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9338. assert(params[@"can_create_backorder"]!=nil);
  9339. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9340. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9341. NSString *collectId = params[@"collectId"];
  9342. 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];
  9343. __block NSString *product_id = @"";
  9344. __block NSString *qty = @"";
  9345. __block int number_of_outOfStock = 0;
  9346. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9347. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9348. int productId = sqlite3_column_int(stmt, 0);
  9349. int item_qty = sqlite3_column_int(stmt, 1);
  9350. int availability = sqlite3_column_int(stmt, 2);
  9351. int _id = sqlite3_column_int(stmt, 3);
  9352. if (![params[@"can_create_backorder"] boolValue]) {
  9353. // 库存小于购买量为缺货
  9354. if (availability >= item_qty) {
  9355. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9356. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9357. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9358. } else {
  9359. number_of_outOfStock++;
  9360. }
  9361. } else {
  9362. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9363. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9364. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9365. }
  9366. }];
  9367. NSMutableDictionary *retDic = nil;
  9368. if (![params[@"can_create_backorder"] boolValue]) {
  9369. if (delete_collectId.count == 0) {
  9370. retDic = [NSMutableDictionary dictionary];
  9371. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9372. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9373. return [RAConvertor dict2data:retDic];
  9374. }
  9375. }
  9376. if (product_id.length > 1) {
  9377. product_id = [product_id substringFromIndex:1];
  9378. }
  9379. if (qty.length > 1) {
  9380. qty = [qty substringFromIndex:1];
  9381. }
  9382. NSString *orderCode = params[@"orderCode"];
  9383. if (!orderCode) {
  9384. orderCode = @"";
  9385. }
  9386. NSDictionary *newParams = @{
  9387. @"product_id" : product_id,
  9388. @"orderCode" : orderCode,
  9389. @"qty" : qty,
  9390. @"can_create_backorder":params[@"can_create_backorder"]
  9391. };
  9392. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9393. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9394. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9395. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9396. NSInteger ret = [wish_return[@"result"] intValue];
  9397. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9398. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9399. }
  9400. if (![params[@"can_create_backorder"] boolValue]) {
  9401. if (number_of_outOfStock > 0) {
  9402. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9403. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9404. }
  9405. }
  9406. return [RAConvertor dict2data:retDic];
  9407. }
  9408. #pragma mark - portfolio
  9409. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9410. // assert(params[@"contact_id"]!=nil);
  9411. assert(params[@"user"]!=nil);
  9412. assert(params[@"can_see_price"]!=nil);
  9413. int sort = [[params valueForKey:@"sort"] intValue];
  9414. int offset = [[params valueForKey:@"offset"] intValue];
  9415. int limit = [[params valueForKey:@"limit"] intValue];
  9416. NSString *orderBy = @"";
  9417. switch (sort) {
  9418. case 0:{
  9419. orderBy = @"p.modify_time desc";
  9420. }
  9421. break;
  9422. case 1:{
  9423. orderBy = @"modify_time asc";
  9424. }
  9425. break;
  9426. case 2:{
  9427. orderBy = @"p.name asc";
  9428. }
  9429. break;
  9430. case 3:{
  9431. orderBy = @"p.name desc";
  9432. }
  9433. break;
  9434. case 4:{
  9435. orderBy = @"p.description asc";
  9436. }
  9437. break;
  9438. case 5: {
  9439. orderBy = @"m.default_category asc";
  9440. }
  9441. default:
  9442. break;
  9443. }
  9444. // 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];
  9445. 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];
  9446. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9447. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9448. sqlite3 *db = [iSalesDB get_db];
  9449. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9450. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9451. int product_id = sqlite3_column_int(stmt, 0);
  9452. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9453. NSString *name = [self textAtColumn:1 statement:stmt];
  9454. NSString *description = [self textAtColumn:2 statement:stmt];
  9455. double price = sqlite3_column_double(stmt, 3);
  9456. double discount = sqlite3_column_double(stmt,4);
  9457. int qty = sqlite3_column_int(stmt, 5);
  9458. int percentage = sqlite3_column_int(stmt, 6);
  9459. int item_id = sqlite3_column_int(stmt, 7);
  9460. // int fashion_id = sqlite3_column_int(stmt, 8);
  9461. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9462. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9463. double percent = sqlite3_column_double(stmt, 11);
  9464. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9465. if ([price_null isEqualToString:@"null"]) {
  9466. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9467. }
  9468. NSMutableDictionary *item = @{
  9469. @"linenotes": line_note,
  9470. @"check": @(1),
  9471. @"product_id": product_id_string,
  9472. @"available_qty": @(qty),
  9473. @"available_percent" : @(percent),
  9474. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9475. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9476. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9477. @"img": img_path,
  9478. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9479. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9480. }.mutableCopy;
  9481. if (percentage) {
  9482. [item removeObjectForKey:@"available_qty"];
  9483. } else {
  9484. [item removeObjectForKey:@"available_percent"];
  9485. }
  9486. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9487. if ([qty_null isEqualToString:@"null"]) {
  9488. [item removeObjectForKey:@"available_qty"];
  9489. }
  9490. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9491. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9492. }];
  9493. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9494. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9495. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9496. }
  9497. [iSalesDB close_db:db];
  9498. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9499. [dic setValue:@"" forKey:@"email_content"];
  9500. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9501. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9502. return [RAConvertor dict2data:dic];
  9503. }
  9504. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9505. __block int qty = 0;
  9506. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9507. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9508. qty = sqlite3_column_int(stmt, 0);
  9509. }];
  9510. return qty;
  9511. }
  9512. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9513. {
  9514. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9515. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9516. // NSMutableDictionary * values = params[@"replaceValue"];
  9517. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9518. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9519. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9520. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9521. NSString *pdf_path = @"";
  9522. if (direct) {
  9523. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9524. } else {
  9525. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9526. }
  9527. NSString *create_user = [self valueInParams:params key:@"user"];
  9528. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9529. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9530. // model info
  9531. // 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];
  9532. // V1.90 more color
  9533. 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];
  9534. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9535. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9536. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9537. sqlite3 *db = [iSalesDB get_db];
  9538. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9539. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9540. int product_id = sqlite3_column_int(stmt, 0);
  9541. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9542. double price = sqlite3_column_double(stmt, 1);
  9543. double discount = sqlite3_column_double(stmt,2);
  9544. int qty = sqlite3_column_int(stmt, 3);
  9545. int percentage = sqlite3_column_int(stmt, 4);
  9546. int item_id = sqlite3_column_int(stmt, 5);
  9547. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9548. double percent = sqlite3_column_double(stmt, 7);
  9549. int more_color = sqlite3_column_int(stmt, 8);
  9550. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9551. /* if ([price_null isEqualToString:@"null"]) {
  9552. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9553. }
  9554. */
  9555. [product_ids_string appendFormat:@"%@,",product_id_string];
  9556. // Regular Price
  9557. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9558. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9559. // QTY
  9560. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9561. if ([qty_null isEqualToString:@"null"]) {
  9562. // 查available
  9563. qty = [self model_QTY:product_id_string db:db];
  9564. }
  9565. if (percentage) {
  9566. qty = qty * percent / 100;
  9567. }
  9568. // Special Price
  9569. if ([price_null isEqualToString:@"null"]) {
  9570. // price = regular price
  9571. price = [regular_price_str doubleValue];
  9572. }
  9573. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9574. if (![discount_null isEqualToString:@"null"]) {
  9575. price = price * (1 - discount / 100.0);
  9576. }
  9577. NSMutableDictionary *item = @{
  9578. @"line_note": line_note,
  9579. @"product_id": product_id_string,
  9580. @"available_qty": @(qty),
  9581. @"more_color":@(more_color),
  9582. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9583. @"regular_price" : regular_price_str,
  9584. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9585. }.mutableCopy;
  9586. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9587. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9588. }];
  9589. [iSalesDB close_db:db];
  9590. if (product_ids_string.length > 0) {
  9591. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9592. }
  9593. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9594. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9595. return [RAConvertor dict2data:resultDictionary];
  9596. }
  9597. NSString *model_info = [RAConvertor dict2string:dic];
  9598. // 创建PDF
  9599. // 在preview情况下保存,则不需要新建了
  9600. if (direct) {
  9601. NSMutableDictionary *tear_sheet_params = params;
  9602. // if (tear_sheet_id) {
  9603. // tear_sheet_params = values;
  9604. // }
  9605. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9606. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9607. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9608. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9609. resultDictionary = pdfInfo.mutableCopy;
  9610. } else { // 创建PDF失败
  9611. return pdfData;
  9612. }
  9613. } else {
  9614. // pdf_path 就是本地路径
  9615. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9616. }
  9617. // 将文件移动到PDF Cache文件夹
  9618. NSString *newPath = [pdf_path lastPathComponent];
  9619. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9620. NSString *cachefolder = [paths objectAtIndex:0];
  9621. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9622. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9623. NSFileManager *fileManager = [NSFileManager defaultManager];
  9624. NSError *error = nil;
  9625. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9626. if (error) { // 移动文件失败
  9627. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9628. return [RAConvertor dict2data:resultDictionary];
  9629. }
  9630. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9631. pdf_path = [newPath lastPathComponent];
  9632. // 保存信息
  9633. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9634. NSString *off_params = [RAConvertor dict2string:params];
  9635. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9636. pdf_path = [self translateSingleQuote:pdf_path];
  9637. create_user = [self translateSingleQuote:create_user];
  9638. tear_note = [self translateSingleQuote:tear_note];
  9639. tear_name = [self translateSingleQuote:tear_name];
  9640. model_info = [self translateSingleQuote:model_info];
  9641. configureParams = [self translateSingleQuote:configureParams];
  9642. off_params = [self translateSingleQuote:off_params];
  9643. 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];
  9644. if (tear_sheet_id) {
  9645. int _id = [tear_sheet_id intValue];
  9646. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9647. }
  9648. int result = [iSalesDB execSql:save_pdf_sql];
  9649. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9650. //
  9651. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9652. if (remove_Item) {
  9653. // portfolioId
  9654. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9655. }
  9656. return [RAConvertor dict2data:resultDictionary];
  9657. }
  9658. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9659. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  9660. }
  9661. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9662. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  9663. }
  9664. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9665. int offset = [[params valueForKey:@"offset"] intValue];
  9666. int limit = [[params valueForKey:@"limit"] intValue];
  9667. NSString *keyword = [params valueForKey:@"keyWord"];
  9668. NSString *where = @"where is_delete is null or is_delete = 0";
  9669. if (keyword.length) {
  9670. keyword = [self translateSingleQuote:keyword];
  9671. 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];
  9672. }
  9673. 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
  9674. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9675. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9676. NSString *cachefolder = [paths objectAtIndex:0];
  9677. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9678. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9679. NSString *name = [self textAtColumn:0 statement:stmt];
  9680. NSString *note = [self textAtColumn:1 statement:stmt];
  9681. NSString *time = [self textAtColumn:2 statement:stmt];
  9682. NSString *user = [self textAtColumn:3 statement:stmt];
  9683. NSString *path = [self textAtColumn:4 statement:stmt];
  9684. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9685. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9686. path = [pdfFolder stringByAppendingPathComponent:path];
  9687. BOOL bdir=NO;
  9688. NSFileManager* fileManager = [NSFileManager defaultManager];
  9689. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9690. {
  9691. //pdf文件不存在
  9692. path=nil;
  9693. }
  9694. time = [self changeDateTimeFormate:time];
  9695. time = [time stringByAppendingString:@" PST"];
  9696. int sheet_id = sqlite3_column_int(stmt, 5);
  9697. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9698. item[@"tearsheetsId"]=@(sheet_id);
  9699. item[@"pdf_path"]=path;
  9700. item[@"create_time"]=time;
  9701. item[@"create_user"]=user;
  9702. item[@"tear_note"]=note;
  9703. item[@"tear_name"]=name;
  9704. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9705. item[@"model_info"]=model_info;
  9706. item[@"off_params"]=off_params;
  9707. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9708. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9709. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9710. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9711. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9712. }];
  9713. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9714. return [RAConvertor dict2data:dic];
  9715. }
  9716. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9717. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9718. // NSString *user = [params objectForKey:@"user"];
  9719. // 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];
  9720. 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];
  9721. int result = [iSalesDB execSql:sql];
  9722. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9723. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9724. if (result == RESULT_TRUE) {
  9725. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9726. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9727. sqlite3 *db = [iSalesDB get_db];
  9728. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9729. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9730. [iSalesDB close_db:db];
  9731. }
  9732. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9733. return [RAConvertor dict2data:dic];
  9734. }
  9735. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9736. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9737. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9738. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9739. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9740. NSDictionary *company_item = @{
  9741. @"control": @"text",
  9742. @"keyboard": @"text",
  9743. @"name": @"company_name",
  9744. @"value": COMPANY_FULL_NAME,
  9745. @"aname": @"Company Name"
  9746. };
  9747. [section_0 setObject:company_item forKey:@"item_0"];
  9748. [dic setObject:section_0 forKey:@"section_0"];
  9749. // Regurlar Price
  9750. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9751. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9752. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9753. __block long val_count = 0;
  9754. NSString *sql = @"select name,type,order_by from price order by order_by";
  9755. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9756. NSString *name = [self textAtColumn:0 statement:stmt];
  9757. int type = sqlite3_column_int(stmt, 1);
  9758. int order_by = sqlite3_column_int(stmt, 2);
  9759. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9760. NSDictionary *price_dic = @{
  9761. @"value" : name,
  9762. @"value_id" : [NSNumber numberWithInteger:type],
  9763. @"check" : order_by == 0 ? @(1) : @(0)
  9764. };
  9765. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9766. val_count = ++(*count);
  9767. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9768. val_count = 0;
  9769. }];
  9770. [cadedate setObject:@{@"value" : @"None",
  9771. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9772. val_count++;
  9773. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9774. [price setObject:cadedate forKey:@"cadedate"];
  9775. [section1 setObject:price forKey:@"item_2"];
  9776. [dic setObject:section1 forKey:@"section_1"];
  9777. return [RAConvertor dict2data:dic];
  9778. }
  9779. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9780. NSString *product_id = [params objectForKey:@"fashionId"];
  9781. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9782. __block int result = RESULT_TRUE;
  9783. __block int qty = 0;
  9784. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9785. qty = sqlite3_column_int(stmt, 0);
  9786. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9787. result = RESULT_FALSE;
  9788. }];
  9789. NSMutableDictionary *dic = @{
  9790. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9791. @"mode" : @"Regular Mode",
  9792. @"quantity_available" : @(qty),
  9793. @"result" : @(result),
  9794. }.mutableCopy;
  9795. return [RAConvertor dict2data:dic];
  9796. }
  9797. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9798. NSString *item_ids = [params objectForKey:@"item_id"];
  9799. NSString *line_notes = [params objectForKey:@"notes"];
  9800. NSString *price_str = [params objectForKey:@"price"];
  9801. NSString *discount_str = [params objectForKey:@"discount"];
  9802. NSString *percent = [params objectForKey:@"available_percent"];
  9803. NSString *qty = [params objectForKey:@"available_qty"];
  9804. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9805. int dot = 0;
  9806. if (line_notes) {
  9807. line_notes = [self translateSingleQuote:line_notes];
  9808. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9809. sql = [sql stringByAppendingString:line_notes];
  9810. dot = 1;
  9811. } else {
  9812. line_notes = @"";
  9813. }
  9814. if (price_str) {
  9815. if (dot) {
  9816. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9817. } else {
  9818. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9819. dot = 1;
  9820. }
  9821. sql = [sql stringByAppendingString:price_str];
  9822. } else {
  9823. price_str = @"";
  9824. }
  9825. if (discount_str) {
  9826. if (dot) {
  9827. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9828. } else {
  9829. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9830. dot = 1;
  9831. }
  9832. sql = [sql stringByAppendingString:discount_str];
  9833. } else {
  9834. discount_str = @"";
  9835. }
  9836. if (percent) {
  9837. if (dot) {
  9838. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9839. } else {
  9840. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9841. dot = 1;
  9842. }
  9843. sql = [sql stringByAppendingString:percent];
  9844. } else {
  9845. percent = @"";
  9846. }
  9847. if (qty) {
  9848. if (dot) {
  9849. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9850. } else {
  9851. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9852. dot = 1;
  9853. }
  9854. sql = [sql stringByAppendingString:qty];
  9855. } else {
  9856. qty = @"";
  9857. }
  9858. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9859. sql = [sql stringByAppendingString:where];
  9860. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9861. int result = [iSalesDB execSql:sql];
  9862. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9863. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9864. return [RAConvertor dict2data:dic];
  9865. }
  9866. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9867. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9868. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9869. sqlite3 *db = [iSalesDB get_db];
  9870. 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];
  9871. int result = [iSalesDB execSql:sql db:db];
  9872. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9873. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9874. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9875. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9876. [iSalesDB close_db:db];
  9877. return [RAConvertor dict2data:dic];
  9878. }
  9879. + (void)offline_removePDFWithName:(NSString *)name {
  9880. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9881. NSString *cachefolder = [paths objectAtIndex:0];
  9882. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9883. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9884. NSFileManager *fileManager = [NSFileManager defaultManager];
  9885. [fileManager removeItemAtPath:path error:nil];
  9886. }
  9887. + (void)offline_clear_PDFCache {
  9888. NSFileManager *fileManager = [NSFileManager defaultManager];
  9889. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9890. NSString *cachefolder = [paths objectAtIndex:0];
  9891. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9892. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9893. for (NSString *path in pdf_files) {
  9894. [self offline_removePDFWithName:[path lastPathComponent]];
  9895. }
  9896. }
  9897. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9898. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9899. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9900. NSString *user = [params objectForKey:@"user"];
  9901. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  9902. if (![create_user isEqualToString:user]) {
  9903. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9904. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9905. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9906. return [RAConvertor dict2data:dic];
  9907. }
  9908. 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]];
  9909. __block int is_local = 0;
  9910. __block NSString *path = @"";
  9911. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9912. is_local = sqlite3_column_int(stmt, 0);
  9913. path = [self textAtColumn:1 statement:stmt];
  9914. }];
  9915. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9916. if (is_local == 1) {
  9917. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9918. // 删除文件
  9919. [self offline_removePDFWithName:path];
  9920. }
  9921. int result = [iSalesDB execSql:sql];
  9922. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9923. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9924. return [RAConvertor dict2data:dic];
  9925. }
  9926. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  9927. {
  9928. // assert(add_params[@"contact_id"]!=nil);
  9929. assert(add_params[@"user"]!=nil);
  9930. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9931. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9932. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9933. NSString* where=@"1=1";
  9934. if (ver!=nil) {
  9935. where=@"is_dirty=1";
  9936. }
  9937. 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];
  9938. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9939. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9940. sqlite3 *db = [iSalesDB get_db];
  9941. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9942. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9943. NSInteger _id = sqlite3_column_int(stmt, 0);
  9944. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9945. NSString *name = [self textAtColumn:2 statement:stmt];
  9946. NSString *desc = [self textAtColumn:3 statement:stmt];
  9947. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9948. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9949. NSInteger qty = sqlite3_column_int(stmt, 6);
  9950. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9951. double percent = sqlite3_column_double(stmt, 8);
  9952. double price = sqlite3_column_double(stmt, 9);
  9953. double discount = sqlite3_column_double(stmt, 10);
  9954. NSString *img = [self textAtColumn:11 statement:stmt];
  9955. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9956. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9957. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9958. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9959. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9960. if ([price_null isEqualToString:@"null"]) {
  9961. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9962. }
  9963. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9964. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9965. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9966. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9967. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9968. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9969. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9970. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9971. if ([qty_null isEqualToString:@"null"]) {
  9972. // [item setValue:@"null" forKey:@"available_qty"];
  9973. } else {
  9974. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9975. }
  9976. if ([is_percent_null isEqualToString:@"null"]) {
  9977. // [item setValue:@"null" forKey:@"percentage"];
  9978. } else {
  9979. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9980. }
  9981. if ([percent_null isEqualToString:@"null"]) {
  9982. // [item setValue:@"null" forKey:@"percent"];
  9983. } else {
  9984. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9985. }
  9986. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9987. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9988. [item setValue:name forKey:@"name"];
  9989. [item setValue:desc forKey:@"description"];
  9990. [item setValue:img forKey:@"img"];
  9991. [item setValue:line_note forKey:@"line_note"];
  9992. [item setValue:create_time forKey:@"createtime"];
  9993. [item setValue:modify_time forKey:@"modifytime"];
  9994. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9995. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9996. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9997. }];
  9998. [iSalesDB close_db:db];
  9999. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10000. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10001. }
  10002. return ret;
  10003. }
  10004. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10005. {
  10006. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10007. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10008. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10009. NSString* where=@"1=1";
  10010. if (ver!=nil) {
  10011. where=@"is_dirty=1";
  10012. }
  10013. 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];
  10014. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10015. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10016. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10017. NSInteger _id = sqlite3_column_int(stmt, 0);
  10018. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10019. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10020. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10021. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10022. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10023. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10024. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10025. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10026. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10027. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10028. NSString *uuid = [NSUUID UUID].UUIDString;
  10029. int is_delete = sqlite3_column_int(stmt, 11);
  10030. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10031. int is_local = sqlite3_column_int(stmt, 12);
  10032. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10033. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10034. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10035. }
  10036. [item setObject:pdf_path forKey:@"pdf_path"];
  10037. [item setObject:create_user forKey:@"create_user"];
  10038. [item setObject:tear_note forKey:@"tear_note"];
  10039. [item setObject:tear_name forKey:@"tear_name"];
  10040. [item setObject:model_info forKey:@"model_info"];
  10041. [item setObject:createtime forKey:@"createtime"];
  10042. [item setObject:modifytime forKey:@"modifytime"];
  10043. [item setObject:urlParams forKey:@"urlParams"];
  10044. [item setObject:off_params forKey:@"off_params"];
  10045. [item setObject:uuid forKey:@"pdf_token"];
  10046. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10047. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10048. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10049. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10050. } else {
  10051. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10052. }
  10053. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10054. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10055. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10056. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10057. }];
  10058. return ret;
  10059. }
  10060. #pragma mark 2020
  10061. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10062. {
  10063. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10064. sqlite3 *db = [iSalesDB get_db];
  10065. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10066. if (contactType) {
  10067. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10068. } else {
  10069. contactType = @"1 = 1";
  10070. }
  10071. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10072. DebugLog(@"offline contact list keyword: %@",keyword);
  10073. // advanced search
  10074. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10075. if (contact_name) {
  10076. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10077. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10078. } else {
  10079. contact_name = @"";
  10080. }
  10081. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10082. if (customer_phone) {
  10083. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10084. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10085. } else {
  10086. customer_phone = @"";
  10087. }
  10088. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10089. if (customer_fax) {
  10090. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10091. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10092. } else {
  10093. customer_fax = @"";
  10094. }
  10095. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10096. if (customer_zipcode) {
  10097. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10098. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10099. } else {
  10100. customer_zipcode = @"";
  10101. }
  10102. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10103. if (customer_sales_rep) {
  10104. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10105. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10106. } else {
  10107. customer_sales_rep = @"";
  10108. }
  10109. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10110. if (customer_state) {
  10111. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10112. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10113. } else {
  10114. customer_state = @"";
  10115. }
  10116. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10117. if (customer_name) {
  10118. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10119. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10120. } else {
  10121. customer_name = @"";
  10122. }
  10123. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10124. if (customer_country) {
  10125. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10126. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10127. } else {
  10128. customer_country = @"";
  10129. }
  10130. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10131. if (customer_cid) {
  10132. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10133. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  10134. } else {
  10135. customer_cid = @"";
  10136. }
  10137. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  10138. if (customer_city) {
  10139. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10140. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  10141. } else {
  10142. customer_city = @"";
  10143. }
  10144. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  10145. if (customer_address) {
  10146. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10147. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  10148. } else {
  10149. customer_address = @"";
  10150. }
  10151. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  10152. if (customer_email) {
  10153. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10154. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  10155. } else {
  10156. customer_email = @"";
  10157. }
  10158. NSString *price_name = [params valueForKey:@"price_name"];
  10159. if (price_name) {
  10160. if ([price_name containsString:@","]) {
  10161. // 首先从 price表中查处name
  10162. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  10163. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  10164. for (int i = 1;i < pArray.count;i++) {
  10165. NSString *p = pArray[i];
  10166. [mutablePStr appendFormat:@" or type = %@ ",p];
  10167. }
  10168. [mutablePStr appendString:@";"];
  10169. __block NSMutableArray *price_name_array = [NSMutableArray array];
  10170. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10171. char *name = (char *)sqlite3_column_text(stmt, 0);
  10172. if (!name)
  10173. name = "";
  10174. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  10175. }];
  10176. // 再根据name 拼sql
  10177. NSMutableString *mutable_price_name = [NSMutableString string];
  10178. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  10179. for (int i = 1; i < price_name_array.count; i++) {
  10180. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  10181. }
  10182. [mutable_price_name appendString:@")"];
  10183. price_name = mutable_price_name;
  10184. } else {
  10185. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  10186. if ([price_name isEqualToString:@""]) {
  10187. price_name = @"";
  10188. } else {
  10189. __block NSString *price;
  10190. price_name = [self translateSingleQuote:price_name];
  10191. [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) {
  10192. char *p = (char *)sqlite3_column_text(stmt, 0);
  10193. if (p == NULL) {
  10194. p = "";
  10195. }
  10196. price = [NSString stringWithUTF8String:p];
  10197. }];
  10198. if ([price isEqualToString:@""]) {
  10199. price_name = @"";
  10200. } else {
  10201. price = [self translateSingleQuote:price];
  10202. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  10203. }
  10204. }
  10205. }
  10206. } else {
  10207. price_name = @"";
  10208. }
  10209. int limit = [[params valueForKey:@"limit"] intValue];
  10210. int offset = [[params valueForKey:@"offset"] intValue];
  10211. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10212. 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];
  10213. 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];
  10214. // int result= [iSalesDB AddExFunction:db];
  10215. int count =0;
  10216. NSString *sqlQuery = nil;
  10217. if(keyword.length==0)
  10218. {
  10219. // 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];
  10220. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  10221. sqlQuery = sql;
  10222. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  10223. }
  10224. else
  10225. {
  10226. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  10227. keyword = keyword.lowercaseString;
  10228. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10229. 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];
  10230. 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]];
  10231. }
  10232. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10233. sqlite3_stmt * statement;
  10234. [ret setValue:@"2" forKey:@"result"];
  10235. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  10236. // 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";
  10237. int i = 0;
  10238. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10239. {
  10240. while (sqlite3_step(statement) == SQLITE_ROW)
  10241. {
  10242. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10243. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10244. int editable = sqlite3_column_int(statement, 0);
  10245. char *company_name = (char*)sqlite3_column_text(statement, 1);
  10246. NSString *nscompany_name =nil;
  10247. if(company_name==nil)
  10248. nscompany_name=@"";
  10249. else
  10250. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  10251. char *country = (char*)sqlite3_column_text(statement, 2);
  10252. if(country==nil)
  10253. country="";
  10254. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10255. // char *addr = (char*)sqlite3_column_text(statement, 3);
  10256. // if(addr==nil)
  10257. // addr="";
  10258. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10259. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10260. if(zipcode==nil)
  10261. zipcode="";
  10262. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10263. char *state = (char*)sqlite3_column_text(statement, 5);
  10264. if(state==nil)
  10265. state="";
  10266. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10267. char *city = (char*)sqlite3_column_text(statement, 6);
  10268. if(city==nil)
  10269. city="";
  10270. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10271. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10272. // NSString *nscontact_name = nil;
  10273. // if(contact_name==nil)
  10274. // nscontact_name=@"";
  10275. // else
  10276. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10277. char *phone = (char*)sqlite3_column_text(statement, 8);
  10278. NSString *nsphone = nil;
  10279. if(phone==nil)
  10280. nsphone=@"";
  10281. else
  10282. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10283. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10284. if(contact_id==nil)
  10285. contact_id="";
  10286. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10287. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10288. if(addr_1==nil)
  10289. addr_1="";
  10290. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10291. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10292. if(addr_2==nil)
  10293. addr_2="";
  10294. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10295. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10296. if(addr_3==nil)
  10297. addr_3="";
  10298. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10299. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10300. if(addr_4==nil)
  10301. addr_4="";
  10302. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10303. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10304. if(first_name==nil)
  10305. first_name="";
  10306. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10307. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10308. if(last_name==nil)
  10309. last_name="";
  10310. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10311. char *fax = (char*)sqlite3_column_text(statement, 16);
  10312. NSString *nsfax = nil;
  10313. if(fax==nil)
  10314. nsfax=@"";
  10315. else
  10316. {
  10317. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10318. if(nsfax.length>0)
  10319. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10320. }
  10321. char *email = (char*)sqlite3_column_text(statement, 17);
  10322. NSString *nsemail = nil;
  10323. if(email==nil)
  10324. nsemail=@"";
  10325. else
  10326. {
  10327. nsemail= [[NSString alloc]initWithUTF8String:email];
  10328. if(nsemail.length>0)
  10329. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10330. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10331. }
  10332. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10333. [arr_name addObject:nsfirst_name];
  10334. [arr_name addObject:nslast_name];
  10335. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10336. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10337. {
  10338. // decrypt
  10339. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10340. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10341. nsphone=[AESCrypt fastdecrypt:nsphone];
  10342. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10343. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10344. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10345. }
  10346. [arr_addr addObject:nscompany_name];
  10347. [arr_addr addObject:nscontact_name];
  10348. [arr_addr addObject:@"<br>"];
  10349. [arr_addr addObject:nsaddr_1];
  10350. [arr_addr addObject:nsaddr_2];
  10351. [arr_addr addObject:nsaddr_3];
  10352. [arr_addr addObject:nsaddr_4];
  10353. //[arr_addr addObject:nsaddr];
  10354. [arr_addr addObject:nszipcode];
  10355. [arr_addr addObject:nscity];
  10356. [arr_addr addObject:nsstate];
  10357. [arr_addr addObject:nscountry];
  10358. [arr_addr addObject:@"<br>"];
  10359. [arr_addr addObject:nsphone];
  10360. [arr_addr addObject:nsfax];
  10361. [arr_addr addObject:nsemail];
  10362. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10363. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10364. [item setValue:name forKey:@"name"];
  10365. [item setValue:nscontact_id forKey:@"contact_id"];
  10366. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10367. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10368. i++;
  10369. }
  10370. sqlite3_finalize(statement);
  10371. }
  10372. [iSalesDB close_db:db];
  10373. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10374. dispatch_async(dispatch_get_main_queue(), ^{
  10375. UIApplication * app = [UIApplication sharedApplication];
  10376. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10377. [ret setValue:appDelegate.mode forKey:@"mode"];
  10378. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10379. result(ret);
  10380. });
  10381. return ;
  10382. });
  10383. }
  10384. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10385. //{
  10386. //
  10387. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10388. //
  10389. // NSString* contactId = [params valueForKey:@"contactId"];
  10390. //
  10391. //
  10392. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10393. //
  10394. //
  10395. //
  10396. //
  10397. //
  10398. //
  10399. // sqlite3 *db = [iSalesDB get_db];
  10400. //
  10401. //
  10402. //
  10403. //
  10404. //
  10405. // NSString *sqlQuery = nil;
  10406. //
  10407. //
  10408. // {
  10409. // 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];
  10410. //
  10411. // }
  10412. //
  10413. //
  10414. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10415. // sqlite3_stmt * statement;
  10416. //
  10417. //
  10418. // [ret setValue:@"2" forKey:@"result"];
  10419. //
  10420. //
  10421. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10422. // {
  10423. //
  10424. // //int i = 0;
  10425. // if (sqlite3_step(statement) == SQLITE_ROW)
  10426. // {
  10427. //
  10428. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10429. //
  10430. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10431. //
  10432. //
  10433. // // int editable = sqlite3_column_int(statement, 0);
  10434. //
  10435. //
  10436. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10437. // NSString *nscompany_name =nil;
  10438. // if(company_name==nil)
  10439. // nscompany_name=@"";
  10440. // else
  10441. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10442. //
  10443. //
  10444. // char *country = (char*)sqlite3_column_text(statement, 2);
  10445. // if(country==nil)
  10446. // country="";
  10447. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10448. //
  10449. //
  10450. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10451. // // if(addr==nil)
  10452. // // addr="";
  10453. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10454. //
  10455. //
  10456. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10457. // if(zipcode==nil)
  10458. // zipcode="";
  10459. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10460. //
  10461. //
  10462. // char *state = (char*)sqlite3_column_text(statement, 5);
  10463. // if(state==nil)
  10464. // state="";
  10465. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10466. //
  10467. // char *city = (char*)sqlite3_column_text(statement, 6);
  10468. // if(city==nil)
  10469. // city="";
  10470. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10471. //
  10472. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10473. // // NSString *nscontact_name = nil;
  10474. // // if(contact_name==nil)
  10475. // // nscontact_name=@"";
  10476. // // else
  10477. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10478. //
  10479. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10480. // NSString *nsphone = nil;
  10481. // if(phone==nil)
  10482. // nsphone=@"";
  10483. // else
  10484. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10485. //
  10486. //
  10487. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10488. // // if(contact_id==nil)
  10489. // // contact_id="";
  10490. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10491. //
  10492. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10493. // if(addr_1==nil)
  10494. // addr_1="";
  10495. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10496. //
  10497. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10498. // if(addr_2==nil)
  10499. // addr_2="";
  10500. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10501. //
  10502. //
  10503. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10504. // if(addr_3==nil)
  10505. // addr_3="";
  10506. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10507. //
  10508. //
  10509. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10510. // if(addr_4==nil)
  10511. // addr_4="";
  10512. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10513. //
  10514. //
  10515. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10516. // if(first_name==nil)
  10517. // first_name="";
  10518. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10519. //
  10520. //
  10521. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10522. // if(last_name==nil)
  10523. // last_name="";
  10524. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10525. //
  10526. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10527. // NSString *nsfax = nil;
  10528. // if(fax==nil)
  10529. // nsfax=@"";
  10530. // else
  10531. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10532. //
  10533. // char *email = (char*)sqlite3_column_text(statement, 17);
  10534. // NSString *nsemail = nil;
  10535. // if(email==nil)
  10536. // nsemail=@"";
  10537. // else
  10538. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10539. //
  10540. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10541. // NSString *nsimg_0 = nil;
  10542. // if(img_0==nil)
  10543. // nsimg_0=@"";
  10544. // else
  10545. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10546. //
  10547. // [self copy_bcardImg:nsimg_0];
  10548. //
  10549. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10550. // NSString *nsimg_1 = nil;
  10551. // if(img_1==nil)
  10552. // nsimg_1=@"";
  10553. // else
  10554. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10555. // [self copy_bcardImg:nsimg_1];
  10556. //
  10557. //
  10558. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10559. // NSString *nsimg_2 = nil;
  10560. // if(img_2==nil)
  10561. // nsimg_2=@"";
  10562. // else
  10563. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10564. // [self copy_bcardImg:nsimg_2];
  10565. //
  10566. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10567. // NSString *nsprice_type = nil;
  10568. // if(price_type==nil)
  10569. // nsprice_type=@"";
  10570. // else
  10571. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10572. //
  10573. //
  10574. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10575. // NSString *nsnotes = nil;
  10576. // if(notes==nil)
  10577. // nsnotes=@"";
  10578. // else
  10579. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10580. //
  10581. //
  10582. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10583. // NSString *nssalesrep = nil;
  10584. // if(salesrep==nil)
  10585. // nssalesrep=@"";
  10586. // else
  10587. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10588. //
  10589. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10590. //
  10591. //
  10592. // {
  10593. // // decrypt
  10594. //
  10595. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10596. //
  10597. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10598. //
  10599. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10600. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10601. //
  10602. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10603. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10604. //
  10605. // }
  10606. //
  10607. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10608. //
  10609. // [arr_name addObject:nsfirst_name];
  10610. // [arr_name addObject:nslast_name];
  10611. //
  10612. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10613. //
  10614. //
  10615. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10616. //
  10617. // [arr_ext addObject:nsaddr_1];
  10618. // [arr_ext addObject:nsaddr_2];
  10619. // [arr_ext addObject:nsaddr_3];
  10620. // [arr_ext addObject:nsaddr_4];
  10621. // [arr_ext addObject:@"\r\n"];
  10622. //
  10623. // [arr_ext addObject:nscity];
  10624. // [arr_ext addObject:nsstate];
  10625. // [arr_ext addObject:nszipcode];
  10626. // [arr_ext addObject:nscountry];
  10627. //
  10628. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10629. //
  10630. //
  10631. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10632. //
  10633. //
  10634. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10635. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10636. // [item setValue:nscountry forKey:@"customer_country"];
  10637. // [item setValue:nsphone forKey:@"customer_phone"];
  10638. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10639. // [item setValue:nscompany_name forKey:@"customer_name"];
  10640. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10641. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10642. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10643. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10644. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10645. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10646. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10647. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  10648. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  10649. // [item setValue:nslast_name forKey:@"customer_last_name"];
  10650. // [item setValue:nscity forKey:@"customer_city"];
  10651. // [item setValue:nsstate forKey:@"customer_state"];
  10652. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  10653. // [item setValue:contactId forKey:@"customer_cid"];
  10654. // [item setValue:nscontact_name forKey:@"customer_contact"];
  10655. // [item setValue:nsfax forKey:@"customer_fax"];
  10656. // [item setValue:nsemail forKey:@"customer_email"];
  10657. // [item setValue:contact_type forKey:@"customer_contact_type"];
  10658. //
  10659. //
  10660. // [ret setObject:item forKey:@"customerInfo"];
  10661. // // i++;
  10662. //
  10663. //
  10664. //
  10665. // }
  10666. //
  10667. //
  10668. //
  10669. //
  10670. // sqlite3_finalize(statement);
  10671. // }
  10672. //
  10673. //
  10674. //
  10675. // [iSalesDB close_db:db];
  10676. //
  10677. //
  10678. //
  10679. //
  10680. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10681. // dispatch_async(dispatch_get_main_queue(), ^{
  10682. // UIApplication * app = [UIApplication sharedApplication];
  10683. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10684. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10685. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10686. // result(ret);
  10687. // });
  10688. //
  10689. // return ;
  10690. // });
  10691. //}
  10692. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10693. {
  10694. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10695. NSString* order_code= appDelegate.order_code;
  10696. NSString* user = appDelegate.user;
  10697. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10698. NSString* category = [params valueForKey:@"category"];
  10699. if (!category || [category isEqualToString:@""]) {
  10700. category = @"%";
  10701. }
  10702. category = [self translateSingleQuote:category];
  10703. int limit = [[params valueForKey:@"limit"] intValue];
  10704. int offset = [[params valueForKey:@"offset"] intValue];
  10705. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10706. NSString *limit_str = @"";
  10707. if (limited) {
  10708. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10709. }
  10710. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10711. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10712. sqlite3 *db = [iSalesDB get_db];
  10713. // [iSalesDB AddExFunction:db];
  10714. int count;
  10715. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  10716. 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];
  10717. double price_min = 0;
  10718. double price_max = 0;
  10719. if ([params.allKeys containsObject:@"alert"]) {
  10720. // alert
  10721. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  10722. NSString *alert = params[@"alert"];
  10723. if ([alert isEqualToString:@"Display All"]) {
  10724. alert = [NSString stringWithFormat:@""];
  10725. } else {
  10726. alert = [self translateSingleQuote:alert];
  10727. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  10728. }
  10729. // available
  10730. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  10731. NSString *available = params[@"available"];
  10732. NSString *available_condition;
  10733. if ([available isEqualToString:@"Display All"]) {
  10734. available_condition = @"";
  10735. } else if ([available isEqualToString:@"Available Now"]) {
  10736. available_condition = @"and availability > 0";
  10737. } else {
  10738. available_condition = @"and availability == 0";
  10739. }
  10740. // best seller
  10741. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  10742. NSString *best_seller = @"";
  10743. NSString *order_best_seller = @"m.name asc";
  10744. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  10745. best_seller = @"and best_seller > 0";
  10746. order_best_seller = @"m.best_seller desc,m.name asc";
  10747. }
  10748. // price
  10749. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  10750. NSString *price = params[@"price"];
  10751. price_min = 0;
  10752. price_max = MAXFLOAT;
  10753. if (user && price != nil) {
  10754. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  10755. NSMutableString *priceName = [NSMutableString string];
  10756. for (int i = 0; i < priceTypeArray.count; i++) {
  10757. NSString *pricetype = priceTypeArray[i];
  10758. pricetype = [self translateSingleQuote:pricetype];
  10759. if (i == 0) {
  10760. [priceName appendFormat:@"'%@'",pricetype];
  10761. } else {
  10762. [priceName appendFormat:@",'%@'",pricetype];
  10763. }
  10764. }
  10765. if ([price isEqualToString:@"Display All"]) {
  10766. price = [NSString stringWithFormat:@""];
  10767. } else if([price containsString:@"+"]){
  10768. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  10769. price_min = [price doubleValue];
  10770. 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];
  10771. } else {
  10772. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  10773. price_min = [[priceArray objectAtIndex:0] doubleValue];
  10774. price_max = [[priceArray objectAtIndex:1] doubleValue];
  10775. 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];
  10776. }
  10777. } else {
  10778. price = @"";
  10779. }
  10780. // sold_by_qty : Sold in quantities of %@
  10781. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  10782. NSString *qty = params[@"sold_by_qty"];
  10783. if ([qty isEqualToString:@"Display All"]) {
  10784. qty = @"";
  10785. } else {
  10786. qty = [self translateSingleQuote:qty];
  10787. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  10788. }
  10789. // cate
  10790. // category = [self translateSingleQuote:category];
  10791. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  10792. // cate mutiple selection
  10793. NSString *category_id = params[@"category"];
  10794. NSMutableArray *cate_id_array = nil;
  10795. NSMutableString *cateWhere = [NSMutableString string];
  10796. if ([category_id isEqualToString:@""] || !category_id) {
  10797. [cateWhere appendString:@"1 = 1"];
  10798. } else {
  10799. if ([category_id containsString:@","]) {
  10800. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  10801. } else {
  10802. cate_id_array = [@[category_id] mutableCopy];
  10803. }
  10804. [cateWhere appendString:@"("];
  10805. for (int i = 0; i < cate_id_array.count; i++) {
  10806. if (i == 0) {
  10807. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10808. } else {
  10809. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10810. }
  10811. }
  10812. [cateWhere appendString:@")"];
  10813. }
  10814. // where bestseller > 0 order by bestseller desc
  10815. // sql query: alert availability(int) best_seller(int) price qty
  10816. 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];
  10817. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  10818. }
  10819. DebugLog(@"offline category where: %@",where);
  10820. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  10821. if (!user) {
  10822. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  10823. }
  10824. [ret setValue:filter forKey:@"filter"];
  10825. DebugLog(@"offline_category sql:%@",sqlQuery);
  10826. sqlite3_stmt * statement;
  10827. [ret setValue:@"2" forKey:@"result"];
  10828. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10829. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10830. // int count=0;
  10831. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10832. {
  10833. int i=0;
  10834. while (sqlite3_step(statement) == SQLITE_ROW)
  10835. {
  10836. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10837. char *name = (char*)sqlite3_column_text(statement, 0);
  10838. if(name==nil)
  10839. name="";
  10840. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10841. char *description = (char*)sqlite3_column_text(statement, 1);
  10842. if(description==nil)
  10843. description="";
  10844. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10845. int product_id = sqlite3_column_int(statement, 2);
  10846. int wid = sqlite3_column_int(statement, 3);
  10847. int closeout = sqlite3_column_int(statement, 4);
  10848. int cid = sqlite3_column_int(statement, 5);
  10849. int wisdelete = sqlite3_column_int(statement, 6);
  10850. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  10851. int more_color = sqlite3_column_int(statement, 8);
  10852. // Defaul Category ID
  10853. __block NSString *categoryID = nil;
  10854. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  10855. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10856. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  10857. if(default_category==nil)
  10858. default_category="";
  10859. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  10860. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  10861. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  10862. categoryID = nsdefault_category;
  10863. }];
  10864. if (!categoryID.length) {
  10865. NSString *cateIDs = params[@"category"];
  10866. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  10867. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  10868. for (NSString *cateID in requestCategoryArr) {
  10869. BOOL needBreak = NO;
  10870. for (NSString *itemCateIDBox in itemCategoryArr) {
  10871. if (itemCateIDBox.length > 4) {
  10872. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  10873. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  10874. if ([itemCategoryID isEqualToString:cateID]) {
  10875. needBreak = YES;
  10876. categoryID = itemCategoryID;
  10877. break;
  10878. }
  10879. }
  10880. }
  10881. if (needBreak) {
  10882. break;
  10883. }
  10884. }
  10885. }
  10886. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10887. if(wid !=0 && wisdelete != 1)
  10888. [item setValue:@"true" forKey:@"wish_exists"];
  10889. else
  10890. [item setValue:@"false" forKey:@"wish_exists"];
  10891. if(closeout==0)
  10892. [item setValue:@"false" forKey:@"is_closeout"];
  10893. else
  10894. [item setValue:@"true" forKey:@"is_closeout"];
  10895. if(cid==0)
  10896. [item setValue:@"false" forKey:@"cart_exists"];
  10897. else
  10898. [item setValue:@"true" forKey:@"cart_exists"];
  10899. if (more_color == 0) {
  10900. [item setObject:@(false) forKey:@"more_color"];
  10901. } else if (more_color == 1) {
  10902. [item setObject:@(true) forKey:@"more_color"];
  10903. }
  10904. [item addEntriesFromDictionary:imgjson];
  10905. // [item setValue:nsurl forKey:@"img"];
  10906. [item setValue:nsname forKey:@"name"];
  10907. [item setValue:nsdescription forKey:@"description"];
  10908. if (categoryID) {
  10909. [item setValue:categoryID forKey:@"item_category_id"];
  10910. }
  10911. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  10912. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10913. i++;
  10914. }
  10915. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  10916. [ret setObject:items forKey:@"items"];
  10917. sqlite3_finalize(statement);
  10918. } else {
  10919. DebugLog(@"nothing...");
  10920. }
  10921. DebugLog(@"count:%d",count);
  10922. [iSalesDB close_db:db];
  10923. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10924. dispatch_async(dispatch_get_main_queue(), ^{
  10925. // UIApplication * app = [UIApplication sharedApplication];
  10926. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10927. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10928. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10929. result(ret);
  10930. });
  10931. });
  10932. // NSString* orderCode = [params valueForKey:@"orderCode"];
  10933. }
  10934. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10935. {
  10936. [self categoryList:params limited:YES completionHandler:result];
  10937. }
  10938. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10939. {
  10940. UIApplication * app = [UIApplication sharedApplication];
  10941. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10942. NSString* order_code = appDelegate.order_code;
  10943. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10944. NSString* keyword = [params valueForKey:@"keyword"];
  10945. keyword=keyword.lowercaseString;
  10946. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  10947. int limit = [[params valueForKey:@"limit"] intValue];
  10948. int offset = [[params valueForKey:@"offset"] intValue];
  10949. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10950. NSString *limit_str = @"";
  10951. if (limited) {
  10952. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10953. }
  10954. sqlite3 *db = [iSalesDB get_db];
  10955. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  10956. NSString *sqlQuery = nil;
  10957. if(exactMatch )
  10958. 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 ;
  10959. else
  10960. 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
  10961. DebugLog(@"offline_search sql:%@",sqlQuery);
  10962. sqlite3_stmt * statement;
  10963. [ret setValue:@"2" forKey:@"result"];
  10964. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10965. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10966. // int count=0;
  10967. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10968. {
  10969. int i=0;
  10970. while (sqlite3_step(statement) == SQLITE_ROW)
  10971. {
  10972. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10973. // char *name = (char*)sqlite3_column_text(statement, 1);
  10974. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  10975. char *name = (char*)sqlite3_column_text(statement, 0);
  10976. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10977. char *description = (char*)sqlite3_column_text(statement, 1);
  10978. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10979. int product_id = sqlite3_column_int(statement, 2);
  10980. // char *url = (char*)sqlite3_column_text(statement, 3);
  10981. // if(url==nil)
  10982. // url="";
  10983. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  10984. int wid = sqlite3_column_int(statement, 3);
  10985. int closeout = sqlite3_column_int(statement, 4);
  10986. int cid = sqlite3_column_int(statement, 5);
  10987. int wisdelete = sqlite3_column_int(statement, 6);
  10988. int more_color = sqlite3_column_int(statement, 7);
  10989. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10990. if(wid !=0 && wisdelete != 1)
  10991. [item setValue:@"true" forKey:@"wish_exists"];
  10992. else
  10993. [item setValue:@"false" forKey:@"wish_exists"];
  10994. if(closeout==0)
  10995. [item setValue:@"false" forKey:@"is_closeout"];
  10996. else
  10997. [item setValue:@"true" forKey:@"is_closeout"];
  10998. if(cid==0)
  10999. [item setValue:@"false" forKey:@"cart_exists"];
  11000. else
  11001. [item setValue:@"true" forKey:@"cart_exists"];
  11002. if (more_color == 0) {
  11003. [item setObject:@(false) forKey:@"more_color"];
  11004. } else if (more_color == 1) {
  11005. [item setObject:@(true) forKey:@"more_color"];
  11006. }
  11007. [item addEntriesFromDictionary:imgjson];
  11008. // [item setValue:nsurl forKey:@"img"];
  11009. [item setValue:nsname forKey:@"fash_name"];
  11010. [item setValue:nsdescription forKey:@"description"];
  11011. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11012. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11013. i++;
  11014. }
  11015. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11016. [ret setObject:items forKey:@"items"];
  11017. sqlite3_finalize(statement);
  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. result(ret);
  11024. });
  11025. });
  11026. }
  11027. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11028. {
  11029. [self search:params limited:YES completionHandler:result];
  11030. }
  11031. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11032. //{
  11033. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11034. // NSString* order_code = appDelegate.order_code;
  11035. // NSString* user = appDelegate.user;
  11036. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11037. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  11038. //
  11039. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11040. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11041. //
  11042. // // category
  11043. // NSDictionary *category_menu = [self offline_category_menu];
  11044. // [filter setValue:category_menu forKey:@"category"];
  11045. //
  11046. // NSString* where= nil;
  11047. // NSString* orderby= @"m.name";
  11048. // if (!filterSearch) {
  11049. // int covertype = [[params valueForKey:@"covertype"] intValue];
  11050. //
  11051. // switch (covertype) {
  11052. // case 0:
  11053. // {
  11054. // where=@"m.category like'%%#005#%%'";
  11055. // break;
  11056. // }
  11057. // case 1:
  11058. // {
  11059. // where=@"m.alert like '%QS%'";
  11060. // break;
  11061. // }
  11062. // case 2:
  11063. // {
  11064. // where=@"m.availability>0";
  11065. // break;
  11066. // }
  11067. // case 3:
  11068. // {
  11069. // where=@"m.best_seller>0";
  11070. // orderby=@"m.best_seller desc,m.name asc";
  11071. // break;
  11072. // }
  11073. // default:
  11074. // where=@"1=1";
  11075. // break;
  11076. // }
  11077. //
  11078. // }
  11079. //
  11080. //
  11081. // int limit = [[params valueForKey:@"limit"] intValue];
  11082. // int offset = [[params valueForKey:@"offset"] intValue];
  11083. //
  11084. // NSString *limit_str = @"";
  11085. // if (limited) {
  11086. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  11087. // }
  11088. //
  11089. //
  11090. // sqlite3 *db = [iSalesDB get_db];
  11091. // // [iSalesDB AddExFunction:db];
  11092. //
  11093. // int count;
  11094. //
  11095. // NSString *sqlQuery = nil;
  11096. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  11097. // 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];
  11098. //
  11099. //
  11100. // double price_min = 0;
  11101. // double price_max = 0;
  11102. // if (filterSearch) {
  11103. // // alert
  11104. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11105. // NSString *alert = params[@"alert"];
  11106. // if ([alert isEqualToString:@"Display All"]) {
  11107. // alert = [NSString stringWithFormat:@""];
  11108. // } else {
  11109. // alert = [self translateSingleQuote:alert];
  11110. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11111. // }
  11112. //
  11113. // // available
  11114. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11115. // NSString *available = params[@"available"];
  11116. // NSString *available_condition;
  11117. // if ([available isEqualToString:@"Display All"]) {
  11118. // available_condition = @"";
  11119. // } else if ([available isEqualToString:@"Available Now"]) {
  11120. // available_condition = @"and availability > 0";
  11121. // } else {
  11122. // available_condition = @"and availability == 0";
  11123. // }
  11124. //
  11125. // // best seller
  11126. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11127. // NSString *best_seller = @"";
  11128. // NSString *order_best_seller = @"m.name asc";
  11129. //
  11130. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11131. // best_seller = @"and best_seller > 0";
  11132. // order_best_seller = @"m.best_seller desc,m.name asc";
  11133. // }
  11134. //
  11135. // // price
  11136. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11137. // NSString *price = params[@"price"];
  11138. // price_min = 0;
  11139. // price_max = MAXFLOAT;
  11140. // if (user) {
  11141. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11142. // NSMutableString *priceName = [NSMutableString string];
  11143. // for (int i = 0; i < priceTypeArray.count; i++) {
  11144. // NSString *pricetype = priceTypeArray[i];
  11145. // pricetype = [self translateSingleQuote:pricetype];
  11146. // if (i == 0) {
  11147. // [priceName appendFormat:@"'%@'",pricetype];
  11148. // } else {
  11149. // [priceName appendFormat:@",'%@'",pricetype];
  11150. // }
  11151. // }
  11152. //
  11153. // if ([price isEqualToString:@"Display All"]) {
  11154. // price = [NSString stringWithFormat:@""];
  11155. // } else if([price containsString:@"+"]){
  11156. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11157. // price_min = [price doubleValue];
  11158. // 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];
  11159. // } else {
  11160. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11161. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  11162. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  11163. // 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];
  11164. // }
  11165. // } else {
  11166. // price = @"";
  11167. // }
  11168. //
  11169. // // sold_by_qty : Sold in quantities of %@
  11170. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11171. // NSString *qty = params[@"sold_by_qty"];
  11172. // if ([qty isEqualToString:@"Display All"]) {
  11173. // qty = @"";
  11174. // } else {
  11175. // qty = [self translateSingleQuote:qty];
  11176. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11177. // }
  11178. //
  11179. // // category
  11180. // NSString *category_id = params[@"ctgId"];
  11181. // NSMutableArray *cate_id_array = nil;
  11182. // NSMutableString *cateWhere = [NSMutableString string];
  11183. //
  11184. // if ([category_id isEqualToString:@""] || !category_id) {
  11185. // [cateWhere appendString:@"1 = 1"];
  11186. // } else {
  11187. //
  11188. // if ([category_id containsString:@","]) {
  11189. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11190. // } else {
  11191. // cate_id_array = [@[category_id] mutableCopy];
  11192. // }
  11193. // /*-----------*/
  11194. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  11195. //
  11196. // [cateWhere appendString:@"("];
  11197. // for (int i = 0; i < cate_id_array.count; i++) {
  11198. //
  11199. // for (NSString *key0 in cateDic.allKeys) {
  11200. //
  11201. // if ([key0 containsString:@"category_"]) {
  11202. //
  11203. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  11204. //
  11205. // for (NSString *key1 in category0.allKeys) {
  11206. //
  11207. // if ([key1 containsString:@"category_"]) {
  11208. //
  11209. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  11210. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  11211. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11212. //
  11213. // cate_id_array[i] = [category1 objectForKey:@"id"];
  11214. // if (i == 0) {
  11215. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11216. // } else {
  11217. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11218. // }
  11219. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11220. // [category0 setValue:category1 forKey:key1];
  11221. // [cateDic setValue:category0 forKey:key0];
  11222. //
  11223. // }
  11224. //
  11225. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  11226. //
  11227. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  11228. //
  11229. // cate_id_array[i] = [category0 objectForKey:@"id"];
  11230. // if (i == 0) {
  11231. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11232. // } else {
  11233. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11234. // }
  11235. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  11236. // [cateDic setValue:category0 forKey:key0];
  11237. //
  11238. // }
  11239. //
  11240. // }
  11241. //
  11242. // }
  11243. //
  11244. // }
  11245. //
  11246. // }
  11247. //
  11248. // }
  11249. // [cateWhere appendString:@")"];
  11250. //
  11251. // [filter setValue:cateDic forKey:@"category"];
  11252. // }
  11253. //
  11254. // // where bestseller > 0 order by bestseller desc
  11255. // // sql query: alert availability(int) best_seller(int) price qty
  11256. //
  11257. // 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];
  11258. //
  11259. //
  11260. // // count
  11261. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11262. //
  11263. // }
  11264. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11265. //
  11266. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11267. //
  11268. //
  11269. //
  11270. //
  11271. // if (!user) {
  11272. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11273. // }
  11274. //
  11275. // [ret setValue:filter forKey:@"filter"];
  11276. //
  11277. //
  11278. //
  11279. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11280. //
  11281. // sqlite3_stmt * statement;
  11282. // [ret setValue:@"2" forKey:@"result"];
  11283. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11284. //
  11285. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11286. // // int count=0;
  11287. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11288. // {
  11289. //
  11290. // int i=0;
  11291. // while (sqlite3_step(statement) == SQLITE_ROW)
  11292. // {
  11293. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11294. //
  11295. //
  11296. // char *name = (char*)sqlite3_column_text(statement, 0);
  11297. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11298. //
  11299. // char *description = (char*)sqlite3_column_text(statement, 1);
  11300. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11301. //
  11302. //
  11303. //
  11304. // int product_id = sqlite3_column_int(statement, 2);
  11305. //
  11306. //
  11307. // int wid = sqlite3_column_int(statement, 3);
  11308. // int closeout = sqlite3_column_int(statement, 4);
  11309. // int cid = sqlite3_column_int(statement, 5);
  11310. // int wisdelete = sqlite3_column_int(statement, 6);
  11311. // int more_color = sqlite3_column_int(statement, 7);
  11312. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11313. //
  11314. // if(wid !=0 && wisdelete != 1)
  11315. // [item setValue:@"true" forKey:@"wish_exists"];
  11316. // else
  11317. // [item setValue:@"false" forKey:@"wish_exists"];
  11318. //
  11319. // if(closeout==0)
  11320. // [item setValue:@"false" forKey:@"is_closeout"];
  11321. // else
  11322. // [item setValue:@"true" forKey:@"is_closeout"];
  11323. //
  11324. // if(cid==0)
  11325. // [item setValue:@"false" forKey:@"cart_exists"];
  11326. // else
  11327. // [item setValue:@"true" forKey:@"cart_exists"];
  11328. //
  11329. // if (more_color == 0) {
  11330. // [item setObject:@(false) forKey:@"more_color"];
  11331. // } else if (more_color == 1) {
  11332. // [item setObject:@(true) forKey:@"more_color"];
  11333. // }
  11334. //
  11335. // [item addEntriesFromDictionary:imgjson];
  11336. //
  11337. //
  11338. // // [item setValue:nsurl forKey:@"img"];
  11339. // [item setValue:nsname forKey:@"fash_name"];
  11340. // [item setValue:nsdescription forKey:@"description"];
  11341. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11342. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11343. // i++;
  11344. // }
  11345. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11346. // [ret setObject:items forKey:@"items"];
  11347. // sqlite3_finalize(statement);
  11348. // }
  11349. //
  11350. // [iSalesDB close_db:db];
  11351. //
  11352. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11353. //
  11354. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11355. // dispatch_async(dispatch_get_main_queue(), ^{
  11356. //
  11357. // result(ret);
  11358. // });
  11359. //
  11360. // });
  11361. //
  11362. //
  11363. //
  11364. //}
  11365. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11366. //{
  11367. // [self itemsearch:params limited:YES completionHandler:result];
  11368. //}
  11369. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11370. {
  11371. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11372. NSString* offline_command=params[@"offline_Command"];
  11373. NSMutableDictionary* ret=nil;
  11374. if([offline_command isEqualToString:@"model_NIYMAL"])
  11375. {
  11376. NSString* category = params[@"category"];
  11377. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11378. }
  11379. dispatch_async(dispatch_get_main_queue(), ^{
  11380. result(ret);
  11381. });
  11382. });
  11383. }
  11384. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11385. {
  11386. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11387. int sort = [[params objectForKey:@"sort"] intValue];
  11388. NSString *sort_str = @"";
  11389. switch (sort) {
  11390. case 0:{
  11391. sort_str = @"order by w.modify_time desc";
  11392. }
  11393. break;
  11394. case 1:{
  11395. sort_str = @"order by w.modify_time asc";
  11396. }
  11397. break;
  11398. case 2:{
  11399. sort_str = @"order by m.name asc";
  11400. }
  11401. break;
  11402. case 3:{
  11403. sort_str = @"order by m.name desc";
  11404. }
  11405. break;
  11406. case 4:{
  11407. sort_str = @"order by m.description asc";
  11408. }
  11409. break;
  11410. default:
  11411. break;
  11412. }
  11413. // NSString* user = appDelegate.user;
  11414. sqlite3 *db = [iSalesDB get_db];
  11415. // order by w.create_time
  11416. 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];
  11417. // 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];
  11418. sqlite3_stmt * statement;
  11419. NSDate *date1 = [NSDate date];
  11420. // NSDate *date2 = nil;
  11421. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11422. int count=0;
  11423. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11424. {
  11425. while (sqlite3_step(statement) == SQLITE_ROW)
  11426. {
  11427. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11428. int product_id = sqlite3_column_double(statement, 0);
  11429. char *description = (char*)sqlite3_column_text(statement, 1);
  11430. if(description==nil)
  11431. description= "";
  11432. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11433. int item_id = sqlite3_column_double(statement, 2);
  11434. NSDate *date_image = [NSDate date];
  11435. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11436. printf("image : ");
  11437. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11438. // char *url = (char*)sqlite3_column_text(statement, 3);
  11439. // if(url==nil)
  11440. // url="";
  11441. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11442. int qty = sqlite3_column_int(statement, 3);
  11443. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11444. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11445. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11446. item[@"description"]= nsdescription;
  11447. item[@"img"]= nsurl;
  11448. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11449. count++;
  11450. }
  11451. printf("total time:");
  11452. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11453. ret[@"count"]= [NSNumber numberWithInt:count];
  11454. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11455. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11456. ret[@"result"]= [NSNumber numberWithInt:2];
  11457. sqlite3_finalize(statement);
  11458. }
  11459. [iSalesDB close_db:db];
  11460. dispatch_async(dispatch_get_main_queue(), ^{
  11461. UIApplication * app = [UIApplication sharedApplication];
  11462. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11463. appDelegate.wish_count =count;
  11464. [appDelegate update_count_mark];
  11465. result(ret);
  11466. });
  11467. });
  11468. }
  11469. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11470. {
  11471. // UIApplication * app = [UIApplication sharedApplication];
  11472. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11473. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11474. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11475. sqlite3 *db = [iSalesDB get_db];
  11476. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11477. NSString* product_id=params[@"product_id"];
  11478. NSString *item_count_str = params[@"item_count"];
  11479. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11480. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11481. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11482. // NSString *sql = @"";
  11483. for(int i=0;i<arr.count;i++)
  11484. {
  11485. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11486. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11487. __block int cart_count = 0;
  11488. if (!item_count_str) {
  11489. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11490. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11491. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11492. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11493. cart_count = [[model_set_components lastObject] intValue];
  11494. }];
  11495. }
  11496. if(count==0)
  11497. {
  11498. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11499. sqlite3_stmt *stmt;
  11500. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11501. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11502. if (item_count_arr) {
  11503. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11504. } else {
  11505. sqlite3_bind_int(stmt,2,cart_count);
  11506. }
  11507. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11508. [iSalesDB execSql:@"ROLLBACK" db:db];
  11509. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11510. [iSalesDB close_db:db];
  11511. DebugLog(@"add to wishlist error");
  11512. dispatch_async(dispatch_get_main_queue(), ^{
  11513. result(ret);
  11514. });
  11515. }
  11516. } else {
  11517. int qty = 0;
  11518. if (item_count_arr) {
  11519. qty = [item_count_arr[i] intValue];
  11520. } else {
  11521. qty = cart_count;
  11522. }
  11523. 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]];
  11524. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11525. [iSalesDB execSql:@"ROLLBACK" db:db];
  11526. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11527. [iSalesDB close_db:db];
  11528. DebugLog(@"add to wishlist error");
  11529. dispatch_async(dispatch_get_main_queue(), ^{
  11530. result(ret);
  11531. });
  11532. }
  11533. }
  11534. }
  11535. // [iSalesDB execSql:sql db:db];
  11536. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11537. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11538. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11539. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11540. [iSalesDB close_db:db];
  11541. ret[@"result"]= [NSNumber numberWithInt:2];
  11542. dispatch_async(dispatch_get_main_queue(), ^{
  11543. UIApplication * app = [UIApplication sharedApplication];
  11544. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11545. appDelegate.wish_count =count;
  11546. [appDelegate update_count_mark];
  11547. result(ret);
  11548. });
  11549. });
  11550. }
  11551. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11552. {
  11553. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11554. bool bLogin = appDelegate.bLogin;
  11555. __block NSString* contact_id = appDelegate.contact_id;
  11556. __block NSString* user = appDelegate.user;
  11557. __block NSString* order_code = appDelegate.order_code;
  11558. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11559. NSString* model_name = [params valueForKey:@"product_name"];
  11560. NSString* product_id = [params valueForKey:@"product_id"];
  11561. NSString* upc_code = [params valueForKey:@"upc_code"];
  11562. NSString* category = [params valueForKey:@"category"];
  11563. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11564. if(category==nil) {
  11565. category = default_category_id;
  11566. } else {
  11567. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11568. // 参数重有多个category id
  11569. if (arr_0.count > 1) {
  11570. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11571. // 取交集
  11572. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11573. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11574. [set_0 intersectSet:set_1];
  11575. if (set_0.count == 1) {
  11576. category = (NSString *)[set_0 anyObject];
  11577. } else {
  11578. if ([set_0 containsObject:default_category_id]) {
  11579. category = default_category_id;
  11580. } else {
  11581. category = (NSString *)[set_0 anyObject];
  11582. }
  11583. }
  11584. }
  11585. }
  11586. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11587. sqlite3 *db = [iSalesDB get_db];
  11588. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11589. NSString *sqlQuery = nil;
  11590. if(product_id==nil)
  11591. if(model_name==nil)
  11592. {
  11593. 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='%@';
  11594. }
  11595. else
  11596. {
  11597. 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='%@';
  11598. }
  11599. else
  11600. 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=%@;
  11601. sqlite3_stmt * statement;
  11602. [ret setValue:@"2" forKey:@"result"];
  11603. [ret setValue:@"3" forKey:@"detail_section_count"];
  11604. // int count=0;
  11605. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11606. {
  11607. if (sqlite3_step(statement) == SQLITE_ROW)
  11608. {
  11609. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11610. char *name = (char*)sqlite3_column_text(statement, 0);
  11611. if(name==nil)
  11612. name="";
  11613. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11614. char *description = (char*)sqlite3_column_text(statement, 1);
  11615. if(description==nil)
  11616. description="";
  11617. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11618. int product_id = sqlite3_column_int(statement, 2);
  11619. char *color = (char*)sqlite3_column_text(statement, 3);
  11620. if(color==nil)
  11621. color="";
  11622. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11623. //
  11624. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11625. // if(legcolor==nil)
  11626. // legcolor="";
  11627. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11628. //
  11629. //
  11630. int availability = sqlite3_column_int(statement, 5);
  11631. //
  11632. int incoming_stock = sqlite3_column_int(statement, 6);
  11633. char *demension = (char*)sqlite3_column_text(statement, 7);
  11634. if(demension==nil)
  11635. demension="";
  11636. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11637. // ,,,,,,,,,
  11638. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11639. if(seat_height==nil)
  11640. seat_height="";
  11641. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11642. char *material = (char*)sqlite3_column_text(statement, 9);
  11643. if(material==nil)
  11644. material="";
  11645. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11646. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11647. if(box_dim==nil)
  11648. box_dim="";
  11649. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11650. char *volume = (char*)sqlite3_column_text(statement, 11);
  11651. if(volume==nil)
  11652. volume="";
  11653. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11654. double weight = sqlite3_column_double(statement, 12);
  11655. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11656. if(model_set==nil)
  11657. model_set="";
  11658. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11659. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11660. if(load_ability==nil)
  11661. load_ability="";
  11662. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11663. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11664. // if(default_category==nil)
  11665. // default_category="";
  11666. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11667. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11668. if(fabric_content==nil)
  11669. fabric_content="";
  11670. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11671. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11672. if(assembling==nil)
  11673. assembling="";
  11674. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11675. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11676. if(made_in==nil)
  11677. made_in="";
  11678. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11679. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11680. if(special_remarks==nil)
  11681. special_remarks="";
  11682. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11683. int stockUcom = sqlite3_column_double(statement, 20);
  11684. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11685. if(product_group==nil)
  11686. product_group="";
  11687. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11688. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11689. // if(fashion_selector==nil)
  11690. // fashion_selector="";
  11691. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11692. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11693. if(selector_field==nil)
  11694. selector_field="";
  11695. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11696. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11697. if(property_field==nil)
  11698. property_field="";
  11699. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11700. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11701. if(packaging==nil)
  11702. packaging="";
  11703. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11704. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11705. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11706. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11707. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11708. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11709. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11710. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11711. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11712. NSString* Availability=nil;
  11713. if(availability>0)
  11714. Availability=[NSString stringWithFormat:@"%d",availability];
  11715. else
  11716. Availability = @"Out of Stock";
  11717. [img_section setValue:Availability forKey:@"Availability"];
  11718. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11719. char *eta = (char*)sqlite3_column_text(statement, 25);
  11720. if(eta==nil)
  11721. eta="";
  11722. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11723. if ([nseta isEqualToString:@"null"]) {
  11724. nseta = @"";
  11725. }
  11726. if (availability <= 0) {
  11727. [img_section setValue:nseta forKey:@"ETA"];
  11728. }
  11729. int item_id = sqlite3_column_int(statement, 26);
  11730. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11731. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11732. NSString* Price=nil;
  11733. if(bLogin==false)
  11734. Price=@"Must Sign in.";
  11735. else
  11736. {
  11737. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11738. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11739. if(price==nil)
  11740. Price=@"No Price.";
  11741. else
  11742. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11743. }
  11744. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11745. [img_section setValue:Price forKey:@"price"];
  11746. [img_section setValue:nsname forKey:@"model_name"];
  11747. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11748. if (order_code) { // 离线order code即so#
  11749. 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];
  11750. __block int cartQTY = 0;
  11751. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11752. cartQTY = sqlite3_column_int(stmt, 0);
  11753. }];
  11754. if (cartQTY > 0) {
  11755. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11756. }
  11757. }
  11758. [ret setObject:img_section forKey:@"img_section"];
  11759. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11760. int detail0_item_count=0;
  11761. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11762. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11763. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11764. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11765. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11766. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11767. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11768. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11769. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11770. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11771. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11772. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11773. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11774. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11775. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  11776. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  11777. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  11778. {
  11779. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  11780. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11781. }
  11782. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  11783. [detail0_section setValue:@"kv" forKey:@"type"];
  11784. [detail0_section setValue:@"Product Information" forKey:@"title"];
  11785. [ret setObject:detail0_section forKey:@"detail_0"];
  11786. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  11787. // [detail1_section setValue:@"detail" forKey:@"target"];
  11788. // [detail1_section setValue:@"popup" forKey:@"action"];
  11789. // [detail1_section setValue:@"content" forKey:@"type"];
  11790. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  11791. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  11792. // [detail1_section setValue:@"true" forKey:@"single_row"];
  11793. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  11794. [ret setObject:detail1_section forKey:@"detail_1"];
  11795. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  11796. [detail2_section setValue:@"detail" forKey:@"target"];
  11797. [detail2_section setValue:@"popup" forKey:@"action"];
  11798. [detail2_section setValue:@"content" forKey:@"type"];
  11799. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  11800. [detail2_section setValue:@"true" forKey:@"single_row"];
  11801. [detail2_section setValue:@"local" forKey:@"data"];
  11802. [ret setObject:detail2_section forKey:@"detail_2"];
  11803. }
  11804. sqlite3_finalize(statement);
  11805. }
  11806. else
  11807. {
  11808. [ret setValue:@"8" forKey:@"result"];
  11809. }
  11810. // DebugLog(@"count:%d",count);
  11811. [iSalesDB close_db:db];
  11812. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11813. dispatch_async(dispatch_get_main_queue(), ^{
  11814. result(ret);
  11815. });
  11816. });
  11817. }
  11818. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11819. //{
  11820. //
  11821. // UIApplication * app = [UIApplication sharedApplication];
  11822. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11823. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11824. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11825. // sqlite3 *db = [iSalesDB get_db];
  11826. // NSString* collectId=params[@"collectId"];
  11827. //
  11828. //
  11829. //
  11830. //
  11831. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  11832. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  11833. // [iSalesDB execSql:sqlQuery db:db];
  11834. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11835. // [iSalesDB close_db:db];
  11836. //
  11837. //
  11838. // ret[@"result"]= [NSNumber numberWithInt:2];
  11839. // dispatch_async(dispatch_get_main_queue(), ^{
  11840. //
  11841. // appDelegate.wish_count =count;
  11842. //
  11843. // [appDelegate update_count_mark];
  11844. // result(ret);
  11845. // });
  11846. //
  11847. // });
  11848. //}
  11849. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11850. {
  11851. UIApplication * app = [UIApplication sharedApplication];
  11852. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11853. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11854. [iSalesDB disable_trigger];
  11855. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11856. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11857. [iSalesDB enable_trigger];
  11858. dispatch_async(dispatch_get_main_queue(), ^{
  11859. //
  11860. // NSString* user = [params valueForKey:@"user"];
  11861. //
  11862. // NSString* password = [params valueForKey:@"password"];
  11863. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11864. ret[@"result"]=[NSNumber numberWithInt:2 ];
  11865. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  11866. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  11867. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  11868. [appDelegate update_count_mark];
  11869. appDelegate.can_show_price =false;
  11870. appDelegate.can_see_price =false;
  11871. appDelegate.can_create_portfolio =false;
  11872. appDelegate.can_create_order =false;
  11873. appDelegate.can_cancel_order =false;
  11874. appDelegate.can_set_cart_price =false;
  11875. appDelegate.can_delete_order =false;
  11876. appDelegate.can_submit_order =false;
  11877. appDelegate.can_set_tearsheet_price =false;
  11878. appDelegate.can_update_contact_info = false;
  11879. appDelegate.save_order_logout = false;
  11880. appDelegate.submit_order_logout = false;
  11881. appDelegate.alert_sold_in_quantities = false;
  11882. appDelegate.ipad_perm =nil ;
  11883. appDelegate.user_type = USER_ROLE_UNKNOWN;
  11884. appDelegate.OrderFilter= nil;
  11885. [appDelegate SetSo:nil];
  11886. [appDelegate set_main_button_panel];
  11887. result(ret);
  11888. });
  11889. });
  11890. }
  11891. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11892. {
  11893. // iSalesDB.
  11894. // UIApplication * app = [UIApplication sharedApplication];
  11895. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11896. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11897. [iSalesDB disable_trigger];
  11898. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11899. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11900. [iSalesDB enable_trigger];
  11901. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11902. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  11903. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  11904. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  11905. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  11906. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  11907. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  11908. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  11909. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  11910. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  11911. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  11912. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  11913. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  11914. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  11915. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  11916. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  11917. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  11918. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  11919. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  11920. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  11921. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  11922. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  11923. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  11924. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11925. [arr_name addObject:customer_first_name];
  11926. [arr_name addObject:customer_last_name];
  11927. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11928. // default ship from
  11929. 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';";
  11930. __block NSString *cid = @"";
  11931. __block NSString *name = @"";
  11932. __block NSString *ext = @"";
  11933. __block NSString *contact = @"";
  11934. __block NSString *email = @"";
  11935. __block NSString *fax = @"";
  11936. __block NSString *phone = @"";
  11937. sqlite3 *db = [iSalesDB get_db];
  11938. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  11939. cid = [self textAtColumn:0 statement:statment];
  11940. name = [self textAtColumn:1 statement:statment];
  11941. ext = [self textAtColumn:2 statement:statment];
  11942. contact = [self textAtColumn:3 statement:statment];
  11943. email = [self textAtColumn:4 statement:statment];
  11944. fax = [self textAtColumn:5 statement:statment];
  11945. phone = [self textAtColumn:6 statement:statment];
  11946. }];
  11947. NSString* so_id = [self get_offline_soid:db];
  11948. if(so_id==nil)
  11949. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  11950. 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];
  11951. int result_code =[iSalesDB execSql:sql_neworder db:db];
  11952. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  11953. //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'
  11954. //soId
  11955. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  11956. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  11957. sqlite3_stmt * statement;
  11958. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11959. {
  11960. if (sqlite3_step(statement) == SQLITE_ROW)
  11961. {
  11962. // char *name = (char*)sqlite3_column_text(statement, 1);
  11963. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11964. //ret = sqlite3_column_int(statement, 0);
  11965. char *soId = (char*)sqlite3_column_text(statement, 0);
  11966. if(soId==nil)
  11967. soId="";
  11968. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  11969. [ret setValue:nssoId forKey:@"soId"];
  11970. [ret setValue:nssoId forKey:@"orderCode"];
  11971. }
  11972. sqlite3_finalize(statement);
  11973. }
  11974. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  11975. [ret setValue:@"Regular Mode" forKey:@"mode"];
  11976. [iSalesDB close_db:db];
  11977. dispatch_async(dispatch_get_main_queue(), ^{
  11978. result(ret);
  11979. });
  11980. });
  11981. }
  11982. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11983. {
  11984. UIApplication * app = [UIApplication sharedApplication];
  11985. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11986. bool can_create_backorder= appDelegate.can_create_backorder;
  11987. NSString* user = appDelegate.user;
  11988. NSString* contact_id = appDelegate.contact_id;
  11989. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11990. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11991. sqlite3 *db = [iSalesDB get_db];
  11992. NSString* orderCode=params[@"orderCode"];
  11993. int sort = [[params objectForKey:@"sort"] intValue];
  11994. NSString *sort_str = @"";
  11995. switch (sort) {
  11996. case 0:{
  11997. sort_str = @"order by c.modify_time desc";
  11998. }
  11999. break;
  12000. case 1:{
  12001. sort_str = @"order by c.modify_time asc";
  12002. }
  12003. break;
  12004. case 2:{
  12005. sort_str = @"order by m.name asc";
  12006. }
  12007. break;
  12008. case 3:{
  12009. sort_str = @"order by m.name desc";
  12010. }
  12011. break;
  12012. case 4:{
  12013. sort_str = @"order by m.description asc";
  12014. }
  12015. break;
  12016. default:
  12017. break;
  12018. }
  12019. 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 ];
  12020. // 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 ];
  12021. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12022. DebugLog(@"offline_login sql:%@",sqlQuery);
  12023. sqlite3_stmt * statement;
  12024. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12025. NSDate *date1 = [NSDate date];
  12026. int count=0;
  12027. int cart_count=0;
  12028. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12029. if ( dbresult== SQLITE_OK)
  12030. {
  12031. while (sqlite3_step(statement) == SQLITE_ROW)
  12032. {
  12033. // NSDate *row_date = [NSDate date];
  12034. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12035. int product_id = sqlite3_column_int(statement, 0);
  12036. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12037. int item_id = sqlite3_column_int(statement, 7);
  12038. NSString* Price=nil;
  12039. if(str_price==nil)
  12040. {
  12041. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12042. // NSDate *price_date = [NSDate date];
  12043. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12044. // DebugLog(@"price time interval");
  12045. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12046. if(price==nil)
  12047. Price=@"No Price.";
  12048. else
  12049. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12050. }
  12051. else
  12052. {
  12053. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12054. }
  12055. double discount = sqlite3_column_double(statement, 2);
  12056. int item_count = sqlite3_column_int(statement, 3);
  12057. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12058. NSString *nsline_note=nil;
  12059. if(line_note!=nil)
  12060. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12061. char *name = (char*)sqlite3_column_text(statement, 5);
  12062. NSString *nsname=nil;
  12063. if(name!=nil)
  12064. nsname= [[NSString alloc]initWithUTF8String:name];
  12065. char *description = (char*)sqlite3_column_text(statement, 6);
  12066. NSString *nsdescription=nil;
  12067. if(description!=nil)
  12068. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12069. int stockUom = sqlite3_column_int(statement, 8);
  12070. int _id = sqlite3_column_int(statement, 9);
  12071. int availability = sqlite3_column_int(statement, 10);
  12072. // NSDate *subtotal_date = [NSDate date];
  12073. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12074. // DebugLog(@"subtotal_date time interval");
  12075. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12076. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12077. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12078. int carton=[bsubtotaljson[@"carton"] intValue];
  12079. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12080. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12081. // NSDate *img_date = [NSDate date];
  12082. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12083. // DebugLog(@"img_date time interval");
  12084. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12085. itemjson[@"model"]=nsname;
  12086. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12087. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12088. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12089. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12090. itemjson[@"check"]=@"true";
  12091. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12092. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12093. itemjson[@"unit_price"]=Price;
  12094. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12095. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12096. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12097. itemjson[@"note"]=nsline_note;
  12098. if (!can_create_backorder) {
  12099. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12100. }
  12101. // NSDate *date2 = [NSDate date];
  12102. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12103. // DebugLog(@"model_bundle time interval");
  12104. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12105. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12106. count++;
  12107. // DebugLog(@"row time interval");
  12108. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12109. }
  12110. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12111. sqlite3_finalize(statement);
  12112. }
  12113. DebugLog(@"request cart total time interval");
  12114. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12115. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12116. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12117. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12118. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12119. ret[@"count"]=[NSNumber numberWithInt:count ];
  12120. ret[@"mode"]=@"Regular Mode";
  12121. [iSalesDB close_db:db];
  12122. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12123. DebugLog(@"general notes :%@",general_note);
  12124. ret[@"general_note"]= general_note;
  12125. dispatch_async(dispatch_get_main_queue(), ^{
  12126. result(ret);
  12127. });
  12128. });
  12129. }
  12130. @end