OLDataProvider.m 720 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "AppDelegate.h"
  15. #import "pdfCreator.h"
  16. #import "RASingleton.h"
  17. #import "QRCodeGenerator.h"
  18. #import "config.h"
  19. #import "ImageUtils.h"
  20. #import "RAConvertor.h"
  21. #import "ActiveViewController.h"
  22. #import "RANetwork.h"
  23. #import "RADataProvider.h"
  24. @interface OLDataProvider ()
  25. @end
  26. @implementation OLDataProvider
  27. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  28. {
  29. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  30. NSString* ret=@"No Price.";
  31. sqlite3_stmt * statement;
  32. // int count=0;
  33. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  34. {
  35. if (sqlite3_step(statement) == SQLITE_ROW)
  36. {
  37. char *price = (char*)sqlite3_column_text(statement, 0);
  38. if(price==nil)
  39. price="";
  40. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  41. double dp= [nsprice doubleValue];
  42. ret=[NSString stringWithFormat:@"%.2f",dp];
  43. }
  44. sqlite3_finalize(statement);
  45. }
  46. return ret;
  47. // // [iSalesDB close_db:db];
  48. //
  49. // return nil;
  50. //// if(dprice==DBL_MAX)
  51. //// ret= nil;
  52. //// else
  53. //// ret= [NSNumber numberWithDouble:dprice];
  54. //// return ret;
  55. }
  56. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  57. {
  58. NSString* ret= nil;
  59. NSString *sqlQuery = nil;
  60. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  61. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  62. sqlite3_stmt * statement;
  63. // int count=0;
  64. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  65. {
  66. if (sqlite3_step(statement) == SQLITE_ROW)
  67. {
  68. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  69. if(imgurl==nil)
  70. imgurl="";
  71. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  72. ret=nsimgurl;
  73. }
  74. sqlite3_finalize(statement);
  75. }
  76. else
  77. {
  78. ret=@"";
  79. }
  80. // [iSalesDB close_db:db];
  81. // DebugLog(@"data string: %@",ret );
  82. return ret;
  83. }
  84. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  85. {
  86. // assert(params[@"contact_id"]!=nil);
  87. assert(params[@"user"]!=nil);
  88. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  89. //生成portfolio pdf需要的数据
  90. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  91. data[@"npd_url"]=COMPANY_WEB;
  92. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  93. [formatter setDateFormat:@"MMMM yyyy"];
  94. NSString* date = [formatter stringFromDate:[NSDate date]];
  95. data[@"create_month"]=date;
  96. data[@"company_name"]=COMPANY_FULL_NAME;
  97. data[@"catalog_name"]=params[@"catalog_name"];
  98. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  99. data[@"TOTAL_PAGE"]=0;
  100. data[@"CURRENT_PAGE"]=0;
  101. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  102. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  103. bool show_stockout=[params[@"show_stockout"] boolValue];
  104. NSString *sql = nil;
  105. // v1.90 more color
  106. NSString *group_by = @"";
  107. if(values==nil)
  108. {
  109. if (show_group_by) {
  110. group_by = @"group by b.product_group ";
  111. }
  112. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id %@ order by a.name",params[@"product_ids"],params[@"product_ids"],group_by];
  113. }
  114. else
  115. {
  116. if (show_group_by) {
  117. group_by = @"group by product_group ";
  118. }
  119. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  120. }
  121. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  122. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  123. sqlite3 *db = [iSalesDB get_db];
  124. // NSDictionary *resultDic =
  125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  126. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  127. int product_id = sqlite3_column_int(stmt, 0);
  128. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  129. NSString *name = [self textAtColumn:1 statement:stmt];
  130. NSString *description = [self textAtColumn:2 statement:stmt];
  131. double price = sqlite3_column_double(stmt, 3);
  132. NSString *s_price = [self textAtColumn:3 statement:stmt];
  133. double discount = sqlite3_column_double(stmt,4);
  134. int qty = sqlite3_column_int(stmt, 5);
  135. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  136. int is_percentage = sqlite3_column_int(stmt, 6);
  137. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  138. int item_id = sqlite3_column_int(stmt, 7);
  139. // int fashion_id = sqlite3_column_int(stmt, 8);
  140. NSString *line_note = [self textAtColumn:9 statement:stmt];
  141. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  142. double percent = sqlite3_column_double(stmt, 10);
  143. // int portfolio_id = sqlite3_column_int(stmt, 11);
  144. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  145. int availability = sqlite3_column_int(stmt, 12);
  146. NSString *color = [self textAtColumn:13 statement:stmt];
  147. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  148. NSString *demension = [self textAtColumn:15 statement:stmt];
  149. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  150. NSString *material = [self textAtColumn:17 statement:stmt];
  151. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  152. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  153. double volume = sqlite3_column_double(stmt, 19);
  154. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  155. double weight = sqlite3_column_double(stmt, 20);
  156. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  157. int model_set = sqlite3_column_int(stmt, 21);
  158. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  159. double load_ability = sqlite3_column_double(stmt, 22);
  160. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  161. NSString *assembling = [self textAtColumn:24 statement:stmt];
  162. NSString *made_in = [self textAtColumn:25 statement:stmt];
  163. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  164. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  165. float bprice=0;
  166. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  167. for(int b=0;b< [bundle[@"count"] intValue];b++)
  168. {
  169. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  170. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  171. }
  172. if(gprice!=nil)
  173. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  174. if(s_percent.length==0||is_percentage==0)
  175. {
  176. percent=100.0;
  177. if([s_qty isEqualToString:@"null"])
  178. qty=availability;
  179. }
  180. else
  181. {
  182. qty=availability;
  183. }
  184. qty=qty*percent/100+0.5;
  185. for(int i=0;i<[values[@"count"] intValue];i++)
  186. {
  187. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  188. if([valueitem[@"product_id"] intValue]==product_id)
  189. {
  190. line_note=valueitem[@"line_note"];
  191. qty=[valueitem[@"available_qty"] intValue];
  192. if([[valueitem allKeys] containsObject:@"available_qty"])
  193. s_qty=[NSString stringWithFormat:@"%d",qty];
  194. else
  195. s_qty=@"null";
  196. if(valueitem[@"regular_price"]!=nil)
  197. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  198. s_price=valueitem[@"special_price"];
  199. price=[valueitem[@"special_price"] floatValue];
  200. discount=0;
  201. // v1.90
  202. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  203. }
  204. }
  205. if(is_percentage==0&&qty==0&&!show_stockout)
  206. {
  207. return;
  208. }
  209. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  210. {
  211. return;
  212. }
  213. NSString* set_price=@"";
  214. if([params[@"entered_price"] boolValue])
  215. {
  216. if (s_price==nil ) {
  217. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  218. }
  219. else
  220. {
  221. if(price*(1-discount/100.0)!=gprice.floatValue)
  222. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  223. }
  224. }
  225. NSString* get_price=@"";
  226. {
  227. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  228. // DebugLog(@"price time interval");
  229. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  230. if(gprice==nil)
  231. get_price=@"Price:No Price.";
  232. else
  233. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  234. }
  235. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  236. // if ([qty_null isEqualToString:@"null"]) {
  237. // qty=availability;
  238. // }
  239. //
  240. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  241. // if ([qty_percent_null isEqualToString:@"null"]) {
  242. // percentage=1;
  243. // }
  244. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  245. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  246. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  247. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  248. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  249. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  250. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  251. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  252. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  253. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  254. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  255. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  256. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  257. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  258. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  259. if([params[@"availability"] boolValue]==false)
  260. str_availability=@"";
  261. else
  262. {
  263. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  264. str_availability=@"<b>Availability:</b> In Production";
  265. }
  266. if([params[@"color"] boolValue]==false || color.length==0 )
  267. str_color=@"";
  268. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  269. str_model_set=@"";
  270. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  271. str_legcolor=@"";
  272. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  273. str_demension=@"";
  274. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  275. str_seat_height=@"";
  276. if([params[@"material"] boolValue]==false || material.length==0 )
  277. str_meterial=@"";
  278. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  279. str_box_dim=@"";
  280. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  281. str_volume=@"";
  282. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  283. str_weight=@"";
  284. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  285. str_load_ability=@"";
  286. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  287. str_fabric_content=@"";
  288. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  289. str_assembling=@"";
  290. if([params[@"made"] boolValue]==false || made_in.length==0 )
  291. str_made_in=@"";
  292. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  293. str_line_note=@"";
  294. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  295. [arr_detail addObject:str_availability];
  296. [arr_detail addObject:str_color];
  297. [arr_detail addObject:str_model_set];
  298. [arr_detail addObject:str_legcolor];
  299. [arr_detail addObject:str_demension];
  300. [arr_detail addObject:str_seat_height];
  301. [arr_detail addObject:str_meterial];
  302. [arr_detail addObject:str_box_dim];
  303. [arr_detail addObject:str_volume];
  304. [arr_detail addObject:str_weight];
  305. [arr_detail addObject:str_load_ability];
  306. [arr_detail addObject:str_fabric_content];
  307. [arr_detail addObject:str_assembling];
  308. [arr_detail addObject:str_made_in];
  309. [arr_detail addObject:str_line_note];
  310. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  311. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  312. //model image;
  313. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  314. // qr image
  315. NSString* qrpath=nil;
  316. if([params[@"show_barcode"] boolValue])
  317. {
  318. NSString* temp = NSTemporaryDirectory();
  319. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  320. qrpath=[temp stringByAppendingPathComponent:filename];
  321. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  322. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  323. }
  324. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  325. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  326. item[@"img"]=img_path;
  327. item[@"img_qr"]=qrpath;
  328. item[@"special_price"]=set_price;
  329. item[@"price"]=get_price;
  330. item[@"name"]=name;
  331. item[@"description"]=description;
  332. item[@"detail"]=detail;
  333. item[@"img"]=img_path;
  334. if (show_group_by && more_color_int != 0) {
  335. item[@"more_color"] = more_color_path;
  336. }
  337. // @{
  338. // //@"linenotes": line_note,
  339. //// @"product_id": product_id_string,
  340. //// @"available_qty": @(qty),
  341. //// @"available_percent" : @(percent),
  342. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  343. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  344. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"":
  352. // }.mutableCopy;
  353. // if (percentage) {
  354. // [item removeObjectForKey:@"available_qty"];
  355. // } else {
  356. // [item removeObjectForKey:@"available_percent"];
  357. // }
  358. //
  359. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  360. // if ([qty_null isEqualToString:@"null"]) {
  361. // [item removeObjectForKey:@"available_qty"];
  362. // }
  363. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  364. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  365. }];
  366. [iSalesDB close_db:db];
  367. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  368. // [dic setValue:@"" forKey:@"email_content"];
  369. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  370. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  371. //
  372. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  373. // cell[@"count"]=[NSNumber numberWithInt:10];
  374. grid[@"cell0"]=dic;
  375. data[@"grid0"]=grid;
  376. return data;
  377. }
  378. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  379. {
  380. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  381. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  382. NSString *cache_folder=[paths objectAtIndex:0];
  383. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  384. BOOL bdir=NO;
  385. NSFileManager* fileManager = [NSFileManager defaultManager];
  386. if(PDF_DEBUG)
  387. {
  388. NSData *data = [NSData dataWithContentsOfFile:default_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  393. {
  394. NSError * error=nil;
  395. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  396. {
  397. return nil;
  398. }
  399. }
  400. NSData *data = [NSData dataWithContentsOfFile:template_path];
  401. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  402. return ret;
  403. }
  404. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  405. {
  406. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  407. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  408. DebugLog(@"offline_login sql:%@",sqlQuery);
  409. sqlite3_stmt * statement;
  410. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  411. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  412. {
  413. if (sqlite3_step(statement) == SQLITE_ROW)
  414. {
  415. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  416. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  417. // int can_show_price = sqlite3_column_int(statement, 0);
  418. // int can_see_price = sqlite3_column_int(statement, 1);
  419. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  420. // if(contact_id==nil)
  421. // contact_id="";
  422. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  423. // int user_type = sqlite3_column_int(statement, 3);
  424. // int can_cancel_order = sqlite3_column_int(statement, 4);
  425. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  426. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  427. // int can_delete_order = sqlite3_column_int(statement, 7);
  428. // int can_submit_order = sqlite3_column_int(statement, 8);
  429. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  430. // int can_create_order = sqlite3_column_int(statement, 10);
  431. // char *mode = (char*)sqlite3_column_text(statement, 11);
  432. // if(mode==nil)
  433. // mode="";
  434. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  435. // char *username = (char*)sqlite3_column_text(statement, 12);
  436. // if(username==nil)
  437. // username="";
  438. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  439. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  440. char *first_name = (char*)sqlite3_column_text(statement, 14);
  441. if(first_name==nil)
  442. first_name="";
  443. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  444. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  445. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  446. // [header setValue:nscontact_id forKey:@"contact_id"];
  447. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  448. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  449. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  450. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  451. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  452. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  453. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  454. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  455. //
  456. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  457. //
  458. // [header setValue:nsusername forKey:@"username"];
  459. //
  460. // NSError* error=nil;
  461. //
  462. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  463. // [header setValue:statusFilter forKey:@"statusFilter"];
  464. //
  465. //
  466. //
  467. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  468. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  469. //
  470. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  471. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  472. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  473. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  474. //
  475. // [ret setObject:header forKey:@"header"];
  476. [ret setValue:nsfirst_name forKey:@"first_name"];
  477. }
  478. sqlite3_finalize(statement);
  479. }
  480. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  481. return ret;
  482. }
  483. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  484. {
  485. assert(user!=nil);
  486. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  487. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  488. data[@"npd_url"]=COMPANY_WEB;
  489. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  490. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  491. NSString* date = [formatter stringFromDate:[NSDate date]];
  492. data[@"print_date"]=date;
  493. data[@"company_name"]=COMPANY_FULL_NAME;
  494. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  495. data[@"TOTAL_PAGE"]=0;
  496. data[@"CURRENT_PAGE"]=0;
  497. // [formatter setDateFormat:@"MM/dd/yyyy"];
  498. // date = [formatter stringFromDate:[NSDate date]];
  499. // data[@"creat_date"]=date;
  500. NSString* temp = NSTemporaryDirectory();
  501. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  502. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  503. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  504. // bool bwrite=
  505. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  506. data[@"img_barcode"]=qrpath;
  507. data[@"so_id"]=order_code;
  508. data[@"company"]=COMPANY_FULL_NAME;
  509. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  510. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  511. sqlite3 *db = [iSalesDB get_db];
  512. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  513. sqlite3_stmt * statement;
  514. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  515. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  516. row0[@"count"]=[NSNumber numberWithInt:1];
  517. datasource[@"row0"]=row0;
  518. NSString* print_status=@"";
  519. NSString* order_type=@"Offline Quote";
  520. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  521. {
  522. if (sqlite3_step(statement) == SQLITE_ROW)
  523. {
  524. int erpOrderStatus = sqlite3_column_int(statement, 0);
  525. int status = sqlite3_column_int(statement, 26);
  526. if(status==2)
  527. {
  528. switch (erpOrderStatus) {
  529. case 10:
  530. // order_type=@"Quoted Order";
  531. print_status=@"Quote";
  532. break;
  533. case 11:
  534. print_status=@"Booked";
  535. break;
  536. case 12:
  537. print_status=@"Picked";
  538. break;
  539. case 13:
  540. print_status=@"Shipped";
  541. break;
  542. case 14:
  543. print_status=@"Closed";
  544. break;
  545. case 15:
  546. print_status=@"Canceled";
  547. break;
  548. case 16:
  549. print_status=@"All";
  550. break;
  551. case 18:
  552. print_status=@"Processing";
  553. break;
  554. default:
  555. break;
  556. }
  557. }
  558. else{
  559. switch (status) {
  560. case 0:
  561. {
  562. print_status=@"Quote";
  563. break;
  564. }
  565. case 1:
  566. {
  567. print_status=@"Saved";
  568. break;
  569. }
  570. case 3:
  571. {
  572. //canceled
  573. print_status=@"Canceled";
  574. break;
  575. }
  576. default:
  577. break;
  578. }
  579. }
  580. NSString* sender_name = [self textAtColumn:1 statement:statement];
  581. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  582. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  583. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  584. NSString* sender_email = [self textAtColumn:5 statement:statement];
  585. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  586. NSString* billing_name = [self textAtColumn:7 statement:statement];
  587. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  588. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  589. if(billing_phone.length>0)
  590. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  591. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  592. if(billing_fax.length>0)
  593. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  594. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  595. [billto_arr0 addObject:billing_phone];
  596. [billto_arr0 addObject:billing_fax];
  597. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  598. NSString* billing_email = [self textAtColumn:11 statement:statement];
  599. if(billing_email.length>0)
  600. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  601. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  602. if(billing_contact.length>0)
  603. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  604. NSString* receive_name = [self textAtColumn:12 statement:statement];
  605. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  606. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  607. if(receive_phone.length>0)
  608. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  609. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  610. if(receive_fax.length>0)
  611. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  612. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  613. [receive_arr0 addObject:receive_phone];
  614. [receive_arr0 addObject:receive_fax];
  615. NSString* receive_email = [self textAtColumn:16 statement:statement];
  616. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  617. if(receive_email.length>0)
  618. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  619. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  620. if(receive_contact.length>0)
  621. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  622. NSString* poNumber = [self textAtColumn:17 statement:statement];
  623. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  624. NSString* carrier = [self textAtColumn:19 statement:statement];
  625. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  626. NSString* terms = [self textAtColumn:21 statement:statement];
  627. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  628. NSString* port_destination = [self textAtColumn:23 statement:statement];
  629. NSString* etd = [self textAtColumn:24 statement:statement];
  630. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  631. if(sign_picpath.length==0)
  632. sign_picpath=nil;
  633. NSString* create_date = [self textAtColumn:27 statement:statement];
  634. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  635. NSString* general_notes=[self textAtColumn:33 statement:statement];
  636. NSString *customerID = [self textAtColumn:34 statement:statement];
  637. if (customerID.length == 0) {
  638. customerID = nil;
  639. }
  640. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  641. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  642. if(general_notes.length>0)
  643. {
  644. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  645. row2[@"item_0"]=@{@"g_notes":general_notes };
  646. row2[@"count"]=[NSNumber numberWithInt:1];
  647. datasource[@"row2"]=row2;
  648. }
  649. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  650. [billto_arr addObject:billing_ext];
  651. [billto_arr addObject:billing_pf];
  652. [billto_arr addObject:billing_contact];
  653. [billto_arr addObject:billing_email];
  654. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  655. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  656. [receive_arr addObject:receive_ext];
  657. [receive_arr addObject:receive_pf];
  658. [receive_arr addObject:receive_contact];
  659. [receive_arr addObject:receive_email];
  660. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  661. float shipping = sqlite3_column_double(statement, 28);
  662. float lift_gate_value = sqlite3_column_double(statement, 29);
  663. float handling_fee_value = sqlite3_column_double(statement, 30);
  664. data[@"company"]=sender_name;
  665. data[@"shipfrom_addr"]=sender_ext;
  666. data[@"shipfrom_phone"]=sender_phone;
  667. data[@"shipfrom_fax"]=sender_fax;
  668. data[@"shipfrom_email"]=sender_email;
  669. data[@"warehouse_name"]=warehouse_name;
  670. data[@"bill_to_name"]=billing_name;
  671. data[@"bill_to_ext"]=billto;
  672. data[@"ship_to_name"]=receive_name;
  673. data[@"ship_to_ext"]=receive;
  674. data[@"po_no"]=poNumber;
  675. data[@"rep"]=sales_rep;
  676. data[@"e_schdule"]=etd;
  677. data[@"sales_terms"]=sales_terms;
  678. data[@"port_destination"]=port_destination;
  679. data[@"terms"]=terms;
  680. data[@"truck_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) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1363. {
  1364. if(submitlist==nil)
  1365. return;
  1366. NSString *userdir = [OLDataProvider getUserPath];
  1367. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1368. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1369. }
  1370. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1371. {
  1372. if(orderlist==nil)
  1373. return;
  1374. NSString *userdir = [OLDataProvider getUserPath];
  1375. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1376. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1377. }
  1378. +(NSMutableDictionary*) getScanSubmitList
  1379. {
  1380. NSString *userdir = [OLDataProvider getUserPath];
  1381. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1382. NSMutableDictionary* submitlist = nil;
  1383. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1384. submitlist = [NSMutableDictionary new];
  1385. }
  1386. else
  1387. {
  1388. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1389. }
  1390. return submitlist;
  1391. }
  1392. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1393. //// NSString *userdir = [self getUserPath:user];
  1394. // NSMutableDictionary* orderlist = nil;
  1395. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1396. //
  1397. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1398. //
  1399. //
  1400. // orderlist = [NSMutableDictionary new];
  1401. //
  1402. // // [orderlist addObject:appDelegate.order_code];
  1403. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1404. //
  1405. // }
  1406. // else
  1407. // {
  1408. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1409. //
  1410. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1411. //
  1412. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1413. // }
  1414. // return orderlist;
  1415. //}
  1416. +(NSMutableDictionary*) getScanOrderList{
  1417. NSString *userdir = [OLDataProvider getUserPath];
  1418. NSMutableDictionary* orderlist = nil;
  1419. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1420. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1421. orderlist = [NSMutableDictionary new];
  1422. // [orderlist addObject:appDelegate.order_code];
  1423. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1424. }
  1425. else
  1426. {
  1427. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1428. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1429. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1430. }
  1431. return orderlist;
  1432. }
  1433. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1434. {
  1435. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1436. // NSString *documents = [paths objectAtIndex:0];
  1437. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1438. // NSString *userdir = [OLDataProvider getUserPath];
  1439. NSMutableDictionary* orderlist = [self getScanOrderList];
  1440. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1441. int count = [orderlist[@"count"] intValue];
  1442. if(count==0)
  1443. {
  1444. // order list 为空,不用维护
  1445. #ifdef RA_NOTIFICATION
  1446. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1447. #else
  1448. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1449. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1450. #endif
  1451. return;
  1452. }
  1453. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1454. //
  1455. // // order list 为空,不用维护
  1456. //
  1457. //
  1458. // // [orderlist addObject:appDelegate.order_code];
  1459. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1460. //
  1461. // }
  1462. // else
  1463. // {
  1464. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1465. //
  1466. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1467. //
  1468. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1469. // }
  1470. //==================== order list 删除
  1471. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1472. NSMutableDictionary* submititem =nil;
  1473. int newcount = 0;
  1474. for(int i=0;i<count;i++)
  1475. {
  1476. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1477. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1478. {
  1479. submititem = item;
  1480. continue;
  1481. }
  1482. else
  1483. {
  1484. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1485. newcount++;
  1486. }
  1487. }
  1488. if(submititem==nil)
  1489. {
  1490. //order list 里没有和当前打开的order code一致的order
  1491. //这种情况往往是因为重复点提交按钮,order已关闭。
  1492. return;
  1493. }
  1494. newlist[@"count"] = @(newcount);
  1495. [self saveScanOrderList:newlist];
  1496. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1497. //////////////////---=============================== submitlist 添加
  1498. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1499. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1500. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1501. //
  1502. // submitlist = [NSMutableDictionary new];
  1503. //
  1504. //
  1505. // }
  1506. // else
  1507. // {
  1508. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1509. //
  1510. //
  1511. // }
  1512. int submitcount =[submitlist[@"count"] intValue];
  1513. submititem[@"order_status"] = soid;//@"Submitted Order";
  1514. submititem[@"customer_name"] = company;//@"Submitted Order";
  1515. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1516. submitlist[@"count"] = @(submitcount+1);
  1517. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1518. [self saveScanSubmitList:submitlist];
  1519. #ifdef RA_NOTIFICATION
  1520. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1521. #else
  1522. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1523. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1524. #endif
  1525. }
  1526. +(void) save2submitScanOrder1
  1527. {
  1528. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1529. // NSString *documents = [paths objectAtIndex:0];
  1530. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1531. // NSString *userdir = [OLDataProvider getUserPath];
  1532. NSMutableDictionary* orderlist = [self getScanOrderList];
  1533. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1534. int count = [orderlist[@"count"] intValue];
  1535. if(count==0)
  1536. {
  1537. // order list 为空,不用维护
  1538. #ifdef RA_NOTIFICATION
  1539. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1540. #else
  1541. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1542. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1543. #endif
  1544. return;
  1545. return;
  1546. }
  1547. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1548. int newcount = 0;
  1549. for(int i=0;i<count;i++)
  1550. {
  1551. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1552. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1553. continue;
  1554. else
  1555. {
  1556. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1557. newcount++;
  1558. }
  1559. }
  1560. newlist[@"count"] = @(newcount);
  1561. [self saveScanOrderList:newlist];
  1562. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1563. #ifdef RA_NOTIFICATION
  1564. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1565. #else
  1566. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1567. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1568. #endif
  1569. }
  1570. //+(void) createScanTempFolder
  1571. //{
  1572. // return;
  1573. //// NSString* scanFilePath = [self getScanTempPath];
  1574. //// BOOL bdir=YES;
  1575. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1576. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1577. //// {
  1578. ////
  1579. //// NSError *error = nil;
  1580. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1581. ////
  1582. //// if(!bsuccess)
  1583. //// DebugLog(@"Create cache folder failed");
  1584. ////
  1585. //// // if(bsuccess)
  1586. //// // {
  1587. //// // sqlite3 *db = [self get_db];
  1588. //// //
  1589. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1590. //// // [iSalesDB close_db:db];
  1591. //// // }
  1592. //// }
  1593. //}
  1594. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1595. {
  1596. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1597. NSString *documents = [paths objectAtIndex:0];
  1598. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1599. NSString* servername = addressDic[@"name"];
  1600. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1601. NSString* user = appDelegate.user;
  1602. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1603. orderpath = [orderpath stringByAppendingPathComponent:user];
  1604. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1605. BOOL bdir=YES;
  1606. NSFileManager* fileManager = [NSFileManager defaultManager];
  1607. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1608. {
  1609. NSError *error = nil;
  1610. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1611. if(!bsuccess)
  1612. DebugLog(@"Create cache folder failed");
  1613. // if(bsuccess)
  1614. // {
  1615. // sqlite3 *db = [self get_db];
  1616. //
  1617. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1618. // [iSalesDB close_db:db];
  1619. // }
  1620. }
  1621. return orderpath;
  1622. }
  1623. +(NSString*) getScanOrderPath
  1624. {
  1625. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1626. NSString *documents = [paths objectAtIndex:0];
  1627. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1628. NSString* servername = addressDic[@"name"];
  1629. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1630. NSString* user = appDelegate.user;
  1631. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1632. orderpath = [orderpath stringByAppendingPathComponent:user];
  1633. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1634. BOOL bdir=YES;
  1635. NSFileManager* fileManager = [NSFileManager defaultManager];
  1636. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1637. {
  1638. NSError *error = nil;
  1639. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1640. if(!bsuccess)
  1641. DebugLog(@"Create cache folder failed");
  1642. // if(bsuccess)
  1643. // {
  1644. // sqlite3 *db = [self get_db];
  1645. //
  1646. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1647. // [iSalesDB close_db:db];
  1648. // }
  1649. }
  1650. return orderpath;
  1651. }
  1652. //+(NSString*) getScanTempPath
  1653. //{
  1654. // return nil;
  1655. ////
  1656. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1657. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1658. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1659. //}
  1660. //+(void) moveScanTemp2Order
  1661. //{
  1662. // return;
  1663. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1664. ////
  1665. //// NSError *error = nil;
  1666. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1667. ////
  1668. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1669. //// // for(int i=0;i<arr_files.count;i++)
  1670. //// // {
  1671. //// // NSString* file=arr_files[i];
  1672. //// //
  1673. //// //
  1674. //// // }
  1675. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1676. ////
  1677. //
  1678. //}
  1679. //+(NSString*) getUserPath: (NSString*) user
  1680. //{
  1681. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1682. // NSString *documents = [paths objectAtIndex:0];
  1683. //
  1684. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1685. //
  1686. // NSString* servername = addressDic[@"name"];
  1687. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1688. //// NSString* user = appDelegate.user;
  1689. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1690. // userpath = [userpath stringByAppendingPathComponent:user];
  1691. // return userpath;
  1692. //}
  1693. +(NSString*) getServerPath
  1694. {
  1695. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1696. NSString *documents = [paths objectAtIndex:0];
  1697. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1698. NSString* servername = addressDic[@"name"];
  1699. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1700. // NSString* user = appDelegate.user;
  1701. // NSString* user = RASingleton.sharedInstance.user;
  1702. NSString* serverpath = [documents stringByAppendingPathComponent:servername];
  1703. // userpath = [userpath stringByAppendingPathComponent:user];
  1704. NSFileManager* fileManager = [NSFileManager defaultManager];
  1705. BOOL bdir=YES;
  1706. if(! [fileManager fileExistsAtPath:serverpath isDirectory:&bdir])
  1707. {
  1708. NSError *error = nil;
  1709. bool bsuccess=[fileManager createDirectoryAtPath:serverpath withIntermediateDirectories:YES attributes:nil error:&error];
  1710. }
  1711. return serverpath;
  1712. }
  1713. +(NSString*) getUserPath
  1714. {
  1715. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1716. NSString *documents = [paths objectAtIndex:0];
  1717. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1718. NSString* servername = addressDic[@"name"];
  1719. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1720. // NSString* user = appDelegate.user;
  1721. NSString* user = RASingleton.sharedInstance.user;
  1722. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1723. userpath = [userpath stringByAppendingPathComponent:user];
  1724. NSFileManager* fileManager = [NSFileManager defaultManager];
  1725. BOOL bdir=YES;
  1726. if(! [fileManager fileExistsAtPath:userpath isDirectory:&bdir])
  1727. {
  1728. NSError *error = nil;
  1729. bool bsuccess=[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:&error];
  1730. }
  1731. return userpath;
  1732. }
  1733. +(bool) isSavedScanOrder:(NSString*) order_code
  1734. {
  1735. if(order_code == nil)
  1736. return false;
  1737. NSMutableDictionary * orderlist=[self getScanOrderList];
  1738. int count = [orderlist[@"count"] intValue];
  1739. for(int i=0;i<count;i++)
  1740. {
  1741. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1742. if([item[@"order_code"] isEqualToString: order_code ])
  1743. {
  1744. return true;
  1745. }
  1746. }
  1747. return false;
  1748. }
  1749. +(NSString*) getScanPath
  1750. {
  1751. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1752. if(appDelegate.order_code==nil)
  1753. {
  1754. appDelegate.order_code=[[NSUUID new] UUIDString];
  1755. }
  1756. return [self getScanOrderPath];
  1757. // else
  1758. // {
  1759. // [self createScanTempFolder];
  1760. // return [self getScanTempPath];
  1761. // }
  1762. }
  1763. +(void) scanCloseOrder
  1764. {
  1765. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1766. {
  1767. // 当前订单先关闭
  1768. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1769. appDelegate.order_code = nil;
  1770. // RASingleton.sharedInstance.scan_temp_code = nil;
  1771. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1772. { appDelegate.customerInfo = nil;
  1773. appDelegate.contact_id = nil;
  1774. }
  1775. RASingleton.sharedInstance.scan_cart = nil;
  1776. RASingleton.sharedInstance.price_type = 1;
  1777. // [appDelegate updateScanButton:false];
  1778. [appDelegate update_count_mark];
  1779. [appDelegate closeOrder];
  1780. }
  1781. }
  1782. +(NSString*) createScanTempCode
  1783. {
  1784. return [[NSUUID new] UUIDString];
  1785. }
  1786. +(NSString*) scanTemplatePath:(NSString*)file
  1787. {
  1788. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1789. NSString *documents = [paths objectAtIndex:0];
  1790. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1791. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1792. return templatefile;
  1793. }
  1794. +(void) realguestLogin
  1795. {
  1796. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1797. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1798. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1799. }
  1800. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1801. {
  1802. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1803. NSString *documents = [paths objectAtIndex:0];
  1804. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1805. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1806. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1807. return [dict mutableCopy];
  1808. }
  1809. +(void) deleteSubmittedOrder:(NSString*) order_code
  1810. {
  1811. NSString *userdir = [OLDataProvider getUserPath];
  1812. if(order_code.length==0)
  1813. return;
  1814. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1815. // NSString *documents = [paths objectAtIndex:0];
  1816. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1817. // if([appDelegate.order_code isEqualToString:order_code])
  1818. // {
  1819. // //要删除的订单是当前打开的订单;
  1820. // appDelegate.order_code = nil;
  1821. // appDelegate.customerInfo = nil;
  1822. // RASingleton.sharedInstance.scan_cart = nil;
  1823. // appDelegate.contact_id = nil;
  1824. // // [appDelegate updateScanButton:false];
  1825. //
  1826. // [appDelegate update_count_mark];
  1827. // }
  1828. //删除订单目录
  1829. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1830. //维护订单列表
  1831. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1832. int count = [submitlist[@"count"] intValue];
  1833. if(count==0)
  1834. {
  1835. // order list 为空,不用维护
  1836. #ifdef RA_NOTIFICATION
  1837. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1838. #else
  1839. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1840. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1841. #endif
  1842. return;
  1843. }
  1844. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1845. //
  1846. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1847. //
  1848. //
  1849. //
  1850. // // [orderlist addObject:appDelegate.order_code];
  1851. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1852. //
  1853. // }
  1854. // else
  1855. // {
  1856. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1857. //
  1858. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1859. //
  1860. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1861. // }
  1862. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1863. int newcount = 0;
  1864. for(int i=0;i<count;i++)
  1865. {
  1866. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1867. if([item[@"order_code"] isEqualToString: order_code ])
  1868. continue;
  1869. else
  1870. {
  1871. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1872. newcount++;
  1873. }
  1874. }
  1875. newlist[@"count"] = @(newcount);
  1876. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1877. [self saveScanSubmitList:newlist];
  1878. #ifdef RA_NOTIFICATION
  1879. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1880. #else
  1881. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1882. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1883. #endif
  1884. }
  1885. +(void) updateScanOrder:(NSString*) order_code
  1886. {
  1887. #ifdef SCANNER_ORDER
  1888. NSString *userdir = [OLDataProvider getUserPath];
  1889. if(order_code.length==0)
  1890. return;
  1891. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1892. // NSString *documents = [paths objectAtIndex:0];
  1893. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1894. NSMutableDictionary* orderlist = [self getScanOrderList];
  1895. int count = [orderlist[@"count"] intValue];
  1896. if(count==0)
  1897. {
  1898. // order list 为空,不用维护
  1899. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1900. return;
  1901. }
  1902. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1903. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1904. //
  1905. //
  1906. //
  1907. // // [orderlist addObject:appDelegate.order_code];
  1908. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1909. //
  1910. // }
  1911. // else
  1912. // {
  1913. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1914. //
  1915. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1916. //
  1917. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1918. // }
  1919. //
  1920. //
  1921. // int count = [orderlist[@"count"] intValue];
  1922. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1923. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1924. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1925. float p =0;
  1926. float s =0;
  1927. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1928. {
  1929. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1930. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1931. int mcount = [cartitem[@"count"] intValue];
  1932. double msurcharge =0;
  1933. // remove net price
  1934. // if(RASingleton.sharedInstance.price_type==1)
  1935. // {
  1936. // if([cartitem[@"special_price"] boolValue])
  1937. // {
  1938. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1939. // }
  1940. // else
  1941. // {
  1942. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1943. // }
  1944. // }
  1945. s+=msurcharge;
  1946. }
  1947. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1948. int order_qty=0;
  1949. for(int c=0;c<[section[@"count"] intValue];c++)
  1950. {
  1951. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1952. order_qty+=[item[@"count"] intValue];
  1953. }
  1954. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1955. // int newcount = 0;
  1956. for(int i=0;i<count;i++)
  1957. {
  1958. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1959. if([item[@"order_code"] isEqualToString: order_code ])
  1960. {
  1961. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1962. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1963. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1964. }
  1965. }
  1966. // newlist[@"count"] = @(newcount);
  1967. [self saveScanOrderList:orderlist];
  1968. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1969. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1970. #endif
  1971. }
  1972. +(void) deleteScanOrder:(NSString*) order_code
  1973. {
  1974. NSString *userdir = [OLDataProvider getUserPath];
  1975. if(order_code.length==0)
  1976. return;
  1977. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1978. // NSString *documents = [paths objectAtIndex:0];
  1979. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1980. if([appDelegate.order_code isEqualToString:order_code])
  1981. {
  1982. //要删除的订单是当前打开的订单;
  1983. // appDelegate.order_code = nil;
  1984. // appDelegate.customerInfo = nil;
  1985. // RASingleton.sharedInstance.scan_cart = nil;
  1986. // appDelegate.contact_id = nil;
  1987. [self scanCloseOrder];
  1988. // [appDelegate updateScanButton:false];
  1989. [appDelegate update_count_mark];
  1990. }
  1991. //删除订单目录
  1992. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1993. //维护订单列表
  1994. NSMutableDictionary* orderlist = [self getScanOrderList];
  1995. int count = [orderlist[@"count"] intValue];
  1996. if(count==0)
  1997. {
  1998. // order list 为空,不用维护
  1999. #ifdef RA_NOTIFICATION
  2000. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2001. #else
  2002. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2003. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2004. #endif
  2005. return;
  2006. }
  2007. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  2008. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  2009. //
  2010. //
  2011. //
  2012. // // [orderlist addObject:appDelegate.order_code];
  2013. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2014. //
  2015. // }
  2016. // else
  2017. // {
  2018. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  2019. //
  2020. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  2021. //
  2022. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  2023. // }
  2024. NSMutableDictionary* newlist = [NSMutableDictionary new];
  2025. int newcount = 0;
  2026. for(int i=0;i<count;i++)
  2027. {
  2028. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  2029. if([item[@"order_code"] isEqualToString: order_code ])
  2030. continue;
  2031. else
  2032. {
  2033. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  2034. newcount++;
  2035. }
  2036. }
  2037. newlist[@"count"] = @(newcount);
  2038. [self saveScanOrderList:newlist];
  2039. // [RAUtils dicttofile:orderlistPath dict:newlist];
  2040. #ifdef RA_NOTIFICATION
  2041. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2042. #else
  2043. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2044. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  2045. #endif
  2046. }
  2047. #ifdef SCANNER_ORDER
  2048. +(void) scanRefreshCart
  2049. {
  2050. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2051. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  2052. NSMutableDictionary * section = cart[@"section_0"];
  2053. NSMutableDictionary* newsection = [NSMutableDictionary new];
  2054. int count = [section[@"count"] intValue];
  2055. int newcount = 0;
  2056. for(int i=0;i<count;i++)
  2057. {
  2058. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2059. NSString* model = item[@"model"];
  2060. NSDictionary* modeljson=appDelegate.scan_model[model];
  2061. if(modeljson==nil)
  2062. continue;
  2063. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  2064. cartitem[@"note"]=item[@"note"];
  2065. cartitem[@"discount"]=item[@"discount"];
  2066. // [item removeObjectForKey:@"discount"];
  2067. // [item removeObjectForKey:@"note"];
  2068. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  2069. newcount++;
  2070. }
  2071. newsection[@"count"]=@(newcount);
  2072. newsection[@"available"] = @(1);
  2073. newsection[@"title"] = @"Available List";
  2074. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  2075. }
  2076. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  2077. {
  2078. // _modelJson = modelJson;
  2079. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2080. // self.labelPrice0.text = appDelegate.price0_name;
  2081. // self.labelPrice1.text = appDelegate.price1_name;
  2082. // self.labelPrice2.text = appDelegate.price2_name;
  2083. // self.labelPrice3.text = appDelegate.price3_name;
  2084. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2085. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  2086. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  2087. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  2088. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  2089. NSLog( [RAConvertor dict2string:_modelJson]);
  2090. NSString * port=_modelJson[@"port"];
  2091. if(port.length==0)
  2092. port = @"N/A";
  2093. NSString * origin=_modelJson[@"origin"];
  2094. if(origin.length==0)
  2095. origin = @"N/A";
  2096. NSString * dimension=_modelJson[@"dimension"];
  2097. if(dimension.length==0)
  2098. dimension = @"N/A";
  2099. // _labelModel.text=_modelJson[@"model"];
  2100. // _labelDescription.text=_modelJson[@"description"];
  2101. // _labelDimension.text=dimension;
  2102. // _labelCuft.text=_modelJson[@"unit_cuft"];
  2103. // _labelOrigin.text=origin;
  2104. // _labelPort.text=port;
  2105. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  2106. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  2107. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  2108. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  2109. if(![price0 isEqualToString: @"N/A"])
  2110. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  2111. if(![price1 isEqualToString: @"N/A"])
  2112. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  2113. if(![price2 isEqualToString: @"N/A"])
  2114. {
  2115. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  2116. // if(RASingleton.sharedInstance.price_type==1)
  2117. _modelJson [@"special_price"] = @true;
  2118. }
  2119. if(![price3 isEqualToString: @"N/A"])
  2120. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  2121. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  2122. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  2123. {
  2124. // if(RASingleton.sharedInstance.price_type==1)
  2125. _modelJson [@"net_price"] = @true;
  2126. // remove net price
  2127. _modelJson [@"net_price"] = @false;
  2128. }
  2129. //
  2130. // if(![price1 isEqualToString:@"N/A"])
  2131. // {
  2132. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  2133. // _modelJson[@"price2"]= price2;
  2134. // }
  2135. // else
  2136. // {
  2137. // price2=@"N/A";
  2138. // }
  2139. //
  2140. // if([price0 isEqualToString:@"N/A"])
  2141. // _labelPriceCTNR.text= @"";//price0;
  2142. // else
  2143. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2144. //
  2145. // if([price3 isEqualToString:@"N/A"])
  2146. // _labelPriceNCA.text= price3;
  2147. // else
  2148. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2149. // _labelPrice25p.text=price2;
  2150. //
  2151. // if([_modelJson [@"special_price"] boolValue])
  2152. // {
  2153. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2154. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2155. // else
  2156. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2157. // }
  2158. // else
  2159. // {
  2160. // _labelPriceSpecial.text=@"";//@"N/A";
  2161. // }
  2162. //
  2163. // if([_modelJson [@"net_price"] boolValue])
  2164. // {
  2165. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2166. //// {
  2167. //// _labelPriceNet.text=_modelJson[@"price1"];
  2168. ////// _labelPriceNCA.text=@"N/A";
  2169. //// }
  2170. //// else
  2171. //// {
  2172. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2173. ////// _labelPriceNCA.text=@"N/A";
  2174. //// }
  2175. //
  2176. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2177. // {
  2178. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2179. //// _labelPriceNCA.text=@"N/A";
  2180. // }
  2181. // else
  2182. // {
  2183. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2184. //// _labelPriceNCA.text=@"N/A";
  2185. // }
  2186. //
  2187. // }
  2188. // else
  2189. // {
  2190. // _labelPriceNet.text=@"";//@"N/A";
  2191. // }
  2192. //
  2193. // {
  2194. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2195. // {
  2196. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2197. //// _labelPriceNCA.text=@"N/A";
  2198. // }
  2199. // else
  2200. // {
  2201. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2202. //// _labelPriceNet.text=@"N/A";
  2203. // }
  2204. // }
  2205. NSString * available=_modelJson[@"available"];
  2206. if(available.length==0)
  2207. available = @"N/A";
  2208. // _labelAvailable.text=available;
  2209. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2210. if(unit_cuft.length==0)
  2211. unit_cuft = @"N/A";
  2212. else
  2213. {
  2214. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2215. }
  2216. NSString * cell_price;
  2217. if(RASingleton.sharedInstance.price_type==1)
  2218. {
  2219. // if(_modelJson [@"net_price"])
  2220. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2221. // else if(_modelJson [@"special_price"])
  2222. // {
  2223. // unit_price= _modelJson[@"price2"];
  2224. // }
  2225. // else
  2226. // {
  2227. // unit_price= _modelJson[@"price1"];
  2228. // }
  2229. }
  2230. else
  2231. {
  2232. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2233. }
  2234. NSString * mpack=_modelJson[@"stockUom"];
  2235. if([mpack isEqualToString:@"N/A"])
  2236. mpack=@"1";
  2237. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2238. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2239. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2240. NSMutableDictionary* jitem = nil;
  2241. jitem = [_modelJson mutableCopy];
  2242. int stockUom =[jitem[@"stockUom"] intValue];
  2243. if(stockUom==0)
  2244. stockUom=1;
  2245. bool newitem = true;
  2246. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2247. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2248. // for(int i=0;i<count;i++)
  2249. // {
  2250. //
  2251. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2252. //
  2253. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2254. // {
  2255. //
  2256. //// int oldcount = [litem[@"stockUom"] intValue];
  2257. // newitem = false;
  2258. //
  2259. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2260. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2261. //
  2262. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2263. // break;
  2264. //
  2265. //
  2266. // }
  2267. //
  2268. //
  2269. //
  2270. // }
  2271. // if(true)
  2272. // {
  2273. jitem[@"count"]=@(stockUom);
  2274. jitem[@"check"]=@(true);
  2275. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2276. NSString * unit_price;
  2277. if(RASingleton.sharedInstance.price_type==0)
  2278. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2279. else if(_modelJson [@"special_price"])
  2280. {
  2281. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2282. }
  2283. else if(_modelJson [@"net_price"])
  2284. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2285. else
  2286. {
  2287. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2288. }
  2289. if([unit_price isEqualToString:@"N/A"])
  2290. unit_price = @"0";
  2291. else
  2292. {
  2293. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2294. }
  2295. jitem[@"unit_price"] = unit_price;
  2296. jitem[@"erp_unit_price"] = unit_price;
  2297. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2298. // section[@"count"]= @(count+1);
  2299. // count++;
  2300. // }
  2301. return jitem;
  2302. }
  2303. +(void) saveScanCart:(NSMutableDictionary*) cart
  2304. {
  2305. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2306. if(appDelegate.order_code.length<=0)
  2307. return;
  2308. if(cart==nil)
  2309. return;
  2310. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2311. // NSString *documents = [paths objectAtIndex:0];
  2312. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2313. if(RASingleton.sharedInstance.price_type==0)
  2314. {
  2315. NSMutableDictionary * section = cart[@"section_0"];
  2316. int count = [section[@"count"] intValue];
  2317. for(int i=0;i<count;i++)
  2318. {
  2319. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2320. [item removeObjectForKey:@"discount"];
  2321. [item removeObjectForKey:@"note"];
  2322. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2323. }
  2324. cart[@"section_0"]=section;
  2325. }
  2326. NSString *orderdir = [self getScanPath];
  2327. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2328. cart[@"price_group"] = appDelegate.price_group;
  2329. [RAUtils dicttofile:cartpath dict:cart];
  2330. }
  2331. #endif
  2332. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2333. {
  2334. assert(add_params[@"user"]!=nil);
  2335. // assert(add_params[@"contact_id"]!=nil);
  2336. // assert(add_params[@"password"]!=nil);
  2337. NSString* serial= [[NSUUID UUID] UUIDString];
  2338. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2339. NSString *cachefolder = [paths objectAtIndex:0];
  2340. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2341. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2342. NSFileManager* fileManager = [NSFileManager defaultManager];
  2343. BOOL bdir=YES;
  2344. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2345. {
  2346. NSError *error = nil;
  2347. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2348. if(!bsuccess)
  2349. DebugLog(@"Create UPLOAD folder failed");
  2350. }
  2351. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2352. ret[@"contact"]=[self prepareContact:serial];
  2353. ret[@"wishlist"]=[self prepareWishlist:serial];
  2354. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2355. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2356. ret[@"view_portfolio"] = [self preparePDF:serial];
  2357. NSString* str= [RAConvertor dict2string:ret];
  2358. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2359. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2360. NSError *error=nil;
  2361. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2362. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2363. ZipArchive* zip = [[ZipArchive alloc] init];
  2364. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2365. for(int i=0;i<arr_files.count;i++)
  2366. {
  2367. NSString* file=arr_files[i];
  2368. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2369. }
  2370. if( ![zip CloseZipFile2] )
  2371. {
  2372. zippath = @"";
  2373. }
  2374. ret[@"file"]=zippath;
  2375. return ret;
  2376. }
  2377. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2378. {
  2379. if(filename.length==0)
  2380. return false;
  2381. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2382. bool ret=false;
  2383. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2384. NSString *cachefolder = [paths objectAtIndex:0];
  2385. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2386. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2387. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2388. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2389. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2390. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2391. NSFileManager* fileManager = [NSFileManager defaultManager];
  2392. BOOL bdir=NO;
  2393. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2394. {
  2395. NSError *error = nil;
  2396. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2397. {
  2398. ret=false;
  2399. }
  2400. else
  2401. {
  2402. ret=true;
  2403. }
  2404. }
  2405. return ret;
  2406. }
  2407. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2408. {
  2409. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2410. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2411. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2412. NSString* where=@" 1=1";
  2413. if (ver!=nil) {
  2414. where=@"is_dirty=1";
  2415. }
  2416. sqlite3 *db = [iSalesDB get_db];
  2417. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2418. sqlite3_stmt * statement;
  2419. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2420. int count=0;
  2421. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2422. if ( dbresult== SQLITE_OK)
  2423. {
  2424. while (sqlite3_step(statement) == SQLITE_ROW)
  2425. {
  2426. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2427. int _id = sqlite3_column_int(statement, 0);
  2428. int product_id = sqlite3_column_int(statement, 1);
  2429. int item_id = sqlite3_column_int(statement, 2);
  2430. int qty = sqlite3_column_int(statement, 3);
  2431. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2432. NSString* nscreate_time=nil;
  2433. if(create_time!=nil)
  2434. {
  2435. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2436. }
  2437. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2438. NSString* nsmodify_time=nil;
  2439. if(modify_time!=nil)
  2440. {
  2441. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2442. }
  2443. int is_delete = sqlite3_column_int(statement, 6);
  2444. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2445. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2446. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2447. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2448. itemjson[@"create_time"]=nscreate_time;
  2449. itemjson[@"modify_time"]=nsmodify_time;
  2450. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2451. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2452. count++;
  2453. }
  2454. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2455. sqlite3_finalize(statement);
  2456. }
  2457. ret[@"count"]=[NSNumber numberWithInt:count ];
  2458. [iSalesDB close_db:db];
  2459. return ret;
  2460. }
  2461. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2462. {
  2463. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2464. // UIApplication * app = [UIApplication sharedApplication];
  2465. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2466. assert(add_params[@"user"]!=nil);
  2467. // assert(add_params[@"password"]!=nil);
  2468. 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 ];
  2469. // 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 ];
  2470. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2471. DebugLog(@"offline_login sql:%@",sqlQuery);
  2472. sqlite3_stmt * statement;
  2473. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2474. int count=0;
  2475. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2476. if ( dbresult== SQLITE_OK)
  2477. {
  2478. while (sqlite3_step(statement) == SQLITE_ROW)
  2479. {
  2480. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2481. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2482. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2483. int product_id = sqlite3_column_int(statement, 0);
  2484. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2485. int item_id = sqlite3_column_int(statement, 7);
  2486. NSString* Price=nil;
  2487. if(str_price==nil)
  2488. {
  2489. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2490. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2491. if(price==nil)
  2492. Price=@"No Price.";
  2493. else
  2494. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2495. }
  2496. else
  2497. {
  2498. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2499. }
  2500. double discount = sqlite3_column_double(statement, 2);
  2501. int item_count = sqlite3_column_int(statement, 3);
  2502. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2503. NSString *nsline_note=nil;
  2504. if(line_note!=nil)
  2505. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2506. char *name = (char*)sqlite3_column_text(statement, 5);
  2507. NSString *nsname=nil;
  2508. if(name!=nil)
  2509. nsname= [[NSString alloc]initWithUTF8String:name];
  2510. char *description = (char*)sqlite3_column_text(statement, 6);
  2511. NSString *nsdescription=nil;
  2512. if(description!=nil)
  2513. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2514. // int stockUom = sqlite3_column_int(statement, 8);
  2515. // int _id = sqlite3_column_int(statement, 9);
  2516. //
  2517. //
  2518. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2519. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2520. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2521. // int carton=[bsubtotaljson[@"carton"] intValue];
  2522. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2523. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2524. itemjson[@"model"]=nsname;
  2525. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2526. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2527. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2528. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2529. // itemjson[@"check"]=@"true";
  2530. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2531. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2532. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2533. itemjson[@"unit_price"]=Price;
  2534. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2535. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2536. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2537. itemjson[@"note"]=nsline_note;
  2538. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2539. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2540. count++;
  2541. }
  2542. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2543. sqlite3_finalize(statement);
  2544. }
  2545. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2546. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2547. return ret;
  2548. }
  2549. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2550. {
  2551. assert(add_params[@"user"]!=nil);
  2552. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2553. sqlite3 *db = [iSalesDB get_db];
  2554. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id,discount from offline_order where order_id is null";
  2555. // for(int i=0;i<arr_soid.count;i++)
  2556. sqlite3_stmt * statement;
  2557. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2558. //int count=0;
  2559. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2560. if ( dbresult== SQLITE_OK)
  2561. {
  2562. int count=0;
  2563. while (sqlite3_step(statement) == SQLITE_ROW)
  2564. {
  2565. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2566. int _id = sqlite3_column_int(statement, 0);
  2567. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2568. NSString* nssync_data=nil;
  2569. if(sync_data!=nil)
  2570. {
  2571. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2572. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2573. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2574. }
  2575. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2576. NSString* nsimg_1=nil;
  2577. if(img_1!=nil)
  2578. {
  2579. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2580. [self copy_upImg:serial file:nsimg_1];
  2581. }
  2582. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2583. NSString* nsso_no=nil;
  2584. if(so_no!=nil)
  2585. {
  2586. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2587. }
  2588. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2589. itemjson[@"order_type"]=@"submit order";
  2590. else
  2591. itemjson[@"order_type"]=@"archive order";
  2592. double discount =sqlite3_column_double(statement, 4);
  2593. itemjson[@"discount"]=@(discount);
  2594. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2595. itemjson[@"json_data"]= nssync_data;
  2596. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2597. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2598. count++;
  2599. }
  2600. ret[@"count"]=[NSNumber numberWithInt:count ];
  2601. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2602. }
  2603. sqlite3_finalize(statement);
  2604. [iSalesDB close_db:db];
  2605. return ret;
  2606. }
  2607. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2608. {
  2609. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2610. sqlite3 *db = [iSalesDB get_db];
  2611. // UIApplication * app = [UIApplication sharedApplication];
  2612. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2613. 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";
  2614. sqlite3_stmt * statement;
  2615. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2616. int count=0;
  2617. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2618. if ( dbresult== SQLITE_OK)
  2619. {
  2620. while (sqlite3_step(statement) == SQLITE_ROW)
  2621. {
  2622. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2623. int _id = sqlite3_column_int(statement, 0);
  2624. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2625. NSString* nsimg_0=nil;
  2626. if(img_0!=nil)
  2627. {
  2628. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2629. [self copy_upImg:serial file:nsimg_0];
  2630. }
  2631. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2632. NSString* nsimg_1=nil;
  2633. if(img_1!=nil)
  2634. {
  2635. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2636. [self copy_upImg:serial file:nsimg_1];
  2637. }
  2638. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2639. NSString* nsimg_2=nil;
  2640. if(img_2!=nil)
  2641. {
  2642. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2643. [self copy_upImg:serial file:nsimg_2];
  2644. }
  2645. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2646. NSString* nssync_data=nil;
  2647. if(sync_data!=nil)
  2648. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2649. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2650. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2651. itemjson[@"json_data"]= nssync_data;
  2652. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2653. count++;
  2654. }
  2655. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2656. sqlite3_finalize(statement);
  2657. }
  2658. ret[@"count"]=[NSNumber numberWithInt:count ];
  2659. [iSalesDB close_db:db];
  2660. return ret;
  2661. }
  2662. +(bool) check_offlinedata
  2663. {
  2664. // UIApplication * app = [UIApplication sharedApplication];
  2665. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2666. sqlite3 *db = [iSalesDB get_db];
  2667. NSString * where=@"1=1";
  2668. // if(appDelegate.user!=nil)
  2669. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2670. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2671. [iSalesDB close_db:db];
  2672. if(count==0)
  2673. {
  2674. return false;
  2675. }
  2676. return true;
  2677. //
  2678. //[iSalesDB close_db:db];
  2679. }
  2680. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2681. {
  2682. // UIApplication * app = [UIApplication sharedApplication];
  2683. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2684. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2685. sqlite3 *db = [iSalesDB get_db];
  2686. NSString* collectId=params[@"collectId"];
  2687. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2688. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2689. [iSalesDB execSql:sqlQuery db:db];
  2690. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2691. [iSalesDB close_db:db];
  2692. // appDelegate.wish_count =count;
  2693. //
  2694. // [appDelegate update_count_mark];
  2695. ret[@"result"]= [NSNumber numberWithInt:2];
  2696. ret[@"wish_count"]=@(count);
  2697. return ret;
  2698. }
  2699. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2700. //{
  2701. //
  2702. // UIApplication * app = [UIApplication sharedApplication];
  2703. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2704. //
  2705. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2706. // sqlite3 *db = [iSalesDB get_db];
  2707. // NSString* product_id=params[@"product_id"];
  2708. //
  2709. //
  2710. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2711. //
  2712. //
  2713. // for(int i=0;i<arr.count;i++)
  2714. // {
  2715. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2716. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2717. // if(count==0)
  2718. // {
  2719. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2720. // [iSalesDB execSql:sqlQuery db:db];
  2721. // }
  2722. // }
  2723. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2724. // [iSalesDB close_db:db];
  2725. //
  2726. // appDelegate.wish_count =count;
  2727. //
  2728. // [appDelegate update_count_mark];
  2729. // ret[@"result"]= [NSNumber numberWithInt:2];
  2730. // return ret;
  2731. // //
  2732. // //return ret;
  2733. //}
  2734. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2735. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2736. DebugLog(@"time interval: %lf",interval);
  2737. }
  2738. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2739. //{
  2740. // UIApplication * app = [UIApplication sharedApplication];
  2741. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2742. //
  2743. // int sort = [[params objectForKey:@"sort"] intValue];
  2744. // NSString *sort_str = @"";
  2745. // switch (sort) {
  2746. // case 0:{
  2747. // sort_str = @"order by w.modify_time desc";
  2748. // }
  2749. // break;
  2750. // case 1:{
  2751. // sort_str = @"order by w.modify_time asc";
  2752. // }
  2753. // break;
  2754. // case 2:{
  2755. // sort_str = @"order by m.name asc";
  2756. // }
  2757. // break;
  2758. // case 3:{
  2759. // sort_str = @"order by m.name desc";
  2760. // }
  2761. // break;
  2762. // case 4:{
  2763. // sort_str = @"order by m.description asc";
  2764. // }
  2765. // break;
  2766. //
  2767. // default:
  2768. // break;
  2769. //
  2770. // }
  2771. //
  2772. //
  2773. //// NSString* user = appDelegate.user;
  2774. //
  2775. // sqlite3 *db = [iSalesDB get_db];
  2776. //
  2777. // // order by w.create_time
  2778. // 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];
  2779. //
  2780. //// 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];
  2781. //
  2782. //
  2783. // sqlite3_stmt * statement;
  2784. //
  2785. // NSDate *date1 = [NSDate date];
  2786. //// NSDate *date2 = nil;
  2787. //
  2788. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2789. // int count=0;
  2790. //
  2791. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2792. // {
  2793. //
  2794. //
  2795. // while (sqlite3_step(statement) == SQLITE_ROW)
  2796. // {
  2797. //
  2798. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2799. //
  2800. // int product_id = sqlite3_column_double(statement, 0);
  2801. //
  2802. //
  2803. //
  2804. //
  2805. //
  2806. // char *description = (char*)sqlite3_column_text(statement, 1);
  2807. // if(description==nil)
  2808. // description= "";
  2809. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2810. //
  2811. // int item_id = sqlite3_column_double(statement, 2);
  2812. //
  2813. // NSDate *date_image = [NSDate date];
  2814. //
  2815. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2816. //
  2817. // printf("image : ");
  2818. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2819. //
  2820. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2821. // // if(url==nil)
  2822. // // url="";
  2823. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2824. //
  2825. // int qty = sqlite3_column_int(statement, 3);
  2826. //
  2827. //
  2828. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2829. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2830. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2831. // item[@"description"]= nsdescription;
  2832. // item[@"img"]= nsurl;
  2833. //
  2834. //
  2835. //
  2836. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2837. // count++;
  2838. //
  2839. // }
  2840. // printf("total time:");
  2841. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2842. //
  2843. // ret[@"count"]= [NSNumber numberWithInt:count];
  2844. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2845. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2846. // ret[@"result"]= [NSNumber numberWithInt:2];
  2847. //
  2848. //
  2849. // appDelegate.wish_count =count;
  2850. //
  2851. // [appDelegate update_count_mark];
  2852. // sqlite3_finalize(statement);
  2853. //
  2854. //
  2855. //
  2856. //
  2857. // }
  2858. //
  2859. // [iSalesDB close_db:db];
  2860. //
  2861. // return ret;
  2862. //}
  2863. +(NSDictionary*) offline_notimpl
  2864. {
  2865. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2866. ret[@"result"]=@"8";
  2867. ret[@"err_msg"]=@"offline mode does not support this function.";
  2868. return ret;
  2869. }
  2870. +(NSDictionary*) offline_home
  2871. {
  2872. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2873. NSString *cachefolder = [paths objectAtIndex:0];
  2874. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2875. NSData* json =nil;
  2876. json=[NSData dataWithContentsOfFile:img_cache];
  2877. if(json==nil)
  2878. return nil;
  2879. NSError *error=nil;
  2880. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2881. return menu;
  2882. }
  2883. +(NSDictionary*) offline_category_menu
  2884. {
  2885. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2886. NSString *cachefolder = [paths objectAtIndex:0];
  2887. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2888. NSData* json =nil;
  2889. json=[NSData dataWithContentsOfFile:img_cache];
  2890. if(json==nil)
  2891. return nil;
  2892. NSError *error=nil;
  2893. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2894. return menu;
  2895. }
  2896. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2897. {
  2898. NSString* offline_command=params[@"offline_Command"];
  2899. NSDictionary* ret=nil;
  2900. if([offline_command isEqualToString:@"model_NIYMAL"])
  2901. {
  2902. NSString* category = params[@"category"];
  2903. ret = [self refresh_model_NIYMAL:category];
  2904. }
  2905. return ret;
  2906. }
  2907. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2908. {
  2909. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2910. [ret setValue:@"2" forKey:@"result"];
  2911. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2912. sqlite3* db= [iSalesDB get_db];
  2913. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2914. [iSalesDB close_db:db];
  2915. [ret setObject:detail1_section forKey:@"detail_1"];
  2916. return ret;
  2917. }
  2918. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2919. {
  2920. //assert(user!=nil);
  2921. // UIApplication * app = [UIApplication sharedApplication];
  2922. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2923. // NSArray* arr1 = [self get_user_all_price_type];
  2924. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2925. // NSSet *set1 = [NSSet setWithArray:arr1];
  2926. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2927. // if(appDelegate.contact_id==nil)
  2928. // set2=[set1 mutableCopy];
  2929. // else
  2930. // [set2 intersectsSet:set1];
  2931. // NSArray *retarr = [set2 allObjects];
  2932. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2933. // sqlite3 *db = [iSalesDB get_db];
  2934. NSString* sqlQuery = nil;
  2935. if(contact_id==nil)
  2936. {
  2937. if(!blogin)
  2938. return nil;
  2939. 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];
  2940. }
  2941. else
  2942. 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];
  2943. sqlite3_stmt * statement;
  2944. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2945. int count=0;
  2946. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2947. {
  2948. while (sqlite3_step(statement) == SQLITE_ROW)
  2949. {
  2950. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2951. char *name = (char*)sqlite3_column_text(statement, 0);
  2952. if(name==nil)
  2953. name="";
  2954. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2955. // double price = sqlite3_column_double(statement, 1);
  2956. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2957. // if(isnull==nil)
  2958. // item[nsname]= @"No Price";
  2959. // else
  2960. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2961. char *price = (char*)sqlite3_column_text(statement, 1);
  2962. if(price!=nil)
  2963. {
  2964. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2965. nsprice=[AESCrypt fastdecrypt:nsprice];
  2966. if(nsprice.length>0)
  2967. {
  2968. double dp= [nsprice doubleValue];
  2969. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2970. }
  2971. }
  2972. else
  2973. {
  2974. item[nsname]= @"No Price";
  2975. }
  2976. // int type= sqlite3_column_int(statement, 2);
  2977. //item[@"type"]=@"price";
  2978. // item[nsname]= nsprice;
  2979. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2980. count++;
  2981. }
  2982. ret[@"count"]= [NSNumber numberWithInt:count];
  2983. sqlite3_finalize(statement);
  2984. }
  2985. // [iSalesDB close_db:db];
  2986. return ret;
  2987. }
  2988. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2989. //{
  2990. // NSArray* arr1 = [self get_user_all_price_type:db];
  2991. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2992. //
  2993. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2994. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2995. // // [set2 intersectsSet:set1];
  2996. // //
  2997. // //
  2998. // // NSArray *retarr = [set2 allObjects];
  2999. //
  3000. // NSString* whereprice=nil;
  3001. // if(contact_id==nil)
  3002. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3003. // else
  3004. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3005. //
  3006. //
  3007. // // sqlite3 *db = [iSalesDB get_db];
  3008. //
  3009. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  3010. // sqlite3_stmt * statement;
  3011. //
  3012. //
  3013. // NSNumber* ret = nil;
  3014. // double dprice=DBL_MAX;
  3015. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3016. // {
  3017. //
  3018. //
  3019. // while (sqlite3_step(statement) == SQLITE_ROW)
  3020. // {
  3021. //
  3022. // // double val = sqlite3_column_double(statement, 0);
  3023. // char *price = (char*)sqlite3_column_text(statement, 0);
  3024. // if(price!=nil)
  3025. // {
  3026. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3027. // nsprice=[AESCrypt fastdecrypt:nsprice];
  3028. // if(nsprice.length>0)
  3029. // {
  3030. // double dp= [nsprice doubleValue];
  3031. // if(dp<dprice)
  3032. // dprice=dp;
  3033. // }
  3034. // }
  3035. // }
  3036. //
  3037. //
  3038. //
  3039. //
  3040. // sqlite3_finalize(statement);
  3041. //
  3042. //
  3043. //
  3044. //
  3045. // }
  3046. //
  3047. // // [iSalesDB close_db:db];
  3048. //
  3049. // if(dprice==DBL_MAX)
  3050. // ret= nil;
  3051. // else
  3052. // ret= [NSNumber numberWithDouble:dprice];
  3053. // return ret;
  3054. //}
  3055. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  3056. {
  3057. assert(user!=nil);
  3058. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  3059. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  3060. NSString* whereprice=nil;
  3061. if(contact_id==nil)
  3062. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  3063. else
  3064. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  3065. // sqlite3 *db = [iSalesDB get_db];
  3066. NSString *productIdCondition = @"1 = 1";
  3067. if (product_id) {
  3068. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  3069. }
  3070. NSString *itemIdCondition = @"";
  3071. if (item_id) {
  3072. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  3073. }
  3074. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  3075. sqlite3_stmt * statement;
  3076. NSNumber* ret = nil;
  3077. double dprice=DBL_MAX;
  3078. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3079. {
  3080. while (sqlite3_step(statement) == SQLITE_ROW)
  3081. {
  3082. // double val = sqlite3_column_double(statement, 0);
  3083. char *price = (char*)sqlite3_column_text(statement, 0);
  3084. if(price!=nil)
  3085. {
  3086. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  3087. nsprice=[AESCrypt fastdecrypt:nsprice];
  3088. if(nsprice.length>0)
  3089. {
  3090. double dp= [nsprice doubleValue];
  3091. if(dp<dprice)
  3092. dprice=dp;
  3093. }
  3094. }
  3095. }
  3096. sqlite3_finalize(statement);
  3097. }
  3098. // [iSalesDB close_db:db];
  3099. if(dprice==DBL_MAX)
  3100. ret= nil;
  3101. else
  3102. ret= [NSNumber numberWithDouble:dprice];
  3103. return ret;
  3104. }
  3105. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  3106. {
  3107. assert(user!=nil);
  3108. NSArray* ret=nil;
  3109. // sqlite3 *db = [iSalesDB get_db];
  3110. // no customer assigned , use login user contact_id
  3111. // UIApplication * app = [UIApplication sharedApplication];
  3112. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3113. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  3114. sqlite3_stmt * statement;
  3115. // int count=0;
  3116. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3117. {
  3118. if (sqlite3_step(statement) == SQLITE_ROW)
  3119. {
  3120. char *val = (char*)sqlite3_column_text(statement, 0);
  3121. if(val==nil)
  3122. val="";
  3123. NSString* price = [[NSString alloc]initWithUTF8String:val];
  3124. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  3125. }
  3126. sqlite3_finalize(statement);
  3127. }
  3128. // [iSalesDB close_db:db];
  3129. return ret;
  3130. }
  3131. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  3132. {
  3133. // sqlite3 *db = [iSalesDB get_db];
  3134. if(contact_id==nil)
  3135. {
  3136. // no customer assigned , use login user contact_id
  3137. // UIApplication * app = [UIApplication sharedApplication];
  3138. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3139. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  3140. sqlite3_stmt * statement;
  3141. // int count=0;
  3142. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3143. {
  3144. if (sqlite3_step(statement) == SQLITE_ROW)
  3145. {
  3146. char *val = (char*)sqlite3_column_text(statement, 0);
  3147. if(val==nil)
  3148. val="";
  3149. contact_id = [[NSString alloc]initWithUTF8String:val];
  3150. }
  3151. sqlite3_finalize(statement);
  3152. }
  3153. if(contact_id.length<=0)
  3154. {
  3155. // [iSalesDB close_db:db];
  3156. return nil;
  3157. }
  3158. }
  3159. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3160. sqlite3_stmt * statement;
  3161. NSArray* ret=nil;
  3162. // int count=0;
  3163. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3164. {
  3165. if (sqlite3_step(statement) == SQLITE_ROW)
  3166. {
  3167. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3168. if(price_type==nil)
  3169. price_type="";
  3170. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3171. if(nsprice_type.length>0)
  3172. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3173. else
  3174. ret=nil;
  3175. }
  3176. sqlite3_finalize(statement);
  3177. }
  3178. // [iSalesDB close_db:db];
  3179. return ret;
  3180. }
  3181. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3182. {
  3183. NSString* ret= nil;
  3184. // sqlite3 *db = [iSalesDB get_db];
  3185. NSString *sqlQuery = nil;
  3186. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3187. // 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;
  3188. // 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
  3189. if(product_id==nil && model_name)
  3190. 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;
  3191. else if (product_id)
  3192. 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
  3193. sqlite3_stmt * statement;
  3194. // int count=0;
  3195. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3196. {
  3197. if (sqlite3_step(statement) == SQLITE_ROW)
  3198. {
  3199. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3200. if(imgurl==nil)
  3201. imgurl="";
  3202. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3203. ret=nsimgurl;
  3204. }
  3205. sqlite3_finalize(statement);
  3206. }
  3207. else
  3208. {
  3209. [ret setValue:@"8" forKey:@"result"];
  3210. }
  3211. // [iSalesDB close_db:db];
  3212. DebugLog(@"data string: %@",ret );
  3213. return ret;
  3214. }
  3215. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3216. {
  3217. NSString* ret= nil;
  3218. sqlite3 *db = [iSalesDB get_db];
  3219. NSString *sqlQuery = nil;
  3220. if(product_id==nil)
  3221. if(upc_code==nil)
  3222. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3223. else
  3224. 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='%@';
  3225. else
  3226. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3227. sqlite3_stmt * statement;
  3228. // int count=0;
  3229. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3230. {
  3231. if (sqlite3_step(statement) == SQLITE_ROW)
  3232. {
  3233. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3234. if(default_category==nil)
  3235. default_category="";
  3236. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3237. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3238. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3239. ret=nsdefault_category;
  3240. }
  3241. sqlite3_finalize(statement);
  3242. }
  3243. else
  3244. {
  3245. [ret setValue:@"8" forKey:@"result"];
  3246. }
  3247. [iSalesDB close_db:db];
  3248. DebugLog(@"data string: %@",ret );
  3249. return ret;
  3250. }
  3251. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3252. NSString *sql = nil;
  3253. if (product_id != nil) {
  3254. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3255. } else {
  3256. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3257. }
  3258. NSString *result_set = [iSalesDB jk_queryText:sql];
  3259. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3260. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3261. return result_set;
  3262. }
  3263. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3264. //{
  3265. //
  3266. //
  3267. // assert(params[@"user"]!=nil);
  3268. //
  3269. //
  3270. //
  3271. // NSString* model_name = [params valueForKey:@"product_name"];
  3272. //
  3273. // NSString* product_id = [params valueForKey:@"product_id"];
  3274. //
  3275. // NSString* category = [params valueForKey:@"category"];
  3276. //
  3277. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3278. // if(category==nil) {
  3279. // category = default_category_id;
  3280. // } else {
  3281. //
  3282. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3283. // // 参数重有多个category id
  3284. // if (arr_0.count > 1) {
  3285. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3286. //
  3287. // // 取交集
  3288. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3289. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3290. // [set_0 intersectSet:set_1];
  3291. //
  3292. // if (set_0.count == 1) {
  3293. // category = (NSString *)[set_0 anyObject];
  3294. // } else {
  3295. // if ([set_0 containsObject:default_category_id]) {
  3296. // category = default_category_id;
  3297. // } else {
  3298. // category = (NSString *)[set_0 anyObject];
  3299. // }
  3300. // }
  3301. // }
  3302. // }
  3303. //
  3304. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3305. //
  3306. //
  3307. //
  3308. //
  3309. //
  3310. //
  3311. // sqlite3 *db = [iSalesDB get_db];
  3312. //
  3313. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3314. //
  3315. //
  3316. // NSString *sqlQuery = nil;
  3317. //
  3318. // if(product_id==nil)
  3319. // 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='%@';
  3320. // else
  3321. //
  3322. // 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=%@;
  3323. //
  3324. //
  3325. // sqlite3_stmt * statement;
  3326. // [ret setValue:@"2" forKey:@"result"];
  3327. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3328. //
  3329. // // int count=0;
  3330. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3331. // {
  3332. //
  3333. //
  3334. // if (sqlite3_step(statement) == SQLITE_ROW)
  3335. // {
  3336. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3337. //
  3338. //
  3339. // char *name = (char*)sqlite3_column_text(statement, 0);
  3340. // if(name==nil)
  3341. // name="";
  3342. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3343. //
  3344. // char *description = (char*)sqlite3_column_text(statement, 1);
  3345. // if(description==nil)
  3346. // description="";
  3347. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3348. //
  3349. //
  3350. // int product_id = sqlite3_column_int(statement, 2);
  3351. //
  3352. //
  3353. // char *color = (char*)sqlite3_column_text(statement, 3);
  3354. // if(color==nil)
  3355. // color="";
  3356. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3357. // //
  3358. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3359. // // if(legcolor==nil)
  3360. // // legcolor="";
  3361. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3362. // //
  3363. // //
  3364. // int availability = sqlite3_column_int(statement, 5);
  3365. // //
  3366. // int incoming_stock = sqlite3_column_int(statement, 6);
  3367. //
  3368. //
  3369. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3370. // if(demension==nil)
  3371. // demension="";
  3372. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3373. //
  3374. //
  3375. //
  3376. // // ,,,,,,,,,
  3377. //
  3378. //
  3379. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3380. // if(seat_height==nil)
  3381. // seat_height="";
  3382. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3383. //
  3384. //
  3385. // char *material = (char*)sqlite3_column_text(statement, 9);
  3386. // if(material==nil)
  3387. // material="";
  3388. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3389. //
  3390. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3391. // if(box_dim==nil)
  3392. // box_dim="";
  3393. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3394. //
  3395. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3396. // if(volume==nil)
  3397. // volume="";
  3398. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3399. //
  3400. // double weight = sqlite3_column_double(statement, 12);
  3401. //
  3402. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3403. // if(model_set==nil)
  3404. // model_set="";
  3405. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3406. //
  3407. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3408. // if(load_ability==nil)
  3409. // load_ability="";
  3410. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3411. //
  3412. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3413. //// if(default_category==nil)
  3414. //// default_category="";
  3415. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3416. //
  3417. //
  3418. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3419. // if(fabric_content==nil)
  3420. // fabric_content="";
  3421. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3422. //
  3423. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3424. // if(assembling==nil)
  3425. // assembling="";
  3426. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3427. //
  3428. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3429. // if(made_in==nil)
  3430. // made_in="";
  3431. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3432. //
  3433. //
  3434. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3435. // if(special_remarks==nil)
  3436. // special_remarks="";
  3437. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3438. //
  3439. //
  3440. // int stockUcom = sqlite3_column_double(statement, 20);
  3441. //
  3442. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3443. // if(product_group==nil)
  3444. // product_group="";
  3445. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3446. //
  3447. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3448. // // if(fashion_selector==nil)
  3449. // // fashion_selector="";
  3450. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3451. //
  3452. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3453. // if(selector_field==nil)
  3454. // selector_field="";
  3455. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3456. //
  3457. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3458. // if(property_field==nil)
  3459. // property_field="";
  3460. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3461. //
  3462. //
  3463. //
  3464. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3465. // if(packaging==nil)
  3466. // packaging="";
  3467. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3468. //
  3469. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3470. //
  3471. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3472. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3473. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3474. //
  3475. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3476. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3477. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3478. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3479. //
  3480. //
  3481. //
  3482. //
  3483. //
  3484. // NSString* Availability=nil;
  3485. // if(availability>0)
  3486. // Availability=[NSString stringWithFormat:@"%d",availability];
  3487. // else
  3488. // Availability = @"Out of Stock";
  3489. //
  3490. // [img_section setValue:Availability forKey:@"Availability"];
  3491. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3492. //
  3493. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3494. //
  3495. //
  3496. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3497. // if(eta==nil)
  3498. // eta="";
  3499. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3500. // if ([nseta isEqualToString:@"null"]) {
  3501. // nseta = @"";
  3502. // }
  3503. // if (availability <= 0) {
  3504. // [img_section setValue:nseta forKey:@"ETA"];
  3505. // }
  3506. //
  3507. //
  3508. // int item_id = sqlite3_column_int(statement, 26);
  3509. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3510. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3511. //
  3512. // NSString* Price=nil;
  3513. // if(appDelegate.bLogin==false)
  3514. // Price=@"Must Sign in.";
  3515. // else
  3516. // {
  3517. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3518. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3519. // if(price==nil)
  3520. // Price=@"No Price.";
  3521. // else
  3522. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3523. // }
  3524. //
  3525. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3526. // [img_section setValue:Price forKey:@"price"];
  3527. // [img_section setValue:nsname forKey:@"model_name"];
  3528. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3529. //
  3530. // if (appDelegate.order_code) { // 离线order code即so#
  3531. //
  3532. // 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];
  3533. // __block int cartQTY = 0;
  3534. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3535. //
  3536. // cartQTY = sqlite3_column_int(stmt, 0);
  3537. //
  3538. // }];
  3539. //
  3540. // if (cartQTY > 0) {
  3541. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3542. // }
  3543. // }
  3544. //
  3545. // [ret setObject:img_section forKey:@"img_section"];
  3546. //
  3547. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3548. // int detail0_item_count=0;
  3549. //
  3550. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3551. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3552. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3553. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3554. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3555. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3556. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3557. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3558. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3559. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3560. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3561. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3562. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3563. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3564. //
  3565. //
  3566. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3567. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3568. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3569. // {
  3570. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3571. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3572. // }
  3573. //
  3574. //
  3575. //
  3576. //
  3577. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3578. // [detail0_section setValue:@"kv" forKey:@"type"];
  3579. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3580. //
  3581. // [ret setObject:detail0_section forKey:@"detail_0"];
  3582. //
  3583. //
  3584. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3585. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3586. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3587. // // [detail1_section setValue:@"content" forKey:@"type"];
  3588. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3589. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3590. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3591. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3592. // [ret setObject:detail1_section forKey:@"detail_1"];
  3593. //
  3594. //
  3595. //
  3596. //
  3597. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3598. // [detail2_section setValue:@"detail" forKey:@"target"];
  3599. // [detail2_section setValue:@"popup" forKey:@"action"];
  3600. // [detail2_section setValue:@"content" forKey:@"type"];
  3601. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3602. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3603. // [detail2_section setValue:@"local" forKey:@"data"];
  3604. // [ret setObject:detail2_section forKey:@"detail_2"];
  3605. // }
  3606. //
  3607. //
  3608. //
  3609. //
  3610. // sqlite3_finalize(statement);
  3611. // }
  3612. // else
  3613. // {
  3614. // [ret setValue:@"8" forKey:@"result"];
  3615. // }
  3616. //// DebugLog(@"count:%d",count);
  3617. //
  3618. //
  3619. // [iSalesDB close_db:db];
  3620. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3621. //
  3622. // return ret;
  3623. //}
  3624. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3625. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3626. NSString* keyword = [params valueForKey:@"keyword"];
  3627. keyword=keyword.lowercaseString;
  3628. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3629. int limit = [[params valueForKey:@"limit"] intValue];
  3630. int offset = [[params valueForKey:@"offset"] intValue];
  3631. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3632. NSString *limit_str = @"";
  3633. if (limited) {
  3634. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3635. }
  3636. sqlite3 *db = [iSalesDB get_db];
  3637. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3638. // UIApplication * app = [UIApplication sharedApplication];
  3639. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3640. NSString *sqlQuery = nil;
  3641. if(exactMatch )
  3642. 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 ;
  3643. else
  3644. 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
  3645. DebugLog(@"offline_search sql:%@",sqlQuery);
  3646. sqlite3_stmt * statement;
  3647. [ret setValue:@"2" forKey:@"result"];
  3648. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3649. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3650. // int count=0;
  3651. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3652. {
  3653. int i=0;
  3654. while (sqlite3_step(statement) == SQLITE_ROW)
  3655. {
  3656. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3657. // char *name = (char*)sqlite3_column_text(statement, 1);
  3658. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3659. char *name = (char*)sqlite3_column_text(statement, 0);
  3660. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3661. char *description = (char*)sqlite3_column_text(statement, 1);
  3662. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3663. int product_id = sqlite3_column_int(statement, 2);
  3664. // char *url = (char*)sqlite3_column_text(statement, 3);
  3665. // if(url==nil)
  3666. // url="";
  3667. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3668. int wid = sqlite3_column_int(statement, 3);
  3669. int closeout = sqlite3_column_int(statement, 4);
  3670. int cid = sqlite3_column_int(statement, 5);
  3671. int wisdelete = sqlite3_column_int(statement, 6);
  3672. int more_color = sqlite3_column_int(statement, 7);
  3673. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3674. if(wid !=0 && wisdelete != 1)
  3675. [item setValue:@"true" forKey:@"wish_exists"];
  3676. else
  3677. [item setValue:@"false" forKey:@"wish_exists"];
  3678. if(closeout==0)
  3679. [item setValue:@"false" forKey:@"is_closeout"];
  3680. else
  3681. [item setValue:@"true" forKey:@"is_closeout"];
  3682. if(cid==0)
  3683. [item setValue:@"false" forKey:@"cart_exists"];
  3684. else
  3685. [item setValue:@"true" forKey:@"cart_exists"];
  3686. if (more_color == 0) {
  3687. [item setObject:@(false) forKey:@"more_color"];
  3688. } else if (more_color == 1) {
  3689. [item setObject:@(true) forKey:@"more_color"];
  3690. }
  3691. [item addEntriesFromDictionary:imgjson];
  3692. // [item setValue:nsurl forKey:@"img"];
  3693. [item setValue:nsname forKey:@"fash_name"];
  3694. [item setValue:nsdescription forKey:@"description"];
  3695. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3696. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3697. i++;
  3698. }
  3699. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3700. [ret setObject:items forKey:@"items"];
  3701. sqlite3_finalize(statement);
  3702. }
  3703. DebugLog(@"count:%d",count);
  3704. [iSalesDB close_db:db];
  3705. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3706. return ret;
  3707. }
  3708. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3709. {
  3710. return [self search:params limited:YES];
  3711. }
  3712. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3713. //{
  3714. // UIApplication * app = [UIApplication sharedApplication];
  3715. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3716. //
  3717. //// [iSalesDB disable_trigger]
  3718. // [iSalesDB disable_trigger];
  3719. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3720. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3721. //
  3722. // [iSalesDB enable_trigger];
  3723. //// [iSalesDB enable_trigger]
  3724. // //
  3725. // // NSString* user = [params valueForKey:@"user"];
  3726. // //
  3727. // // NSString* password = [params valueForKey:@"password"];
  3728. //
  3729. //
  3730. //
  3731. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3732. //
  3733. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3734. //
  3735. //
  3736. //
  3737. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3738. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3739. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3740. //
  3741. // [appDelegate update_count_mark];
  3742. //
  3743. //
  3744. // appDelegate.can_show_price =false;
  3745. // appDelegate.can_see_price =false;
  3746. // appDelegate.can_create_portfolio =false;
  3747. // appDelegate.can_create_order =false;
  3748. //
  3749. //
  3750. // appDelegate.can_cancel_order =false;
  3751. // appDelegate.can_set_cart_price =false;
  3752. // appDelegate.can_delete_order =false;
  3753. // appDelegate.can_submit_order =false;
  3754. // appDelegate.can_set_tearsheet_price =false;
  3755. // appDelegate.can_update_contact_info = false;
  3756. //
  3757. // appDelegate.save_order_logout = false;
  3758. // appDelegate.submit_order_logout = false;
  3759. // appDelegate.alert_sold_in_quantities = false;
  3760. //
  3761. // appDelegate.ipad_perm =nil ;
  3762. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3763. // appDelegate.OrderFilter= nil;
  3764. // [appDelegate SetSo:nil];
  3765. // [appDelegate set_main_button_panel];
  3766. //
  3767. //
  3768. // // sqlite3 *db = [iSalesDB get_db];
  3769. // //
  3770. // //
  3771. // //
  3772. // //
  3773. // //
  3774. // // 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"]];
  3775. // //
  3776. // //
  3777. // //
  3778. // //
  3779. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3780. // // sqlite3_stmt * statement;
  3781. // //
  3782. // //
  3783. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3784. // //
  3785. // //
  3786. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3787. // // {
  3788. // //
  3789. // //
  3790. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3791. // // {
  3792. // //
  3793. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3794. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3795. // //
  3796. // //
  3797. // //
  3798. // // int can_show_price = sqlite3_column_int(statement, 0);
  3799. // // int can_see_price = sqlite3_column_int(statement, 1);
  3800. // //
  3801. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3802. // // if(contact_id==nil)
  3803. // // contact_id="";
  3804. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3805. // //
  3806. // // int user_type = sqlite3_column_int(statement, 3);
  3807. // //
  3808. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3809. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3810. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3811. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3812. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3813. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3814. // // int can_create_order = sqlite3_column_int(statement, 10);
  3815. // //
  3816. // //
  3817. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3818. // // if(mode==nil)
  3819. // // mode="";
  3820. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3821. // //
  3822. // //
  3823. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3824. // // if(username==nil)
  3825. // // username="";
  3826. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3827. // //
  3828. // //
  3829. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3830. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3831. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3832. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3833. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3834. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3835. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3836. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3837. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3838. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3839. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3840. // //
  3841. // // [header setValue:nsusername forKey:@"username"];
  3842. // //
  3843. // //
  3844. // // [ret setObject:header forKey:@"header"];
  3845. // // [ret setValue:nsmode forKey:@"mode"];
  3846. // //
  3847. // //
  3848. // // }
  3849. // //
  3850. // //
  3851. // //
  3852. // // sqlite3_finalize(statement);
  3853. // // }
  3854. // //
  3855. // //
  3856. // //
  3857. // // [iSalesDB close_db:db];
  3858. // //
  3859. // //
  3860. // //
  3861. // //
  3862. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3863. //
  3864. // return ret;
  3865. //}
  3866. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3867. //{
  3868. //
  3869. // UIApplication * app = [UIApplication sharedApplication];
  3870. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3871. //
  3872. // [iSalesDB disable_trigger];
  3873. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3874. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3875. // [iSalesDB enable_trigger];
  3876. //
  3877. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3878. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3879. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3880. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3881. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3882. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3883. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3884. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3885. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3886. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3887. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3888. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3889. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3890. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3891. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3892. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3893. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3894. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3895. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3896. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3897. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3898. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3899. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3900. //
  3901. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3902. //
  3903. // [arr_name addObject:customer_first_name];
  3904. // [arr_name addObject:customer_last_name];
  3905. //
  3906. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3907. //
  3908. // // default ship from
  3909. // 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';";
  3910. //
  3911. // __block NSString *cid = @"";
  3912. // __block NSString *name = @"";
  3913. // __block NSString *ext = @"";
  3914. // __block NSString *contact = @"";
  3915. // __block NSString *email = @"";
  3916. // __block NSString *fax = @"";
  3917. // __block NSString *phone = @"";
  3918. //
  3919. // sqlite3 *db = [iSalesDB get_db];
  3920. //
  3921. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3922. //
  3923. // cid = [self textAtColumn:0 statement:statment];
  3924. // name = [self textAtColumn:1 statement:statment];
  3925. // ext = [self textAtColumn:2 statement:statment];
  3926. // contact = [self textAtColumn:3 statement:statment];
  3927. // email = [self textAtColumn:4 statement:statment];
  3928. // fax = [self textAtColumn:5 statement:statment];
  3929. // phone = [self textAtColumn:6 statement:statment];
  3930. //
  3931. // }];
  3932. //
  3933. // NSString* so_id = [self get_offline_soid:db];
  3934. // if(so_id==nil)
  3935. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3936. //
  3937. // 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];
  3938. //
  3939. //
  3940. //
  3941. // int result =[iSalesDB execSql:sql_neworder db:db];
  3942. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3943. //
  3944. //
  3945. //
  3946. // //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'
  3947. // //soId
  3948. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3949. //
  3950. //
  3951. //
  3952. //
  3953. //
  3954. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3955. // sqlite3_stmt * statement;
  3956. //
  3957. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3958. // {
  3959. // if (sqlite3_step(statement) == SQLITE_ROW)
  3960. // {
  3961. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3962. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3963. //
  3964. // //ret = sqlite3_column_int(statement, 0);
  3965. //
  3966. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3967. // if(soId==nil)
  3968. // soId="";
  3969. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3970. // [ret setValue:nssoId forKey:@"soId"];
  3971. // [ret setValue:nssoId forKey:@"orderCode"];
  3972. //
  3973. // }
  3974. // sqlite3_finalize(statement);
  3975. // }
  3976. //
  3977. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3978. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3979. //
  3980. //
  3981. // [iSalesDB close_db:db];
  3982. //
  3983. // return [RAConvertor dict2data:ret];
  3984. //
  3985. //}
  3986. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3987. {
  3988. //assert(params[@"order_code"]);
  3989. // assert(params[@"order_code"]);
  3990. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3991. NSString* orderCode = [params valueForKey:@"orderCode"];
  3992. NSString* app_order_code= params[@"appDelegate.order_code"];
  3993. // UIApplication * app = [UIApplication sharedApplication];
  3994. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3995. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3996. {
  3997. [iSalesDB disable_trigger];
  3998. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3999. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  4000. [iSalesDB enable_trigger];
  4001. }
  4002. sqlite3 *db = [iSalesDB get_db];
  4003. int cart_count=[self query_ordercartcount:orderCode db:db];
  4004. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4005. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4006. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4007. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4008. [iSalesDB close_db:db];
  4009. return [RAConvertor dict2data:ret];
  4010. }
  4011. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  4012. {
  4013. assert(params[@"can_create_backorder"]!=nil);
  4014. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4015. sqlite3 *db = [iSalesDB get_db];
  4016. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4017. int count =0;
  4018. if(params[@"count"]!=nil)
  4019. {
  4020. count = [params[@"count"] intValue];
  4021. }
  4022. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4023. NSString* product_id=params[@"product_id"];
  4024. NSString* orderCode=params[@"orderCode"];
  4025. NSString *qty = params[@"qty"];
  4026. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  4027. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  4028. __block int number_of_outOfStock = 0;
  4029. for(int i=0;i<arr_id.count;i++)
  4030. {
  4031. NSInteger item_qty= count;
  4032. if (qty) {
  4033. item_qty = [qty_arr[i] integerValue];
  4034. }
  4035. if(item_qty==0)
  4036. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  4037. // 检查新加Model数量是否大于库存
  4038. if (![params[@"can_create_backorder"] boolValue]) {
  4039. __block BOOL needContinue = NO;
  4040. [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) {
  4041. int availability = sqlite3_column_int(stmt, 0);
  4042. // 库存小于购买量为缺货
  4043. if (availability < item_qty || availability <= 0) {
  4044. number_of_outOfStock++;
  4045. needContinue = YES;
  4046. }
  4047. }];
  4048. if (needContinue) {
  4049. continue;
  4050. }
  4051. }
  4052. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  4053. NSString* sql=nil;
  4054. sqlite3_stmt *stmt = nil;
  4055. BOOL shouldStep = NO;
  4056. if(_id<0)
  4057. {
  4058. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  4059. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  4060. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4061. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  4062. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  4063. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4064. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  4065. shouldStep = YES;
  4066. }
  4067. else
  4068. {
  4069. if (qty) { // wish list move to cart
  4070. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  4071. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4072. sqlite3_bind_int(stmt, 1, _id);
  4073. shouldStep = YES;
  4074. } else {
  4075. 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];
  4076. __block BOOL update = YES;
  4077. if (![params[@"can_create_backorder"] boolValue]) {
  4078. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4079. int newQTY = sqlite3_column_int(stmt, 0);
  4080. int availability = sqlite3_column_int(stmt, 1);
  4081. if (newQTY > availability) { // 库存不够
  4082. update = NO;
  4083. number_of_outOfStock++;
  4084. }
  4085. }];
  4086. }
  4087. if (update) {
  4088. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  4089. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  4090. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  4091. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  4092. shouldStep = YES;
  4093. }
  4094. }
  4095. }
  4096. if (shouldStep) {
  4097. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4098. [iSalesDB execSql:@"ROLLBACK" db:db];
  4099. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4100. [iSalesDB close_db:db];
  4101. DebugLog(@"add to cart error");
  4102. return [RAConvertor dict2data:ret];
  4103. }
  4104. }
  4105. }
  4106. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4107. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4108. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4109. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4110. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4111. if (![params[@"can_create_backorder"] boolValue]) {
  4112. if (number_of_outOfStock > 0) {
  4113. ret[@"result"]=[NSNumber numberWithInt:8];
  4114. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  4115. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  4116. }
  4117. }
  4118. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4119. [iSalesDB close_db:db];
  4120. return [RAConvertor dict2data:ret];
  4121. }
  4122. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  4123. {
  4124. // UIApplication * app = [UIApplication sharedApplication];
  4125. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4126. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4127. 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];
  4128. sqlite3_stmt * statement;
  4129. int count=0;
  4130. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4131. {
  4132. while (sqlite3_step(statement) == SQLITE_ROW)
  4133. {
  4134. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  4135. int bitem_id = sqlite3_column_int(statement, 0);
  4136. int bitem_qty = sqlite3_column_int(statement, 1);
  4137. char *name = (char*)sqlite3_column_text(statement, 2);
  4138. if(name==nil)
  4139. name="";
  4140. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  4141. char *description = (char*)sqlite3_column_text(statement, 3);
  4142. if(description==nil)
  4143. description="";
  4144. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4145. double unit_price = sqlite3_column_double(statement, 4);
  4146. int use_unitprice = sqlite3_column_int(statement, 5);
  4147. if(use_unitprice!=1)
  4148. {
  4149. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4150. }
  4151. itemjson[@"model"]=nsname;
  4152. itemjson[@"description"]=nsdescription;
  4153. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4154. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4155. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4156. if(compute)
  4157. {
  4158. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4159. }
  4160. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4161. count++;
  4162. }
  4163. sqlite3_finalize(statement);
  4164. }
  4165. ret[@"count"]=@(count);
  4166. if(count==0)
  4167. return nil;
  4168. else
  4169. return ret;
  4170. }
  4171. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4172. {
  4173. //compute: add part to subtotal;
  4174. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4175. dict_item[@(item_id)]=@"1";
  4176. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4177. double cuft=0;
  4178. double weight=0;
  4179. int carton=0;
  4180. int impack=0;
  4181. 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];
  4182. sqlite3_stmt * statement;
  4183. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4184. {
  4185. if (sqlite3_step(statement) == SQLITE_ROW)
  4186. {
  4187. double ulength = sqlite3_column_double(statement, 0);
  4188. double uwidth = sqlite3_column_double(statement, 1);
  4189. double uheight = sqlite3_column_double(statement, 2);
  4190. double uweight = sqlite3_column_double(statement, 3);
  4191. double mlength = sqlite3_column_double(statement, 4);
  4192. double mwidth = sqlite3_column_double(statement, 5);
  4193. double mheight = sqlite3_column_double(statement, 6);
  4194. double mweight = sqlite3_column_double(statement, 7);
  4195. double ilength = sqlite3_column_double(statement, 8);
  4196. double iwidth = sqlite3_column_double(statement, 9);
  4197. double iheight = sqlite3_column_double(statement, 10);
  4198. double iweight = sqlite3_column_double(statement, 11);
  4199. // int pcs = sqlite3_column_int(statement,12);
  4200. int mpack = sqlite3_column_int(statement, 13);
  4201. impack = mpack;
  4202. int ipack = sqlite3_column_int(statement, 14);
  4203. double ucbf = sqlite3_column_double(statement, 15);
  4204. // double icbf = sqlite3_column_double(statement, 16);
  4205. // double mcbf = sqlite3_column_double(statement, 17);
  4206. if(ipack==0)
  4207. {
  4208. carton= count/mpack ;
  4209. weight = mweight*carton;
  4210. cuft= carton*(mlength*mwidth*mheight);
  4211. int remain=count%mpack;
  4212. if(remain==0)
  4213. {
  4214. //do nothing;
  4215. }
  4216. else
  4217. {
  4218. carton++;
  4219. weight += uweight*remain;
  4220. cuft += (ulength*uwidth*uheight)*remain;
  4221. }
  4222. }
  4223. else
  4224. {
  4225. carton = count/(mpack*ipack);
  4226. weight = mweight*carton;
  4227. cuft= carton*(mlength*mwidth*mheight);
  4228. int remain=count%(mpack*ipack);
  4229. if(remain==0)
  4230. {
  4231. // do nothing;
  4232. }
  4233. else
  4234. {
  4235. carton++;
  4236. int icarton =remain/ipack;
  4237. int iremain=remain%ipack;
  4238. weight += iweight*icarton;
  4239. cuft += (ilength*iwidth*iheight)*icarton;
  4240. if(iremain==0)
  4241. {
  4242. //do nothing;
  4243. }
  4244. else
  4245. {
  4246. weight += uweight*iremain;
  4247. cuft += (ulength*uwidth*uheight)*iremain;
  4248. }
  4249. }
  4250. }
  4251. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4252. cuft=ucbf*count;
  4253. weight= uweight*count;
  4254. #endif
  4255. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4256. cuft=ucbf*count;
  4257. weight= uweight*count;
  4258. #endif
  4259. }
  4260. sqlite3_finalize(statement);
  4261. }
  4262. if(compute)
  4263. {
  4264. NSArray * arr_count=nil;
  4265. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4266. for(int i=0;i<arr_bundle.count;i++)
  4267. {
  4268. dict_item[arr_bundle[i]]=@"1";
  4269. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4270. cuft+=[bundlejson[@"cuft"] doubleValue];
  4271. weight+=[bundlejson[@"weight"] doubleValue];
  4272. carton+=[bundlejson[@"carton"] intValue];
  4273. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4274. }
  4275. }
  4276. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4277. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4278. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4279. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4280. ret[@"items"]=dict_item;
  4281. return ret;
  4282. }
  4283. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4284. {
  4285. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4286. // 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 ];
  4287. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4288. DebugLog(@"offline_login sql:%@",sqlQuery);
  4289. sqlite3_stmt * statement;
  4290. int cart_count=0;
  4291. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4292. if ( dbresult== SQLITE_OK)
  4293. {
  4294. while (sqlite3_step(statement) == SQLITE_ROW)
  4295. {
  4296. int item_id = sqlite3_column_int(statement, 0);
  4297. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4298. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4299. }
  4300. sqlite3_finalize(statement);
  4301. }
  4302. return cart_count;
  4303. }
  4304. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4305. //{
  4306. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4307. // sqlite3 *db = [iSalesDB get_db];
  4308. // UIApplication * app = [UIApplication sharedApplication];
  4309. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4310. //
  4311. // NSString* orderCode=params[@"orderCode"];
  4312. //
  4313. // int sort = [[params objectForKey:@"sort"] intValue];
  4314. // NSString *sort_str = @"";
  4315. // switch (sort) {
  4316. // case 0:{
  4317. // sort_str = @"order by c.modify_time desc";
  4318. // }
  4319. // break;
  4320. // case 1:{
  4321. // sort_str = @"order by c.modify_time asc";
  4322. // }
  4323. // break;
  4324. // case 2:{
  4325. // sort_str = @"order by m.name asc";
  4326. // }
  4327. // break;
  4328. // case 3:{
  4329. // sort_str = @"order by m.name desc";
  4330. // }
  4331. // break;
  4332. // case 4:{
  4333. // sort_str = @"order by m.description asc";
  4334. // }
  4335. // break;
  4336. //
  4337. // default:
  4338. // break;
  4339. //
  4340. // }
  4341. //
  4342. //
  4343. //
  4344. // 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 ];
  4345. //
  4346. //
  4347. //// 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 ];
  4348. //
  4349. //
  4350. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4351. //
  4352. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4353. // sqlite3_stmt * statement;
  4354. //
  4355. //
  4356. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4357. //
  4358. // NSDate *date1 = [NSDate date];
  4359. //
  4360. // int count=0;
  4361. // int cart_count=0;
  4362. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4363. // if ( dbresult== SQLITE_OK)
  4364. // {
  4365. //
  4366. //
  4367. // while (sqlite3_step(statement) == SQLITE_ROW)
  4368. // {
  4369. //// NSDate *row_date = [NSDate date];
  4370. //
  4371. //
  4372. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4373. //
  4374. //
  4375. //
  4376. //
  4377. // int product_id = sqlite3_column_int(statement, 0);
  4378. //
  4379. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4380. //
  4381. // int item_id = sqlite3_column_int(statement, 7);
  4382. //
  4383. // NSString* Price=nil;
  4384. // if(str_price==nil)
  4385. // {
  4386. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4387. //// NSDate *price_date = [NSDate date];
  4388. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4389. //// DebugLog(@"price time interval");
  4390. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4391. //
  4392. // if(price==nil)
  4393. // Price=@"No Price.";
  4394. // else
  4395. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4396. // }
  4397. // else
  4398. // {
  4399. //
  4400. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4401. // }
  4402. //
  4403. //
  4404. // double discount = sqlite3_column_double(statement, 2);
  4405. // int item_count = sqlite3_column_int(statement, 3);
  4406. //
  4407. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4408. // NSString *nsline_note=nil;
  4409. // if(line_note!=nil)
  4410. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4411. //
  4412. //
  4413. // char *name = (char*)sqlite3_column_text(statement, 5);
  4414. // NSString *nsname=nil;
  4415. // if(name!=nil)
  4416. // nsname= [[NSString alloc]initWithUTF8String:name];
  4417. //
  4418. // char *description = (char*)sqlite3_column_text(statement, 6);
  4419. // NSString *nsdescription=nil;
  4420. // if(description!=nil)
  4421. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4422. //
  4423. //
  4424. //
  4425. // int stockUom = sqlite3_column_int(statement, 8);
  4426. // int _id = sqlite3_column_int(statement, 9);
  4427. // int availability = sqlite3_column_int(statement, 10);
  4428. //
  4429. //// NSDate *subtotal_date = [NSDate date];
  4430. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4431. //// DebugLog(@"subtotal_date time interval");
  4432. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4433. //
  4434. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4435. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4436. // int carton=[bsubtotaljson[@"carton"] intValue];
  4437. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4438. //
  4439. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4440. //// NSDate *img_date = [NSDate date];
  4441. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4442. //// DebugLog(@"img_date time interval");
  4443. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4444. //
  4445. // itemjson[@"model"]=nsname;
  4446. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4447. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4448. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4449. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4450. // itemjson[@"check"]=@"true";
  4451. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4452. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4453. // itemjson[@"unit_price"]=Price;
  4454. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4455. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4456. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4457. // itemjson[@"note"]=nsline_note;
  4458. // if (!appDelegate.can_create_backorder) {
  4459. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4460. // }
  4461. //// NSDate *date2 = [NSDate date];
  4462. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4463. //// DebugLog(@"model_bundle time interval");
  4464. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4465. //
  4466. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4467. // count++;
  4468. //
  4469. //// DebugLog(@"row time interval");
  4470. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4471. // }
  4472. //
  4473. //
  4474. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4475. // sqlite3_finalize(statement);
  4476. // }
  4477. //
  4478. //
  4479. // DebugLog(@"request cart total time interval");
  4480. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4481. //
  4482. //
  4483. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4484. //
  4485. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4486. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4487. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4488. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4489. //
  4490. // ret[@"mode"]=@"Regular Mode";
  4491. //
  4492. // [iSalesDB close_db:db];
  4493. //
  4494. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4495. //
  4496. // DebugLog(@"general notes :%@",general_note);
  4497. //
  4498. // ret[@"general_note"]= general_note;
  4499. //
  4500. // return [RAConvertor dict2data:ret];
  4501. //}
  4502. +(NSData*) offline_login :(NSMutableDictionary *) params
  4503. {
  4504. NSString* user = [params valueForKey:@"user"];
  4505. NSString* password = [params valueForKey:@"password"];
  4506. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4507. sqlite3 *db = [iSalesDB get_db];
  4508. 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"]];
  4509. DebugLog(@"offline_login sql:%@",sqlQuery);
  4510. sqlite3_stmt * statement;
  4511. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4512. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4513. {
  4514. if (sqlite3_step(statement) == SQLITE_ROW)
  4515. {
  4516. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4517. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4518. int can_show_price = sqlite3_column_int(statement, 0);
  4519. int can_see_price = sqlite3_column_int(statement, 1);
  4520. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4521. if(contact_id==nil)
  4522. contact_id="";
  4523. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4524. int user_type = sqlite3_column_int(statement, 3);
  4525. int can_cancel_order = sqlite3_column_int(statement, 4);
  4526. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4527. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4528. int can_delete_order = sqlite3_column_int(statement, 7);
  4529. int can_submit_order = sqlite3_column_int(statement, 8);
  4530. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4531. int can_create_order = sqlite3_column_int(statement, 10);
  4532. char *mode = (char*)sqlite3_column_text(statement, 11);
  4533. if(mode==nil)
  4534. mode="";
  4535. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4536. char *username = (char*)sqlite3_column_text(statement, 12);
  4537. if(username==nil)
  4538. username="";
  4539. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4540. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4541. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4542. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4543. [header setValue:nscontact_id forKey:@"contact_id"];
  4544. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4545. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4546. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4547. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4548. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4549. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4550. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4551. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4552. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4553. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4554. [header setValue:nsusername forKey:@"username"];
  4555. NSError* error=nil;
  4556. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4557. [header setValue:statusFilter forKey:@"statusFilter"];
  4558. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4559. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4560. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4561. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4562. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4563. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4564. [ret setObject:header forKey:@"header"];
  4565. [ret setValue:nsmode forKey:@"mode"];
  4566. }
  4567. sqlite3_finalize(statement);
  4568. }
  4569. [iSalesDB close_db:db];
  4570. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4571. return [RAConvertor dict2data:ret];
  4572. }
  4573. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4574. {
  4575. assert(params[@"mode"]!=nil);
  4576. NSString* contactId = [params valueForKey:@"contactId"];
  4577. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4578. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4579. sqlite3 *db = [iSalesDB get_db];
  4580. NSString *sqlQuery = nil;
  4581. {
  4582. 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,carrier from offline_contact where contact_id='%@'",contactId];
  4583. }
  4584. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4585. sqlite3_stmt * statement;
  4586. [ret setValue:@"2" forKey:@"result"];
  4587. int carrier = -1;
  4588. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4589. {
  4590. //int i = 0;
  4591. if (sqlite3_step(statement) == SQLITE_ROW)
  4592. {
  4593. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4594. // int editable = sqlite3_column_int(statement, 0);
  4595. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4596. NSString *nscompany_name =nil;
  4597. if(company_name==nil)
  4598. nscompany_name=@"";
  4599. else
  4600. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4601. char *country = (char*)sqlite3_column_text(statement, 2);
  4602. if(country==nil)
  4603. country="";
  4604. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4605. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4606. // if(addr==nil)
  4607. // addr="";
  4608. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4609. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4610. if(zipcode==nil)
  4611. zipcode="";
  4612. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4613. char *state = (char*)sqlite3_column_text(statement, 5);
  4614. if(state==nil)
  4615. state="";
  4616. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4617. char *city = (char*)sqlite3_column_text(statement, 6);
  4618. if(city==nil)
  4619. city="";
  4620. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4621. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4622. // NSString *nscontact_name = nil;
  4623. // if(contact_name==nil)
  4624. // nscontact_name=@"";
  4625. // else
  4626. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4627. char *phone = (char*)sqlite3_column_text(statement, 8);
  4628. NSString *nsphone = nil;
  4629. if(phone==nil)
  4630. nsphone=@"";
  4631. else
  4632. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4633. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4634. // if(contact_id==nil)
  4635. // contact_id="";
  4636. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4637. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4638. if(addr_1==nil)
  4639. addr_1="";
  4640. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4641. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4642. if(addr_2==nil)
  4643. addr_2="";
  4644. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4645. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4646. if(addr_3==nil)
  4647. addr_3="";
  4648. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4649. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4650. if(addr_4==nil)
  4651. addr_4="";
  4652. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4653. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4654. if(first_name==nil)
  4655. first_name="";
  4656. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4657. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4658. if(last_name==nil)
  4659. last_name="";
  4660. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4661. char *fax = (char*)sqlite3_column_text(statement, 16);
  4662. NSString *nsfax = nil;
  4663. if(fax==nil)
  4664. nsfax=@"";
  4665. else
  4666. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4667. char *email = (char*)sqlite3_column_text(statement, 17);
  4668. NSString *nsemail = nil;
  4669. if(email==nil)
  4670. nsemail=@"";
  4671. else
  4672. nsemail= [[NSString alloc]initWithUTF8String:email];
  4673. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4674. NSString *nsimg_0 = nil;
  4675. if(img_0==nil)
  4676. nsimg_0=@"";
  4677. else
  4678. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4679. [self copy_bcardImg:nsimg_0];
  4680. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4681. NSString *nsimg_1 = nil;
  4682. if(img_1==nil)
  4683. nsimg_1=@"";
  4684. else
  4685. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4686. [self copy_bcardImg:nsimg_1];
  4687. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4688. NSString *nsimg_2 = nil;
  4689. if(img_2==nil)
  4690. nsimg_2=@"";
  4691. else
  4692. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4693. [self copy_bcardImg:nsimg_2];
  4694. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4695. NSString *nsprice_type = nil;
  4696. if(price_type==nil)
  4697. nsprice_type=@"";
  4698. else
  4699. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4700. char *notes = (char*)sqlite3_column_text(statement, 22);
  4701. NSString *nsnotes = nil;
  4702. if(notes==nil)
  4703. nsnotes=@"";
  4704. else
  4705. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4706. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4707. NSString *nssalesrep = nil;
  4708. if(salesrep==nil)
  4709. nssalesrep=@"";
  4710. else
  4711. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4712. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4713. carrier = sqlite3_column_int(statement, 25);
  4714. {
  4715. // decrypt
  4716. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4717. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4718. nsphone=[AESCrypt fastdecrypt:nsphone];
  4719. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4720. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4721. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4722. }
  4723. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4724. [arr_name addObject:nsfirst_name];
  4725. [arr_name addObject:nslast_name];
  4726. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4727. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4728. [arr_ext addObject:nsaddr_1];
  4729. [arr_ext addObject:nsaddr_2];
  4730. [arr_ext addObject:nsaddr_3];
  4731. [arr_ext addObject:nsaddr_4];
  4732. [arr_ext addObject:@"\r\n"];
  4733. [arr_ext addObject:nscity];
  4734. [arr_ext addObject:nsstate];
  4735. [arr_ext addObject:nszipcode];
  4736. [arr_ext addObject:nscountry];
  4737. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4738. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4739. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4740. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4741. [item setValue:nscountry forKey:@"customer_country"];
  4742. [item setValue:nsphone forKey:@"customer_phone"];
  4743. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4744. [item setValue:nscompany_name forKey:@"customer_name"];
  4745. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4746. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4747. [item setValue:nsext forKey:@"customer_contact_ext"];
  4748. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4749. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4750. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4751. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4752. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4753. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4754. [item setValue:nslast_name forKey:@"customer_last_name"];
  4755. [item setValue:nscity forKey:@"customer_city"];
  4756. [item setValue:nsstate forKey:@"customer_state"];
  4757. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4758. [item setValue:contactId forKey:@"customer_cid"];
  4759. [item setValue:nscontact_name forKey:@"customer_contact"];
  4760. [item setValue:nsfax forKey:@"customer_fax"];
  4761. [item setValue:nsemail forKey:@"customer_email"];
  4762. [item setValue:contact_type forKey:@"customer_contact_type"];
  4763. // i++;
  4764. }
  4765. // UIApplication * app = [UIApplication sharedApplication];
  4766. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4767. [ret setValue:params[@"mode"] forKey:@"mode"];
  4768. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4769. sqlite3_finalize(statement);
  4770. }
  4771. [iSalesDB close_db:db];
  4772. NSString* scarrier = [self offline_getCarrier:carrier];
  4773. item[@"customer_truck_carrier"] = scarrier;
  4774. [ret setObject:item forKey:@"customerInfo"];
  4775. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4776. return ret;
  4777. }
  4778. + (bool) copy_bcardImg:(NSString*) filename
  4779. {
  4780. if(filename.length==0)
  4781. return false;
  4782. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4783. bool ret=false;
  4784. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4785. NSString *cachefolder = [paths objectAtIndex:0];
  4786. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4787. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4788. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4789. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4790. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4791. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4792. //
  4793. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4794. NSFileManager* fileManager = [NSFileManager defaultManager];
  4795. BOOL bdir=NO;
  4796. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4797. {
  4798. NSError *error = nil;
  4799. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4800. {
  4801. ret=false;
  4802. }
  4803. else
  4804. {
  4805. ret=true;
  4806. }
  4807. // NSError *error = nil;
  4808. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4809. //
  4810. // if(!bsuccess)
  4811. // {
  4812. // DebugLog(@"Create offline_createimg folder failed");
  4813. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4814. // return [RAConvertor dict2data:ret];
  4815. // }
  4816. // if(bsuccess)
  4817. // {
  4818. // sqlite3 *db = [self get_db];
  4819. //
  4820. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4821. // [iSalesDB close_db:db];
  4822. // }
  4823. }
  4824. return ret;
  4825. //
  4826. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4827. // if(bsuccess)
  4828. // {
  4829. // NSError *error = nil;
  4830. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4831. // {
  4832. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4833. // }
  4834. // else
  4835. // {
  4836. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4837. //
  4838. // ret[@"img_url_aname"]=filename;
  4839. // ret[@"img_url"]=savedImagePath;
  4840. // }
  4841. // }
  4842. }
  4843. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4844. {
  4845. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4846. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4847. NSString *cachefolder = [paths objectAtIndex:0];
  4848. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4849. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4850. NSFileManager* fileManager = [NSFileManager defaultManager];
  4851. BOOL bdir=YES;
  4852. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4853. {
  4854. NSError *error = nil;
  4855. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4856. if(!bsuccess)
  4857. {
  4858. DebugLog(@"Create offline_createimg folder failed");
  4859. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4860. return [RAConvertor dict2data:ret];
  4861. }
  4862. // if(bsuccess)
  4863. // {
  4864. // sqlite3 *db = [self get_db];
  4865. //
  4866. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4867. // [iSalesDB close_db:db];
  4868. // }
  4869. }
  4870. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4871. {
  4872. NSError *error = nil;
  4873. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4874. if(!bsuccess)
  4875. {
  4876. DebugLog(@"Create img_cache folder failed");
  4877. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4878. return [RAConvertor dict2data:ret];
  4879. }
  4880. // if(bsuccess)
  4881. // {
  4882. // sqlite3 *db = [self get_db];
  4883. //
  4884. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4885. // [iSalesDB close_db:db];
  4886. // }
  4887. }
  4888. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4889. //JEPG格式
  4890. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4891. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4892. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4893. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4894. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4895. if(bsuccess)
  4896. {
  4897. NSError *error = nil;
  4898. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4899. {
  4900. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4901. }
  4902. else
  4903. {
  4904. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4905. ret[@"img_url_aname"]=filename;
  4906. ret[@"img_url"]=filename;
  4907. }
  4908. }
  4909. else
  4910. {
  4911. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4912. }
  4913. return [RAConvertor dict2data:ret];
  4914. }
  4915. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4916. {
  4917. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4918. category = [category substringToIndex:3];
  4919. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4920. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4921. params[@"category"]= category;
  4922. ret[@"params"]= params;
  4923. [ret setValue:@"detail" forKey:@"target"];
  4924. [ret setValue:@"popup" forKey:@"action"];
  4925. [ret setValue:@"content" forKey:@"type"];
  4926. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4927. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4928. [ret setValue:@"true" forKey:@"single_row"];
  4929. [ret setValue:@"true" forKey:@"partial_refresh"];
  4930. // sqlite3 *db = [iSalesDB get_db];
  4931. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4932. NSString *sqlQuery =nil;
  4933. #ifdef BUILD_NPD
  4934. 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 ;
  4935. #else
  4936. 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];
  4937. #endif
  4938. sqlite3_stmt * statement;
  4939. int count = 0;
  4940. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4941. // int count=0;
  4942. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4943. {
  4944. int i=0;
  4945. while (sqlite3_step(statement) == SQLITE_ROW)
  4946. {
  4947. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4948. // char *name = (char*)sqlite3_column_text(statement, 1);
  4949. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4950. char *name = (char*)sqlite3_column_text(statement, 0);
  4951. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4952. int product_id = sqlite3_column_int(statement, 1);
  4953. char *url = (char*)sqlite3_column_text(statement, 2);
  4954. if(url==nil)
  4955. url="";
  4956. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4957. [item setValue:nsurl forKey:@"picture_path"];
  4958. [item setValue:nsname forKey:@"fash_name"];
  4959. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4960. [item setValue:category forKey:@"category"];
  4961. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4962. i++;
  4963. }
  4964. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4965. sqlite3_finalize(statement);
  4966. }
  4967. DebugLog(@"count:%d",count);
  4968. // [iSalesDB close_db:db];
  4969. return ret;
  4970. }
  4971. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4972. {
  4973. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4974. [ret setValue:key forKey:@"key"];
  4975. [ret setValue:value forKey:@"val"];
  4976. [ret setValue:@"price" forKey:@"type"];
  4977. return ret;
  4978. }
  4979. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4980. {
  4981. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4982. [ret setValue:key forKey:@"key"];
  4983. [ret setValue:value forKey:@"val"];
  4984. return ret;
  4985. }
  4986. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4987. {
  4988. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4989. [ret setValue:@"0" forKey:@"img_count"];
  4990. // sqlite3 *db = [iSalesDB get_db];
  4991. 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 ;
  4992. sqlite3_stmt * statement;
  4993. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4994. {
  4995. int i=0;
  4996. if (sqlite3_step(statement) == SQLITE_ROW)
  4997. {
  4998. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4999. // char *name = (char*)sqlite3_column_text(statement, 1);
  5000. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5001. char *value = (char*)sqlite3_column_text(statement, 0);
  5002. if(value==nil)
  5003. value="";
  5004. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5005. char *key = (char*)sqlite3_column_text(statement, 1);
  5006. if(key==nil)
  5007. key="";
  5008. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  5009. [item setValue:nsvalue forKey:@"val"];
  5010. [item setValue:nskey forKey:@"key"];
  5011. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5012. [ret setValue:@"1" forKey:@"count"];
  5013. i++;
  5014. }
  5015. sqlite3_finalize(statement);
  5016. }
  5017. // [iSalesDB close_db:db];
  5018. return ret;
  5019. }
  5020. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  5021. {
  5022. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5023. [ret setValue:@"0" forKey:@"count"];
  5024. // sqlite3 *db = [iSalesDB get_db];
  5025. 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;
  5026. sqlite3_stmt * statement;
  5027. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5028. {
  5029. int i=0;
  5030. while (sqlite3_step(statement) == SQLITE_ROW)
  5031. {
  5032. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5033. // char *name = (char*)sqlite3_column_text(statement, 1);
  5034. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5035. char *value = (char*)sqlite3_column_text(statement, 0);
  5036. if(value==nil)
  5037. value="";
  5038. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  5039. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  5040. if(selector_display==nil)
  5041. selector_display="";
  5042. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  5043. int product_id = sqlite3_column_int(statement, 2);
  5044. char *category = (char*)sqlite3_column_text(statement, 3);
  5045. if(category==nil)
  5046. category="";
  5047. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  5048. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  5049. [item setValue:nsvalue forKey:@"title"];
  5050. [item setValue:url forKey:@"pic_url"];
  5051. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  5052. [params setValue:@"2" forKey:@"count"];
  5053. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  5054. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  5055. [param0 setValue:@"product_id" forKey:@"name"];
  5056. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  5057. [param1 setValue:nscategory forKey:@"val"];
  5058. [param1 setValue:@"category" forKey:@"name"];
  5059. [params setObject:param0 forKey:@"param_0"];
  5060. [params setObject:param1 forKey:@"param_1"];
  5061. [item setObject:params forKey:@"params"];
  5062. [ret setValue:nsselector_display forKey:@"name"];
  5063. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5064. i++;
  5065. }
  5066. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5067. [ret setValue:@"switch" forKey:@"action"];
  5068. sqlite3_finalize(statement);
  5069. }
  5070. // [iSalesDB close_db:db];
  5071. return ret;
  5072. }
  5073. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  5074. {
  5075. // model 在 category search 显示的图片。
  5076. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  5077. // sqlite3 *db = [iSalesDB get_db];
  5078. 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];
  5079. sqlite3_stmt * statement;
  5080. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5081. {
  5082. while (sqlite3_step(statement) == SQLITE_ROW)
  5083. {
  5084. char *url = (char*)sqlite3_column_text(statement, 0);
  5085. if(url==nil)
  5086. url="";
  5087. int type = sqlite3_column_int(statement, 1);
  5088. if(type==0)
  5089. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  5090. else
  5091. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  5092. }
  5093. sqlite3_finalize(statement);
  5094. }
  5095. // [iSalesDB close_db:db];
  5096. return ret;
  5097. }
  5098. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  5099. {
  5100. int item_id=-1;
  5101. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  5102. sqlite3_stmt * statement;
  5103. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5104. {
  5105. if (sqlite3_step(statement) == SQLITE_ROW)
  5106. {
  5107. item_id = sqlite3_column_int(statement, 0);
  5108. }
  5109. sqlite3_finalize(statement);
  5110. }
  5111. return item_id;
  5112. }
  5113. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  5114. {
  5115. // NSString* ret = @"";
  5116. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  5117. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  5118. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  5119. sqlite3_stmt * statement;
  5120. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5121. {
  5122. while (sqlite3_step(statement) == SQLITE_ROW)
  5123. {
  5124. int bitem_id = sqlite3_column_int(statement, 0);
  5125. int bitem_qty = sqlite3_column_int(statement, 1);
  5126. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  5127. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  5128. }
  5129. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  5130. sqlite3_finalize(statement);
  5131. }
  5132. // if(ret==nil)
  5133. // ret=@"";
  5134. *count=arr_count;
  5135. return arr_bundle;
  5136. }
  5137. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5138. {
  5139. // get default sold qty, return -1 if model not found;
  5140. int ret = -1;
  5141. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5142. sqlite3_stmt * statement;
  5143. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5144. {
  5145. if (sqlite3_step(statement) == SQLITE_ROW)
  5146. {
  5147. ret = sqlite3_column_int(statement, 0);
  5148. }
  5149. sqlite3_finalize(statement);
  5150. }
  5151. return ret;
  5152. }
  5153. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5154. {
  5155. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5156. [ret setValue:@"0" forKey:@"img_count"];
  5157. // sqlite3 *db = [iSalesDB get_db];
  5158. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5159. sqlite3_stmt * statement;
  5160. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5161. {
  5162. int i=0;
  5163. while (sqlite3_step(statement) == SQLITE_ROW)
  5164. {
  5165. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5166. // char *name = (char*)sqlite3_column_text(statement, 1);
  5167. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5168. char *url = (char*)sqlite3_column_text(statement, 0);
  5169. if(url==nil)
  5170. url="";
  5171. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5172. [item setValue:nsurl forKey:@"s"];
  5173. [item setValue:nsurl forKey:@"l"];
  5174. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5175. i++;
  5176. }
  5177. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5178. sqlite3_finalize(statement);
  5179. }
  5180. // [iSalesDB close_db:db];
  5181. return ret;
  5182. }
  5183. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5184. // UIApplication * app = [UIApplication sharedApplication];
  5185. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5186. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5187. sqlite3 *db = [iSalesDB get_db];
  5188. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5189. NSString* product_id=params[@"product_id"];
  5190. NSString *item_count_str = params[@"item_count"];
  5191. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5192. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5193. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5194. // NSString *sql = @"";
  5195. for(int i=0;i<arr.count;i++)
  5196. {
  5197. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5198. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5199. __block int cart_count = 0;
  5200. if (!item_count_str) {
  5201. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5202. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5203. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5204. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5205. cart_count = [[model_set_components lastObject] intValue];
  5206. }];
  5207. }
  5208. if(count==0)
  5209. {
  5210. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5211. sqlite3_stmt *stmt;
  5212. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5213. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5214. if (item_count_arr) {
  5215. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5216. } else {
  5217. sqlite3_bind_int(stmt,2,cart_count);
  5218. }
  5219. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5220. [iSalesDB execSql:@"ROLLBACK" db:db];
  5221. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5222. [iSalesDB close_db:db];
  5223. DebugLog(@"add to wishlist error");
  5224. return ret;
  5225. }
  5226. } else {
  5227. int qty = 0;
  5228. if (item_count_arr) {
  5229. qty = [item_count_arr[i] intValue];
  5230. } else {
  5231. qty = cart_count;
  5232. }
  5233. 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]];
  5234. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5235. [iSalesDB execSql:@"ROLLBACK" db:db];
  5236. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5237. [iSalesDB close_db:db];
  5238. DebugLog(@"add to wishlist error");
  5239. return ret;
  5240. }
  5241. }
  5242. }
  5243. // [iSalesDB execSql:sql db:db];
  5244. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5245. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5246. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5247. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5248. [iSalesDB close_db:db];
  5249. ret[@"wish_count"]=@(count);
  5250. ret[@"result"]= [NSNumber numberWithInt:2];
  5251. return ret;
  5252. }
  5253. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5254. // 0 category
  5255. // 1 search
  5256. // 2 itemsearch
  5257. NSData *ret = nil;
  5258. NSDictionary *items = nil;
  5259. switch (from) {
  5260. case 0:{
  5261. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5262. }
  5263. break;
  5264. case 1:{
  5265. items = [[self search:params limited:NO] objectForKey:@"items"];
  5266. }
  5267. break;
  5268. case 2:{
  5269. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5270. }
  5271. break;
  5272. default:
  5273. break;
  5274. }
  5275. if (!items) {
  5276. return ret;
  5277. }
  5278. int count = [[items objectForKey:@"count"] intValue];
  5279. NSMutableString *product_id_str = [@"" mutableCopy];
  5280. for (int i = 0; i < count; i++) {
  5281. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5282. NSDictionary *item = [items objectForKey:key];
  5283. NSString *product_id = [item objectForKey:@"product_id"];
  5284. if (i == 0) {
  5285. [product_id_str appendString:product_id];
  5286. } else {
  5287. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5288. }
  5289. }
  5290. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5291. if ([add_to isEqualToString:@"cart"]) {
  5292. NSString *order_code = [params objectForKey:@"orderCode"];
  5293. if (order_code.length) {
  5294. NSDictionary *newParams = @{
  5295. @"product_id" : product_id_str,
  5296. @"orderCode" : order_code,
  5297. @"can_create_backorder":params[@"can_create_backorder"]
  5298. };
  5299. ret = [self offline_add2cart:newParams.mutableCopy];
  5300. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5301. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5302. ret = [RAConvertor dict2data:retDic];
  5303. }
  5304. } else if([add_to isEqualToString:@"wishlist"]) {
  5305. NSDictionary *newParams = @{
  5306. @"product_id" : product_id_str
  5307. };
  5308. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5309. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5310. ret = [RAConvertor dict2data:retDic];
  5311. } else if([add_to isEqualToString:@"portfolio"]) {
  5312. NSDictionary *newParams = @{
  5313. @"product_id" : product_id_str
  5314. };
  5315. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5316. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5317. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5318. ret = [RAConvertor dict2data:retDic];
  5319. }
  5320. return ret;
  5321. }
  5322. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5323. {
  5324. return [self addAll:params from:0];
  5325. }
  5326. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5327. {
  5328. return [self addAll:params from:1];
  5329. }
  5330. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5331. {
  5332. return [self addAll:params from:2];
  5333. }
  5334. #pragma mark - Jack
  5335. #warning 做SQL操作时转义!!
  5336. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5337. // "val_227" : {
  5338. // "check" : 1,
  5339. // "value" : "US United States",
  5340. // "value_id" : "228"
  5341. // },
  5342. if (!countryCode) {
  5343. countryCode = @"US";
  5344. }
  5345. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5346. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5347. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5348. int code_id = sqlite3_column_int(stmt, 3); // id
  5349. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5350. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5351. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5352. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5353. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5354. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5355. }
  5356. long n = *count;
  5357. *count = n + 1;
  5358. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5359. [container setValue:countryDic forKey:key];
  5360. }] mutableCopy];
  5361. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5362. return ret;
  5363. }
  5364. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5365. // "val_227" : {
  5366. // "check" : 1,
  5367. // "value" : "US United States",
  5368. // "value_id" : "228"
  5369. // },
  5370. __block NSString* OrderCarrier =@"";
  5371. // sqlite3 *db = [iSalesDB get_db];
  5372. // sqlite3_stmt * statement;
  5373. [iSalesDB jk_query:[NSString stringWithFormat:@"select carrier from offline_order where so_id='%@';",order_code] db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5374. OrderCarrier = [self textAtColumn:0 statement:statment];
  5375. // sqlite3_finalize(statement);
  5376. }];
  5377. // [iSalesDB close_db:db];
  5378. __block int c=0;
  5379. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5380. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5381. int code_id = sqlite3_column_int(stmt, 2); // id
  5382. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5383. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5384. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5385. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5386. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5387. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5388. }
  5389. c++;
  5390. long n = *count;
  5391. *count = n + 1;
  5392. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5393. [container setValue:carrierDic forKey:key];
  5394. }] mutableCopy];
  5395. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5396. return ret;
  5397. }
  5398. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5399. // "val_227" : {
  5400. // "check" : 1,
  5401. // "value" : "US United States",
  5402. // "value_id" : "228"
  5403. // },
  5404. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5405. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5406. int code_id = sqlite3_column_int(stmt, 2); // id
  5407. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5408. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5409. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5410. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5411. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5412. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5413. }
  5414. long n = *count;
  5415. *count = n + 1;
  5416. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5417. [container setValue:carrierDic forKey:key];
  5418. }] mutableCopy];
  5419. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5420. return ret;
  5421. }
  5422. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5423. // // "val_227" : {
  5424. // // "check" : 1,
  5425. // // "value" : "US United States",
  5426. // // "value_id" : "228"
  5427. // // },
  5428. //
  5429. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5430. // NSString * code = [iSalesDB jk_queryText:Sql];
  5431. //
  5432. // return [self offline_getCarrier:code];
  5433. //
  5434. //}
  5435. + (NSString *)offline_getCarrier:(int)code {
  5436. // "val_227" : {
  5437. // "check" : 1,
  5438. // "value" : "US United States",
  5439. // "value_id" : "228"
  5440. // },
  5441. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5442. NSString * ret = [iSalesDB jk_queryText:Sql];
  5443. return ret;
  5444. }
  5445. + (int )offline_getCarrierCode:(NSString*)name {
  5446. // "val_227" : {
  5447. // "check" : 1,
  5448. // "value" : "US United States",
  5449. // "value_id" : "228"
  5450. // },
  5451. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5452. NSString * ret = [iSalesDB jk_queryText:Sql];
  5453. return [ret intValue];
  5454. }
  5455. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5456. countryCode = [self translateSingleQuote:countryCode];
  5457. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5458. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5459. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5460. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5461. if (name == NULL) {
  5462. name = "";
  5463. }
  5464. if (code == NULL) {
  5465. code = "";
  5466. }
  5467. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5468. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5469. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5470. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5471. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5472. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5473. }
  5474. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5475. [container setValue:stateDic forKey:key];
  5476. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5477. DebugLog(@"query all state error: %@",err_msg);
  5478. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5479. // [stateDic setValue:@"Other" forKey:@"value"];
  5480. // [stateDic setValue:@"" forKey:@"value_id"];
  5481. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5482. //
  5483. // if (state_code && [@"" isEqualToString:state_code]) {
  5484. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5485. // }
  5486. //
  5487. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5488. // [container setValue:stateDic forKey:key];
  5489. }] mutableCopy];
  5490. [ret removeObjectForKey:@"result"];
  5491. // failure 可以不用了,一样的
  5492. if (ret.allKeys.count == 0) {
  5493. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5494. [stateDic setValue:@"Other" forKey:@"value"];
  5495. [stateDic setValue:@"" forKey:@"value_id"];
  5496. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5497. if (state_code && [@"" isEqualToString:state_code]) {
  5498. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5499. }
  5500. NSString *key = [NSString stringWithFormat:@"val_0"];
  5501. [ret setValue:stateDic forKey:key];
  5502. }
  5503. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5504. return ret;
  5505. }
  5506. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5507. codeId = [self translateSingleQuote:codeId];
  5508. 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];
  5509. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5510. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5511. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5512. if (name == NULL) {
  5513. name = "";
  5514. }
  5515. if (code == NULL) {
  5516. code = "";
  5517. }
  5518. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5519. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5520. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5521. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5522. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5523. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5524. }
  5525. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5526. [container setValue:stateDic forKey:key];
  5527. }] mutableCopy];
  5528. [ret removeObjectForKey:@"result"];
  5529. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5530. return ret;
  5531. }
  5532. + (NSDictionary *)offline_getPrice {
  5533. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5534. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5535. char *name = (char *) sqlite3_column_text(stmt, 1);
  5536. int type = sqlite3_column_int(stmt, 2);
  5537. int orderBy = sqlite3_column_int(stmt, 3);
  5538. if (name == NULL) {
  5539. name = "";
  5540. }
  5541. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5542. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5543. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5544. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5545. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5546. if (orderBy == 0) {
  5547. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5548. }
  5549. [container setValue:priceDic forKey:key];
  5550. }] mutableCopy];
  5551. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5552. return ret;
  5553. }
  5554. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5555. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5556. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5557. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5558. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5559. // int _id = sqlite3_column_int(stmt, 0);
  5560. NSString *name = [self textAtColumn:1 statement:stmt];
  5561. NSDictionary *typeDic = @{
  5562. @"value_id" : name,
  5563. @"value" : name,
  5564. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5565. };
  5566. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5567. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5568. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5569. }];
  5570. return ret;
  5571. }
  5572. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5573. // 首先从offline_login表中取出sales_code
  5574. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5575. // NSString *user = app.user;
  5576. user = [self translateSingleQuote:user];
  5577. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5578. __block NSString *user_code = @"";
  5579. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5580. char *code = (char *)sqlite3_column_text(stmt, 0);
  5581. if (code == NULL) {
  5582. code = "";
  5583. }
  5584. user_code = [NSString stringWithUTF8String:code];
  5585. }];
  5586. // 再取所有salesRep
  5587. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5588. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5589. // 1 name 2 code 3 salesrep_id
  5590. char *name = (char *)sqlite3_column_text(stmt, 1);
  5591. char *code = (char *)sqlite3_column_text(stmt, 2);
  5592. int salesrep_id = sqlite3_column_int(stmt, 3);
  5593. if (name == NULL) {
  5594. name = "";
  5595. }
  5596. if (code == NULL) {
  5597. code = "";
  5598. }
  5599. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5600. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5601. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5602. // 比较code 相等则check
  5603. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5604. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5605. }
  5606. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5607. }] mutableCopy];
  5608. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5609. return ret;
  5610. }
  5611. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5612. zipcode = [self translateSingleQuote:zipcode];
  5613. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5614. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5615. char *country = (char *)sqlite3_column_text(stmt, 0);
  5616. char *state = (char *)sqlite3_column_text(stmt, 1);
  5617. char *city = (char *)sqlite3_column_text(stmt, 2);
  5618. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5619. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5620. if (country == NULL) {
  5621. country = "";
  5622. }
  5623. if (state == NULL) {
  5624. state = "";
  5625. }
  5626. if (city == NULL) {
  5627. city = "";
  5628. }
  5629. if (country_code == NULL) {
  5630. country_code = "";
  5631. }
  5632. if (state_code == NULL) {
  5633. state_code = "";
  5634. }
  5635. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5636. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5637. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5638. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5639. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5640. }] mutableCopy];
  5641. return ret;
  5642. }
  5643. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5644. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5645. [item setValue:value forKey:valueKey];
  5646. [dic setValue:item forKey:itemKey];
  5647. }
  5648. + (NSString *)countryCodeByid:(NSString *)code_id {
  5649. NSString *ret = nil;
  5650. code_id = [self translateSingleQuote:code_id];
  5651. sqlite3 *db = [iSalesDB get_db];
  5652. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5653. sqlite3_stmt * statement;
  5654. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5655. while (sqlite3_step(statement) == SQLITE_ROW) {
  5656. char *code = (char *)sqlite3_column_text(statement, 0);
  5657. if (code == NULL) {
  5658. code = "";
  5659. }
  5660. ret = [NSString stringWithUTF8String:code];
  5661. }
  5662. sqlite3_finalize(statement);
  5663. }
  5664. [iSalesDB close_db:db];
  5665. return ret;
  5666. }
  5667. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5668. NSString *ret = nil;
  5669. code = [self translateSingleQuote:code];
  5670. sqlite3 *db = [iSalesDB get_db];
  5671. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5672. sqlite3_stmt * statement;
  5673. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5674. while (sqlite3_step(statement) == SQLITE_ROW) {
  5675. char *_id = (char *)sqlite3_column_text(statement, 0);
  5676. if (_id == NULL) {
  5677. _id = "";
  5678. }
  5679. ret = [NSString stringWithFormat:@"%s",_id];
  5680. }
  5681. sqlite3_finalize(statement);
  5682. }
  5683. [iSalesDB close_db:db];
  5684. return ret;
  5685. }
  5686. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5687. NSString *name = nil;
  5688. codeId = [self translateSingleQuote:codeId];
  5689. sqlite3 *db = [iSalesDB get_db];
  5690. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5691. sqlite3_stmt * statement;
  5692. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5693. while (sqlite3_step(statement) == SQLITE_ROW) {
  5694. char *value = (char *)sqlite3_column_text(statement, 0);
  5695. if (value == NULL) {
  5696. value = "";
  5697. }
  5698. name = [NSString stringWithUTF8String:value];
  5699. }
  5700. sqlite3_finalize(statement);
  5701. }
  5702. [iSalesDB close_db:db];
  5703. return name;
  5704. }
  5705. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5706. NSString *ret = nil;
  5707. sqlite3 *db = [iSalesDB get_db];
  5708. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5709. sqlite3_stmt * statement;
  5710. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5711. while (sqlite3_step(statement) == SQLITE_ROW) {
  5712. char *name = (char *)sqlite3_column_text(statement, 0);
  5713. if (name == NULL) {
  5714. name = "";
  5715. }
  5716. ret = [NSString stringWithUTF8String:name];
  5717. }
  5718. sqlite3_finalize(statement);
  5719. }
  5720. [iSalesDB close_db:db];
  5721. return ret;
  5722. }
  5723. + (NSString *)salesRepCodeById:(NSString *)_id {
  5724. NSString *ret = nil;
  5725. _id = [self translateSingleQuote:_id];
  5726. sqlite3 *db = [iSalesDB get_db];
  5727. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5728. sqlite3_stmt * statement;
  5729. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5730. while (sqlite3_step(statement) == SQLITE_ROW) {
  5731. char *rep = (char *)sqlite3_column_text(statement, 0);
  5732. if (rep == NULL) {
  5733. rep = "";
  5734. }
  5735. ret = [NSString stringWithUTF8String:rep];
  5736. }
  5737. sqlite3_finalize(statement);
  5738. }
  5739. [iSalesDB close_db:db];
  5740. return ret;
  5741. }
  5742. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5743. char *tx = (char *)sqlite3_column_text(stmt, col);
  5744. if (tx == NULL) {
  5745. tx = "";
  5746. }
  5747. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5748. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5749. if (!text) {
  5750. text = @"";
  5751. }
  5752. // 将字符全部为' '的字符串干掉
  5753. int spaceCount = 0;
  5754. for (int i = 0; i < text.length; i++) {
  5755. if ([text characterAtIndex:i] == ' ') {
  5756. spaceCount++;
  5757. }
  5758. }
  5759. if (spaceCount == text.length) {
  5760. text = @"";
  5761. }
  5762. return text;
  5763. }
  5764. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5765. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5766. NSData *data = [NSData dataWithContentsOfFile:path];
  5767. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5768. return ret;
  5769. }
  5770. + (NSString *)textFileName:(NSString *)name {
  5771. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5772. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5773. if (!text) {
  5774. text = @"";
  5775. }
  5776. return text;
  5777. }
  5778. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5779. return [[dic objectForKey:key] mutableCopy];
  5780. }
  5781. + (id)translateSingleQuote:(NSString *)string {
  5782. if ([string isKindOfClass:[NSString class]])
  5783. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5784. return string;
  5785. }
  5786. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5787. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5788. }
  5789. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5790. NSString* ret= nil;
  5791. NSString *sqlQuery = nil;
  5792. // 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
  5793. 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];
  5794. sqlite3_stmt * statement;
  5795. // int count=0;
  5796. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5797. {
  5798. if (sqlite3_step(statement) == SQLITE_ROW)
  5799. {
  5800. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5801. if(imgurl==nil)
  5802. imgurl="";
  5803. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5804. ret=nsimgurl;
  5805. }
  5806. sqlite3_finalize(statement);
  5807. }
  5808. else
  5809. {
  5810. [ret setValue:@"8" forKey:@"result"];
  5811. }
  5812. // [iSalesDB close_db:db];
  5813. // DebugLog(@"data string: %@",ret );
  5814. return ret;
  5815. }
  5816. #pragma mark contact Advanced search
  5817. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5818. {
  5819. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5820. return [RAConvertor dict2data:contactAdvanceDic];
  5821. }
  5822. #pragma mark create new contact
  5823. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5824. assert(params[@"user"]!=nil);
  5825. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5826. NSData *data = [NSData dataWithContentsOfFile:path];
  5827. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5828. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5829. NSString *countryCode = nil;
  5830. NSString *countryCode_id = nil;
  5831. NSString *stateCode = nil;
  5832. NSString *city = nil;
  5833. NSString *zipCode = nil;
  5834. NSString *carrierCode = nil;
  5835. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5836. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5837. NSString *code_id = params[@"country"];
  5838. countryCode_id = code_id;
  5839. countryCode = [self countryCodeByid:code_id];
  5840. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5841. NSString *zip_code = params[@"zipcode"];
  5842. // 剔除全部为空格
  5843. int spaceCount = 0;
  5844. for (int i = 0; i < zip_code.length; i++) {
  5845. if ([zip_code characterAtIndex:i] == ' ') {
  5846. spaceCount++;
  5847. }
  5848. }
  5849. if (spaceCount == zip_code.length) {
  5850. zip_code = @"";
  5851. }
  5852. zipCode = zip_code;
  5853. if (zipCode.length > 0) {
  5854. countryCode_id = params[@"country"];
  5855. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5856. countryCode = [dic valueForKey:@"country_code"];
  5857. if (!countryCode) {
  5858. // countryCode = @"US";
  5859. NSString *code_id = params[@"country"];
  5860. countryCode = [self countryCodeByid:code_id];
  5861. }
  5862. stateCode = [dic valueForKey:@"state_code"];
  5863. if (!stateCode.length) {
  5864. stateCode = params[@"state"];
  5865. }
  5866. city = [dic valueForKey:@"city"];
  5867. if (!city.length) {
  5868. city = params[@"city"];
  5869. }
  5870. // zip code
  5871. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5872. [zipDic setValue:zipCode forKey:@"value"];
  5873. [section_0 setValue:zipDic forKey:@"item_11"];
  5874. } else {
  5875. NSString *code_id = params[@"country"];
  5876. countryCode = [self countryCodeByid:code_id];
  5877. stateCode = params[@"state"];
  5878. city = params[@"city"];
  5879. }
  5880. }
  5881. } else {
  5882. // default: US United States
  5883. countryCode = @"US";
  5884. countryCode_id = @"228";
  5885. }
  5886. // country
  5887. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5888. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5889. // state
  5890. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5891. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5892. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5893. // city
  5894. if (city) {
  5895. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5896. [cityDic setValue:city forKey:@"value"];
  5897. [section_0 setValue:cityDic forKey:@"item_13"];
  5898. }
  5899. // price type
  5900. NSDictionary *priceDic = [self offline_getPrice];
  5901. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5902. // contact type
  5903. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5904. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5905. // Sales Rep
  5906. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5907. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5908. #ifdef BUILD_CONTRAST
  5909. //carrier
  5910. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5911. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5912. #endif
  5913. [ret setValue:section_0 forKey:@"section_0"];
  5914. return [RAConvertor dict2data:ret];
  5915. }
  5916. #pragma mark save
  5917. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5918. NSString *addr = nil;
  5919. NSString *contact_name = nil;
  5920. // int carrier = [params[@"carrier"] intValue];
  5921. NSString *companyName = [params objectForKey:@"company"];
  5922. if (companyName) {
  5923. companyName = [AESCrypt fastencrypt:companyName];
  5924. } else {
  5925. companyName = @"";
  5926. }
  5927. DebugLog(@"company");
  5928. companyName = [self translateSingleQuote:companyName];
  5929. NSString *addr1 = [params objectForKey:@"address"];
  5930. NSString *addr2 = [params objectForKey:@"address2"];
  5931. NSString *addr3 = [params objectForKey:@"address_3"];
  5932. NSString *addr4 = [params objectForKey:@"address_4"];
  5933. if (!addr2) {
  5934. addr2 = @"";
  5935. }
  5936. if (!addr3) {
  5937. addr3 = @"";
  5938. }
  5939. if (!addr4) {
  5940. addr4 = @"";
  5941. }
  5942. if (!addr1) {
  5943. addr1 = @"";
  5944. }
  5945. DebugLog(@"addr");
  5946. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5947. addr = [AESCrypt fastencrypt:addr];
  5948. addr = [self translateSingleQuote:addr];
  5949. if (addr1 && ![addr1 isEqualToString:@""]) {
  5950. addr1 = [AESCrypt fastencrypt:addr1];
  5951. }
  5952. addr1 = [self translateSingleQuote:addr1];
  5953. addr2 = [self translateSingleQuote:addr2];
  5954. addr3 = [self translateSingleQuote:addr3];
  5955. addr4 = [self translateSingleQuote:addr4];
  5956. NSString *country = [params objectForKey:@"country"];
  5957. if (country) {
  5958. country = [self countryNameByCountryCodeId:country];
  5959. } else {
  5960. country = @"";
  5961. }
  5962. int carrier = [params[@"carrier"] intValue];
  5963. DebugLog(@"country");
  5964. country = [self translateSingleQuote:country];
  5965. NSString *state = [params objectForKey:@"state"];
  5966. if (!state) {
  5967. state = @"";
  5968. }
  5969. DebugLog(@"state");
  5970. state = [self translateSingleQuote:state];
  5971. NSString *city = [params objectForKey:@"city"];
  5972. if (!city) {
  5973. city = @"";
  5974. }
  5975. city = [self translateSingleQuote:city];
  5976. NSString *zipcode = [params objectForKey:@"zipcode"];
  5977. if (!zipcode) {
  5978. zipcode = @"";
  5979. }
  5980. DebugLog(@"zip");
  5981. zipcode = [self translateSingleQuote:zipcode];
  5982. NSString *fistName = [params objectForKey:@"firstname"];
  5983. if (!fistName) {
  5984. fistName = @"";
  5985. }
  5986. NSString *lastName = [params objectForKey:@"lastname"];
  5987. if (!lastName) {
  5988. lastName = @"";
  5989. }
  5990. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5991. DebugLog(@"contact_name");
  5992. contact_name = [self translateSingleQuote:contact_name];
  5993. fistName = [self translateSingleQuote:fistName];
  5994. lastName = [self translateSingleQuote:lastName];
  5995. NSString *phone = [params objectForKey:@"phone"];
  5996. if (phone) {
  5997. phone = [AESCrypt fastencrypt:phone];
  5998. } else {
  5999. phone = @"";
  6000. }
  6001. DebugLog(@"PHONE");
  6002. phone = [self translateSingleQuote:phone];
  6003. NSString *fax = [params objectForKey:@"fax"];
  6004. if (!fax) {
  6005. fax = @"";
  6006. }
  6007. DebugLog(@"FAX");
  6008. fax = [self translateSingleQuote:fax];
  6009. NSString *email = [params objectForKey:@"email"];
  6010. if (!email) {
  6011. email = @"";
  6012. }
  6013. DebugLog(@"EMAIL:%@",email);
  6014. email = [self translateSingleQuote:email];
  6015. NSString *notes = [params objectForKey:@"contact_notes"];
  6016. if (!notes) {
  6017. notes = @"";
  6018. }
  6019. DebugLog(@"NOTE:%@",notes);
  6020. notes = [self translateSingleQuote:notes];
  6021. NSString *price = [params objectForKey:@"price_name"];
  6022. if (price) {
  6023. price = [self priceNameByPriceId:price];
  6024. } else {
  6025. price = @"";
  6026. }
  6027. DebugLog(@"PRICE");
  6028. price = [self translateSingleQuote:price];
  6029. NSString *salesRep = [params objectForKey:@"sales_rep"];
  6030. if (salesRep) {
  6031. salesRep = [self salesRepCodeById:salesRep];
  6032. } else {
  6033. salesRep = @"";
  6034. }
  6035. salesRep = [self translateSingleQuote:salesRep];
  6036. NSString *img = [params objectForKey:@"business_card"];
  6037. NSArray *array = [img componentsSeparatedByString:@","];
  6038. NSString *img_0 = array[0];
  6039. if (!img_0) {
  6040. img_0 = @"";
  6041. }
  6042. img_0 = [self translateSingleQuote:img_0];
  6043. NSString *img_1 = array[1];
  6044. if (!img_1) {
  6045. img_1 = @"";
  6046. }
  6047. img_1 = [self translateSingleQuote:img_1];
  6048. NSString *img_2 = array[2];
  6049. if (!img_2) {
  6050. img_2 = @"";
  6051. }
  6052. img_2 = [self translateSingleQuote:img_2];
  6053. NSString *contact_id = [NSUUID UUID].UUIDString;
  6054. NSString *contact_type = [params objectForKey:@"type_name"];
  6055. if (!contact_type) {
  6056. contact_type = @"";
  6057. }
  6058. contact_type = [self translateSingleQuote:contact_type];
  6059. // 判断更新时是否为customer
  6060. if (update) {
  6061. contact_id = [params objectForKey:@"contact_id"];
  6062. if (!contact_id) {
  6063. contact_id = @"";
  6064. }
  6065. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  6066. __block int customer = 0;
  6067. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6068. customer = sqlite3_column_int(stmt, 0);
  6069. }];
  6070. isCustomer = customer ? YES : NO;
  6071. }
  6072. NSMutableDictionary *sync_dic = [params mutableCopy];
  6073. if (isCustomer) {
  6074. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  6075. } else {
  6076. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  6077. }
  6078. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  6079. NSString *sync_data = nil;
  6080. NSString *sql = nil;
  6081. if (update){
  6082. contact_id = [params objectForKey:@"contact_id"];
  6083. if (!contact_id) {
  6084. contact_id = @"";
  6085. }
  6086. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6087. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6088. sync_data = [RAConvertor dict2string:sync_dic];
  6089. sync_data = [self translateSingleQuote:sync_data];
  6090. 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 = '%@' ,carrier = %d 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,carrier,contact_id];
  6091. } else {
  6092. contact_id = [self translateSingleQuote:contact_id];
  6093. [sync_dic setValue:contact_id forKey:@"contact_id"];
  6094. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  6095. sync_data = [RAConvertor dict2string:sync_dic];
  6096. sync_data = [self translateSingleQuote:sync_data];
  6097. 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,carrier) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@',%d)",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,carrier];
  6098. }
  6099. int result = [iSalesDB execSql:sql];
  6100. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  6101. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  6102. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  6103. }
  6104. #pragma mark save new contact
  6105. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  6106. {
  6107. return [self offline_saveContact:params update:NO isCustomer:YES];
  6108. }
  6109. #pragma mark edit contact
  6110. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  6111. {
  6112. assert(params[@"user"]!=nil);
  6113. // {
  6114. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  6115. // password = 123456;
  6116. // user = EvanK;
  6117. // }
  6118. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  6119. NSData *data = [NSData dataWithContentsOfFile:path];
  6120. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6121. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  6122. NSString *countryCode = nil;
  6123. NSString *countryCode_id = nil;
  6124. NSString *stateCode = nil;
  6125. __block int carrierCode = -1;
  6126. /*------contact infor------*/
  6127. __block NSString *country = nil;
  6128. __block NSString *company_name = nil;
  6129. __block NSString *contact_id = params[@"contact_id"];
  6130. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  6131. __block NSString *zipcode = nil;
  6132. __block NSString *state = nil; // state_code
  6133. __block NSString *city = nil; //
  6134. __block NSString *firt_name,*last_name;
  6135. __block NSString *phone,*fax,*email;
  6136. __block NSString *notes,*price_type,*sales_rep;
  6137. __block NSString *img_0,*img_1,*img_2;
  6138. __block NSString *contact_type;
  6139. contact_id = [self translateSingleQuote:contact_id];
  6140. 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,carrier from offline_contact where contact_id = '%@';",contact_id];
  6141. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6142. country = [self textAtColumn:0 statement:stmt]; // country name
  6143. company_name = [self textAtColumn:1 statement:stmt];
  6144. addr_1 = [self textAtColumn:2 statement:stmt];
  6145. addr_2 = [self textAtColumn:3 statement:stmt];
  6146. addr_3 = [self textAtColumn:4 statement:stmt];
  6147. addr_4 = [self textAtColumn:5 statement:stmt];
  6148. zipcode = [self textAtColumn:6 statement:stmt];
  6149. state = [self textAtColumn:7 statement:stmt]; // state code
  6150. city = [self textAtColumn:8 statement:stmt];
  6151. firt_name = [self textAtColumn:9 statement:stmt];
  6152. last_name = [self textAtColumn:10 statement:stmt];
  6153. phone = [self textAtColumn:11 statement:stmt];
  6154. fax = [self textAtColumn:12 statement:stmt];
  6155. email = [self textAtColumn:13 statement:stmt];
  6156. notes = [self textAtColumn:14 statement:stmt];
  6157. price_type = [self textAtColumn:15 statement:stmt]; // name
  6158. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6159. img_0 = [self textAtColumn:17 statement:stmt];
  6160. img_1 = [self textAtColumn:18 statement:stmt];
  6161. img_2 = [self textAtColumn:19 statement:stmt];
  6162. contact_type = [self textAtColumn:20 statement:stmt];
  6163. carrierCode = sqlite3_column_int(stmt, 21);
  6164. }];
  6165. // decrypt
  6166. if (company_name) {
  6167. company_name = [AESCrypt fastdecrypt:company_name];
  6168. }
  6169. if (addr_1) {
  6170. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6171. }
  6172. if (phone) {
  6173. phone = [AESCrypt fastdecrypt:phone];
  6174. }
  6175. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6176. countryCode = [iSalesDB jk_queryText:countrySql];
  6177. stateCode = state;
  6178. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6179. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6180. NSString *code_id = params[@"country"];
  6181. countryCode_id = code_id;
  6182. countryCode = [self countryCodeByid:code_id];
  6183. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6184. NSString *zip_code = params[@"zipcode"];
  6185. // 剔除全部为空格
  6186. int spaceCount = 0;
  6187. for (int i = 0; i < zip_code.length; i++) {
  6188. if ([zip_code characterAtIndex:i] == ' ') {
  6189. spaceCount++;
  6190. }
  6191. }
  6192. if (spaceCount == zip_code.length) {
  6193. zip_code = @"";
  6194. }
  6195. if (zipcode.length > 0) {
  6196. zipcode = zip_code;
  6197. countryCode_id = params[@"country"];
  6198. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6199. countryCode = [dic valueForKey:@"country_code"];
  6200. if (!countryCode) {
  6201. NSString *code_id = params[@"country"];
  6202. countryCode = [self countryCodeByid:code_id];
  6203. }
  6204. stateCode = [dic valueForKey:@"state_code"];
  6205. if (!stateCode.length) {
  6206. stateCode = params[@"state"];
  6207. }
  6208. city = [dic valueForKey:@"city"];
  6209. if (!city.length) {
  6210. city = params[@"city"];
  6211. }
  6212. // zip code
  6213. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6214. [zipDic setValue:zipcode forKey:@"value"];
  6215. [section_0 setValue:zipDic forKey:@"item_8"];
  6216. } else {
  6217. NSString *code_id = params[@"country"];
  6218. countryCode = [self countryCodeByid:code_id];
  6219. stateCode = params[@"state"];
  6220. city = params[@"city"];
  6221. }
  6222. }
  6223. }
  6224. // 0 Country
  6225. // 1 Company Name
  6226. // 2 Contact ID
  6227. // 3 Picture
  6228. // 4 Address 1
  6229. // 5 Address 2
  6230. // 6 Address 3
  6231. // 7 Address 4
  6232. // 8 Zip Code
  6233. // 9 State/Province
  6234. // 10 City
  6235. // 11 Contact First Name
  6236. // 12 Contact Last Name
  6237. // 13 Phone
  6238. // 14 Fax
  6239. // 15 Email
  6240. // 16 Contact Notes
  6241. // 17 Price Type
  6242. // 18 Contact Type
  6243. // 19 Sales Rep
  6244. // country
  6245. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6246. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6247. // company
  6248. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6249. // contact_id
  6250. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6251. // picture
  6252. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6253. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6254. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6255. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6256. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6257. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6258. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6259. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6260. // addr 1 2 3 4
  6261. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6262. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6263. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6264. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6265. // zip code
  6266. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6267. // state
  6268. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6269. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6270. // city
  6271. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6272. // first last
  6273. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6274. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6275. // phone fax email
  6276. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6277. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6278. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6279. // notes
  6280. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6281. // price
  6282. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6283. for (NSString *key in priceDic.allKeys) {
  6284. if ([key containsString:@"val_"]) {
  6285. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6286. if ([dic[@"value"] isEqualToString:price_type]) {
  6287. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6288. [priceDic setValue:dic forKey:key];
  6289. }
  6290. }
  6291. }
  6292. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6293. // Contact Rep
  6294. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6295. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6296. // Sales Rep
  6297. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6298. for (NSString *key in repDic.allKeys) {
  6299. if ([key containsString:@"val_"]) {
  6300. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6301. NSString *value = dic[@"value"];
  6302. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6303. if (code && [code isEqualToString:sales_rep]) {
  6304. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6305. [repDic setValue:dic forKey:key];
  6306. }
  6307. }
  6308. }
  6309. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6310. #ifdef BUILD_CONTRAST
  6311. //carrier
  6312. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6313. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6314. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6315. #endif
  6316. [ret setValue:section_0 forKey:@"section_0"];
  6317. return [RAConvertor dict2data:ret];
  6318. }
  6319. #pragma mark save contact
  6320. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6321. {
  6322. return [self offline_saveContact:params update:YES isCustomer:YES];
  6323. }
  6324. #pragma mark category
  6325. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6326. if (ck) {
  6327. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6328. for (NSString *key in res.allKeys) {
  6329. if (![key isEqualToString:@"count"]) {
  6330. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6331. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6332. if ([val[@"value"] isEqualToString:ck]) {
  6333. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6334. }
  6335. [res setValue:val forKey:key];
  6336. }
  6337. }
  6338. [dic setValue:res forKey:valueKey];
  6339. }
  6340. }
  6341. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6342. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6343. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6344. NSString* category = [params valueForKey:@"category"];
  6345. if (!category || [category isEqualToString:@""]) {
  6346. category = @"%";
  6347. }
  6348. category = [self translateSingleQuote:category];
  6349. int limit = [[params valueForKey:@"limit"] intValue];
  6350. int offset = [[params valueForKey:@"offset"] intValue];
  6351. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6352. NSString *limit_str = @"";
  6353. if (limited) {
  6354. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6355. }
  6356. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6357. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6358. sqlite3 *db = [iSalesDB get_db];
  6359. // [iSalesDB AddExFunction:db];
  6360. int count;
  6361. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6362. 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];
  6363. double price_min = 0;
  6364. double price_max = 0;
  6365. if ([params.allKeys containsObject:@"alert"]) {
  6366. // alert
  6367. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6368. NSString *alert = params[@"alert"];
  6369. if ([alert isEqualToString:@"Display All"]) {
  6370. alert = [NSString stringWithFormat:@""];
  6371. } else {
  6372. alert = [self translateSingleQuote:alert];
  6373. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6374. }
  6375. // available
  6376. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6377. NSString *available = params[@"available"];
  6378. NSString *available_condition;
  6379. if ([available isEqualToString:@"Display All"]) {
  6380. available_condition = @"";
  6381. } else if ([available isEqualToString:@"Available Now"]) {
  6382. available_condition = @"and availability > 0";
  6383. } else {
  6384. available_condition = @"and availability == 0";
  6385. }
  6386. // best seller
  6387. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6388. NSString *best_seller = @"";
  6389. NSString *order_best_seller = @"m.name asc";
  6390. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6391. best_seller = @"and best_seller > 0";
  6392. order_best_seller = @"m.best_seller desc,m.name asc";
  6393. }
  6394. // price
  6395. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6396. NSString *price = params[@"price"];
  6397. price_min = 0;
  6398. price_max = MAXFLOAT;
  6399. if (params[@"user"] && price != nil) {
  6400. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6401. NSMutableString *priceName = [NSMutableString string];
  6402. for (int i = 0; i < priceTypeArray.count; i++) {
  6403. NSString *pricetype = priceTypeArray[i];
  6404. pricetype = [self translateSingleQuote:pricetype];
  6405. if (i == 0) {
  6406. [priceName appendFormat:@"'%@'",pricetype];
  6407. } else {
  6408. [priceName appendFormat:@",'%@'",pricetype];
  6409. }
  6410. }
  6411. if ([price isEqualToString:@"Display All"]) {
  6412. price = [NSString stringWithFormat:@""];
  6413. } else if([price containsString:@"+"]){
  6414. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6415. price_min = [price doubleValue];
  6416. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6417. } else {
  6418. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6419. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6420. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6421. 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];
  6422. }
  6423. } else {
  6424. price = @"";
  6425. }
  6426. // sold_by_qty : Sold in quantities of %@
  6427. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6428. NSString *qty = params[@"sold_by_qty"];
  6429. if ([qty isEqualToString:@"Display All"]) {
  6430. qty = @"";
  6431. } else {
  6432. qty = [self translateSingleQuote:qty];
  6433. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6434. }
  6435. // model name;
  6436. NSString* modelname =params[@"modelName"];
  6437. if(modelname.length==0)
  6438. {
  6439. modelname=@"";
  6440. }
  6441. else
  6442. {
  6443. modelname = modelname.lowercaseString;
  6444. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6445. }
  6446. //modelDescription
  6447. NSString* modelDescription =params[@"modelDescription"];
  6448. if(modelDescription.length==0)
  6449. {
  6450. modelDescription=@"";
  6451. }
  6452. else
  6453. {
  6454. modelDescription = modelDescription.lowercaseString;
  6455. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6456. }
  6457. // cate
  6458. // category = [self translateSingleQuote:category];
  6459. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6460. // cate mutiple selection
  6461. NSString *category_id = params[@"category"];
  6462. NSMutableArray *cate_id_array = nil;
  6463. NSMutableString *cateWhere = [NSMutableString string];
  6464. if ([category_id isEqualToString:@""] || !category_id) {
  6465. [cateWhere appendString:@"1 = 1"];
  6466. } else {
  6467. if ([category_id containsString:@","]) {
  6468. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6469. } else {
  6470. cate_id_array = [@[category_id] mutableCopy];
  6471. }
  6472. [cateWhere appendString:@"("];
  6473. for (int i = 0; i < cate_id_array.count; i++) {
  6474. if (i == 0) {
  6475. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6476. } else {
  6477. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6478. }
  6479. }
  6480. [cateWhere appendString:@")"];
  6481. }
  6482. // where bestseller > 0 order by bestseller desc
  6483. // sql query: alert availability(int) best_seller(int) price qty
  6484. 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];
  6485. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6486. }
  6487. DebugLog(@"offline category where: %@",where);
  6488. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6489. if (!params[@"user"]) {
  6490. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6491. }
  6492. [ret setValue:filter forKey:@"filter"];
  6493. DebugLog(@"offline_category sql:%@",sqlQuery);
  6494. sqlite3_stmt * statement;
  6495. [ret setValue:@"2" forKey:@"result"];
  6496. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6497. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6498. // int count=0;
  6499. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6500. {
  6501. int i=0;
  6502. while (sqlite3_step(statement) == SQLITE_ROW)
  6503. {
  6504. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6505. char *name = (char*)sqlite3_column_text(statement, 0);
  6506. if(name==nil)
  6507. name="";
  6508. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6509. char *description = (char*)sqlite3_column_text(statement, 1);
  6510. if(description==nil)
  6511. description="";
  6512. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6513. int product_id = sqlite3_column_int(statement, 2);
  6514. int wid = sqlite3_column_int(statement, 3);
  6515. int closeout = sqlite3_column_int(statement, 4);
  6516. int cid = sqlite3_column_int(statement, 5);
  6517. int wisdelete = sqlite3_column_int(statement, 6);
  6518. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6519. int more_color = sqlite3_column_int(statement, 8);
  6520. // Defaul Category ID
  6521. __block NSString *categoryID = nil;
  6522. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6523. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6524. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6525. if(default_category==nil)
  6526. default_category="";
  6527. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6528. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6529. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6530. categoryID = nsdefault_category;
  6531. }];
  6532. if (!categoryID.length) {
  6533. NSString *cateIDs = params[@"category"];
  6534. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6535. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6536. for (NSString *cateID in requestCategoryArr) {
  6537. BOOL needBreak = NO;
  6538. for (NSString *itemCateIDBox in itemCategoryArr) {
  6539. if (itemCateIDBox.length > 4) {
  6540. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6541. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6542. if ([itemCategoryID isEqualToString:cateID]) {
  6543. needBreak = YES;
  6544. categoryID = itemCategoryID;
  6545. break;
  6546. }
  6547. }
  6548. }
  6549. if (needBreak) {
  6550. break;
  6551. }
  6552. }
  6553. }
  6554. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6555. if(wid !=0 && wisdelete != 1)
  6556. [item setValue:@"true" forKey:@"wish_exists"];
  6557. else
  6558. [item setValue:@"false" forKey:@"wish_exists"];
  6559. if(closeout==0)
  6560. [item setValue:@"false" forKey:@"is_closeout"];
  6561. else
  6562. [item setValue:@"true" forKey:@"is_closeout"];
  6563. if(cid==0)
  6564. [item setValue:@"false" forKey:@"cart_exists"];
  6565. else
  6566. [item setValue:@"true" forKey:@"cart_exists"];
  6567. if (more_color == 0) {
  6568. [item setObject:@(false) forKey:@"more_color"];
  6569. } else if (more_color == 1) {
  6570. [item setObject:@(true) forKey:@"more_color"];
  6571. }
  6572. [item addEntriesFromDictionary:imgjson];
  6573. // [item setValue:nsurl forKey:@"img"];
  6574. [item setValue:nsname forKey:@"name"];
  6575. [item setValue:nsdescription forKey:@"description"];
  6576. if (categoryID) {
  6577. [item setValue:categoryID forKey:@"item_category_id"];
  6578. }
  6579. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6580. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6581. i++;
  6582. }
  6583. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6584. [ret setObject:items forKey:@"items"];
  6585. sqlite3_finalize(statement);
  6586. } else {
  6587. DebugLog(@"nothing...");
  6588. }
  6589. DebugLog(@"count:%d",count);
  6590. [iSalesDB close_db:db];
  6591. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6592. return ret;
  6593. }
  6594. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6595. {
  6596. return [self categoryList:params limited:YES];
  6597. }
  6598. # pragma mark item search
  6599. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6600. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6601. // assert(params[@"order_code"]);
  6602. // params[@"user"]
  6603. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6604. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6605. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6606. // category
  6607. NSDictionary *category_menu = [self offline_category_menu];
  6608. [filter setValue:category_menu forKey:@"category"];
  6609. NSString* where= nil;
  6610. NSString* orderby= @"m.name";
  6611. if (!filterSearch) {
  6612. int covertype = [[params valueForKey:@"covertype"] intValue];
  6613. switch (covertype) {
  6614. case 0:
  6615. {
  6616. where=@"m.category like'%%#005#%%'";
  6617. break;
  6618. }
  6619. case 1:
  6620. {
  6621. where=@"m.alert like '%QS%'";
  6622. break;
  6623. }
  6624. case 2:
  6625. {
  6626. where=@"m.availability>0";
  6627. break;
  6628. }
  6629. case 3:
  6630. {
  6631. where=@"m.best_seller>0";
  6632. orderby=@"m.best_seller desc,m.name asc";
  6633. break;
  6634. }
  6635. default:
  6636. where=@"1=1";
  6637. break;
  6638. }
  6639. }
  6640. int limit = [[params valueForKey:@"limit"] intValue];
  6641. int offset = [[params valueForKey:@"offset"] intValue];
  6642. NSString *limit_str = @"";
  6643. if (limited) {
  6644. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6645. }
  6646. sqlite3 *db = [iSalesDB get_db];
  6647. // [iSalesDB AddExFunction:db];
  6648. int count;
  6649. NSString *sqlQuery = nil;
  6650. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6651. 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];
  6652. double price_min = 0;
  6653. double price_max = 0;
  6654. if (filterSearch) {
  6655. // alert
  6656. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6657. NSString *alert = params[@"alert"];
  6658. if ([alert isEqualToString:@"Display All"]) {
  6659. alert = [NSString stringWithFormat:@""];
  6660. } else {
  6661. alert = [self translateSingleQuote:alert];
  6662. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6663. }
  6664. // available
  6665. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6666. NSString *available = params[@"available"];
  6667. NSString *available_condition;
  6668. if ([available isEqualToString:@"Display All"]) {
  6669. available_condition = @"";
  6670. } else if ([available isEqualToString:@"Available Now"]) {
  6671. available_condition = @"and availability > 0";
  6672. } else {
  6673. available_condition = @"and availability == 0";
  6674. }
  6675. // best seller
  6676. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6677. NSString *best_seller = @"";
  6678. NSString *order_best_seller = @"m.name asc";
  6679. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6680. best_seller = @"and best_seller > 0";
  6681. order_best_seller = @"m.best_seller desc,m.name asc";
  6682. }
  6683. // price
  6684. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6685. NSString *price = params[@"price"];
  6686. price_min = 0;
  6687. price_max = MAXFLOAT;
  6688. if (params[@"user"]) {
  6689. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6690. NSMutableString *priceName = [NSMutableString string];
  6691. for (int i = 0; i < priceTypeArray.count; i++) {
  6692. NSString *pricetype = priceTypeArray[i];
  6693. pricetype = [self translateSingleQuote:pricetype];
  6694. if (i == 0) {
  6695. [priceName appendFormat:@"'%@'",pricetype];
  6696. } else {
  6697. [priceName appendFormat:@",'%@'",pricetype];
  6698. }
  6699. }
  6700. if ([price isEqualToString:@"Display All"]) {
  6701. price = [NSString stringWithFormat:@""];
  6702. } else if([price containsString:@"+"]){
  6703. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6704. price_min = [price doubleValue];
  6705. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6706. } else {
  6707. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6708. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6709. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6710. 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];
  6711. }
  6712. } else {
  6713. price = @"";
  6714. }
  6715. // sold_by_qty : Sold in quantities of %@
  6716. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6717. NSString *qty = params[@"sold_by_qty"];
  6718. if ([qty isEqualToString:@"Display All"]) {
  6719. qty = @"";
  6720. } else {
  6721. qty = [self translateSingleQuote:qty];
  6722. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6723. }
  6724. // model name;
  6725. NSString* modelname =params[@"modelName"];
  6726. if(modelname.length==0)
  6727. {
  6728. modelname=@"";
  6729. }
  6730. else
  6731. {
  6732. modelname = modelname.lowercaseString;
  6733. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6734. }
  6735. //modelDescription
  6736. NSString* modelDescription =params[@"modelDescription"];
  6737. if(modelDescription.length==0)
  6738. {
  6739. modelDescription=@"";
  6740. }
  6741. else
  6742. {
  6743. modelDescription = modelDescription.lowercaseString;
  6744. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6745. }
  6746. //lower(description) like'%%%@%%'
  6747. // category
  6748. NSString *category_id = params[@"ctgId"];
  6749. NSMutableArray *cate_id_array = nil;
  6750. NSMutableString *cateWhere = [NSMutableString string];
  6751. if ([category_id isEqualToString:@""] || !category_id) {
  6752. [cateWhere appendString:@"1 = 1"];
  6753. } else {
  6754. if ([category_id containsString:@","]) {
  6755. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6756. } else {
  6757. cate_id_array = [@[category_id] mutableCopy];
  6758. }
  6759. /*-----------*/
  6760. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6761. [cateWhere appendString:@"("];
  6762. for (int i = 0; i < cate_id_array.count; i++) {
  6763. for (NSString *key0 in cateDic.allKeys) {
  6764. if ([key0 containsString:@"category_"]) {
  6765. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6766. for (NSString *key1 in category0.allKeys) {
  6767. if ([key1 containsString:@"category_"]) {
  6768. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6769. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6770. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6771. cate_id_array[i] = [category1 objectForKey:@"id"];
  6772. if (i == 0) {
  6773. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6774. } else {
  6775. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6776. }
  6777. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6778. [category0 setValue:category1 forKey:key1];
  6779. [cateDic setValue:category0 forKey:key0];
  6780. }
  6781. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6782. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6783. cate_id_array[i] = [category0 objectForKey:@"id"];
  6784. if (i == 0) {
  6785. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6786. } else {
  6787. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6788. }
  6789. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6790. [cateDic setValue:category0 forKey:key0];
  6791. }
  6792. }
  6793. }
  6794. }
  6795. }
  6796. }
  6797. [cateWhere appendString:@")"];
  6798. [filter setValue:cateDic forKey:@"category"];
  6799. }
  6800. // where bestseller > 0 order by bestseller desc
  6801. // sql query: alert availability(int) best_seller(int) price qty
  6802. 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];
  6803. // count
  6804. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6805. }
  6806. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6807. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6808. if (!params[@"user"]) {
  6809. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6810. }
  6811. [ret setValue:filter forKey:@"filter"];
  6812. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6813. sqlite3_stmt * statement;
  6814. [ret setValue:@"2" forKey:@"result"];
  6815. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6816. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6817. // int count=0;
  6818. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6819. {
  6820. int i=0;
  6821. while (sqlite3_step(statement) == SQLITE_ROW)
  6822. {
  6823. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6824. char *name = (char*)sqlite3_column_text(statement, 0);
  6825. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6826. char *description = (char*)sqlite3_column_text(statement, 1);
  6827. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6828. int product_id = sqlite3_column_int(statement, 2);
  6829. int wid = sqlite3_column_int(statement, 3);
  6830. int closeout = sqlite3_column_int(statement, 4);
  6831. int cid = sqlite3_column_int(statement, 5);
  6832. int wisdelete = sqlite3_column_int(statement, 6);
  6833. int more_color = sqlite3_column_int(statement, 7);
  6834. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6835. if(wid !=0 && wisdelete != 1)
  6836. [item setValue:@"true" forKey:@"wish_exists"];
  6837. else
  6838. [item setValue:@"false" forKey:@"wish_exists"];
  6839. if(closeout==0)
  6840. [item setValue:@"false" forKey:@"is_closeout"];
  6841. else
  6842. [item setValue:@"true" forKey:@"is_closeout"];
  6843. if(cid==0)
  6844. [item setValue:@"false" forKey:@"cart_exists"];
  6845. else
  6846. [item setValue:@"true" forKey:@"cart_exists"];
  6847. if (more_color == 0) {
  6848. [item setObject:@(false) forKey:@"more_color"];
  6849. } else if (more_color == 1) {
  6850. [item setObject:@(true) forKey:@"more_color"];
  6851. }
  6852. [item addEntriesFromDictionary:imgjson];
  6853. // [item setValue:nsurl forKey:@"img"];
  6854. [item setValue:nsname forKey:@"fash_name"];
  6855. [item setValue:nsdescription forKey:@"description"];
  6856. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6857. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6858. i++;
  6859. }
  6860. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6861. [ret setObject:items forKey:@"items"];
  6862. sqlite3_finalize(statement);
  6863. }
  6864. [iSalesDB close_db:db];
  6865. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6866. return ret;
  6867. }
  6868. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6869. {
  6870. return [self itemsearch:params limited:YES];
  6871. }
  6872. #pragma mark order detail
  6873. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6874. if (str1.length == 0) {
  6875. str1 = @"&nbsp";
  6876. }
  6877. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6878. return str;
  6879. }
  6880. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6881. {
  6882. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6883. [fromformatter setDateFormat:from];
  6884. NSDate *date = [fromformatter dateFromString:datetime];
  6885. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6886. [toformatter setDateFormat:to];
  6887. NSString * ret = [toformatter stringFromDate:date];
  6888. return ret;
  6889. }
  6890. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6891. // 把毫秒去掉
  6892. if ([dateTime containsString:@"."]) {
  6893. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6894. }
  6895. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6896. formatter.dateFormat = formate;
  6897. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6898. NSDate *date = [formatter dateFromString:dateTime];
  6899. formatter.dateFormat = newFormate;
  6900. NSString *result = [formatter stringFromDate:date];
  6901. return result ? result : @"";
  6902. }
  6903. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6904. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6905. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6906. }
  6907. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6908. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6909. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6910. }
  6911. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6912. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6913. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6914. }
  6915. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6916. {
  6917. assert(params[@"mode"]!=nil);
  6918. assert(params[@"user"]!=nil);
  6919. DebugLog(@"offline oderdetail params: %@",params);
  6920. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6921. int orderId = [params[@"orderId"] intValue];
  6922. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6923. // decrypt card number and card security code
  6924. // 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 ];
  6925. 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,discount 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,discount from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  6926. sqlite3 *db = [iSalesDB get_db];
  6927. sqlite3_stmt * statement;
  6928. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6929. NSString *nssoid = @"";
  6930. NSString* orderinfo = @"";
  6931. NSString *moreInfo = @"";
  6932. double handlingFee = 0;
  6933. double payments_and_credist = 0;
  6934. double totalPrice = 0;
  6935. double shippingFee = 0;
  6936. double lift_gate = 0;
  6937. NSString *customer_contact = @"";
  6938. NSString *customer_email = @"";
  6939. NSString *customer_fax = @"";
  6940. NSString *customer_phone = @"";
  6941. NSString *customer_city = @"";
  6942. NSString *customer_state = @"";
  6943. NSString *customer_zipcode = @"";
  6944. NSString *customer_country = @"";
  6945. BOOL must_call = NO;
  6946. double discount = 0;
  6947. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6948. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6949. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6950. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6951. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6952. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6953. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6954. {
  6955. if (sqlite3_step(statement) == SQLITE_ROW)
  6956. {
  6957. // int order_id = sqlite3_column_int(statement, 0);
  6958. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6959. // ret[@"sign_url"] = sign_url;
  6960. section_1 = @{
  6961. @"data":sign_url,
  6962. @"title":@"Signature",
  6963. @"type":@"sign_url"
  6964. }.mutableCopy;
  6965. [ret setObject:section_1 forKey:@"section_1"];
  6966. customer_contact = [self textAtColumn:52 statement:statement];
  6967. customer_email = [self textAtColumn:53 statement:statement];
  6968. customer_phone = [self textAtColumn:54 statement:statement];
  6969. customer_fax = [self textAtColumn:55 statement:statement];
  6970. customer_city = [self textAtColumn:60 statement:statement];
  6971. customer_state = [self textAtColumn:61 statement:statement];
  6972. customer_zipcode = [self textAtColumn:62 statement:statement];
  6973. customer_country = [self textAtColumn:63 statement:statement];
  6974. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6975. discount =sqlite3_column_double(statement, 65);
  6976. int offline_edit=sqlite3_column_int(statement, 56);
  6977. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6978. char *soid = (char*)sqlite3_column_text(statement, 1);
  6979. if(soid==nil)
  6980. soid= "";
  6981. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6982. // so#
  6983. ret[@"so#"] = nssoid;
  6984. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6985. if(poNumber==nil)
  6986. poNumber= "";
  6987. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6988. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6989. if(create_time==nil)
  6990. create_time= "";
  6991. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6992. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6993. int status = sqlite3_column_int(statement, 4);
  6994. int erpStatus = sqlite3_column_int(statement, 49);
  6995. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6996. // status
  6997. if (status > 1 && status != 3) {
  6998. status = erpStatus;
  6999. } else if (status == 3) {
  7000. status = 15;
  7001. }
  7002. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  7003. ret[@"order_status"] = nsstatus;
  7004. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7005. if(company_name==nil)
  7006. company_name= "";
  7007. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7008. // company name
  7009. ret[@"company_name"] = nscompany_name;
  7010. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  7011. if(customer_contact==nil)
  7012. customer_contact= "";
  7013. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  7014. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  7015. if(addr_1==nil)
  7016. addr_1="";
  7017. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  7018. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  7019. if(addr_2==nil)
  7020. addr_2="";
  7021. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  7022. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  7023. if(addr_3==nil)
  7024. addr_3="";
  7025. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  7026. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  7027. if(addr_4==nil)
  7028. addr_4="";
  7029. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  7030. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  7031. [arr_addr addObject:nsaddr_1];
  7032. [arr_addr addObject:nsaddr_2];
  7033. [arr_addr addObject:nsaddr_3];
  7034. [arr_addr addObject:nsaddr_4];
  7035. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  7036. [arr_addr addObject:customer_state];
  7037. [arr_addr addObject:customer_zipcode];
  7038. [arr_addr addObject:customer_country];
  7039. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  7040. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  7041. char *logist = (char*)sqlite3_column_text(statement, 11);
  7042. if(logist==nil)
  7043. logist= "";
  7044. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  7045. if (/*status == -11 || */status == 10 || status == 11) {
  7046. nslogist = [self textAtColumn:59 statement:statement];
  7047. };
  7048. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  7049. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  7050. shipping = @"Shipping To Be Quoted";
  7051. } else {
  7052. shippingFee = sqlite3_column_double(statement, 12);
  7053. }
  7054. // Shipping
  7055. // ret[@"Shipping"] = shipping;
  7056. NSDictionary *shipping_item = @{
  7057. @"title":@"Shipping",
  7058. @"value":shipping
  7059. };
  7060. [price_data setObject:shipping_item forKey:@"item_1"];
  7061. int have_lift_gate = sqlite3_column_int(statement, 17);
  7062. lift_gate = sqlite3_column_double(statement, 13);
  7063. // Liftgate Fee(No loading dock)
  7064. if (!have_lift_gate) {
  7065. lift_gate = 0;
  7066. }
  7067. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7068. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  7069. if (sqlite3_column_int(statement, 57)) {
  7070. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  7071. liftgate_value = @"Shipping To Be Quoted";
  7072. }
  7073. NSDictionary *liftgate_item = @{
  7074. @"title":@"Liftgate Fee(No loading dock)",
  7075. @"value":liftgate_value
  7076. };
  7077. [price_data setObject:liftgate_item forKey:@"item_2"];
  7078. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  7079. // [ret removeObjectForKey:@"Shipping"];
  7080. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7081. // }
  7082. //
  7083. // if (have_lift_gate) {
  7084. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  7085. // }
  7086. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  7087. if(general_notes==nil)
  7088. general_notes= "";
  7089. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  7090. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  7091. if(internal_notes==nil)
  7092. internal_notes= "";
  7093. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  7094. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  7095. if(payment_type==nil)
  7096. payment_type= "";
  7097. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  7098. // order info
  7099. orderinfo = [self textFileName:@"order_info.html"];
  7100. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  7101. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  7102. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  7103. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  7104. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  7105. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  7106. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  7107. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  7108. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  7109. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  7110. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  7111. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  7112. NSString *payment = nil;
  7113. // id -> show
  7114. __block NSString *show_pay_type = nspayment_type;
  7115. [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) {
  7116. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  7117. if (show_typ_ch != NULL) {
  7118. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  7119. }
  7120. }];
  7121. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  7122. {
  7123. payment = [self textFileName:@"creditcardpayment.html"];
  7124. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  7125. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  7126. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  7127. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  7128. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  7129. NSString *card_type = [self textAtColumn:42 statement:statement];
  7130. if (card_type.length > 0) { // 显示星号
  7131. card_type = @"****";
  7132. }
  7133. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7134. if (card_number.length > 0 && card_number.length > 4) {
  7135. for (int i = 0; i < card_number.length - 4; i++) {
  7136. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7137. }
  7138. } else {
  7139. card_number = @"";
  7140. }
  7141. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7142. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7143. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7144. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7145. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7146. card_expiration = @"****";
  7147. }
  7148. NSString *card_city = [self textAtColumn:46 statement:statement];
  7149. NSString *card_state = [self textAtColumn:47 statement:statement];
  7150. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7151. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7152. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7153. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7154. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7155. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7156. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7157. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7158. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7159. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7160. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7161. }
  7162. else
  7163. {
  7164. payment=[self textFileName:@"normalpayment.html"];
  7165. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7166. }
  7167. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7168. ret[@"result"]= [NSNumber numberWithInt:2];
  7169. // more info
  7170. moreInfo = [self textFileName:@"more_info.html"];
  7171. /*****ship to******/
  7172. // ShipToCompany_or_&nbsp
  7173. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7174. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7175. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7176. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7177. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7178. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7179. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7180. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7181. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7182. /*****ship from******/
  7183. // ShipFromCompany_or_&nbsp
  7184. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7185. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7186. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7187. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7188. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7189. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7190. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7191. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7192. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7193. /*****freight to******/
  7194. // FreightBillToCompany_or_&nbsp
  7195. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7196. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7197. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7198. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7199. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7200. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7201. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7202. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7203. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7204. /*****merchandise to******/
  7205. // MerchandiseBillToCompany_or_&nbsp
  7206. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7207. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7208. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7209. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7210. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7211. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7212. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7213. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7214. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7215. /*****return to******/
  7216. // ReturnToCompany_or_&nbsp
  7217. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7218. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7219. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7220. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7221. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7222. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7223. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7224. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7225. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7226. //
  7227. // DebugLog(@"more info : %@",moreInfo);
  7228. // handling fee
  7229. handlingFee = sqlite3_column_double(statement, 33);
  7230. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7231. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7232. if (sqlite3_column_int(statement, 58)) {
  7233. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7234. handling_fee_value = @"Shipping To Be Quoted";
  7235. }
  7236. NSDictionary *handling_fee_item = @{
  7237. @"title":@"Handling Fee",
  7238. @"value":handling_fee_value
  7239. };
  7240. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7241. //
  7242. // customer info
  7243. customerID = [self textAtColumn:36 statement:statement];
  7244. // mode
  7245. ret[@"mode"] = params[@"mode"];
  7246. // model_count
  7247. ret[@"model_count"] = @(0);
  7248. }
  7249. sqlite3_finalize(statement);
  7250. }
  7251. [iSalesDB close_db:db];
  7252. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7253. // "customer_email" = "Shui Hu";
  7254. // "customer_fax" = "";
  7255. // "customer_first_name" = F;
  7256. // "customer_last_name" = L;
  7257. // "customer_name" = ",da He Xiang Dong Liu A";
  7258. // "customer_phone" = "Hey Xuan Feng";
  7259. contactInfo[@"customer_phone"] = customer_phone;
  7260. contactInfo[@"customer_fax"] = customer_fax;
  7261. contactInfo[@"customer_email"] = customer_email;
  7262. NSString *first_name = @"";
  7263. NSString *last_name = @"";
  7264. if ([customer_contact isEqualToString:@""]) {
  7265. } else if ([customer_contact containsString:@" "]) {
  7266. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7267. first_name = [customer_contact substringToIndex:first_space_index];
  7268. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7269. }
  7270. contactInfo[@"customer_first_name"] = first_name;
  7271. contactInfo[@"customer_last_name"] = last_name;
  7272. ret[@"customerInfo"] = contactInfo;
  7273. // models
  7274. if (nssoid) {
  7275. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7276. __block double TotalCuft = 0;
  7277. __block double TotalWeight = 0;
  7278. __block int TotalCarton = 0;
  7279. __block double allItemPrice = 0;
  7280. 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];
  7281. sqlite3 *db1 = [iSalesDB get_db];
  7282. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7283. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7284. int product_id = sqlite3_column_int(stmt, 0);
  7285. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7286. int item_id = sqlite3_column_int(stmt, 7);
  7287. NSString* Price=nil;
  7288. if(str_price==nil)
  7289. {
  7290. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7291. if(price==nil)
  7292. Price=@"No Price.";
  7293. else
  7294. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7295. }
  7296. else
  7297. {
  7298. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7299. }
  7300. double discount = sqlite3_column_double(stmt, 2);
  7301. int item_count = sqlite3_column_int(stmt, 3);
  7302. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7303. NSString *nsline_note=nil;
  7304. if(line_note!=nil)
  7305. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7306. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7307. // NSString *nsname = nil;
  7308. // if(name!=nil)
  7309. // nsname= [[NSString alloc]initWithUTF8String:name];
  7310. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7311. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7312. // NSString *nsdescription=nil;
  7313. // if(description!=nil)
  7314. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7315. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7316. // int stockUom = sqlite3_column_int(stmt, 8);
  7317. // int _id = sqlite3_column_int(stmt, 9);
  7318. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7319. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7320. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7321. int carton=[bsubtotaljson[@"carton"] intValue];
  7322. TotalCuft += cuft;
  7323. TotalWeight += weight;
  7324. TotalCarton += carton;
  7325. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7326. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7327. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7328. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7329. itemjson[@"note"]=nsline_note;
  7330. itemjson[@"origin_price"] = Price;
  7331. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7332. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7333. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7334. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7335. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7336. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7337. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7338. if(itemjson[@"combine"] != nil)
  7339. {
  7340. // int citem=0;
  7341. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7342. for(int bc=0;bc<bcount;bc++)
  7343. {
  7344. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7345. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7346. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7347. subTotal += uprice * modulus * item_count;
  7348. }
  7349. }
  7350. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7351. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7352. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7353. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7354. itemjson[@"type"] = @"order_item";
  7355. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7356. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7357. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7358. allItemPrice += subTotal;
  7359. }];
  7360. section_3[@"data"] = model_data;
  7361. section_3[@"type"] = @"sub_order";
  7362. section_3[@"title"] = @"Models";
  7363. section_3[@"switch"] = @(false);
  7364. ret[@"section_3"] = section_3;
  7365. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7366. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7367. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7368. // payments/Credits
  7369. // payments_and_credist = sqlite3_column_double(statement, 34);
  7370. payments_and_credist = allItemPrice;
  7371. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7372. NSDictionary *sub_total_item = @{
  7373. @"title":@"Sub-Total",
  7374. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7375. };
  7376. [price_data setObject:sub_total_item forKey:@"item_0"];
  7377. // // total
  7378. // totalPrice = sqlite3_column_double(statement, 35);
  7379. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7380. } else {
  7381. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7382. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7383. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7384. // payments/Credits
  7385. payments_and_credist = 0;
  7386. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7387. NSDictionary *sub_total_item = @{
  7388. @"title":@"Sub-Total",
  7389. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7390. };
  7391. [price_data setObject:sub_total_item forKey:@"item_0"];
  7392. }
  7393. // total
  7394. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7395. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7396. if(discount>0)
  7397. {
  7398. NSDictionary *total_item = @{
  7399. @"title":@"Total",
  7400. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice*(1-(discount/100.0))]
  7401. };
  7402. NSDictionary *discount_item = @{
  7403. @"title":@"Order Discount",
  7404. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7405. };
  7406. [price_data setObject:discount_item forKey:@"item_4"];
  7407. [price_data setObject:total_item forKey:@"item_5"];
  7408. }
  7409. else
  7410. {
  7411. NSDictionary *total_item = @{
  7412. @"title":@"Total",
  7413. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7414. };
  7415. [price_data setObject:total_item forKey:@"item_4"];
  7416. }
  7417. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7418. section_4 = @{
  7419. @"data":price_data,
  7420. @"title":@"Price Info",
  7421. @"type":@"price_info"
  7422. }.mutableCopy;
  7423. ret[@"section_4"] = section_4;
  7424. // ret[@"order_info"]= orderinfo;
  7425. section_0 = @{
  7426. @"data":orderinfo,
  7427. @"title":@"Order Info",
  7428. @"type":@"order_info"
  7429. }.mutableCopy;
  7430. ret[@"section_0"] = section_0;
  7431. // ret[@"more_order_info"] = moreInfo;
  7432. section_2 = @{
  7433. @"data":moreInfo,
  7434. @"title":@"More Info",
  7435. @"type":@"more_order_info"
  7436. }.mutableCopy;
  7437. ret[@"section_2"] = section_2;
  7438. ret[@"count"] = @(5);
  7439. return [RAConvertor dict2data:ret];
  7440. }
  7441. #pragma mark order list
  7442. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7443. NSString *nsstatus = @"";
  7444. switch (status) {
  7445. case 0:
  7446. {
  7447. nsstatus=@"Temp Order";
  7448. break;
  7449. }
  7450. case 1:
  7451. {
  7452. nsstatus=@"Saved Order";
  7453. break;
  7454. }
  7455. case 2: {
  7456. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7457. break;
  7458. }
  7459. case 3:
  7460. case 15:
  7461. {
  7462. nsstatus=@"Cancelled";
  7463. break;
  7464. }
  7465. case 10:
  7466. {
  7467. nsstatus=@"Quote Submitted";
  7468. break;
  7469. }
  7470. case 11:
  7471. {
  7472. nsstatus=@"Sales Order Submitted";
  7473. break;
  7474. }
  7475. case 12:
  7476. {
  7477. nsstatus=@"Processing";
  7478. break;
  7479. }
  7480. case 13:
  7481. {
  7482. nsstatus=@"Shipped";
  7483. break;
  7484. }
  7485. case 14:
  7486. {
  7487. nsstatus=@"Closed";
  7488. break;
  7489. }
  7490. case -11:
  7491. {
  7492. nsstatus = @"Ready For Submit";
  7493. break;
  7494. }
  7495. default:
  7496. break;
  7497. }
  7498. return nsstatus;
  7499. }
  7500. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7501. {
  7502. double total = 0;
  7503. __block double payments_and_credist = 0;
  7504. __block double allItemPrice = 0;
  7505. // sqlite3 *db1 = [iSalesDB get_db];
  7506. if (so_id) {
  7507. // 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];
  7508. 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];
  7509. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7510. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7511. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7512. int product_id = sqlite3_column_int(stmt, 0);
  7513. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7514. int discount = sqlite3_column_int(stmt, 2);
  7515. int item_count = sqlite3_column_int(stmt, 3);
  7516. // int item_id = sqlite3_column_int(stmt, 7);
  7517. int item_id = sqlite3_column_int(stmt, 4);
  7518. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7519. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7520. NSString* Price=nil;
  7521. if(str_price==nil)
  7522. {
  7523. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7524. if(price==nil)
  7525. Price=@"No Price.";
  7526. else
  7527. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7528. }
  7529. else
  7530. {
  7531. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7532. }
  7533. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7534. itemjson[@"The unit price"]=Price;
  7535. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7536. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7537. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7538. if(itemjson[@"combine"] != nil)
  7539. {
  7540. // int citem=0;
  7541. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7542. for(int bc=0;bc<bcount;bc++)
  7543. {
  7544. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7545. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7546. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7547. subTotal += uprice * modulus * item_count;
  7548. }
  7549. }
  7550. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7551. allItemPrice += subTotal;
  7552. }];
  7553. payments_and_credist = allItemPrice;
  7554. } else {
  7555. // payments/Credits
  7556. payments_and_credist = 0;
  7557. }
  7558. // lift_gate handlingFee shippingFee
  7559. __block double lift_gate = 0;
  7560. __block double handlingFee = 0;
  7561. __block double shippingFee = 0;
  7562. __block double order_discount = 0;
  7563. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7564. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7565. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7566. if (have_lift_gate) {
  7567. lift_gate = sqlite3_column_double(stmt, 1);
  7568. }
  7569. handlingFee = sqlite3_column_double(stmt, 2);
  7570. shippingFee = sqlite3_column_double(stmt, 3);
  7571. order_discount = sqlite3_column_double(stmt, 4);
  7572. }];
  7573. if(order_discount>0)
  7574. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7575. // total
  7576. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7577. if (close) {
  7578. [iSalesDB close_db:db1];
  7579. }
  7580. return total;
  7581. }
  7582. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7583. {
  7584. assert(params[@"user"]!=nil);
  7585. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7586. int limit = [[params valueForKey:@"limit"] intValue];
  7587. int offset = [[params valueForKey:@"offset"] intValue];
  7588. NSString* keyword = [params valueForKey:@"keyWord"];
  7589. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7590. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7591. NSString* where=@"1 = 1";
  7592. if(keyword.length>0)
  7593. 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]];
  7594. if (orderStatus.length > 0) {
  7595. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7596. if (order_status_array.count == 1) {
  7597. int status_value = [[order_status_array firstObject] intValue];
  7598. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7599. if (status_value == 15 || status_value == 3) {
  7600. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7601. } else {
  7602. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7603. }
  7604. } else {
  7605. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7606. }
  7607. } else if (order_status_array.count > 1) {
  7608. for (int i = 0; i < order_status_array.count;i++) {
  7609. NSString *status = order_status_array[i];
  7610. NSString *condition = @" or";
  7611. if (i == 0) {
  7612. condition = @" and (";
  7613. }
  7614. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7615. int status_value = [status intValue];
  7616. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7617. if (status_value == 15 || status_value == 3) {
  7618. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7619. } else {
  7620. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7621. }
  7622. } else {
  7623. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7624. }
  7625. }
  7626. where = [where stringByAppendingString:@" )"];
  7627. }
  7628. }
  7629. 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];
  7630. // DebugLog(@"order list sql: %@",sqlQuery);
  7631. sqlite3 *db = [iSalesDB get_db];
  7632. sqlite3_stmt * statement;
  7633. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7634. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7635. {
  7636. int count=0;
  7637. while (sqlite3_step(statement) == SQLITE_ROW)
  7638. {
  7639. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7640. int order_id = sqlite3_column_double(statement, 0);
  7641. char *soid = (char*)sqlite3_column_text(statement, 1);
  7642. if(soid==nil)
  7643. soid= "";
  7644. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7645. int status = sqlite3_column_double(statement, 2);
  7646. int erpStatus = sqlite3_column_double(statement, 9);
  7647. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7648. if(sales_rep==nil)
  7649. sales_rep= "";
  7650. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7651. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7652. if(create_by==nil)
  7653. create_by= "";
  7654. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7655. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7656. if(company_name==nil)
  7657. company_name= "";
  7658. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7659. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7660. if(create_time==nil)
  7661. create_time= "";
  7662. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7663. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7664. // double total_price = sqlite3_column_double(statement, 7);
  7665. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7666. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7667. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7668. int offline_edit = sqlite3_column_int(statement, 10);
  7669. // ": "JH",
  7670. // "": "$8307.00",
  7671. // "": "MOB1608050001",
  7672. // "": "ArpithaT",
  7673. // "": "1st Stage Property Transformations",
  7674. // "": "JANICE SUTTON",
  7675. // "": 2255,
  7676. // "": "08/02/2016 09:49:18",
  7677. // "": 1,
  7678. // "": "Saved Order"
  7679. // "": "1470384050483",
  7680. // "model_count": "6 / 28"
  7681. item[@"sales_rep"]= nssales_rep;
  7682. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7683. item[@"so#"]= nssoid;
  7684. item[@"create_by"]= nscreate_by;
  7685. item[@"customer_name"]= nscompany_name;
  7686. item[@"customer_contact"] = customer_contact;
  7687. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7688. item[@"purchase_time"]= nscreate_time;
  7689. int statusCode = status;
  7690. if (statusCode == 2) {
  7691. statusCode = erpStatus;
  7692. } else if (statusCode == 3) {
  7693. statusCode = 15;
  7694. }
  7695. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7696. item[@"order_status"]= nsstatus;
  7697. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7698. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7699. // item[@"model_count"]
  7700. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7701. count++;
  7702. }
  7703. ret[@"count"]= [NSNumber numberWithInt:count];
  7704. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7705. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7706. ret[@"result"]= [NSNumber numberWithInt:2];
  7707. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7708. ret[@"time_zone"] = @"PST";
  7709. sqlite3_finalize(statement);
  7710. }
  7711. 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];
  7712. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7713. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7714. [iSalesDB close_db:db];
  7715. return [RAConvertor dict2data:ret];
  7716. }
  7717. #pragma mark update gnotes
  7718. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7719. {
  7720. DebugLog(@"params: %@",params);
  7721. // comments = Meyoyoyoyoyoyoy;
  7722. // orderCode = MOB1608110001;
  7723. // password = 123456;
  7724. // user = EvanK;
  7725. 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]];
  7726. int ret = [iSalesDB execSql:sql];
  7727. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7728. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7729. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7730. // [dic setValue:@"160409" forKey:@"min_ver"];
  7731. return [RAConvertor dict2data:dic];
  7732. }
  7733. #pragma mark move to wishlist
  7734. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7735. {
  7736. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7737. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7738. _id = [NSString stringWithFormat:@"(%@)",_id];
  7739. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7740. sqlite3 *db = [iSalesDB get_db];
  7741. __block NSString *product_id = @"";
  7742. __block NSString *item_count_str = @"";
  7743. // __block NSString *item_id = nil;
  7744. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7745. // product_id = [self textAtColumn:0 statement:stmt];
  7746. int item_count = sqlite3_column_int(stmt, 1);
  7747. // item_id = [self textAtColumn:2 statement:stmt];
  7748. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7749. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7750. if (p_id.length) {
  7751. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7752. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7753. }
  7754. }];
  7755. [iSalesDB close_db:db];
  7756. // 去除第一个,
  7757. if (product_id.length > 1) {
  7758. product_id = [product_id substringFromIndex:1];
  7759. }
  7760. if (item_count_str.length > 1) {
  7761. item_count_str = [item_count_str substringFromIndex:1];
  7762. }
  7763. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7764. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7765. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7766. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7767. sqlite3 *db1 = [iSalesDB get_db];
  7768. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7769. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7770. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7771. // 删除
  7772. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7773. int ret = [iSalesDB execSql:deleteSql db:db1];
  7774. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7775. [iSalesDB close_db:db1];
  7776. return [RAConvertor dict2data:dic];
  7777. }
  7778. #pragma mark cart delete
  7779. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7780. {
  7781. // cartItemId = 548;
  7782. // orderCode = MOB1608110001;
  7783. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7784. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7785. _id = [NSString stringWithFormat:@"(%@)",_id];
  7786. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7787. int ret = [iSalesDB execSql:sql];
  7788. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7789. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7790. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7791. // [dic setValue:@"160409" forKey:@"min_ver"];
  7792. return [RAConvertor dict2data:dic];
  7793. }
  7794. #pragma mark set order discount
  7795. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7796. //{
  7797. // DebugLog(@"cart set price params: %@",params);
  7798. // // "cartitem_id" = 1;
  7799. // // discount = "0.000000";
  7800. // // "item_note" = "";
  7801. // // price = "269.000000";
  7802. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7803. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7804. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7805. //// NSString *price = [self valueInParams:params key:@"price"];
  7806. //
  7807. // // bool badd_price_changed=false;
  7808. // // sqlite3* db=[iSalesDB get_db];
  7809. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7810. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7811. // // NSRange range;
  7812. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7813. // //
  7814. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7815. // // badd_price_changed=true;
  7816. // // [iSalesDB close_db:db];
  7817. // //
  7818. // // if(badd_price_changed)
  7819. // // {
  7820. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7821. // // }
  7822. // //
  7823. //
  7824. //
  7825. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7826. //
  7827. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7828. //
  7829. // int ret = [iSalesDB execSql:sql];
  7830. //
  7831. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7832. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7833. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7834. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7835. //
  7836. //// return [RAConvertor dict2data:dic];
  7837. // return dic;
  7838. //
  7839. //}
  7840. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7841. {
  7842. DebugLog(@"order set discount params: %@",params);
  7843. // "cartitem_id" = 1;
  7844. // discount = "0.000000";
  7845. // "item_note" = "";
  7846. // price = "269.000000";
  7847. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7848. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7849. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7850. // NSString *price = [self valueInParams:params key:@"price"];
  7851. // bool badd_price_changed=false;
  7852. // sqlite3* db=[iSalesDB get_db];
  7853. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7854. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7855. // NSRange range;
  7856. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7857. //
  7858. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7859. // badd_price_changed=true;
  7860. // [iSalesDB close_db:db];
  7861. //
  7862. // if(badd_price_changed)
  7863. // {
  7864. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7865. // }
  7866. //
  7867. // sqlite3 *db = [iSalesDB get_db];
  7868. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7869. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7870. //NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f, total_price=%f where so_id = '%@'",discount.doubleValue,total_price*(1-discount.doubleValue/100.0),order_code];
  7871. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7872. int ret = [iSalesDB execSql:sql];
  7873. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7874. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7875. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7876. // [dic setValue:@"160409" forKey:@"min_ver"];
  7877. // return [RAConvertor dict2data:dic];
  7878. return dic;
  7879. }
  7880. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7881. {
  7882. DebugLog(@"cart set price params: %@",params);
  7883. // "cartitem_id" = 1;
  7884. // discount = "0.000000";
  7885. // "item_note" = "";
  7886. // price = "269.000000";
  7887. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7888. NSString *notes = [self valueInParams:params key:@"item_note"];
  7889. NSString *discount = [self valueInParams:params key:@"discount"];
  7890. NSString *price = [self valueInParams:params key:@"price"];
  7891. // bool badd_price_changed=false;
  7892. // sqlite3* db=[iSalesDB get_db];
  7893. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7894. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7895. // NSRange range;
  7896. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7897. //
  7898. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7899. // badd_price_changed=true;
  7900. // [iSalesDB close_db:db];
  7901. //
  7902. // if(badd_price_changed)
  7903. // {
  7904. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7905. // }
  7906. //
  7907. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7908. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7909. int ret = [iSalesDB execSql:sql];
  7910. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7911. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7912. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7913. // [dic setValue:@"160409" forKey:@"min_ver"];
  7914. return [RAConvertor dict2data:dic];
  7915. }
  7916. #pragma mark set line notes
  7917. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7918. {
  7919. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7920. NSString *notes = [self valueInParams:params key:@"notes"];
  7921. notes = [self translateSingleQuote:notes];
  7922. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7923. int ret = [iSalesDB execSql:sql];
  7924. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7925. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7926. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7927. // [dic setValue:@"160409" forKey:@"min_ver"];
  7928. return [RAConvertor dict2data:dic];
  7929. }
  7930. #pragma mark set qty
  7931. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7932. {
  7933. assert(params[@"can_create_backorder"]!=nil);
  7934. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7935. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7936. int item_count = [params[@"inputInt"] intValue];
  7937. // 购买检查数量大于库存
  7938. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7939. if (!params[@"can_create_backorder"]) {
  7940. 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];
  7941. __block BOOL out_of_stock = NO;
  7942. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7943. int availability = sqlite3_column_int(stmt, 0);
  7944. if (availability < item_count) {
  7945. out_of_stock = YES;
  7946. }
  7947. }];
  7948. if (out_of_stock) { // 缺货
  7949. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7950. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7951. return [RAConvertor dict2data:dic];
  7952. }
  7953. }
  7954. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7955. int ret = [iSalesDB execSql:sql];
  7956. __block int item_id = 0;
  7957. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7958. item_id = sqlite3_column_int(stmt, 0);
  7959. }];
  7960. sqlite3 *db = [iSalesDB get_db];
  7961. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7962. [iSalesDB close_db:db];
  7963. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7964. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7965. // [dic setValue:@"160409" forKey:@"min_ver"];
  7966. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7967. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7968. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7969. return [RAConvertor dict2data:dic];
  7970. }
  7971. #pragma mark place order
  7972. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7973. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7974. if (aname) {
  7975. [dic setValue:aname forKey:@"aname"];
  7976. }
  7977. if (control) {
  7978. [dic setValue:control forKey:@"control"];
  7979. }
  7980. if (keyboard) {
  7981. [dic setValue:keyboard forKey:@"keyboard"];
  7982. }
  7983. if (name) {
  7984. [dic setValue:name forKey:@"name"];
  7985. }
  7986. if (value) {
  7987. [dic setValue:value forKey:@"value"];
  7988. }
  7989. return dic;
  7990. }
  7991. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7992. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7993. orderCode = [self translateSingleQuote:orderCode];
  7994. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7995. 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];
  7996. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7997. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7998. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7999. NSString *img1 = [self textAtColumn:2 statement:stmt];
  8000. NSString *img2 = [self textAtColumn:3 statement:stmt];
  8001. NSString *company_name = [self textAtColumn:4 statement:stmt];
  8002. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  8003. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  8004. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  8005. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  8006. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  8007. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  8008. NSString *email = [self textAtColumn:11 statement:stmt];
  8009. NSString *phone = [self textAtColumn:12 statement:stmt];
  8010. NSString *fax = [self textAtColumn:13 statement:stmt];
  8011. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  8012. NSString *city = [self textAtColumn:15 statement:stmt];
  8013. NSString *state = [self textAtColumn:16 statement:stmt];
  8014. NSString *country = [self textAtColumn:17 statement:stmt];
  8015. NSString *name = [self textAtColumn:18 statement:stmt];
  8016. // contact id
  8017. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  8018. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  8019. [contact_id_dic setValue:@"text" forKey:@"control"];
  8020. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  8021. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  8022. [contact_id_dic setValue:@"true" forKey:@"required"];
  8023. [contact_id_dic setValue:contact_id forKey:@"value"];
  8024. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  8025. // business card
  8026. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  8027. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  8028. [business_card_dic setValue:@"img" forKey:@"control"];
  8029. [business_card_dic setValue:@"1" forKey:@"disable"];
  8030. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  8031. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  8032. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  8033. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  8034. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  8035. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  8036. [business_card_dic setValue:@"business_card" forKey:@"name"];
  8037. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  8038. // fax
  8039. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8040. [customer_dic setValue:fax_dic forKey:@"item_10"];
  8041. // zipcode
  8042. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  8043. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  8044. // city
  8045. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  8046. [customer_dic setValue:city_dic forKey:@"item_12"];
  8047. // state
  8048. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  8049. [customer_dic setValue:state_dic forKey:@"item_13"];
  8050. // country
  8051. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  8052. [customer_dic setValue:country_dic forKey:@"item_14"];
  8053. // company name
  8054. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  8055. [customer_dic setValue:company_dic forKey:@"item_2"];
  8056. // addr_1
  8057. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  8058. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  8059. // addr_2
  8060. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  8061. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  8062. // addr_3
  8063. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  8064. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  8065. // addr_4
  8066. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  8067. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  8068. // Contact
  8069. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  8070. [customer_dic setValue:contact_dic forKey:@"item_7"];
  8071. // email
  8072. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8073. [customer_dic setValue:email_dic forKey:@"item_8"];
  8074. // phone
  8075. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8076. [customer_dic setValue:phone_dic forKey:@"item_9"];
  8077. // title
  8078. [customer_dic setValue:@"Customer" forKey:@"title"];
  8079. // count
  8080. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  8081. }];
  8082. // setting
  8083. NSDictionary *setting = params[@"setting"];
  8084. NSNumber *hide = setting[@"CustomerHide"];
  8085. [customer_dic setValue:hide forKey:@"hide"];
  8086. return customer_dic;
  8087. }
  8088. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8089. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8090. // setting
  8091. NSDictionary *setting = params[@"setting"];
  8092. NSNumber *hide = setting[@"ShipToHide"];
  8093. [dic setValue:hide forKey:@"hide"];
  8094. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8095. orderCode = [self translateSingleQuote:orderCode];
  8096. 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];
  8097. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8098. NSString *cid = [self textAtColumn:0 statement:stmt];
  8099. NSString *name = [self textAtColumn:1 statement:stmt];
  8100. NSString *ext = [self textAtColumn:2 statement:stmt];
  8101. NSString *contact = [self textAtColumn:3 statement:stmt];
  8102. NSString *email = [self textAtColumn:4 statement:stmt];
  8103. NSString *fax = [self textAtColumn:5 statement:stmt];
  8104. NSString *phone = [self textAtColumn:6 statement:stmt];
  8105. // count
  8106. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8107. // title
  8108. [dic setValue:@"Ship To" forKey:@"title"];
  8109. // choose
  8110. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8111. [choose_dic setValue:@"choose" forKey:@"aname"];
  8112. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8113. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8114. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8115. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8116. @"receive_contact" : @"customer_contact",
  8117. @"receive_email" : @"customer_email",
  8118. @"receive_ext" : @"customer_contact_ext",
  8119. @"receive_fax" : @"customer_fax",
  8120. @"receive_name" : @"customer_name",
  8121. @"receive_phone" : @"customer_phone"},
  8122. @"refresh" : [NSNumber numberWithInteger:1],
  8123. @"type" : @"pull"};
  8124. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  8125. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  8126. @"name" : @"Add new address",
  8127. @"refresh" : [NSNumber numberWithInteger:1],
  8128. @"value" : @"new_addr"
  8129. };
  8130. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8131. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8132. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8133. @"receive_contact" : @"customer_contact",
  8134. @"receive_email" : @"customer_email",
  8135. @"receive_ext" : @"customer_contact_ext",
  8136. @"receive_fax" : @"customer_fax",
  8137. @"receive_name" : @"customer_name",
  8138. @"receive_phone" : @"customer_phone"},
  8139. @"name" : @"select_ship_to",
  8140. @"refresh" : [NSNumber numberWithInteger:1],
  8141. @"value" : @"Sales_Order_Ship_To"};
  8142. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8143. [dic setValue:choose_dic forKey:@"item_0"];
  8144. // contact id
  8145. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8146. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8147. [contact_id_dic setValue:@"true" forKey:@"required"];
  8148. [dic setValue:contact_id_dic forKey:@"item_1"];
  8149. // company name
  8150. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8151. [dic setValue:company_name_dic forKey:@"item_2"];
  8152. // address
  8153. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8154. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8155. if ([required integerValue]) {
  8156. [ext_dic setValue:@"true" forKey:@"required"];
  8157. }
  8158. [dic setValue:ext_dic forKey:@"item_3"];
  8159. // contact
  8160. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8161. [dic setValue:contact_dic forKey:@"item_4"];
  8162. // phone
  8163. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8164. [dic setValue:phone_dic forKey:@"item_5"];
  8165. // fax
  8166. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8167. [dic setValue:fax_dic forKey:@"item_6"];
  8168. // email
  8169. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8170. [dic setValue:email_dic forKey:@"item_7"];
  8171. }];
  8172. return dic;
  8173. }
  8174. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8175. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8176. // setting
  8177. NSDictionary *setting = params[@"setting"];
  8178. NSNumber *hide = setting[@"ShipFromHide"];
  8179. [dic setValue:hide forKey:@"hide"];
  8180. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8181. orderCode = [self translateSingleQuote:orderCode];
  8182. 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];
  8183. 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';";
  8184. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8185. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8186. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8187. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8188. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8189. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8190. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8191. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8192. if (!cid.length) {
  8193. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8194. cid = [self textAtColumn:0 statement:statment];
  8195. name = [self textAtColumn:1 statement:statment];
  8196. ext = [self textAtColumn:2 statement:statment];
  8197. contact = [self textAtColumn:3 statement:statment];
  8198. email = [self textAtColumn:4 statement:statment];
  8199. fax = [self textAtColumn:5 statement:statment];
  8200. phone = [self textAtColumn:6 statement:statment];
  8201. }];
  8202. }
  8203. // count
  8204. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8205. // title
  8206. [dic setValue:@"Ship From" forKey:@"title"];
  8207. // hide
  8208. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8209. // choose
  8210. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8211. [choose_dic setValue:@"choose" forKey:@"aname"];
  8212. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8213. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8214. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8215. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8216. @"sender_contact" : @"customer_contact",
  8217. @"sender_email" : @"customer_email",
  8218. @"sender_ext" : @"customer_contact_ext",
  8219. @"sender_fax" : @"customer_fax",
  8220. @"sender_name" : @"customer_name",
  8221. @"sender_phone" : @"customer_phone"},
  8222. @"name" : @"select_cid",
  8223. @"refresh" : [NSNumber numberWithInteger:0],
  8224. @"value" : @"Sales_Order_Ship_From"};
  8225. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8226. [dic setValue:choose_dic forKey:@"item_0"];
  8227. // contact id
  8228. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8229. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8230. [contact_id_dic setValue:@"true" forKey:@"required"];
  8231. [dic setValue:contact_id_dic forKey:@"item_1"];
  8232. // company name
  8233. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8234. [dic setValue:company_name_dic forKey:@"item_2"];
  8235. // address
  8236. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8237. [dic setValue:ext_dic forKey:@"item_3"];
  8238. // contact
  8239. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8240. [dic setValue:contact_dic forKey:@"item_4"];
  8241. // phone
  8242. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8243. [dic setValue:phone_dic forKey:@"item_5"];
  8244. // fax
  8245. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8246. [dic setValue:fax_dic forKey:@"item_6"];
  8247. // email
  8248. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8249. [dic setValue:email_dic forKey:@"item_7"];
  8250. }];
  8251. return dic;
  8252. }
  8253. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8254. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8255. // setting
  8256. NSDictionary *setting = params[@"setting"];
  8257. NSNumber *hide = setting[@"FreightBillToHide"];
  8258. [dic setValue:hide forKey:@"hide"];
  8259. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8260. orderCode = [self translateSingleQuote:orderCode];
  8261. 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];
  8262. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8263. NSString *cid = [self textAtColumn:0 statement:stmt];
  8264. NSString *name = [self textAtColumn:1 statement:stmt];
  8265. NSString *ext = [self textAtColumn:2 statement:stmt];
  8266. NSString *contact = [self textAtColumn:3 statement:stmt];
  8267. NSString *email = [self textAtColumn:4 statement:stmt];
  8268. NSString *fax = [self textAtColumn:5 statement:stmt];
  8269. NSString *phone = [self textAtColumn:6 statement:stmt];
  8270. // count
  8271. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8272. // title
  8273. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8274. // hide
  8275. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8276. // choose
  8277. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8278. [choose_dic setValue:@"choose" forKey:@"aname"];
  8279. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8280. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8281. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8282. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8283. @"shipping_billto_contact" : @"receive_contact",
  8284. @"shipping_billto_email" : @"receive_email",
  8285. @"shipping_billto_ext" : @"receive_ext",
  8286. @"shipping_billto_fax" : @"receive_fax",
  8287. @"shipping_billto_name" : @"receive_name",
  8288. @"shipping_billto_phone" : @"receive_phone"},
  8289. @"type" : @"pull"};
  8290. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8291. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8292. @"type" : @"pull",
  8293. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8294. @"shipping_billto_contact" : @"customer_contact",
  8295. @"shipping_billto_email" : @"customer_email",
  8296. @"shipping_billto_ext" : @"customer_contact_ext",
  8297. @"shipping_billto_fax" : @"customer_fax",
  8298. @"shipping_billto_name" : @"customer_name",
  8299. @"shipping_billto_phone" : @"customer_phone"}
  8300. };
  8301. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8302. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8303. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8304. @"shipping_billto_contact" : @"sender_contact",
  8305. @"shipping_billto_email" : @"sender_email",
  8306. @"shipping_billto_ext" : @"sender_ext",
  8307. @"shipping_billto_fax" : @"sender_fax",
  8308. @"shipping_billto_name" : @"sender_name",
  8309. @"shipping_billto_phone" : @"sender_phone"},
  8310. @"type" : @"pull"
  8311. };
  8312. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8313. NSDictionary *select_freight_bill_to_dic = @{
  8314. @"aname" : @"Select freight bill to",
  8315. @"name" : @"select_cid",
  8316. @"refresh" : [NSNumber numberWithInteger:0],
  8317. @"value" : @"Sales_Order_Freight_Bill_To",
  8318. @"key_map" : @{
  8319. @"shipping_billto_cid" : @"customer_cid",
  8320. @"shipping_billto_contact" : @"customer_contact",
  8321. @"shipping_billto_email" : @"customer_email",
  8322. @"shipping_billto_ext" : @"customer_contact_ext",
  8323. @"shipping_billto_fax" : @"customer_fax",
  8324. @"shipping_billto_name" : @"customer_name",
  8325. @"shipping_billto_phone" : @"customer_phone"
  8326. }
  8327. };
  8328. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8329. [dic setValue:choose_dic forKey:@"item_0"];
  8330. // contact id
  8331. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8332. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8333. [contact_id_dic setValue:@"true" forKey:@"required"];
  8334. [dic setValue:contact_id_dic forKey:@"item_1"];
  8335. // company name
  8336. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8337. [dic setValue:company_name_dic forKey:@"item_2"];
  8338. // address
  8339. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8340. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8341. if ([ext_required integerValue]) {
  8342. [ext_dic setValue:@"true" forKey:@"required"];
  8343. }
  8344. [dic setValue:ext_dic forKey:@"item_3"];
  8345. // contact
  8346. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8347. [dic setValue:contact_dic forKey:@"item_4"];
  8348. // phone
  8349. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8350. [dic setValue:phone_dic forKey:@"item_5"];
  8351. // fax
  8352. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8353. [dic setValue:fax_dic forKey:@"item_6"];
  8354. // email
  8355. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8356. [dic setValue:email_dic forKey:@"item_7"];
  8357. }];
  8358. return dic;
  8359. }
  8360. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8361. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8362. // setting
  8363. NSDictionary *setting = params[@"setting"];
  8364. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8365. [dic setValue:hide forKey:@"hide"];
  8366. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8367. orderCode = [self translateSingleQuote:orderCode];
  8368. 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];
  8369. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8370. NSString *cid = [self textAtColumn:0 statement:stmt];
  8371. NSString *name = [self textAtColumn:1 statement:stmt];
  8372. NSString *ext = [self textAtColumn:2 statement:stmt];
  8373. NSString *contact = [self textAtColumn:3 statement:stmt];
  8374. NSString *email = [self textAtColumn:4 statement:stmt];
  8375. NSString *fax = [self textAtColumn:5 statement:stmt];
  8376. NSString *phone = [self textAtColumn:6 statement:stmt];
  8377. // count
  8378. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8379. // title
  8380. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8381. // hide
  8382. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8383. // choose
  8384. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8385. [choose_dic setValue:@"choose" forKey:@"aname"];
  8386. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8387. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8388. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8389. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8390. @"billing_contact" : @"receive_contact",
  8391. @"billing_email" : @"receive_email",
  8392. @"billing_ext" : @"receive_ext",
  8393. @"billing_fax" : @"receive_fax",
  8394. @"billing_name" : @"receive_name",
  8395. @"billing_phone" : @"receive_phone"},
  8396. @"type" : @"pull"};
  8397. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8398. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8399. @"type" : @"pull",
  8400. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8401. @"billing_contact" : @"customer_contact",
  8402. @"billing_email" : @"customer_email",
  8403. @"billing_ext" : @"customer_contact_ext",
  8404. @"billing_fax" : @"customer_fax",
  8405. @"billing_name" : @"customer_name",
  8406. @"billing_phone" : @"customer_phone"}
  8407. };
  8408. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8409. NSDictionary *select_bill_to_dic = @{
  8410. @"aname" : @"Select bill to",
  8411. @"name" : @"select_cid",
  8412. @"refresh" : [NSNumber numberWithInteger:0],
  8413. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8414. @"key_map" : @{
  8415. @"billing_cid" : @"customer_cid",
  8416. @"billing_contact" : @"customer_contact",
  8417. @"billing_email" : @"customer_email",
  8418. @"billing_ext" : @"customer_contact_ext",
  8419. @"billing_fax" : @"customer_fax",
  8420. @"billing_name" : @"customer_name",
  8421. @"billing_phone" : @"customer_phone"
  8422. }
  8423. };
  8424. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8425. [dic setValue:choose_dic forKey:@"item_0"];
  8426. // contact id
  8427. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8428. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8429. [contact_id_dic setValue:@"true" forKey:@"required"];
  8430. [dic setValue:contact_id_dic forKey:@"item_1"];
  8431. // company name
  8432. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8433. [dic setValue:company_name_dic forKey:@"item_2"];
  8434. // address
  8435. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8436. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8437. if ([ext_required integerValue]) {
  8438. [ext_dic setValue:@"true" forKey:@"required"];
  8439. }
  8440. [dic setValue:ext_dic forKey:@"item_3"];
  8441. // contact
  8442. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8443. [dic setValue:contact_dic forKey:@"item_4"];
  8444. // phone
  8445. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8446. [dic setValue:phone_dic forKey:@"item_5"];
  8447. // fax
  8448. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8449. [dic setValue:fax_dic forKey:@"item_6"];
  8450. // email
  8451. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8452. [dic setValue:email_dic forKey:@"item_7"];
  8453. }];
  8454. return dic;
  8455. }
  8456. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8457. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8458. // setting
  8459. NSDictionary *setting = params[@"setting"];
  8460. NSNumber *hide = setting[@"ReturnToHide"];
  8461. [dic setValue:hide forKey:@"hide"];
  8462. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8463. orderCode = [self translateSingleQuote:orderCode];
  8464. 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];
  8465. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8466. NSString *cid = [self textAtColumn:0 statement:stmt];
  8467. NSString *name = [self textAtColumn:1 statement:stmt];
  8468. NSString *ext = [self textAtColumn:2 statement:stmt];
  8469. NSString *contact = [self textAtColumn:3 statement:stmt];
  8470. NSString *email = [self textAtColumn:4 statement:stmt];
  8471. NSString *fax = [self textAtColumn:5 statement:stmt];
  8472. NSString *phone = [self textAtColumn:6 statement:stmt];
  8473. // count
  8474. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8475. // title
  8476. [dic setValue:@"Return To" forKey:@"title"];
  8477. // hide
  8478. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8479. // choose
  8480. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8481. [choose_dic setValue:@"choose" forKey:@"aname"];
  8482. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8483. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8484. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8485. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8486. @"returnto_contact" : @"sender_contact",
  8487. @"returnto_email" : @"sender_email",
  8488. @"returnto_ext" : @"sender_ext",
  8489. @"returnto_fax" : @"sender_fax",
  8490. @"returnto_name" : @"sender_name",
  8491. @"returnto_phone" : @"sender_phone"},
  8492. @"type" : @"pull"};
  8493. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8494. NSDictionary *select_return_to_dic = @{
  8495. @"aname" : @"Select return to",
  8496. @"name" : @"select_cid",
  8497. @"refresh" : [NSNumber numberWithInteger:0],
  8498. @"value" : @"Contact_Return_To",
  8499. @"key_map" : @{
  8500. @"returnto_cid" : @"customer_cid",
  8501. @"returnto_contact" : @"customer_contact",
  8502. @"returnto_email" : @"customer_email",
  8503. @"returnto_ext" : @"customer_contact_ext",
  8504. @"returnto_fax" : @"customer_fax",
  8505. @"returnto_name" : @"customer_name",
  8506. @"returnto_phone" : @"customer_phone"
  8507. }
  8508. };
  8509. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8510. [dic setValue:choose_dic forKey:@"item_0"];
  8511. // contact id
  8512. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8513. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8514. [contact_id_dic setValue:@"true" forKey:@"required"];
  8515. [dic setValue:contact_id_dic forKey:@"item_1"];
  8516. // company name
  8517. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8518. [dic setValue:company_name_dic forKey:@"item_2"];
  8519. // address
  8520. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8521. [dic setValue:ext_dic forKey:@"item_3"];
  8522. // contact
  8523. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8524. [dic setValue:contact_dic forKey:@"item_4"];
  8525. // phone
  8526. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8527. [dic setValue:phone_dic forKey:@"item_5"];
  8528. // fax
  8529. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8530. [dic setValue:fax_dic forKey:@"item_6"];
  8531. // email
  8532. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8533. [dic setValue:email_dic forKey:@"item_7"];
  8534. }];
  8535. return dic;
  8536. }
  8537. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8538. {
  8539. assert(params[@"user"]!=nil);
  8540. assert(params[@"contact_id"]!=nil);
  8541. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8542. orderCode = [self translateSingleQuote:orderCode];
  8543. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8544. __block double TotalCuft = 0;
  8545. __block double TotalWeight = 0;
  8546. __block int TotalCarton = 0;
  8547. __block double payments = 0;
  8548. // setting
  8549. NSDictionary *setting = params[@"setting"];
  8550. NSNumber *hide = setting[@"ModelInformationHide"];
  8551. [dic setValue:hide forKey:@"hide"];
  8552. 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];
  8553. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8554. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8555. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8556. // item id
  8557. int item_id = sqlite3_column_int(stmt, 0);
  8558. // count
  8559. int item_count = sqlite3_column_int(stmt, 1);
  8560. // stockUom
  8561. int stockUom = sqlite3_column_int(stmt, 2);
  8562. // unit price
  8563. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8564. NSString* Price=nil;
  8565. if([str_price isEqualToString:@""])
  8566. {
  8567. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8568. if(price==nil)
  8569. Price=@"No Price.";
  8570. else
  8571. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8572. }
  8573. else
  8574. {
  8575. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8576. }
  8577. // discount
  8578. double discount = sqlite3_column_double(stmt, 4);
  8579. // name
  8580. NSString *name = [self textAtColumn:5 statement:stmt];
  8581. // description
  8582. NSString *description = [self textAtColumn:6 statement:stmt];
  8583. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8584. // line note
  8585. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8586. int avaulability = sqlite3_column_int(stmt, 8);
  8587. // img
  8588. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8589. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8590. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8591. if(combine != nil)
  8592. {
  8593. // int citem=0;
  8594. int bcount=[[combine valueForKey:@"count"] intValue];
  8595. for(int bc=0;bc<bcount;bc++)
  8596. {
  8597. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8598. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8599. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8600. subTotal += uprice * modulus * item_count;
  8601. }
  8602. }
  8603. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8604. [model_dic setValue:@"model" forKey:@"control"];
  8605. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8606. [model_dic setValue:description forKey:@"description"];
  8607. [model_dic setValue:line_note forKey:@"note"];
  8608. [model_dic setValue:img forKey:@"img_url"];
  8609. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8610. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8611. [model_dic setValue:Price forKey:@"unit_price"];
  8612. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8613. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8614. if (combine) {
  8615. [model_dic setValue:combine forKey:@"combine"];
  8616. }
  8617. // well,what under the row is the info for total
  8618. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8619. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8620. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8621. int carton=[bsubtotaljson[@"carton"] intValue];
  8622. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8623. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8624. TotalCuft += cuft;
  8625. TotalWeight += weight;
  8626. TotalCarton += carton;
  8627. payments += subTotal;
  8628. //---------------------------
  8629. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8630. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8631. }];
  8632. [dic setValue:@"Model Information" forKey:@"title"];
  8633. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8634. return dic;
  8635. }
  8636. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8637. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8638. [dic setValue:@"Remarks Content" forKey:@"title"];
  8639. // setting
  8640. NSDictionary *setting = params[@"setting"];
  8641. NSNumber *hide = setting[@"RemarksContentHide"];
  8642. [dic setValue:hide forKey:@"hide"];
  8643. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8644. orderCode = [self translateSingleQuote:orderCode];
  8645. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8646. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8647. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8648. int mustCall = sqlite3_column_int(stmt, 1);
  8649. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8650. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8651. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8652. NSDictionary *po_dic = @{
  8653. @"aname" : @"PO#",
  8654. @"control" : @"edit",
  8655. @"keyboard" : @"text",
  8656. @"name" : @"poNumber",
  8657. @"value" : poNumber
  8658. };
  8659. #ifdef BUILD_CONTRAST
  8660. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8661. NSDictionary *schedule_dict = @{
  8662. @"aname" : @"Schdule Date",
  8663. @"control" : @"datepicker",
  8664. @"type": @"date",
  8665. @"required": @"true",
  8666. @"name" : @"schedule_date_str",
  8667. @"value" : nsdate
  8668. };
  8669. #endif
  8670. NSDictionary *must_call_dic = @{
  8671. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8672. @"control" : @"switch",
  8673. @"name" : @"must_call",
  8674. @"value" : mustCall ? @"true" : @"false"
  8675. };
  8676. NSDictionary *general_notes_dic = @{
  8677. @"aname" : @"General notes",
  8678. @"control" : @"text_view",
  8679. @"keyboard" : @"text",
  8680. @"name" : @"comments",
  8681. @"value" : generalNotes
  8682. };
  8683. // NSDictionary *internal_notes_dic = @{
  8684. // @"aname" : @"Internal notes",
  8685. // @"control" : @"text_view",
  8686. // @"keyboard" : @"text",
  8687. // @"name" : @"internal_notes",
  8688. // @"value" : internalNotes
  8689. // };
  8690. #ifdef BUILD_CONTRAST
  8691. [dic setValue:po_dic forKey:@"item_0"];
  8692. [dic setValue:schedule_dict forKey:@"item_1"];
  8693. [dic setValue:must_call_dic forKey:@"item_2"];
  8694. [dic setValue:general_notes_dic forKey:@"item_3"];
  8695. #else
  8696. [dic setValue:po_dic forKey:@"item_0"];
  8697. [dic setValue:must_call_dic forKey:@"item_1"];
  8698. [dic setValue:general_notes_dic forKey:@"item_2"];
  8699. #endif
  8700. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8701. }];
  8702. #ifdef BUILD_CONTRAST
  8703. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8704. #else
  8705. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8706. #endif
  8707. return dic;
  8708. }
  8709. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8710. // params
  8711. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8712. orderCode = [self translateSingleQuote:orderCode];
  8713. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8714. // setting
  8715. NSDictionary *setting = params[@"setting"];
  8716. NSNumber *hide = setting[@"OrderTotalHide"];
  8717. [dic setValue:hide forKey:@"hide"];
  8718. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8719. __block double lift_gate_value = 0;
  8720. __block double handling_fee = 0;
  8721. __block double shipping = 0;
  8722. __block double discount = 0;
  8723. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8724. int lift_gate = sqlite3_column_int(stmt, 0);
  8725. lift_gate_value = sqlite3_column_double(stmt, 1);
  8726. shipping = sqlite3_column_double(stmt, 2);
  8727. handling_fee = sqlite3_column_double(stmt, 3);
  8728. discount = sqlite3_column_double(stmt, 4);
  8729. if (!lift_gate) {
  8730. lift_gate_value = 0;
  8731. }
  8732. }];
  8733. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8734. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8735. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8736. double payments = [[total valueForKey:@"payments"] doubleValue];
  8737. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8738. double totalPrice = payments* (1-discount/100.0);
  8739. [dic setValue:@"Order Total" forKey:@"title"];
  8740. NSDictionary *payments_dic = @{
  8741. @"align" : @"right",
  8742. @"aname" : @"Payments/Credits",
  8743. @"control" : @"text",
  8744. @"name" : @"paymentsAndCredits",
  8745. @"type" : @"price",
  8746. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8747. };
  8748. [dic setValue:payments_dic forKey:@"item_0"];
  8749. // version 1.71 remove
  8750. // NSDictionary *handling_fee_dic = @{
  8751. // @"align" : @"right",
  8752. // @"aname" : @"Handling Fee",
  8753. // @"control" : @"text",
  8754. // @"name" : @"handling_fee_value",
  8755. // @"required" : @"true",
  8756. // @"type" : @"price",
  8757. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8758. // };
  8759. NSDictionary *shipping_dic = @{
  8760. @"align" : @"right",
  8761. @"aname" : @"Shipping*",
  8762. @"control" : @"text",
  8763. @"name" : @"shipping",
  8764. @"required" : @"true",
  8765. @"type" : @"price",
  8766. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8767. };
  8768. NSDictionary *lift_gate_dic = @{
  8769. @"align" : @"right",
  8770. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8771. @"control" : @"text",
  8772. @"name" : @"lift_gate_value",
  8773. @"required" : @"true",
  8774. @"type" : @"price",
  8775. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8776. };
  8777. int item_count = 1;
  8778. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8779. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8780. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8781. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8782. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8783. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8784. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8785. } else {
  8786. }
  8787. }
  8788. // version 1.71 remove
  8789. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8790. // NSString* strdiscount;
  8791. if(discount>0)
  8792. {
  8793. NSDictionary *discount_dict = @{
  8794. @"align" : @"right",
  8795. @"aname" : @"Order Discount",
  8796. @"control" : @"text",
  8797. @"name" : @"orderDiscount",
  8798. // @"type" : @"price",
  8799. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8800. };
  8801. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8802. }
  8803. NSDictionary *total_price_dic = @{
  8804. @"align" : @"right",
  8805. @"aname" : @"Total",
  8806. @"control" : @"text",
  8807. @"name" : @"totalPrice",
  8808. @"type" : @"price",
  8809. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8810. };
  8811. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8812. NSDictionary *total_cuft_dic = @{
  8813. @"align" : @"right",
  8814. @"aname" : @"Total Cuft",
  8815. @"control" : @"text",
  8816. @"name" : @"",
  8817. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8818. };
  8819. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8820. NSDictionary *total_weight_dic = @{
  8821. @"align" : @"right",
  8822. @"aname" : @"Total Weight",
  8823. @"control" : @"text",
  8824. @"name" : @"",
  8825. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8826. };
  8827. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8828. NSDictionary *total_carton_dic = @{
  8829. @"align" : @"right",
  8830. @"aname" : @"Total Carton",
  8831. @"control" : @"text",
  8832. @"name" : @"",
  8833. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8834. };
  8835. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8836. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8837. return dic;
  8838. }
  8839. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8840. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8841. orderCode = [self translateSingleQuote:orderCode];
  8842. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8843. // setting
  8844. NSDictionary *setting = params[@"setting"];
  8845. NSNumber *hide = setting[@"SignatureHide"];
  8846. [dic setValue:hide forKey:@"hide"];
  8847. [dic setValue:@"Signature" forKey:@"title"];
  8848. __block NSString *pic_path = @"";
  8849. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8850. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8851. pic_path = [self textAtColumn:0 statement:stmt];
  8852. }];
  8853. NSDictionary *pic_dic = @{
  8854. @"aname" : @"Signature",
  8855. @"avalue" :pic_path,
  8856. @"control" : @"signature",
  8857. @"name" : @"sign_picpath",
  8858. @"value" : pic_path
  8859. };
  8860. [dic setValue:pic_dic forKey:@"item_0"];
  8861. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8862. return dic;
  8863. }
  8864. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8865. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8866. // setting
  8867. NSDictionary *setting = params[@"setting"];
  8868. NSNumber *hide = setting[@"ShippingMethodHide"];
  8869. [dic setValue:hide forKey:@"hide"];
  8870. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8871. orderCode = [self translateSingleQuote:orderCode];
  8872. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8873. __block NSString *logist = @"";
  8874. __block NSString *carrier = @"";
  8875. __block NSString *lift_gate = @"";
  8876. __block int lift_gate_integer = 0;
  8877. __block NSString *logistic_note = @"";
  8878. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8879. logist = [self textAtColumn:0 statement:stmt];
  8880. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8881. logistic_note = [self textAtColumn:2 statement:stmt];
  8882. carrier = [self textAtColumn:3 statement:stmt];
  8883. }];
  8884. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8885. [dic setValue:@"Shipping Method" forKey:@"title"];
  8886. // val_0
  8887. int PERSONAL_PICK_UP_check = 0;
  8888. int USE_MY_CARRIER_check = 0;
  8889. NSString *logistic_note_text = @"";
  8890. int will_call_check = 0;
  8891. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8892. will_call_check = 1;
  8893. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8894. PERSONAL_PICK_UP_check = 1;
  8895. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8896. USE_MY_CARRIER_check = 1;
  8897. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8898. logistic_note = [logistic_note_array firstObject];
  8899. if (logistic_note_array.count > 1) {
  8900. logistic_note_text = [logistic_note_array lastObject];
  8901. }
  8902. }
  8903. }
  8904. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8905. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8906. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8907. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8908. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8909. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8910. NSDictionary *val0_subItem_item0 = @{
  8911. @"aname" : @"Option",
  8912. @"cadedate" : @{
  8913. @"count" : [NSNumber numberWithInteger:2],
  8914. @"val_0" : @{
  8915. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8916. @"refresh" : [NSNumber numberWithInteger:0],
  8917. @"value" : @"PERSONAL PICK UP",
  8918. @"value_id" : @"PERSONAL PICK UP"
  8919. },
  8920. @"val_1" : @{
  8921. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8922. @"refresh" : [NSNumber numberWithInteger:0],
  8923. @"sub_item" : @{
  8924. @"count" : [NSNumber numberWithInteger:1],
  8925. @"item_0" : @{
  8926. @"aname" : @"BOL",
  8927. @"control" : @"edit",
  8928. @"keyboard" : @"text",
  8929. @"name" : @"logist_note_text",
  8930. @"refresh" : [NSNumber numberWithInteger:0],
  8931. @"required" : @"false",
  8932. @"value" : logistic_note_text
  8933. }
  8934. },
  8935. @"value" : @"USE MY CARRIER",
  8936. @"value_id" : @"USE MY CARRIER"
  8937. }
  8938. },
  8939. @"control" : @"enum",
  8940. @"name" : @"logistic_note",
  8941. @"required" : @"true",
  8942. @"single_select" : @"true"
  8943. };
  8944. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8945. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8946. // val_1
  8947. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8948. if([lift_gate isEqualToString:@""]) {
  8949. if (lift_gate_integer == 1) {
  8950. lift_gate = @"true";
  8951. } else {
  8952. lift_gate = @"false";
  8953. }
  8954. }
  8955. // carrier = [self valueInParams:params key:@"carrier"];
  8956. // if([carrier isEqualToString:@""]) {
  8957. //
  8958. // if (lift_gate_integer == 1) {
  8959. // lift_gate = @"true";
  8960. // } else {
  8961. // lift_gate = @"false";
  8962. // }
  8963. //
  8964. // }
  8965. int common_carrier_check = 0;
  8966. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8967. common_carrier_check = 1;
  8968. [params setValue:lift_gate forKey:@"lift_gate"];
  8969. #ifdef BUILD_CONTRAST
  8970. [params setValue:carrier forKey:@"carrier"];
  8971. #endif
  8972. }
  8973. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8974. // NSString * code = [iSalesDB jk_queryText:Sql];
  8975. // [self offline_getCarrierCode:offline_getCarrierCode];
  8976. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8977. NSDictionary *val_1 = @{
  8978. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8979. @"sub_item" : @{
  8980. @"item_0" : @{
  8981. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8982. @"control" : @"switch",
  8983. @"name" : @"lift_gate",
  8984. @"refresh" : [NSNumber numberWithInteger:1],
  8985. @"required" : @"true",
  8986. @"value" : lift_gate
  8987. },
  8988. #ifdef BUILD_CONTRAST
  8989. @"count" : [NSNumber numberWithInteger:2],
  8990. @"item_1" : @{
  8991. @"aname" : @"Carrier",
  8992. @"cadedate" :carrierDic,
  8993. @"control" : @"enum",
  8994. @"name" : @"carrier",
  8995. @"required" : @"false",
  8996. @"value" : @"",
  8997. @"single_select" : @"true"
  8998. }
  8999. #else
  9000. @"count" : [NSNumber numberWithInteger:1]
  9001. #endif
  9002. },
  9003. @"value" : @"COMMON CARRIER",
  9004. @"value_id" : @"COMMON CARRIER"
  9005. };
  9006. // cadedate
  9007. NSDictionary *cadedate = @{
  9008. @"count" : [NSNumber numberWithInteger:2],
  9009. @"val_0" : val_0,
  9010. @"val_1" : val_1
  9011. };
  9012. // item_0
  9013. NSMutableDictionary *item_0 = @{
  9014. @"aname" : @"Shipping",
  9015. @"cadedate" : cadedate,
  9016. @"control" : @"enum",
  9017. @"name" : @"logist",
  9018. @"refresh" : [NSNumber numberWithInteger:1],
  9019. @"single_select" : @"true",
  9020. }.mutableCopy;
  9021. NSNumber *required = setting[@"ShippingMethodRequire"];
  9022. if ([required integerValue]) {
  9023. [item_0 setValue:@"true" forKey:@"required"];
  9024. }
  9025. [dic setValue:item_0 forKey:@"item_0"];
  9026. if ([logist isEqualToString:@"WILL CALL"]) {
  9027. [dic removeObjectForKey:@"lift_gate"];
  9028. }
  9029. return dic;
  9030. }
  9031. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  9032. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9033. orderCode = [self translateSingleQuote:orderCode];
  9034. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  9035. __block int submit_as_integer = 0;
  9036. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9037. submit_as_integer = sqlite3_column_int(stmt, 0);
  9038. }];
  9039. int check11 = 0;
  9040. int check10 = 0;
  9041. if (submit_as_integer == 11) {
  9042. check11 = 1;
  9043. }
  9044. if (submit_as_integer == 10) {
  9045. check10 = 1;
  9046. }
  9047. // section_0
  9048. NSMutableDictionary *dic = @{
  9049. @"count" : @(1),
  9050. @"item_0" : @{
  9051. @"aname" : @"Submit Order As",
  9052. @"cadedate" : @{
  9053. @"count" : @(2),
  9054. @"val_0" : @{
  9055. @"check" : [NSNumber numberWithInteger:check11],
  9056. @"value" : @"Sales Order",
  9057. @"value_id" : @(11)
  9058. },
  9059. @"val_1" : @{
  9060. @"check" : [NSNumber numberWithInteger:check10],
  9061. @"value" : @"Quote",
  9062. @"value_id" : @(10)
  9063. }
  9064. },
  9065. @"control" : @"enum",
  9066. @"name" : @"erpOrderStatus",
  9067. @"required" : @"true",
  9068. @"single_select" : @"true"
  9069. },
  9070. @"title" : @"Order Type"
  9071. }.mutableCopy;
  9072. // setting
  9073. NSDictionary *setting = params[@"setting"];
  9074. NSNumber *hide = setting[@"OrderTypeHide"];
  9075. [dic setValue:hide forKey:@"hide"];
  9076. return dic;
  9077. }
  9078. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  9079. countryCode = [self translateSingleQuote:countryCode];
  9080. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  9081. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9082. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  9083. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  9084. if (name == NULL) {
  9085. name = "";
  9086. }
  9087. if (code == NULL) {
  9088. code = "";
  9089. }
  9090. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9091. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  9092. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  9093. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9094. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  9095. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9096. }
  9097. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  9098. [container setValue:stateDic forKey:key];
  9099. }] mutableCopy];
  9100. [ret removeObjectForKey:@"result"];
  9101. // failure 可以不用了,一样的
  9102. if (ret.allKeys.count == 0) {
  9103. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  9104. [stateDic setValue:@"Other" forKey:@"value"];
  9105. [stateDic setValue:@"" forKey:@"value_id"];
  9106. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  9107. if (state_code && [@"" isEqualToString:state_code]) {
  9108. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  9109. }
  9110. NSString *key = [NSString stringWithFormat:@"val_0"];
  9111. [ret setValue:stateDic forKey:key];
  9112. }
  9113. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  9114. return ret;
  9115. }
  9116. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  9117. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9118. orderCode = [self translateSingleQuote:orderCode];
  9119. 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];
  9120. __block NSString *payType = @"";
  9121. __block NSString *firstName = @"";
  9122. __block NSString *lastName = @"";
  9123. __block NSString *addr1 = @"";
  9124. __block NSString *addr2 = @"";
  9125. __block NSString *zipcode = @"";
  9126. __block NSString *cardType = @"";
  9127. __block NSString *cardNumber = @"";
  9128. __block NSString *securityCode = @"";
  9129. __block NSString *month = @"";
  9130. __block NSString *year = @"";
  9131. __block NSString *city = @"";
  9132. __block NSString *state = @"";
  9133. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9134. payType = [self textAtColumn:0 statement:stmt];
  9135. firstName = [self textAtColumn:1 statement:stmt];
  9136. lastName = [self textAtColumn:2 statement:stmt];
  9137. addr1 = [self textAtColumn:3 statement:stmt];
  9138. addr2 = [self textAtColumn:4 statement:stmt];
  9139. zipcode = [self textAtColumn:5 statement:stmt];
  9140. cardType = [self textAtColumn:6 statement:stmt];
  9141. cardNumber = [self textAtColumn:7 statement:stmt];
  9142. securityCode = [self textAtColumn:8 statement:stmt];
  9143. month = [self textAtColumn:9 statement:stmt];
  9144. year = [self textAtColumn:10 statement:stmt];
  9145. city = [self textAtColumn:11 statement:stmt];
  9146. state = [self textAtColumn:12 statement:stmt];
  9147. }];
  9148. NSString *required = @"true";
  9149. // setting
  9150. NSDictionary *setting = params[@"setting"];
  9151. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9152. if ([requiredNumber integerValue]) {
  9153. required = @"true";
  9154. } else {
  9155. required = @"false";
  9156. }
  9157. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9158. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9159. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9160. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9161. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9162. NSString *type = [self textAtColumn:1 statement:stmt];
  9163. NSMutableDictionary *val = @{
  9164. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9165. @"value" : type,
  9166. @"value_id" : type_id
  9167. }.mutableCopy;
  9168. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9169. NSDictionary *sub_item =nil;
  9170. #ifdef BUILD_CONTRAST
  9171. sub_item = @{
  9172. @"count" : @(3),
  9173. @"item_0" : @{
  9174. @"aname" : @"Number",
  9175. @"control" : @"edit",
  9176. @"keyboard" : @"int",
  9177. @"length" : @"16",
  9178. @"name" : @"credit_card_number",
  9179. @"required" : @"false",
  9180. @"value" : cardNumber
  9181. },
  9182. @"item_1" : @{
  9183. @"aname" : @"Expiration Date",
  9184. @"control" : @"monthpicker",
  9185. @"name" : @"credit_card_expiration",
  9186. @"required" : @"false",
  9187. @"type" : @"date",
  9188. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9189. },
  9190. @"item_2" : @{
  9191. @"aname" : @"Security Code",
  9192. @"control" : @"edit",
  9193. @"keyboard" : @"int",
  9194. @"length" : @"4",
  9195. @"name" : @"credit_card_security_code",
  9196. @"required" : @"false",
  9197. @"value" : securityCode
  9198. },
  9199. };
  9200. #else
  9201. sub_item= @{
  9202. @"count" : @(3),
  9203. @"item_0" : @{
  9204. @"aname" : @"choose",
  9205. @"control" : @"multi_action",
  9206. @"count" : @(1),
  9207. @"item_0" : @{
  9208. @"aname" : @"Same as customer",
  9209. @"key_map" : @{
  9210. @"credit_card_address1" : @"customer_address1",
  9211. @"credit_card_address2" : @"customer_address2",
  9212. @"credit_card_city" : @"customer_city",
  9213. @"credit_card_first_name" : @"customer_first_name",
  9214. @"credit_card_last_name" : @"customer_last_name",
  9215. @"credit_card_state" : @"customer_state",
  9216. @"credit_card_zipcode" : @"customer_zipcode"
  9217. },
  9218. @"type" : @"pull"
  9219. }
  9220. },
  9221. @"item_1" : @{
  9222. @"aname" : @"",
  9223. @"color" : @"red",
  9224. @"control" : @"text",
  9225. @"name" : @"",
  9226. @"value" : @"USA Credit cards only"
  9227. },
  9228. @"item_2" : @{
  9229. @"aname" : @"Fill",
  9230. @"cadedate" : @{
  9231. @"count" : @(2),
  9232. @"val_0" : @{
  9233. @"check" : @(1),
  9234. @"sub_item" : @{
  9235. @"count" : @(11),
  9236. @"item_0" : @{
  9237. @"aname" : @"Type",
  9238. @"cadedate" : @{
  9239. @"count" : @(2),
  9240. @"val_0" : @{
  9241. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9242. @"value" : @"VISA",
  9243. @"value_id" : @"VISA"/*@(0)*/
  9244. },
  9245. @"val_1" : @{
  9246. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9247. @"value" : @"MASTER CARD",
  9248. @"value_id" : @"MASTER CARD"/*@(1)*/
  9249. }
  9250. },
  9251. @"control" : @"enum",
  9252. @"name" : @"credit_card_type",
  9253. @"required" : @"true",
  9254. @"single_select" : @"true"
  9255. },
  9256. @"item_1" : @{
  9257. @"aname" : @"Number",
  9258. @"control" : @"edit",
  9259. @"keyboard" : @"int",
  9260. @"length" : @"16",
  9261. @"name" : @"credit_card_number",
  9262. @"required" : @"true",
  9263. @"value" : cardNumber
  9264. },
  9265. @"item_10" : @{
  9266. @"aname" : @"State",
  9267. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9268. @"control" : @"enum",
  9269. @"enum" : @"true",
  9270. @"name" : @"credit_card_state",
  9271. @"required" : @"true",
  9272. @"single_select" : @"true"
  9273. },
  9274. @"item_2" : @{
  9275. @"aname" : @"Expiration Date",
  9276. @"control" : @"monthpicker",
  9277. @"name" : @"credit_card_expiration",
  9278. @"required" : @"true",
  9279. @"type" : @"date",
  9280. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9281. },
  9282. @"item_3" : @{
  9283. @"aname" : @"Security Code",
  9284. @"control" : @"edit",
  9285. @"keyboard" : @"int",
  9286. @"length" : @"3",
  9287. @"name" : @"credit_card_security_code",
  9288. @"required" : @"true",
  9289. @"value" : securityCode
  9290. },
  9291. @"item_4" : @{
  9292. @"aname" : @"First Name",
  9293. @"control" : @"edit",
  9294. @"keyboard" : @"text",
  9295. @"name" : @"credit_card_first_name",
  9296. @"required" : @"true",
  9297. @"value" : firstName
  9298. },
  9299. @"item_5" : @{
  9300. @"aname" : @"Last Name",
  9301. @"control" : @"edit",
  9302. @"keyboard" : @"text",
  9303. @"name" : @"credit_card_last_name",
  9304. @"required" : @"true",
  9305. @"value" : lastName
  9306. },
  9307. @"item_6" : @{
  9308. @"aname" : @"Address 1",
  9309. @"control" : @"edit",
  9310. @"keyboard" : @"text",
  9311. @"name" : @"credit_card_address1",
  9312. @"required" : @"true",
  9313. @"value" : addr1
  9314. },
  9315. @"item_7" : @{
  9316. @"aname" : @"Address 2",
  9317. @"control" : @"edit",
  9318. @"keyboard" : @"text",
  9319. @"name" : @"credit_card_address2",
  9320. @"value" : addr2
  9321. },
  9322. @"item_8" : @{
  9323. @"aname" : @"zip code",
  9324. @"control" : @"edit",
  9325. @"keyboard" : @"text",
  9326. @"name" : @"credit_card_zipcode",
  9327. @"required" : @"true",
  9328. @"value" : zipcode
  9329. },
  9330. @"item_9" : @{
  9331. @"aname" : @"City",
  9332. @"control" : @"edit",
  9333. @"keyboard" : @"text",
  9334. @"name" : @"credit_card_city",
  9335. @"required" : @"true",
  9336. @"value" : city
  9337. }
  9338. },
  9339. @"value" : @"Fill Now",
  9340. @"value_id" : @""
  9341. },
  9342. @"val_1" : @{
  9343. @"check" : @(0),
  9344. @"value" : @"Fill Later",
  9345. @"value_id" : @""
  9346. }
  9347. },
  9348. @"control" : @"enum",
  9349. @"name" : @"",
  9350. @"single_select" : @"true"
  9351. }
  9352. };
  9353. #endif;
  9354. [val setObject:sub_item forKey:@"sub_item"];
  9355. }
  9356. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9357. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9358. }];
  9359. // "section_2"
  9360. NSMutableDictionary *dic = @{
  9361. @"count" : @(1),
  9362. @"item_0" : @{
  9363. @"aname" : @"Payment",
  9364. @"required" : required,
  9365. @"cadedate" : cadedate,
  9366. // @{
  9367. // @"count" : @(6),
  9368. // @"val_3" : @{
  9369. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9370. // @"value" : @"Check",
  9371. // @"value_id" : @"Check"
  9372. // },
  9373. // @"val_2" : @{
  9374. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9375. // @"value" : @"Cash",
  9376. // @"value_id" : @"Cash"
  9377. // },
  9378. // @"val_1" : @{
  9379. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9380. // @"value" : @"NET 60",
  9381. // @"value_id" : @"NET 30"
  9382. // },
  9383. // @"val_4" : @{
  9384. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9385. // @"value" : @"Wire Transfer",
  9386. // @"value_id" : @"Wire Transfer"
  9387. // },
  9388. // @"val_0" : @{
  9389. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9390. // @"sub_item" : @{
  9391. // @"count" : @(3),
  9392. // @"item_0" : @{
  9393. // @"aname" : @"choose",
  9394. // @"control" : @"multi_action",
  9395. // @"count" : @(1),
  9396. // @"item_0" : @{
  9397. // @"aname" : @"Same as customer",
  9398. // @"key_map" : @{
  9399. // @"credit_card_address1" : @"customer_address1",
  9400. // @"credit_card_address2" : @"customer_address2",
  9401. // @"credit_card_city" : @"customer_city",
  9402. // @"credit_card_first_name" : @"customer_first_name",
  9403. // @"credit_card_last_name" : @"customer_last_name",
  9404. // @"credit_card_state" : @"customer_state",
  9405. // @"credit_card_zipcode" : @"customer_zipcode"
  9406. // },
  9407. // @"type" : @"pull"
  9408. // }
  9409. // },
  9410. // @"item_1" : @{
  9411. // @"aname" : @"",
  9412. // @"color" : @"red",
  9413. // @"control" : @"text",
  9414. // @"name" : @"",
  9415. // @"value" : @"USA Credit cards only"
  9416. // },
  9417. // @"item_2" : @{
  9418. // @"aname" : @"Fill",
  9419. // @"cadedate" : @{
  9420. // @"count" : @(2),
  9421. // @"val_0" : @{
  9422. // @"check" : @(1),
  9423. // @"sub_item" : @{
  9424. // @"count" : @(11),
  9425. // @"item_0" : @{
  9426. // @"aname" : @"Type",
  9427. // @"cadedate" : @{
  9428. // @"count" : @(2),
  9429. // @"val_0" : @{
  9430. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9431. // @"value" : @"VISA",
  9432. // @"value_id" : @(0)
  9433. // },
  9434. // @"val_1" : @{
  9435. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9436. // @"value" : @"MASTER CARD",
  9437. // @"value_id" : @(1)
  9438. // }
  9439. // },
  9440. // @"control" : @"enum",
  9441. // @"name" : @"credit_card_type",
  9442. // @"required" : @"true",
  9443. // @"single_select" : @"true"
  9444. // },
  9445. // @"item_1" : @{
  9446. // @"aname" : @"Number",
  9447. // @"control" : @"edit",
  9448. // @"keyboard" : @"int",
  9449. // @"length" : @"16",
  9450. // @"name" : @"credit_card_number",
  9451. // @"required" : @"true",
  9452. // @"value" : cardNumber
  9453. // },
  9454. // @"item_10" : @{
  9455. // @"aname" : @"State",
  9456. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9457. // @"control" : @"enum",
  9458. // @"enum" : @"true",
  9459. // @"name" : @"credit_card_state",
  9460. // @"required" : @"true",
  9461. // @"single_select" : @"true"
  9462. // },
  9463. // @"item_2" : @{
  9464. // @"aname" : @"Expiration Date",
  9465. // @"control" : @"monthpicker",
  9466. // @"name" : @"credit_card_expiration",
  9467. // @"required" : @"true",
  9468. // @"type" : @"date",
  9469. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9470. // },
  9471. // @"item_3" : @{
  9472. // @"aname" : @"Security Code",
  9473. // @"control" : @"edit",
  9474. // @"keyboard" : @"int",
  9475. // @"length" : @"3",
  9476. // @"name" : @"credit_card_security_code",
  9477. // @"required" : @"true",
  9478. // @"value" : securityCode
  9479. // },
  9480. // @"item_4" : @{
  9481. // @"aname" : @"First Name",
  9482. // @"control" : @"edit",
  9483. // @"keyboard" : @"text",
  9484. // @"name" : @"credit_card_first_name",
  9485. // @"required" : @"true",
  9486. // @"value" : firstName
  9487. // },
  9488. // @"item_5" : @{
  9489. // @"aname" : @"Last Name",
  9490. // @"control" : @"edit",
  9491. // @"keyboard" : @"text",
  9492. // @"name" : @"credit_card_last_name",
  9493. // @"required" : @"true",
  9494. // @"value" : lastName
  9495. // },
  9496. // @"item_6" : @{
  9497. // @"aname" : @"Address 1",
  9498. // @"control" : @"edit",
  9499. // @"keyboard" : @"text",
  9500. // @"name" : @"credit_card_address1",
  9501. // @"required" : @"true",
  9502. // @"value" : addr1
  9503. // },
  9504. // @"item_7" : @{
  9505. // @"aname" : @"Address 2",
  9506. // @"control" : @"edit",
  9507. // @"keyboard" : @"text",
  9508. // @"name" : @"credit_card_address2",
  9509. // @"value" : addr2
  9510. // },
  9511. // @"item_8" : @{
  9512. // @"aname" : @"zip code",
  9513. // @"control" : @"edit",
  9514. // @"keyboard" : @"text",
  9515. // @"name" : @"credit_card_zipcode",
  9516. // @"required" : @"true",
  9517. // @"value" : zipcode
  9518. // },
  9519. // @"item_9" : @{
  9520. // @"aname" : @"City",
  9521. // @"control" : @"edit",
  9522. // @"keyboard" : @"text",
  9523. // @"name" : @"credit_card_city",
  9524. // @"required" : @"true",
  9525. // @"value" : city
  9526. // }
  9527. // },
  9528. // @"value" : @"Fill Now",
  9529. // @"value_id" : @""
  9530. // },
  9531. // @"val_1" : @{
  9532. // @"check" : @(0),
  9533. // @"value" : @"Fill Later",
  9534. // @"value_id" : @""
  9535. // }
  9536. // },
  9537. // @"control" : @"enum",
  9538. // @"name" : @"",
  9539. // @"single_select" : @"true"
  9540. // }
  9541. // },
  9542. // @"value" : @"Visa/Master",
  9543. // @"value_id" : @"Credit Card"
  9544. // },
  9545. // @"val_5" : @{
  9546. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9547. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9548. // @"value_id" : @"FOLLOW EXISTING"
  9549. // }
  9550. // },
  9551. @"control" : @"enum",
  9552. @"name" : @"paymentType",
  9553. @"single_select" : @"true"
  9554. },
  9555. @"title" : @"Payment Information"
  9556. }.mutableCopy;
  9557. NSNumber *hide = setting[@"PaymentInformationHide"];
  9558. [dic setValue:hide forKey:@"hide"];
  9559. return dic;
  9560. }
  9561. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9562. // params
  9563. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9564. orderCode = [self translateSingleQuote:orderCode];
  9565. // 缺货检查
  9566. 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];
  9567. __block BOOL outOfStock = NO;
  9568. sqlite3 *database = db;
  9569. if (!db) {
  9570. database = [iSalesDB get_db];
  9571. }
  9572. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9573. int availability = sqlite3_column_int(stmt, 0);
  9574. int item_qty = sqlite3_column_int(stmt, 1);
  9575. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9576. }];
  9577. if (!db) {
  9578. [iSalesDB close_db:database];
  9579. }
  9580. return outOfStock;
  9581. }
  9582. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9583. {
  9584. assert(params[@"user"]!=nil);
  9585. assert(params[@"contact_id"]!=nil);
  9586. assert(params[@"can_create_backorder"]!=nil);
  9587. sqlite3 *db = [iSalesDB get_db];
  9588. // params
  9589. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9590. orderCode = [self translateSingleQuote:orderCode];
  9591. // 缺货检查
  9592. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9593. if (![params[@"can_create_backorder"] boolValue]) {
  9594. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9595. if (out_of_stock) {
  9596. [iSalesDB close_db:db];
  9597. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9598. [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"];
  9599. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9600. return [RAConvertor dict2data:resultDic];
  9601. }
  9602. }
  9603. // UISetting
  9604. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9605. NSString *cachefolder = [paths objectAtIndex:0];
  9606. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9607. NSData* json =nil;
  9608. json=[NSData dataWithContentsOfFile:img_cache];
  9609. NSError *error=nil;
  9610. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9611. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9612. [params setObject:setting forKey:@"setting"];
  9613. int section_count = 0;
  9614. // 0 Order Type 1 Shipping Method 2 Payment Information
  9615. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9616. // 0 Order Type
  9617. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9618. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9619. [ret setValue:order_type_dic forKey:key0];
  9620. // 1 Shipping Method
  9621. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9622. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9623. [ret setValue:shipping_method_dic forKey:key1];
  9624. // 2 Payment Information
  9625. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9626. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9627. [ret setValue:payment_info_dic forKey:key2];
  9628. // 3 Customer
  9629. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9630. NSDictionary *customer_dic = [self customerDic:params db:db];
  9631. [ret setValue:customer_dic forKey:key3];
  9632. // 4 Ship To
  9633. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9634. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9635. [ret setValue:ship_to_dic forKey:key4];
  9636. // 5 Ship From
  9637. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9638. if (![shipFromDisable integerValue]) {
  9639. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9640. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9641. [ret setValue:ship_from_dic forKey:key5];
  9642. }
  9643. // 6 Freight Bill To
  9644. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9645. if (![freightBillToDisable integerValue]) {
  9646. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9647. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9648. [ret setValue:freight_bill_to forKey:key6];
  9649. }
  9650. // 7 Merchandise Bill To
  9651. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9652. if (![merchandiseBillToDisable integerValue]) {
  9653. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9654. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9655. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9656. }
  9657. // 8 Return To
  9658. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9659. if (![returnToDisable integerValue]) {
  9660. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9661. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9662. [ret setValue:return_to_dic forKey:key8];
  9663. }
  9664. // 9 Model Information
  9665. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9666. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9667. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9668. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9669. [ret setValue:model_info_dic forKey:key9];
  9670. // 10 Remarks Content
  9671. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9672. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9673. [ret setValue:remarks_content_dic forKey:key10];
  9674. // 11 Order Total
  9675. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9676. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9677. [ret setValue:order_total_dic forKey:key11];
  9678. // 12 Signature
  9679. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9680. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9681. [ret setValue:sign_dic forKey:key12];
  9682. [ret setValue:@(section_count) forKey:@"section_count"];
  9683. [iSalesDB close_db:db];
  9684. return [RAConvertor dict2data:ret];
  9685. // return nil;
  9686. }
  9687. #pragma mark addr editor
  9688. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9689. NSMutableDictionary *new_item = [item mutableCopy];
  9690. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9691. return new_item;
  9692. }
  9693. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9694. {
  9695. // "is_subaction" = true;
  9696. // orderCode = MOB1608240002;
  9697. // password = 123456;
  9698. // "subaction_tag" = 1;
  9699. // user = EvanK;
  9700. // {
  9701. // "is_subaction" = true;
  9702. // orderCode = MOB1608240002;
  9703. // password = 123456;
  9704. // "refresh_trigger" = zipcode;
  9705. // "subaction_tag" = 1;
  9706. // user = EvanK;
  9707. // }
  9708. NSString *country_code = nil;
  9709. NSString *zipCode = nil;
  9710. NSString *stateCode = nil;
  9711. NSString *city = nil;
  9712. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9713. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9714. NSString *code_id = params[@"country"];
  9715. country_code = [self countryCodeByid:code_id];
  9716. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9717. NSString *zip_code = params[@"zipcode"];
  9718. // 剔除全部为空格
  9719. int spaceCount = 0;
  9720. for (int i = 0; i < zip_code.length; i++) {
  9721. if ([zip_code characterAtIndex:i] == ' ') {
  9722. spaceCount++;
  9723. }
  9724. }
  9725. if (spaceCount == zip_code.length) {
  9726. zip_code = @"";
  9727. }
  9728. zipCode = zip_code;
  9729. if (zipCode.length > 0) {
  9730. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9731. country_code = [dic valueForKey:@"country_code"];
  9732. if (!country_code) {
  9733. // country_code = @"US";
  9734. NSString *code_id = params[@"country"];
  9735. country_code = [self countryCodeByid:code_id];
  9736. }
  9737. stateCode = [dic valueForKey:@"state_code"];
  9738. if(!stateCode.length) {
  9739. stateCode = params[@"state"];
  9740. }
  9741. city = [dic valueForKey:@"city"];
  9742. if (!city.length) {
  9743. city = params[@"city"];
  9744. }
  9745. // zip code
  9746. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9747. // [zipDic setValue:zipCode forKey:@"value"];
  9748. // [section_0 setValue:zipDic forKey:@"item_11"];
  9749. } else {
  9750. NSString *code_id = params[@"country"];
  9751. country_code = [self countryCodeByid:code_id];
  9752. stateCode = params[@"state"];
  9753. city = params[@"city"];
  9754. }
  9755. }
  9756. }
  9757. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9758. // [ret removeObjectForKey:@"up_params"];
  9759. [ret setObject:@"New Address" forKey:@"title"];
  9760. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9761. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9762. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9763. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9764. // [country_dic removeObjectForKey:@"refresh"];
  9765. // [country_dic removeObjectForKey:@"restore"];
  9766. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9767. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9768. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9769. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9770. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9771. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9772. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9773. // [zip_code_dic removeObjectForKey:@"refresh"];
  9774. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9775. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9776. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9777. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9778. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9779. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9780. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9781. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9782. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9783. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9784. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9785. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9786. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9787. // country
  9788. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9789. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9790. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9791. // state
  9792. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9793. // NSDictionary *tmpDic = @{
  9794. // @"value" : @"Other",
  9795. // @"value_id" : @"",
  9796. // @"check" : [NSNumber numberWithInteger:0]
  9797. // };
  9798. //
  9799. // for (int i = 0; i < allState.allKeys.count; i++) {
  9800. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9801. //
  9802. // NSDictionary *tmp = allState[key];
  9803. // [allState setValue:tmpDic forKey:key];
  9804. // tmpDic = tmp;
  9805. // }
  9806. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9807. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9808. [ret setValue:new_section_0 forKey:@"section_0"];
  9809. return [RAConvertor dict2data:ret];
  9810. }
  9811. #pragma mark save addr
  9812. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9813. {
  9814. // NSString *companyName = [self valueInParams:params key:@"company"];
  9815. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9816. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9817. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9818. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9819. // NSString *countryId = [self valueInParams:params key:@"country"];
  9820. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9821. // NSString *city = [self valueInParams:params key:@"city"];
  9822. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9823. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9824. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9825. // NSString *phone = [self valueInParams:params key:@"phone"];
  9826. // NSString *fax = [self valueInParams:params key:@"fax"];
  9827. // NSString *email = [self valueInParams:params key:@"email"];
  9828. return [self offline_saveContact:params update:NO isCustomer:NO];
  9829. }
  9830. #pragma mark cancel order
  9831. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9832. {
  9833. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9834. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9835. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9836. if (order_id.length) {
  9837. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9838. }
  9839. int ret = [iSalesDB execSql:sql];
  9840. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9841. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9842. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9843. // [dic setValue:@"160409" forKey:@"min_ver"];
  9844. return [RAConvertor dict2data:dic];
  9845. }
  9846. #pragma mark sign order
  9847. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9848. {
  9849. //参考 offline_saveBusinesscard
  9850. DebugLog(@"sign order params: %@",params);
  9851. // orderCode = MOB1608240002;
  9852. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9853. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9854. orderCode = [self translateSingleQuote:orderCode];
  9855. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9856. picPath = [self translateSingleQuote:picPath];
  9857. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9858. int ret = [iSalesDB execSql:sql];
  9859. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9860. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9861. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9862. // [dic setValue:@"160409" forKey:@"min_ver"];
  9863. return [RAConvertor dict2data:dic];
  9864. }
  9865. #pragma mark save order
  9866. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9867. NSString *ret = [self valueInParams:params key:key];
  9868. if (translate) {
  9869. ret = [self translateSingleQuote:ret];
  9870. }
  9871. return ret;
  9872. }
  9873. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9874. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9875. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9876. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9877. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9878. if (schedule_date.length==0 ) {
  9879. schedule_date = @"";
  9880. } else {
  9881. schedule_date = [self rchangeDateFormate:schedule_date];
  9882. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9883. }
  9884. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9885. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9886. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9887. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9888. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9889. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9890. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9891. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9892. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9893. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9894. if (![total_price isEqualToString:@""]) {
  9895. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9896. } else {
  9897. total_price = @"";
  9898. }
  9899. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9900. if ([paymentsAndCredits isEqualToString:@""]) {
  9901. paymentsAndCredits = @"";
  9902. } else {
  9903. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9904. }
  9905. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9906. if ([handling_fee_value isEqualToString:@""]) {
  9907. handling_fee_value = @"";
  9908. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9909. handling_fee_value = @"";
  9910. } else {
  9911. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9912. }
  9913. NSString *handling_fee_placeholder = handling_fee_value;
  9914. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9915. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9916. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9917. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9918. NSString *lift_gate_placeholder = @"";
  9919. if ([lift_gate_value isEqualToString:@""]) {
  9920. lift_gate_placeholder = @"";
  9921. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9922. lift_gate_placeholder = @"";
  9923. } else {
  9924. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9925. }
  9926. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9927. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9928. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9929. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9930. 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];
  9931. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9932. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9933. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9934. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9935. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9936. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9937. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9938. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9939. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9940. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9941. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9942. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9943. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9944. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9945. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9946. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9947. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9948. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9949. if (!number_nil) {
  9950. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9951. }
  9952. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9953. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9954. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9955. if (!security_code_nil) {
  9956. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9957. }
  9958. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9959. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9960. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9961. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9962. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9963. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9964. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9965. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9966. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9967. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9968. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9969. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9970. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9971. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9972. NSString *contact_id = customer_cid;
  9973. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9974. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9975. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9976. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9977. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9978. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9979. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9980. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9981. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9982. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9983. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9984. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9985. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9986. //
  9987. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9988. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9989. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9990. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9991. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9992. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9993. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9994. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9995. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9996. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9997. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9998. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9999. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  10000. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  10001. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  10002. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  10003. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  10004. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  10005. 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];
  10006. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  10007. if (receive_cid.length) {
  10008. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  10009. }
  10010. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  10011. if (receive_name.length) {
  10012. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  10013. }
  10014. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  10015. if (receive_ext.length) {
  10016. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  10017. }
  10018. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  10019. if (receive_contact.length) {
  10020. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  10021. }
  10022. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  10023. if (receive_phone.length) {
  10024. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  10025. }
  10026. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  10027. if (receive_email.length) {
  10028. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  10029. }
  10030. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  10031. if (receive_fax.length) {
  10032. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  10033. }
  10034. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  10035. if (sender_cid.length) {
  10036. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  10037. }
  10038. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  10039. if (sender_name.length) {
  10040. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  10041. }
  10042. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  10043. if (sender_ext.length) {
  10044. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  10045. }
  10046. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  10047. if(sender_contact.length) {
  10048. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  10049. }
  10050. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  10051. if (sender_phone.length) {
  10052. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  10053. }
  10054. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  10055. if (sender_fax.length) {
  10056. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  10057. }
  10058. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  10059. if (sender_email.length) {
  10060. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  10061. }
  10062. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  10063. if (shipping_billto_cid.length) {
  10064. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  10065. }
  10066. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  10067. if (shipping_billto_name.length) {
  10068. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  10069. }
  10070. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  10071. if (shipping_billto_ext.length) {
  10072. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  10073. }
  10074. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  10075. if (shipping_billto_contact.length) {
  10076. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  10077. }
  10078. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  10079. if (shipping_billto_phone.length) {
  10080. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  10081. }
  10082. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  10083. if (shipping_billto_fax.length) {
  10084. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  10085. }
  10086. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  10087. if (shipping_billto_email.length) {
  10088. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  10089. }
  10090. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  10091. if (billing_cid.length) {
  10092. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  10093. }
  10094. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  10095. if (billing_name.length) {
  10096. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  10097. }
  10098. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  10099. if (billing_ext.length) {
  10100. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  10101. }
  10102. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  10103. if (billing_contact.length) {
  10104. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  10105. }
  10106. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  10107. if (billing_phone.length) {
  10108. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  10109. }
  10110. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  10111. if (billing_fax.length) {
  10112. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  10113. }
  10114. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  10115. if (billing_email.length) {
  10116. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  10117. }
  10118. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  10119. if (returnto_cid.length) {
  10120. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  10121. }
  10122. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  10123. if (returnto_name.length) {
  10124. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  10125. }
  10126. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10127. if (returnto_ext.length) {
  10128. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10129. }
  10130. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10131. if (returnto_contact.length) {
  10132. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10133. }
  10134. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10135. if (returnto_phone.length) {
  10136. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10137. }
  10138. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10139. if (returnto_fax.length) {
  10140. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10141. }
  10142. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10143. if (returnto_email.length) {
  10144. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10145. }
  10146. NSString *order_status = @"status = 1,";
  10147. if (submit) {
  10148. order_status = @"status = -11,";
  10149. }
  10150. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10151. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10152. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10153. NSString *sync_sql = @"";
  10154. if (submit) {
  10155. param[@"truck_carrier"]=carrier;
  10156. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10157. [param setValue:sales_rep forKey:@"sales_rep"];
  10158. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10159. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10160. }
  10161. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",schedule_date, order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,scarrier,so_id,sync_sql,so_id];
  10162. DebugLog(@"save order contactSql: %@",contactSql);
  10163. DebugLog(@"save order orderSql: %@",orderSql);
  10164. // int contact_ret = [iSalesDB execSql:contactSql];
  10165. int order_ret = [iSalesDB execSql:orderSql];
  10166. int ret = order_ret;
  10167. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10168. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10169. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10170. // [dic setValue:@"160409" forKey:@"min_ver"];
  10171. [dic setObject:so_id forKey:@"so#"];
  10172. return [RAConvertor dict2data:dic];
  10173. }
  10174. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10175. {
  10176. // id foo = nil;
  10177. // NSMutableArray *a = @[].mutableCopy;
  10178. // [a addObject:foo];
  10179. return [self saveorder:param submit:NO];
  10180. }
  10181. #pragma mark add to cart by name
  10182. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10183. {
  10184. NSString *orderCode = [params objectForKey:@"orderCode"];
  10185. NSDictionary *newParams = @{
  10186. @"product_id" : params[@"product_id_string"],
  10187. @"orderCode" : orderCode,
  10188. @"can_create_backorder":params[@"can_create_backorder"]
  10189. };
  10190. return [self offline_add2cart:[newParams mutableCopy]];
  10191. }
  10192. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10193. {
  10194. NSString *orderCode = [params objectForKey:@"orderCode"];
  10195. NSString *upccode = [params objectForKey:@"upc_code"];
  10196. // product_name = [self translateSingleQuote:product_name];
  10197. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10198. // bool search_upc = [params[@"search_upc"] boolValue];
  10199. sqlite3 *db = [iSalesDB get_db];
  10200. __block NSMutableString *product_id_string = [NSMutableString string];
  10201. // NSString *name = [product_name_array objectAtIndex:i];
  10202. NSString *sql =nil;
  10203. 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];
  10204. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10205. int product_id = sqlite3_column_int(stmt, 0);
  10206. [product_id_string appendFormat:@"%d",product_id];
  10207. }];
  10208. if (!orderCode) {
  10209. orderCode = @"";
  10210. }
  10211. NSDictionary *newParams = @{
  10212. @"product_id" : product_id_string,
  10213. @"orderCode" : orderCode,
  10214. @"can_create_backorder":params[@"can_create_backorder"]
  10215. };
  10216. [iSalesDB close_db:db];
  10217. return [self offline_add2cart:[newParams mutableCopy]];
  10218. }
  10219. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10220. {
  10221. NSString *orderCode = [params objectForKey:@"orderCode"];
  10222. NSString *product_name = [params objectForKey:@"product_name"];
  10223. product_name = [self translateSingleQuote:product_name];
  10224. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10225. bool search_upc = [params[@"search_upc"] boolValue];
  10226. sqlite3 *db = [iSalesDB get_db];
  10227. __block NSMutableString *product_id_string = [NSMutableString string];
  10228. for (int i = 0; i < product_name_array.count; i++) {
  10229. NSString *name = [product_name_array objectAtIndex:i];
  10230. NSString *sql =nil;
  10231. if(search_upc)
  10232. 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];
  10233. else
  10234. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10235. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10236. int product_id = sqlite3_column_int(stmt, 0);
  10237. if (i == product_name_array.count - 1) {
  10238. [product_id_string appendFormat:@"%d",product_id];
  10239. } else {
  10240. [product_id_string appendFormat:@"%d,",product_id];
  10241. }
  10242. }];
  10243. }
  10244. if (!orderCode) {
  10245. orderCode = @"";
  10246. }
  10247. NSDictionary *newParams = @{
  10248. @"product_id" : product_id_string,
  10249. @"orderCode" : orderCode,
  10250. @"can_create_backorder":params[@"can_create_backorder"]
  10251. };
  10252. [iSalesDB close_db:db];
  10253. return [self offline_add2cart:[newParams mutableCopy]];
  10254. }
  10255. #pragma mark reset order
  10256. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10257. {
  10258. // UIApplication * app = [UIApplication sharedApplication];
  10259. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10260. [iSalesDB disable_trigger];
  10261. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10262. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10263. [iSalesDB enable_trigger];
  10264. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10265. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10266. return [RAConvertor dict2data:dic];
  10267. }
  10268. #pragma mark submit order
  10269. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10270. {
  10271. return [self saveorder:params submit:YES];
  10272. }
  10273. #pragma mark copy order
  10274. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10275. {
  10276. NSMutableDictionary *ret = @{}.mutableCopy;
  10277. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10278. sqlite3 *db = [iSalesDB get_db];
  10279. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10280. // 首先查看联系人是否active
  10281. 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];
  10282. __block int customer_is_active = 1;
  10283. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10284. customer_is_active = sqlite3_column_int(stmt, 0);
  10285. }];
  10286. if (!customer_is_active) {
  10287. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10288. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10289. [iSalesDB close_db:db];
  10290. return [RAConvertor dict2data:ret];
  10291. }
  10292. // new order
  10293. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10294. NSString *new_order_code = [self get_offline_soid:db];
  10295. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10296. user = [self translateSingleQuote:user];
  10297. 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
  10298. __block int result = 1;
  10299. result = [iSalesDB execSql:insert_order_sql db:db];
  10300. if (!result) {
  10301. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10302. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10303. [iSalesDB close_db:db];
  10304. return [RAConvertor dict2data:ret];
  10305. }
  10306. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10307. // __block NSString *product_id = @"";
  10308. __weak typeof(self) weakSelf = self;
  10309. 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];
  10310. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10311. int is_active = sqlite3_column_int(stmt, 1);
  10312. if (is_active) {
  10313. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10314. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10315. 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];
  10316. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10317. }
  10318. }];
  10319. // product_id = [product_id substringFromIndex:1];
  10320. //
  10321. // [self offline_add2cart:@{}.mutableCopy];
  10322. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10323. [iSalesDB close_db:db];
  10324. if (result) {
  10325. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10326. } else {
  10327. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10328. }
  10329. [ret setObject:new_order_code forKey:@"so_id"];
  10330. return [RAConvertor dict2data:ret];
  10331. }
  10332. #pragma mark move wish list to cart
  10333. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10334. assert(params[@"can_create_backorder"]!=nil);
  10335. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10336. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10337. NSString *collectId = params[@"collectId"];
  10338. 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];
  10339. __block NSString *product_id = @"";
  10340. __block NSString *qty = @"";
  10341. __block int number_of_outOfStock = 0;
  10342. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10343. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10344. int productId = sqlite3_column_int(stmt, 0);
  10345. int item_qty = sqlite3_column_int(stmt, 1);
  10346. int availability = sqlite3_column_int(stmt, 2);
  10347. int _id = sqlite3_column_int(stmt, 3);
  10348. if (![params[@"can_create_backorder"] boolValue]) {
  10349. // 库存小于购买量为缺货
  10350. if (availability >= item_qty) {
  10351. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10352. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10353. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10354. } else {
  10355. number_of_outOfStock++;
  10356. }
  10357. } else {
  10358. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10359. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10360. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10361. }
  10362. }];
  10363. NSMutableDictionary *retDic = nil;
  10364. if (![params[@"can_create_backorder"] boolValue]) {
  10365. if (delete_collectId.count == 0) {
  10366. retDic = [NSMutableDictionary dictionary];
  10367. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10368. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10369. return [RAConvertor dict2data:retDic];
  10370. }
  10371. }
  10372. if (product_id.length > 1) {
  10373. product_id = [product_id substringFromIndex:1];
  10374. }
  10375. if (qty.length > 1) {
  10376. qty = [qty substringFromIndex:1];
  10377. }
  10378. NSString *orderCode = params[@"orderCode"];
  10379. if (!orderCode) {
  10380. orderCode = @"";
  10381. }
  10382. NSDictionary *newParams = @{
  10383. @"product_id" : product_id,
  10384. @"orderCode" : orderCode,
  10385. @"qty" : qty,
  10386. @"can_create_backorder":params[@"can_create_backorder"]
  10387. };
  10388. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10389. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10390. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10391. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10392. NSInteger ret = [wish_return[@"result"] intValue];
  10393. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10394. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10395. }
  10396. if (![params[@"can_create_backorder"] boolValue]) {
  10397. if (number_of_outOfStock > 0) {
  10398. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10399. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10400. }
  10401. }
  10402. return [RAConvertor dict2data:retDic];
  10403. }
  10404. #pragma mark - portfolio
  10405. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10406. // assert(params[@"contact_id"]!=nil);
  10407. assert(params[@"user"]!=nil);
  10408. assert(params[@"can_see_price"]!=nil);
  10409. int sort = [[params valueForKey:@"sort"] intValue];
  10410. int offset = [[params valueForKey:@"offset"] intValue];
  10411. int limit = [[params valueForKey:@"limit"] intValue];
  10412. NSString *orderBy = @"";
  10413. switch (sort) {
  10414. case 0:{
  10415. orderBy = @"p.modify_time desc";
  10416. }
  10417. break;
  10418. case 1:{
  10419. orderBy = @"modify_time asc";
  10420. }
  10421. break;
  10422. case 2:{
  10423. orderBy = @"p.name asc";
  10424. }
  10425. break;
  10426. case 3:{
  10427. orderBy = @"p.name desc";
  10428. }
  10429. break;
  10430. case 4:{
  10431. orderBy = @"p.description asc";
  10432. }
  10433. break;
  10434. case 5: {
  10435. orderBy = @"m.default_category asc";
  10436. }
  10437. default:
  10438. break;
  10439. }
  10440. // 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];
  10441. 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];
  10442. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10443. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10444. sqlite3 *db = [iSalesDB get_db];
  10445. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10446. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10447. int product_id = sqlite3_column_int(stmt, 0);
  10448. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10449. NSString *name = [self textAtColumn:1 statement:stmt];
  10450. NSString *description = [self textAtColumn:2 statement:stmt];
  10451. double price = sqlite3_column_double(stmt, 3);
  10452. double discount = sqlite3_column_double(stmt,4);
  10453. int qty = sqlite3_column_int(stmt, 5);
  10454. int percentage = sqlite3_column_int(stmt, 6);
  10455. int item_id = sqlite3_column_int(stmt, 7);
  10456. // int fashion_id = sqlite3_column_int(stmt, 8);
  10457. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10458. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10459. double percent = sqlite3_column_double(stmt, 11);
  10460. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10461. if ([price_null isEqualToString:@"null"]) {
  10462. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10463. }
  10464. NSMutableDictionary *item = @{
  10465. @"linenotes": line_note,
  10466. @"check": @(1),
  10467. @"product_id": product_id_string,
  10468. @"available_qty": @(qty),
  10469. @"available_percent" : @(percent),
  10470. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10471. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10472. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10473. @"img": img_path,
  10474. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10475. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10476. }.mutableCopy;
  10477. if (percentage) {
  10478. [item removeObjectForKey:@"available_qty"];
  10479. } else {
  10480. [item removeObjectForKey:@"available_percent"];
  10481. }
  10482. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10483. if ([qty_null isEqualToString:@"null"]) {
  10484. [item removeObjectForKey:@"available_qty"];
  10485. }
  10486. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10487. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10488. }];
  10489. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10490. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10491. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10492. }
  10493. [iSalesDB close_db:db];
  10494. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10495. [dic setValue:@"" forKey:@"email_content"];
  10496. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10497. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10498. return [RAConvertor dict2data:dic];
  10499. }
  10500. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10501. __block int qty = 0;
  10502. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10503. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10504. qty = sqlite3_column_int(stmt, 0);
  10505. }];
  10506. return qty;
  10507. }
  10508. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10509. {
  10510. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10511. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10512. // NSMutableDictionary * values = params[@"replaceValue"];
  10513. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10514. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10515. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10516. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10517. NSString *pdf_path = @"";
  10518. if (direct) {
  10519. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10520. } else {
  10521. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10522. }
  10523. NSString *create_user = [self valueInParams:params key:@"user"];
  10524. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10525. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10526. // model info
  10527. // 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];
  10528. // V1.90 more color
  10529. 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];
  10530. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10531. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10532. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10533. sqlite3 *db = [iSalesDB get_db];
  10534. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10535. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10536. int product_id = sqlite3_column_int(stmt, 0);
  10537. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10538. double price = sqlite3_column_double(stmt, 1);
  10539. double discount = sqlite3_column_double(stmt,2);
  10540. int qty = sqlite3_column_int(stmt, 3);
  10541. int percentage = sqlite3_column_int(stmt, 4);
  10542. int item_id = sqlite3_column_int(stmt, 5);
  10543. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10544. double percent = sqlite3_column_double(stmt, 7);
  10545. int more_color = sqlite3_column_int(stmt, 8);
  10546. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10547. /* if ([price_null isEqualToString:@"null"]) {
  10548. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10549. }
  10550. */
  10551. [product_ids_string appendFormat:@"%@,",product_id_string];
  10552. // Regular Price
  10553. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10554. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10555. // QTY
  10556. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10557. if ([qty_null isEqualToString:@"null"]) {
  10558. // 查available
  10559. qty = [self model_QTY:product_id_string db:db];
  10560. }
  10561. if (percentage) {
  10562. qty = qty * percent / 100;
  10563. }
  10564. // Special Price
  10565. if ([price_null isEqualToString:@"null"]) {
  10566. // price = regular price
  10567. price = [regular_price_str doubleValue];
  10568. }
  10569. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10570. if (![discount_null isEqualToString:@"null"]) {
  10571. price = price * (1 - discount / 100.0);
  10572. }
  10573. NSMutableDictionary *item = @{
  10574. @"line_note": line_note,
  10575. @"product_id": product_id_string,
  10576. @"available_qty": @(qty),
  10577. @"more_color":@(more_color),
  10578. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10579. @"regular_price" : regular_price_str,
  10580. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10581. }.mutableCopy;
  10582. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10583. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10584. }];
  10585. [iSalesDB close_db:db];
  10586. if (product_ids_string.length > 0) {
  10587. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10588. }
  10589. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10590. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10591. return [RAConvertor dict2data:resultDictionary];
  10592. }
  10593. NSString *model_info = [RAConvertor dict2string:dic];
  10594. // 创建PDF
  10595. // 在preview情况下保存,则不需要新建了
  10596. if (direct) {
  10597. NSMutableDictionary *tear_sheet_params = params;
  10598. // if (tear_sheet_id) {
  10599. // tear_sheet_params = values;
  10600. // }
  10601. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10602. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10603. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10604. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10605. resultDictionary = pdfInfo.mutableCopy;
  10606. } else { // 创建PDF失败
  10607. return pdfData;
  10608. }
  10609. } else {
  10610. // pdf_path 就是本地路径
  10611. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10612. }
  10613. // 将文件移动到PDF Cache文件夹
  10614. NSString *newPath = [pdf_path lastPathComponent];
  10615. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10616. NSString *cachefolder = [paths objectAtIndex:0];
  10617. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10618. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10619. NSFileManager *fileManager = [NSFileManager defaultManager];
  10620. NSError *error = nil;
  10621. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10622. if (error) { // 移动文件失败
  10623. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10624. return [RAConvertor dict2data:resultDictionary];
  10625. }
  10626. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10627. pdf_path = [newPath lastPathComponent];
  10628. // 保存信息
  10629. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10630. NSString *off_params = [RAConvertor dict2string:params];
  10631. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10632. pdf_path = [self translateSingleQuote:pdf_path];
  10633. create_user = [self translateSingleQuote:create_user];
  10634. tear_note = [self translateSingleQuote:tear_note];
  10635. tear_name = [self translateSingleQuote:tear_name];
  10636. model_info = [self translateSingleQuote:model_info];
  10637. configureParams = [self translateSingleQuote:configureParams];
  10638. off_params = [self translateSingleQuote:off_params];
  10639. 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];
  10640. if (tear_sheet_id) {
  10641. int _id = [tear_sheet_id intValue];
  10642. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10643. }
  10644. int result = [iSalesDB execSql:save_pdf_sql];
  10645. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10646. //
  10647. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10648. if (remove_Item) {
  10649. // portfolioId
  10650. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10651. }
  10652. return [RAConvertor dict2data:resultDictionary];
  10653. }
  10654. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10655. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10656. }
  10657. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10658. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10659. }
  10660. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10661. int offset = [[params valueForKey:@"offset"] intValue];
  10662. int limit = [[params valueForKey:@"limit"] intValue];
  10663. NSString *keyword = [params valueForKey:@"keyWord"];
  10664. NSString *where = @"where is_delete is null or is_delete = 0";
  10665. if (keyword.length) {
  10666. keyword = [self translateSingleQuote:keyword];
  10667. 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];
  10668. }
  10669. 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
  10670. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10671. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10672. NSString *cachefolder = [paths objectAtIndex:0];
  10673. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10674. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10675. NSString *name = [self textAtColumn:0 statement:stmt];
  10676. NSString *note = [self textAtColumn:1 statement:stmt];
  10677. NSString *time = [self textAtColumn:2 statement:stmt];
  10678. NSString *user = [self textAtColumn:3 statement:stmt];
  10679. NSString *path = [self textAtColumn:4 statement:stmt];
  10680. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10681. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10682. path = [pdfFolder stringByAppendingPathComponent:path];
  10683. BOOL bdir=NO;
  10684. NSFileManager* fileManager = [NSFileManager defaultManager];
  10685. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10686. {
  10687. //pdf文件不存在
  10688. path=nil;
  10689. }
  10690. time = [self changeDateTimeFormate:time];
  10691. time = [time stringByAppendingString:@" PST"];
  10692. int sheet_id = sqlite3_column_int(stmt, 5);
  10693. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10694. item[@"tearsheetsId"]=@(sheet_id);
  10695. item[@"pdf_path"]=path;
  10696. item[@"create_time"]=time;
  10697. item[@"create_user"]=user;
  10698. item[@"tear_note"]=note;
  10699. item[@"tear_name"]=name;
  10700. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10701. item[@"model_info"]=model_info;
  10702. item[@"off_params"]=off_params;
  10703. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10704. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10705. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10706. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10707. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10708. }];
  10709. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10710. return [RAConvertor dict2data:dic];
  10711. }
  10712. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10713. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10714. // NSString *user = [params objectForKey:@"user"];
  10715. // 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];
  10716. 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];
  10717. int result = [iSalesDB execSql:sql];
  10718. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10719. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10720. if (result == RESULT_TRUE) {
  10721. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10722. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10723. sqlite3 *db = [iSalesDB get_db];
  10724. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10725. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10726. [iSalesDB close_db:db];
  10727. }
  10728. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10729. return [RAConvertor dict2data:dic];
  10730. }
  10731. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10732. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10733. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10734. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10735. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10736. NSDictionary *company_item = @{
  10737. @"control": @"text",
  10738. @"keyboard": @"text",
  10739. @"name": @"company_name",
  10740. @"value": COMPANY_FULL_NAME,
  10741. @"aname": @"Company Name"
  10742. };
  10743. [section_0 setObject:company_item forKey:@"item_0"];
  10744. [dic setObject:section_0 forKey:@"section_0"];
  10745. // Regurlar Price
  10746. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10747. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10748. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10749. __block long val_count = 0;
  10750. NSString *sql = @"select name,type,order_by from price order by order_by";
  10751. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10752. NSString *name = [self textAtColumn:0 statement:stmt];
  10753. int type = sqlite3_column_int(stmt, 1);
  10754. int order_by = sqlite3_column_int(stmt, 2);
  10755. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10756. NSDictionary *price_dic = @{
  10757. @"value" : name,
  10758. @"value_id" : [NSNumber numberWithInteger:type],
  10759. @"check" : order_by == 0 ? @(1) : @(0)
  10760. };
  10761. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10762. val_count = ++(*count);
  10763. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10764. val_count = 0;
  10765. }];
  10766. [cadedate setObject:@{@"value" : @"None",
  10767. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10768. val_count++;
  10769. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10770. [price setObject:cadedate forKey:@"cadedate"];
  10771. [section1 setObject:price forKey:@"item_2"];
  10772. [dic setObject:section1 forKey:@"section_1"];
  10773. return [RAConvertor dict2data:dic];
  10774. }
  10775. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10776. NSString *product_id = [params objectForKey:@"fashionId"];
  10777. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10778. __block int result = RESULT_TRUE;
  10779. __block int qty = 0;
  10780. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10781. qty = sqlite3_column_int(stmt, 0);
  10782. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10783. result = RESULT_FALSE;
  10784. }];
  10785. NSMutableDictionary *dic = @{
  10786. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10787. @"mode" : @"Regular Mode",
  10788. @"quantity_available" : @(qty),
  10789. @"result" : @(result),
  10790. }.mutableCopy;
  10791. return [RAConvertor dict2data:dic];
  10792. }
  10793. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10794. NSString *item_ids = [params objectForKey:@"item_id"];
  10795. NSString *line_notes = [params objectForKey:@"notes"];
  10796. NSString *price_str = [params objectForKey:@"price"];
  10797. NSString *discount_str = [params objectForKey:@"discount"];
  10798. NSString *percent = [params objectForKey:@"available_percent"];
  10799. NSString *qty = [params objectForKey:@"available_qty"];
  10800. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10801. int dot = 0;
  10802. if (line_notes) {
  10803. line_notes = [self translateSingleQuote:line_notes];
  10804. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10805. sql = [sql stringByAppendingString:line_notes];
  10806. dot = 1;
  10807. } else {
  10808. line_notes = @"";
  10809. }
  10810. if (price_str) {
  10811. if (dot) {
  10812. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10813. } else {
  10814. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10815. dot = 1;
  10816. }
  10817. sql = [sql stringByAppendingString:price_str];
  10818. } else {
  10819. price_str = @"";
  10820. }
  10821. if (discount_str) {
  10822. if (dot) {
  10823. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10824. } else {
  10825. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10826. dot = 1;
  10827. }
  10828. sql = [sql stringByAppendingString:discount_str];
  10829. } else {
  10830. discount_str = @"";
  10831. }
  10832. if (percent) {
  10833. if (dot) {
  10834. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10835. } else {
  10836. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10837. dot = 1;
  10838. }
  10839. sql = [sql stringByAppendingString:percent];
  10840. } else {
  10841. percent = @"";
  10842. }
  10843. if (qty) {
  10844. if (dot) {
  10845. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10846. } else {
  10847. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10848. dot = 1;
  10849. }
  10850. sql = [sql stringByAppendingString:qty];
  10851. } else {
  10852. qty = @"";
  10853. }
  10854. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10855. sql = [sql stringByAppendingString:where];
  10856. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10857. int result = [iSalesDB execSql:sql];
  10858. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10859. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10860. return [RAConvertor dict2data:dic];
  10861. }
  10862. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10863. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10864. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10865. sqlite3 *db = [iSalesDB get_db];
  10866. 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];
  10867. int result = [iSalesDB execSql:sql db:db];
  10868. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10869. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10870. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10871. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10872. [iSalesDB close_db:db];
  10873. return [RAConvertor dict2data:dic];
  10874. }
  10875. + (void)offline_removePDFWithName:(NSString *)name {
  10876. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10877. NSString *cachefolder = [paths objectAtIndex:0];
  10878. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10879. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10880. NSFileManager *fileManager = [NSFileManager defaultManager];
  10881. [fileManager removeItemAtPath:path error:nil];
  10882. }
  10883. + (void)offline_clear_PDFCache {
  10884. NSFileManager *fileManager = [NSFileManager defaultManager];
  10885. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10886. NSString *cachefolder = [paths objectAtIndex:0];
  10887. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10888. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10889. for (NSString *path in pdf_files) {
  10890. [self offline_removePDFWithName:[path lastPathComponent]];
  10891. }
  10892. }
  10893. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10894. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10895. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10896. NSString *user = [params objectForKey:@"user"];
  10897. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10898. if (![create_user isEqualToString:user]) {
  10899. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10900. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10901. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10902. return [RAConvertor dict2data:dic];
  10903. }
  10904. 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]];
  10905. __block int is_local = 0;
  10906. __block NSString *path = @"";
  10907. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10908. is_local = sqlite3_column_int(stmt, 0);
  10909. path = [self textAtColumn:1 statement:stmt];
  10910. }];
  10911. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10912. if (is_local == 1) {
  10913. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10914. // 删除文件
  10915. [self offline_removePDFWithName:path];
  10916. }
  10917. int result = [iSalesDB execSql:sql];
  10918. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10919. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10920. return [RAConvertor dict2data:dic];
  10921. }
  10922. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10923. {
  10924. // assert(add_params[@"contact_id"]!=nil);
  10925. assert(add_params[@"user"]!=nil);
  10926. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10927. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10928. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10929. NSString* where=@"1=1";
  10930. if (ver!=nil) {
  10931. where=@"is_dirty=1";
  10932. }
  10933. 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];
  10934. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10935. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10936. sqlite3 *db = [iSalesDB get_db];
  10937. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10938. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10939. NSInteger _id = sqlite3_column_int(stmt, 0);
  10940. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10941. NSString *name = [self textAtColumn:2 statement:stmt];
  10942. NSString *desc = [self textAtColumn:3 statement:stmt];
  10943. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10944. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10945. NSInteger qty = sqlite3_column_int(stmt, 6);
  10946. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10947. double percent = sqlite3_column_double(stmt, 8);
  10948. double price = sqlite3_column_double(stmt, 9);
  10949. double discount = sqlite3_column_double(stmt, 10);
  10950. NSString *img = [self textAtColumn:11 statement:stmt];
  10951. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10952. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10953. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10954. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10955. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10956. if ([price_null isEqualToString:@"null"]) {
  10957. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10958. }
  10959. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10960. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10961. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10962. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10963. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10964. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10965. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10966. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10967. if ([qty_null isEqualToString:@"null"]) {
  10968. // [item setValue:@"null" forKey:@"available_qty"];
  10969. } else {
  10970. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10971. }
  10972. if ([is_percent_null isEqualToString:@"null"]) {
  10973. // [item setValue:@"null" forKey:@"percentage"];
  10974. } else {
  10975. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10976. }
  10977. if ([percent_null isEqualToString:@"null"]) {
  10978. // [item setValue:@"null" forKey:@"percent"];
  10979. } else {
  10980. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10981. }
  10982. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10983. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10984. [item setValue:name forKey:@"name"];
  10985. [item setValue:desc forKey:@"description"];
  10986. [item setValue:img forKey:@"img"];
  10987. [item setValue:line_note forKey:@"line_note"];
  10988. [item setValue:create_time forKey:@"createtime"];
  10989. [item setValue:modify_time forKey:@"modifytime"];
  10990. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10991. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10992. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10993. }];
  10994. [iSalesDB close_db:db];
  10995. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10996. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10997. }
  10998. return ret;
  10999. }
  11000. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  11001. {
  11002. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11003. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  11004. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  11005. NSString* where=@"1=1";
  11006. if (ver!=nil) {
  11007. where=@"is_dirty=1";
  11008. }
  11009. 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];
  11010. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11011. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11012. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  11013. NSInteger _id = sqlite3_column_int(stmt, 0);
  11014. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  11015. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  11016. NSString *create_user = [self textAtColumn:3 statement:stmt];
  11017. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  11018. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  11019. NSString *model_info = [self textAtColumn:6 statement:stmt];
  11020. NSString *createtime = [self textAtColumn:7 statement:stmt];
  11021. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  11022. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  11023. NSString *off_params = [self textAtColumn:10 statement:stmt];
  11024. NSString *uuid = [NSUUID UUID].UUIDString;
  11025. int is_delete = sqlite3_column_int(stmt, 11);
  11026. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  11027. int is_local = sqlite3_column_int(stmt, 12);
  11028. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  11029. if (![tearsheet_id_null isEqualToString:@"null"]) {
  11030. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  11031. }
  11032. [item setObject:pdf_path forKey:@"pdf_path"];
  11033. [item setObject:create_user forKey:@"create_user"];
  11034. [item setObject:tear_note forKey:@"tear_note"];
  11035. [item setObject:tear_name forKey:@"tear_name"];
  11036. [item setObject:model_info forKey:@"model_info"];
  11037. [item setObject:createtime forKey:@"createtime"];
  11038. [item setObject:modifytime forKey:@"modifytime"];
  11039. [item setObject:urlParams forKey:@"urlParams"];
  11040. [item setObject:off_params forKey:@"off_params"];
  11041. [item setObject:uuid forKey:@"pdf_token"];
  11042. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  11043. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  11044. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  11045. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  11046. } else {
  11047. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  11048. }
  11049. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  11050. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  11051. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  11052. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  11053. }];
  11054. return ret;
  11055. }
  11056. #pragma mark 2020
  11057. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11058. {
  11059. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11060. sqlite3 *db = [iSalesDB get_db];
  11061. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  11062. if (contactType) {
  11063. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  11064. } else {
  11065. contactType = @"1 = 1";
  11066. }
  11067. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  11068. DebugLog(@"offline contact list keyword: %@",keyword);
  11069. // advanced search
  11070. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  11071. if (contact_name) {
  11072. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11073. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  11074. } else {
  11075. contact_name = @"";
  11076. }
  11077. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  11078. if (customer_phone) {
  11079. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11080. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  11081. } else {
  11082. customer_phone = @"";
  11083. }
  11084. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  11085. if (customer_fax) {
  11086. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11087. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  11088. } else {
  11089. customer_fax = @"";
  11090. }
  11091. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  11092. if (customer_zipcode) {
  11093. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11094. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  11095. } else {
  11096. customer_zipcode = @"";
  11097. }
  11098. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  11099. if (customer_sales_rep) {
  11100. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11101. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  11102. } else {
  11103. customer_sales_rep = @"";
  11104. }
  11105. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  11106. if (customer_state) {
  11107. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11108. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  11109. } else {
  11110. customer_state = @"";
  11111. }
  11112. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  11113. if (customer_name) {
  11114. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11115. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  11116. } else {
  11117. customer_name = @"";
  11118. }
  11119. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  11120. if (customer_country) {
  11121. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11122. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  11123. } else {
  11124. customer_country = @"";
  11125. }
  11126. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11127. if (customer_cid) {
  11128. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11129. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11130. } else {
  11131. customer_cid = @"";
  11132. }
  11133. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11134. if (customer_city) {
  11135. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11136. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11137. } else {
  11138. customer_city = @"";
  11139. }
  11140. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11141. if (customer_address) {
  11142. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11143. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11144. } else {
  11145. customer_address = @"";
  11146. }
  11147. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11148. if (customer_email) {
  11149. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11150. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11151. } else {
  11152. customer_email = @"";
  11153. }
  11154. NSString *price_name = [params valueForKey:@"price_name"];
  11155. if (price_name) {
  11156. if ([price_name containsString:@","]) {
  11157. // 首先从 price表中查处name
  11158. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11159. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11160. for (int i = 1;i < pArray.count;i++) {
  11161. NSString *p = pArray[i];
  11162. [mutablePStr appendFormat:@" or type = %@ ",p];
  11163. }
  11164. [mutablePStr appendString:@";"];
  11165. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11166. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11167. char *name = (char *)sqlite3_column_text(stmt, 0);
  11168. if (!name)
  11169. name = "";
  11170. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11171. }];
  11172. // 再根据name 拼sql
  11173. NSMutableString *mutable_price_name = [NSMutableString string];
  11174. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11175. for (int i = 1; i < price_name_array.count; i++) {
  11176. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11177. }
  11178. [mutable_price_name appendString:@")"];
  11179. price_name = mutable_price_name;
  11180. } else {
  11181. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11182. if ([price_name isEqualToString:@""]) {
  11183. price_name = @"";
  11184. } else {
  11185. __block NSString *price;
  11186. price_name = [self translateSingleQuote:price_name];
  11187. [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) {
  11188. char *p = (char *)sqlite3_column_text(stmt, 0);
  11189. if (p == NULL) {
  11190. p = "";
  11191. }
  11192. price = [NSString stringWithUTF8String:p];
  11193. }];
  11194. if ([price isEqualToString:@""]) {
  11195. price_name = @"";
  11196. } else {
  11197. price = [self translateSingleQuote:price];
  11198. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11199. }
  11200. }
  11201. }
  11202. } else {
  11203. price_name = @"";
  11204. }
  11205. int limit = [[params valueForKey:@"limit"] intValue];
  11206. int offset = [[params valueForKey:@"offset"] intValue];
  11207. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11208. 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];
  11209. 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];
  11210. // int result= [iSalesDB AddExFunction:db];
  11211. int count =0;
  11212. NSString *sqlQuery = nil;
  11213. if(keyword.length==0)
  11214. {
  11215. // 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];
  11216. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11217. sqlQuery = sql;
  11218. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11219. }
  11220. else
  11221. {
  11222. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11223. keyword = keyword.lowercaseString;
  11224. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11225. 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];
  11226. 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]];
  11227. }
  11228. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11229. sqlite3_stmt * statement;
  11230. [ret setValue:@"2" forKey:@"result"];
  11231. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11232. // 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";
  11233. int i = 0;
  11234. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11235. {
  11236. while (sqlite3_step(statement) == SQLITE_ROW)
  11237. {
  11238. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11239. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11240. int editable = sqlite3_column_int(statement, 0);
  11241. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11242. NSString *nscompany_name =nil;
  11243. if(company_name==nil)
  11244. nscompany_name=@"";
  11245. else
  11246. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11247. char *country = (char*)sqlite3_column_text(statement, 2);
  11248. if(country==nil)
  11249. country="";
  11250. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11251. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11252. // if(addr==nil)
  11253. // addr="";
  11254. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11255. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11256. if(zipcode==nil)
  11257. zipcode="";
  11258. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11259. char *state = (char*)sqlite3_column_text(statement, 5);
  11260. if(state==nil)
  11261. state="";
  11262. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11263. char *city = (char*)sqlite3_column_text(statement, 6);
  11264. if(city==nil)
  11265. city="";
  11266. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11267. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11268. // NSString *nscontact_name = nil;
  11269. // if(contact_name==nil)
  11270. // nscontact_name=@"";
  11271. // else
  11272. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11273. char *phone = (char*)sqlite3_column_text(statement, 8);
  11274. NSString *nsphone = nil;
  11275. if(phone==nil)
  11276. nsphone=@"";
  11277. else
  11278. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11279. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11280. if(contact_id==nil)
  11281. contact_id="";
  11282. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11283. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11284. if(addr_1==nil)
  11285. addr_1="";
  11286. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11287. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11288. if(addr_2==nil)
  11289. addr_2="";
  11290. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11291. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11292. if(addr_3==nil)
  11293. addr_3="";
  11294. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11295. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11296. if(addr_4==nil)
  11297. addr_4="";
  11298. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11299. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11300. if(first_name==nil)
  11301. first_name="";
  11302. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11303. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11304. if(last_name==nil)
  11305. last_name="";
  11306. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11307. char *fax = (char*)sqlite3_column_text(statement, 16);
  11308. NSString *nsfax = nil;
  11309. if(fax==nil)
  11310. nsfax=@"";
  11311. else
  11312. {
  11313. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11314. if(nsfax.length>0)
  11315. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11316. }
  11317. char *email = (char*)sqlite3_column_text(statement, 17);
  11318. NSString *nsemail = nil;
  11319. if(email==nil)
  11320. nsemail=@"";
  11321. else
  11322. {
  11323. nsemail= [[NSString alloc]initWithUTF8String:email];
  11324. if(nsemail.length>0)
  11325. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11326. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11327. }
  11328. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11329. [arr_name addObject:nsfirst_name];
  11330. [arr_name addObject:nslast_name];
  11331. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11332. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11333. {
  11334. // decrypt
  11335. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11336. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11337. nsphone=[AESCrypt fastdecrypt:nsphone];
  11338. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11339. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11340. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11341. }
  11342. [arr_addr addObject:nscompany_name];
  11343. [arr_addr addObject:nscontact_name];
  11344. [arr_addr addObject:@"<br>"];
  11345. [arr_addr addObject:nsaddr_1];
  11346. [arr_addr addObject:nsaddr_2];
  11347. [arr_addr addObject:nsaddr_3];
  11348. [arr_addr addObject:nsaddr_4];
  11349. //[arr_addr addObject:nsaddr];
  11350. [arr_addr addObject:nszipcode];
  11351. [arr_addr addObject:nscity];
  11352. [arr_addr addObject:nsstate];
  11353. [arr_addr addObject:nscountry];
  11354. [arr_addr addObject:@"<br>"];
  11355. [arr_addr addObject:nsphone];
  11356. [arr_addr addObject:nsfax];
  11357. [arr_addr addObject:nsemail];
  11358. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11359. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11360. [item setValue:name forKey:@"name"];
  11361. [item setValue:nscontact_id forKey:@"contact_id"];
  11362. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11363. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11364. i++;
  11365. }
  11366. sqlite3_finalize(statement);
  11367. }
  11368. [iSalesDB close_db:db];
  11369. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11370. dispatch_async(dispatch_get_main_queue(), ^{
  11371. UIApplication * app = [UIApplication sharedApplication];
  11372. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11373. [ret setValue:appDelegate.mode forKey:@"mode"];
  11374. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11375. result(ret);
  11376. });
  11377. return ;
  11378. });
  11379. }
  11380. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11381. //{
  11382. //
  11383. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11384. //
  11385. // NSString* contactId = [params valueForKey:@"contactId"];
  11386. //
  11387. //
  11388. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11389. //
  11390. //
  11391. //
  11392. //
  11393. //
  11394. //
  11395. // sqlite3 *db = [iSalesDB get_db];
  11396. //
  11397. //
  11398. //
  11399. //
  11400. //
  11401. // NSString *sqlQuery = nil;
  11402. //
  11403. //
  11404. // {
  11405. // 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];
  11406. //
  11407. // }
  11408. //
  11409. //
  11410. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11411. // sqlite3_stmt * statement;
  11412. //
  11413. //
  11414. // [ret setValue:@"2" forKey:@"result"];
  11415. //
  11416. //
  11417. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11418. // {
  11419. //
  11420. // //int i = 0;
  11421. // if (sqlite3_step(statement) == SQLITE_ROW)
  11422. // {
  11423. //
  11424. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11425. //
  11426. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11427. //
  11428. //
  11429. // // int editable = sqlite3_column_int(statement, 0);
  11430. //
  11431. //
  11432. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  11433. // NSString *nscompany_name =nil;
  11434. // if(company_name==nil)
  11435. // nscompany_name=@"";
  11436. // else
  11437. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  11438. //
  11439. //
  11440. // char *country = (char*)sqlite3_column_text(statement, 2);
  11441. // if(country==nil)
  11442. // country="";
  11443. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11444. //
  11445. //
  11446. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  11447. // // if(addr==nil)
  11448. // // addr="";
  11449. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11450. //
  11451. //
  11452. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11453. // if(zipcode==nil)
  11454. // zipcode="";
  11455. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11456. //
  11457. //
  11458. // char *state = (char*)sqlite3_column_text(statement, 5);
  11459. // if(state==nil)
  11460. // state="";
  11461. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11462. //
  11463. // char *city = (char*)sqlite3_column_text(statement, 6);
  11464. // if(city==nil)
  11465. // city="";
  11466. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11467. //
  11468. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11469. // // NSString *nscontact_name = nil;
  11470. // // if(contact_name==nil)
  11471. // // nscontact_name=@"";
  11472. // // else
  11473. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11474. //
  11475. // char *phone = (char*)sqlite3_column_text(statement, 8);
  11476. // NSString *nsphone = nil;
  11477. // if(phone==nil)
  11478. // nsphone=@"";
  11479. // else
  11480. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  11481. //
  11482. //
  11483. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11484. // // if(contact_id==nil)
  11485. // // contact_id="";
  11486. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11487. //
  11488. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11489. // if(addr_1==nil)
  11490. // addr_1="";
  11491. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11492. //
  11493. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11494. // if(addr_2==nil)
  11495. // addr_2="";
  11496. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11497. //
  11498. //
  11499. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11500. // if(addr_3==nil)
  11501. // addr_3="";
  11502. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11503. //
  11504. //
  11505. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11506. // if(addr_4==nil)
  11507. // addr_4="";
  11508. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11509. //
  11510. //
  11511. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  11512. // if(first_name==nil)
  11513. // first_name="";
  11514. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11515. //
  11516. //
  11517. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  11518. // if(last_name==nil)
  11519. // last_name="";
  11520. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11521. //
  11522. // char *fax = (char*)sqlite3_column_text(statement, 16);
  11523. // NSString *nsfax = nil;
  11524. // if(fax==nil)
  11525. // nsfax=@"";
  11526. // else
  11527. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  11528. //
  11529. // char *email = (char*)sqlite3_column_text(statement, 17);
  11530. // NSString *nsemail = nil;
  11531. // if(email==nil)
  11532. // nsemail=@"";
  11533. // else
  11534. // nsemail= [[NSString alloc]initWithUTF8String:email];
  11535. //
  11536. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  11537. // NSString *nsimg_0 = nil;
  11538. // if(img_0==nil)
  11539. // nsimg_0=@"";
  11540. // else
  11541. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  11542. //
  11543. // [self copy_bcardImg:nsimg_0];
  11544. //
  11545. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  11546. // NSString *nsimg_1 = nil;
  11547. // if(img_1==nil)
  11548. // nsimg_1=@"";
  11549. // else
  11550. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  11551. // [self copy_bcardImg:nsimg_1];
  11552. //
  11553. //
  11554. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  11555. // NSString *nsimg_2 = nil;
  11556. // if(img_2==nil)
  11557. // nsimg_2=@"";
  11558. // else
  11559. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  11560. // [self copy_bcardImg:nsimg_2];
  11561. //
  11562. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  11563. // NSString *nsprice_type = nil;
  11564. // if(price_type==nil)
  11565. // nsprice_type=@"";
  11566. // else
  11567. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  11568. //
  11569. //
  11570. // char *notes = (char*)sqlite3_column_text(statement, 22);
  11571. // NSString *nsnotes = nil;
  11572. // if(notes==nil)
  11573. // nsnotes=@"";
  11574. // else
  11575. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  11576. //
  11577. //
  11578. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  11579. // NSString *nssalesrep = nil;
  11580. // if(salesrep==nil)
  11581. // nssalesrep=@"";
  11582. // else
  11583. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  11584. //
  11585. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  11586. //
  11587. //
  11588. // {
  11589. // // decrypt
  11590. //
  11591. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11592. //
  11593. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11594. //
  11595. // nsphone=[AESCrypt fastdecrypt:nsphone];
  11596. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11597. //
  11598. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11599. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11600. //
  11601. // }
  11602. //
  11603. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11604. //
  11605. // [arr_name addObject:nsfirst_name];
  11606. // [arr_name addObject:nslast_name];
  11607. //
  11608. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11609. //
  11610. //
  11611. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  11612. //
  11613. // [arr_ext addObject:nsaddr_1];
  11614. // [arr_ext addObject:nsaddr_2];
  11615. // [arr_ext addObject:nsaddr_3];
  11616. // [arr_ext addObject:nsaddr_4];
  11617. // [arr_ext addObject:@"\r\n"];
  11618. //
  11619. // [arr_ext addObject:nscity];
  11620. // [arr_ext addObject:nsstate];
  11621. // [arr_ext addObject:nszipcode];
  11622. // [arr_ext addObject:nscountry];
  11623. //
  11624. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  11625. //
  11626. //
  11627. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  11628. //
  11629. //
  11630. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  11631. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  11632. // [item setValue:nscountry forKey:@"customer_country"];
  11633. // [item setValue:nsphone forKey:@"customer_phone"];
  11634. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  11635. // [item setValue:nscompany_name forKey:@"customer_name"];
  11636. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  11637. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  11638. // [item setValue:nsext forKey:@"customer_contact_ext"];
  11639. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  11640. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  11641. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  11642. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  11643. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  11644. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  11645. // [item setValue:nslast_name forKey:@"customer_last_name"];
  11646. // [item setValue:nscity forKey:@"customer_city"];
  11647. // [item setValue:nsstate forKey:@"customer_state"];
  11648. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  11649. // [item setValue:contactId forKey:@"customer_cid"];
  11650. // [item setValue:nscontact_name forKey:@"customer_contact"];
  11651. // [item setValue:nsfax forKey:@"customer_fax"];
  11652. // [item setValue:nsemail forKey:@"customer_email"];
  11653. // [item setValue:contact_type forKey:@"customer_contact_type"];
  11654. //
  11655. //
  11656. // [ret setObject:item forKey:@"customerInfo"];
  11657. // // i++;
  11658. //
  11659. //
  11660. //
  11661. // }
  11662. //
  11663. //
  11664. //
  11665. //
  11666. // sqlite3_finalize(statement);
  11667. // }
  11668. //
  11669. //
  11670. //
  11671. // [iSalesDB close_db:db];
  11672. //
  11673. //
  11674. //
  11675. //
  11676. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11677. // dispatch_async(dispatch_get_main_queue(), ^{
  11678. // UIApplication * app = [UIApplication sharedApplication];
  11679. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11680. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11681. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11682. // result(ret);
  11683. // });
  11684. //
  11685. // return ;
  11686. // });
  11687. //}
  11688. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11689. {
  11690. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11691. NSString* order_code= appDelegate.order_code;
  11692. NSString* user = appDelegate.user;
  11693. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11694. NSString* category = [params valueForKey:@"category"];
  11695. if (!category || [category isEqualToString:@""]) {
  11696. category = @"%";
  11697. }
  11698. category = [self translateSingleQuote:category];
  11699. int limit = [[params valueForKey:@"limit"] intValue];
  11700. int offset = [[params valueForKey:@"offset"] intValue];
  11701. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11702. NSString *limit_str = @"";
  11703. if (limited) {
  11704. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11705. }
  11706. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11707. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11708. sqlite3 *db = [iSalesDB get_db];
  11709. // [iSalesDB AddExFunction:db];
  11710. int count;
  11711. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11712. 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];
  11713. double price_min = 0;
  11714. double price_max = 0;
  11715. if ([params.allKeys containsObject:@"alert"]) {
  11716. // alert
  11717. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11718. NSString *alert = params[@"alert"];
  11719. if ([alert isEqualToString:@"Display All"]) {
  11720. alert = [NSString stringWithFormat:@""];
  11721. } else {
  11722. alert = [self translateSingleQuote:alert];
  11723. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11724. }
  11725. // available
  11726. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11727. NSString *available = params[@"available"];
  11728. NSString *available_condition;
  11729. if ([available isEqualToString:@"Display All"]) {
  11730. available_condition = @"";
  11731. } else if ([available isEqualToString:@"Available Now"]) {
  11732. available_condition = @"and availability > 0";
  11733. } else {
  11734. available_condition = @"and availability == 0";
  11735. }
  11736. // best seller
  11737. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11738. NSString *best_seller = @"";
  11739. NSString *order_best_seller = @"m.name asc";
  11740. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11741. best_seller = @"and best_seller > 0";
  11742. order_best_seller = @"m.best_seller desc,m.name asc";
  11743. }
  11744. // price
  11745. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11746. NSString *price = params[@"price"];
  11747. price_min = 0;
  11748. price_max = MAXFLOAT;
  11749. if (user && price != nil) {
  11750. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11751. NSMutableString *priceName = [NSMutableString string];
  11752. for (int i = 0; i < priceTypeArray.count; i++) {
  11753. NSString *pricetype = priceTypeArray[i];
  11754. pricetype = [self translateSingleQuote:pricetype];
  11755. if (i == 0) {
  11756. [priceName appendFormat:@"'%@'",pricetype];
  11757. } else {
  11758. [priceName appendFormat:@",'%@'",pricetype];
  11759. }
  11760. }
  11761. if ([price isEqualToString:@"Display All"]) {
  11762. price = [NSString stringWithFormat:@""];
  11763. } else if([price containsString:@"+"]){
  11764. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11765. price_min = [price doubleValue];
  11766. 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];
  11767. } else {
  11768. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11769. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11770. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11771. 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];
  11772. }
  11773. } else {
  11774. price = @"";
  11775. }
  11776. // sold_by_qty : Sold in quantities of %@
  11777. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11778. NSString *qty = params[@"sold_by_qty"];
  11779. if ([qty isEqualToString:@"Display All"]) {
  11780. qty = @"";
  11781. } else {
  11782. qty = [self translateSingleQuote:qty];
  11783. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11784. }
  11785. // cate
  11786. // category = [self translateSingleQuote:category];
  11787. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11788. // cate mutiple selection
  11789. NSString *category_id = params[@"category"];
  11790. NSMutableArray *cate_id_array = nil;
  11791. NSMutableString *cateWhere = [NSMutableString string];
  11792. if ([category_id isEqualToString:@""] || !category_id) {
  11793. [cateWhere appendString:@"1 = 1"];
  11794. } else {
  11795. if ([category_id containsString:@","]) {
  11796. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11797. } else {
  11798. cate_id_array = [@[category_id] mutableCopy];
  11799. }
  11800. [cateWhere appendString:@"("];
  11801. for (int i = 0; i < cate_id_array.count; i++) {
  11802. if (i == 0) {
  11803. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11804. } else {
  11805. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11806. }
  11807. }
  11808. [cateWhere appendString:@")"];
  11809. }
  11810. // where bestseller > 0 order by bestseller desc
  11811. // sql query: alert availability(int) best_seller(int) price qty
  11812. 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];
  11813. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11814. }
  11815. DebugLog(@"offline category where: %@",where);
  11816. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11817. if (!user) {
  11818. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11819. }
  11820. [ret setValue:filter forKey:@"filter"];
  11821. DebugLog(@"offline_category sql:%@",sqlQuery);
  11822. sqlite3_stmt * statement;
  11823. [ret setValue:@"2" forKey:@"result"];
  11824. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11825. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11826. // int count=0;
  11827. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11828. {
  11829. int i=0;
  11830. while (sqlite3_step(statement) == SQLITE_ROW)
  11831. {
  11832. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11833. char *name = (char*)sqlite3_column_text(statement, 0);
  11834. if(name==nil)
  11835. name="";
  11836. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11837. char *description = (char*)sqlite3_column_text(statement, 1);
  11838. if(description==nil)
  11839. description="";
  11840. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11841. int product_id = sqlite3_column_int(statement, 2);
  11842. int wid = sqlite3_column_int(statement, 3);
  11843. int closeout = sqlite3_column_int(statement, 4);
  11844. int cid = sqlite3_column_int(statement, 5);
  11845. int wisdelete = sqlite3_column_int(statement, 6);
  11846. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11847. int more_color = sqlite3_column_int(statement, 8);
  11848. // Defaul Category ID
  11849. __block NSString *categoryID = nil;
  11850. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11851. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11852. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11853. if(default_category==nil)
  11854. default_category="";
  11855. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11856. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11857. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11858. categoryID = nsdefault_category;
  11859. }];
  11860. if (!categoryID.length) {
  11861. NSString *cateIDs = params[@"category"];
  11862. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11863. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11864. for (NSString *cateID in requestCategoryArr) {
  11865. BOOL needBreak = NO;
  11866. for (NSString *itemCateIDBox in itemCategoryArr) {
  11867. if (itemCateIDBox.length > 4) {
  11868. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11869. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11870. if ([itemCategoryID isEqualToString:cateID]) {
  11871. needBreak = YES;
  11872. categoryID = itemCategoryID;
  11873. break;
  11874. }
  11875. }
  11876. }
  11877. if (needBreak) {
  11878. break;
  11879. }
  11880. }
  11881. }
  11882. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11883. if(wid !=0 && wisdelete != 1)
  11884. [item setValue:@"true" forKey:@"wish_exists"];
  11885. else
  11886. [item setValue:@"false" forKey:@"wish_exists"];
  11887. if(closeout==0)
  11888. [item setValue:@"false" forKey:@"is_closeout"];
  11889. else
  11890. [item setValue:@"true" forKey:@"is_closeout"];
  11891. if(cid==0)
  11892. [item setValue:@"false" forKey:@"cart_exists"];
  11893. else
  11894. [item setValue:@"true" forKey:@"cart_exists"];
  11895. if (more_color == 0) {
  11896. [item setObject:@(false) forKey:@"more_color"];
  11897. } else if (more_color == 1) {
  11898. [item setObject:@(true) forKey:@"more_color"];
  11899. }
  11900. [item addEntriesFromDictionary:imgjson];
  11901. // [item setValue:nsurl forKey:@"img"];
  11902. [item setValue:nsname forKey:@"name"];
  11903. [item setValue:nsdescription forKey:@"description"];
  11904. if (categoryID) {
  11905. [item setValue:categoryID forKey:@"item_category_id"];
  11906. }
  11907. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11908. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11909. i++;
  11910. }
  11911. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11912. [ret setObject:items forKey:@"items"];
  11913. sqlite3_finalize(statement);
  11914. } else {
  11915. DebugLog(@"nothing...");
  11916. }
  11917. DebugLog(@"count:%d",count);
  11918. [iSalesDB close_db:db];
  11919. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11920. dispatch_async(dispatch_get_main_queue(), ^{
  11921. // UIApplication * app = [UIApplication sharedApplication];
  11922. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11923. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11924. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11925. result(ret);
  11926. });
  11927. });
  11928. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11929. }
  11930. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11931. {
  11932. [self categoryList:params limited:YES completionHandler:result];
  11933. }
  11934. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11935. {
  11936. UIApplication * app = [UIApplication sharedApplication];
  11937. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11938. NSString* order_code = appDelegate.order_code;
  11939. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11940. NSString* keyword = [params valueForKey:@"keyword"];
  11941. keyword=keyword.lowercaseString;
  11942. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11943. int limit = [[params valueForKey:@"limit"] intValue];
  11944. int offset = [[params valueForKey:@"offset"] intValue];
  11945. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11946. NSString *limit_str = @"";
  11947. if (limited) {
  11948. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11949. }
  11950. sqlite3 *db = [iSalesDB get_db];
  11951. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11952. NSString *sqlQuery = nil;
  11953. if(exactMatch )
  11954. 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 ;
  11955. else
  11956. 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
  11957. DebugLog(@"offline_search sql:%@",sqlQuery);
  11958. sqlite3_stmt * statement;
  11959. [ret setValue:@"2" forKey:@"result"];
  11960. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11961. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11962. // int count=0;
  11963. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11964. {
  11965. int i=0;
  11966. while (sqlite3_step(statement) == SQLITE_ROW)
  11967. {
  11968. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11969. // char *name = (char*)sqlite3_column_text(statement, 1);
  11970. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11971. char *name = (char*)sqlite3_column_text(statement, 0);
  11972. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11973. char *description = (char*)sqlite3_column_text(statement, 1);
  11974. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11975. int product_id = sqlite3_column_int(statement, 2);
  11976. // char *url = (char*)sqlite3_column_text(statement, 3);
  11977. // if(url==nil)
  11978. // url="";
  11979. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11980. int wid = sqlite3_column_int(statement, 3);
  11981. int closeout = sqlite3_column_int(statement, 4);
  11982. int cid = sqlite3_column_int(statement, 5);
  11983. int wisdelete = sqlite3_column_int(statement, 6);
  11984. int more_color = sqlite3_column_int(statement, 7);
  11985. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11986. if(wid !=0 && wisdelete != 1)
  11987. [item setValue:@"true" forKey:@"wish_exists"];
  11988. else
  11989. [item setValue:@"false" forKey:@"wish_exists"];
  11990. if(closeout==0)
  11991. [item setValue:@"false" forKey:@"is_closeout"];
  11992. else
  11993. [item setValue:@"true" forKey:@"is_closeout"];
  11994. if(cid==0)
  11995. [item setValue:@"false" forKey:@"cart_exists"];
  11996. else
  11997. [item setValue:@"true" forKey:@"cart_exists"];
  11998. if (more_color == 0) {
  11999. [item setObject:@(false) forKey:@"more_color"];
  12000. } else if (more_color == 1) {
  12001. [item setObject:@(true) forKey:@"more_color"];
  12002. }
  12003. [item addEntriesFromDictionary:imgjson];
  12004. // [item setValue:nsurl forKey:@"img"];
  12005. [item setValue:nsname forKey:@"fash_name"];
  12006. [item setValue:nsdescription forKey:@"description"];
  12007. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12008. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12009. i++;
  12010. }
  12011. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12012. [ret setObject:items forKey:@"items"];
  12013. sqlite3_finalize(statement);
  12014. }
  12015. DebugLog(@"count:%d",count);
  12016. [iSalesDB close_db:db];
  12017. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12018. dispatch_async(dispatch_get_main_queue(), ^{
  12019. result(ret);
  12020. });
  12021. });
  12022. }
  12023. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12024. {
  12025. [self search:params limited:YES completionHandler:result];
  12026. }
  12027. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  12028. //{
  12029. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  12030. // NSString* order_code = appDelegate.order_code;
  12031. // NSString* user = appDelegate.user;
  12032. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12033. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  12034. //
  12035. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  12036. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  12037. //
  12038. // // category
  12039. // NSDictionary *category_menu = [self offline_category_menu];
  12040. // [filter setValue:category_menu forKey:@"category"];
  12041. //
  12042. // NSString* where= nil;
  12043. // NSString* orderby= @"m.name";
  12044. // if (!filterSearch) {
  12045. // int covertype = [[params valueForKey:@"covertype"] intValue];
  12046. //
  12047. // switch (covertype) {
  12048. // case 0:
  12049. // {
  12050. // where=@"m.category like'%%#005#%%'";
  12051. // break;
  12052. // }
  12053. // case 1:
  12054. // {
  12055. // where=@"m.alert like '%QS%'";
  12056. // break;
  12057. // }
  12058. // case 2:
  12059. // {
  12060. // where=@"m.availability>0";
  12061. // break;
  12062. // }
  12063. // case 3:
  12064. // {
  12065. // where=@"m.best_seller>0";
  12066. // orderby=@"m.best_seller desc,m.name asc";
  12067. // break;
  12068. // }
  12069. // default:
  12070. // where=@"1=1";
  12071. // break;
  12072. // }
  12073. //
  12074. // }
  12075. //
  12076. //
  12077. // int limit = [[params valueForKey:@"limit"] intValue];
  12078. // int offset = [[params valueForKey:@"offset"] intValue];
  12079. //
  12080. // NSString *limit_str = @"";
  12081. // if (limited) {
  12082. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  12083. // }
  12084. //
  12085. //
  12086. // sqlite3 *db = [iSalesDB get_db];
  12087. // // [iSalesDB AddExFunction:db];
  12088. //
  12089. // int count;
  12090. //
  12091. // NSString *sqlQuery = nil;
  12092. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  12093. // 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];
  12094. //
  12095. //
  12096. // double price_min = 0;
  12097. // double price_max = 0;
  12098. // if (filterSearch) {
  12099. // // alert
  12100. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  12101. // NSString *alert = params[@"alert"];
  12102. // if ([alert isEqualToString:@"Display All"]) {
  12103. // alert = [NSString stringWithFormat:@""];
  12104. // } else {
  12105. // alert = [self translateSingleQuote:alert];
  12106. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  12107. // }
  12108. //
  12109. // // available
  12110. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  12111. // NSString *available = params[@"available"];
  12112. // NSString *available_condition;
  12113. // if ([available isEqualToString:@"Display All"]) {
  12114. // available_condition = @"";
  12115. // } else if ([available isEqualToString:@"Available Now"]) {
  12116. // available_condition = @"and availability > 0";
  12117. // } else {
  12118. // available_condition = @"and availability == 0";
  12119. // }
  12120. //
  12121. // // best seller
  12122. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  12123. // NSString *best_seller = @"";
  12124. // NSString *order_best_seller = @"m.name asc";
  12125. //
  12126. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  12127. // best_seller = @"and best_seller > 0";
  12128. // order_best_seller = @"m.best_seller desc,m.name asc";
  12129. // }
  12130. //
  12131. // // price
  12132. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  12133. // NSString *price = params[@"price"];
  12134. // price_min = 0;
  12135. // price_max = MAXFLOAT;
  12136. // if (user) {
  12137. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  12138. // NSMutableString *priceName = [NSMutableString string];
  12139. // for (int i = 0; i < priceTypeArray.count; i++) {
  12140. // NSString *pricetype = priceTypeArray[i];
  12141. // pricetype = [self translateSingleQuote:pricetype];
  12142. // if (i == 0) {
  12143. // [priceName appendFormat:@"'%@'",pricetype];
  12144. // } else {
  12145. // [priceName appendFormat:@",'%@'",pricetype];
  12146. // }
  12147. // }
  12148. //
  12149. // if ([price isEqualToString:@"Display All"]) {
  12150. // price = [NSString stringWithFormat:@""];
  12151. // } else if([price containsString:@"+"]){
  12152. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  12153. // price_min = [price doubleValue];
  12154. // 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];
  12155. // } else {
  12156. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  12157. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  12158. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  12159. // 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];
  12160. // }
  12161. // } else {
  12162. // price = @"";
  12163. // }
  12164. //
  12165. // // sold_by_qty : Sold in quantities of %@
  12166. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  12167. // NSString *qty = params[@"sold_by_qty"];
  12168. // if ([qty isEqualToString:@"Display All"]) {
  12169. // qty = @"";
  12170. // } else {
  12171. // qty = [self translateSingleQuote:qty];
  12172. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  12173. // }
  12174. //
  12175. // // category
  12176. // NSString *category_id = params[@"ctgId"];
  12177. // NSMutableArray *cate_id_array = nil;
  12178. // NSMutableString *cateWhere = [NSMutableString string];
  12179. //
  12180. // if ([category_id isEqualToString:@""] || !category_id) {
  12181. // [cateWhere appendString:@"1 = 1"];
  12182. // } else {
  12183. //
  12184. // if ([category_id containsString:@","]) {
  12185. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  12186. // } else {
  12187. // cate_id_array = [@[category_id] mutableCopy];
  12188. // }
  12189. // /*-----------*/
  12190. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  12191. //
  12192. // [cateWhere appendString:@"("];
  12193. // for (int i = 0; i < cate_id_array.count; i++) {
  12194. //
  12195. // for (NSString *key0 in cateDic.allKeys) {
  12196. //
  12197. // if ([key0 containsString:@"category_"]) {
  12198. //
  12199. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  12200. //
  12201. // for (NSString *key1 in category0.allKeys) {
  12202. //
  12203. // if ([key1 containsString:@"category_"]) {
  12204. //
  12205. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  12206. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  12207. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12208. //
  12209. // cate_id_array[i] = [category1 objectForKey:@"id"];
  12210. // if (i == 0) {
  12211. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12212. // } else {
  12213. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12214. // }
  12215. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12216. // [category0 setValue:category1 forKey:key1];
  12217. // [cateDic setValue:category0 forKey:key0];
  12218. //
  12219. // }
  12220. //
  12221. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  12222. //
  12223. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  12224. //
  12225. // cate_id_array[i] = [category0 objectForKey:@"id"];
  12226. // if (i == 0) {
  12227. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  12228. // } else {
  12229. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  12230. // }
  12231. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  12232. // [cateDic setValue:category0 forKey:key0];
  12233. //
  12234. // }
  12235. //
  12236. // }
  12237. //
  12238. // }
  12239. //
  12240. // }
  12241. //
  12242. // }
  12243. //
  12244. // }
  12245. // [cateWhere appendString:@")"];
  12246. //
  12247. // [filter setValue:cateDic forKey:@"category"];
  12248. // }
  12249. //
  12250. // // where bestseller > 0 order by bestseller desc
  12251. // // sql query: alert availability(int) best_seller(int) price qty
  12252. //
  12253. // 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];
  12254. //
  12255. //
  12256. // // count
  12257. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  12258. //
  12259. // }
  12260. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  12261. //
  12262. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  12263. //
  12264. //
  12265. //
  12266. //
  12267. // if (!user) {
  12268. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  12269. // }
  12270. //
  12271. // [ret setValue:filter forKey:@"filter"];
  12272. //
  12273. //
  12274. //
  12275. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  12276. //
  12277. // sqlite3_stmt * statement;
  12278. // [ret setValue:@"2" forKey:@"result"];
  12279. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  12280. //
  12281. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  12282. // // int count=0;
  12283. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12284. // {
  12285. //
  12286. // int i=0;
  12287. // while (sqlite3_step(statement) == SQLITE_ROW)
  12288. // {
  12289. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12290. //
  12291. //
  12292. // char *name = (char*)sqlite3_column_text(statement, 0);
  12293. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12294. //
  12295. // char *description = (char*)sqlite3_column_text(statement, 1);
  12296. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12297. //
  12298. //
  12299. //
  12300. // int product_id = sqlite3_column_int(statement, 2);
  12301. //
  12302. //
  12303. // int wid = sqlite3_column_int(statement, 3);
  12304. // int closeout = sqlite3_column_int(statement, 4);
  12305. // int cid = sqlite3_column_int(statement, 5);
  12306. // int wisdelete = sqlite3_column_int(statement, 6);
  12307. // int more_color = sqlite3_column_int(statement, 7);
  12308. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  12309. //
  12310. // if(wid !=0 && wisdelete != 1)
  12311. // [item setValue:@"true" forKey:@"wish_exists"];
  12312. // else
  12313. // [item setValue:@"false" forKey:@"wish_exists"];
  12314. //
  12315. // if(closeout==0)
  12316. // [item setValue:@"false" forKey:@"is_closeout"];
  12317. // else
  12318. // [item setValue:@"true" forKey:@"is_closeout"];
  12319. //
  12320. // if(cid==0)
  12321. // [item setValue:@"false" forKey:@"cart_exists"];
  12322. // else
  12323. // [item setValue:@"true" forKey:@"cart_exists"];
  12324. //
  12325. // if (more_color == 0) {
  12326. // [item setObject:@(false) forKey:@"more_color"];
  12327. // } else if (more_color == 1) {
  12328. // [item setObject:@(true) forKey:@"more_color"];
  12329. // }
  12330. //
  12331. // [item addEntriesFromDictionary:imgjson];
  12332. //
  12333. //
  12334. // // [item setValue:nsurl forKey:@"img"];
  12335. // [item setValue:nsname forKey:@"fash_name"];
  12336. // [item setValue:nsdescription forKey:@"description"];
  12337. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12338. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  12339. // i++;
  12340. // }
  12341. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  12342. // [ret setObject:items forKey:@"items"];
  12343. // sqlite3_finalize(statement);
  12344. // }
  12345. //
  12346. // [iSalesDB close_db:db];
  12347. //
  12348. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12349. //
  12350. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12351. // dispatch_async(dispatch_get_main_queue(), ^{
  12352. //
  12353. // result(ret);
  12354. // });
  12355. //
  12356. // });
  12357. //
  12358. //
  12359. //
  12360. //}
  12361. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12362. //{
  12363. // [self itemsearch:params limited:YES completionHandler:result];
  12364. //}
  12365. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12366. {
  12367. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12368. NSString* offline_command=params[@"offline_Command"];
  12369. NSMutableDictionary* ret=nil;
  12370. if([offline_command isEqualToString:@"model_NIYMAL"])
  12371. {
  12372. NSString* category = params[@"category"];
  12373. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  12374. }
  12375. dispatch_async(dispatch_get_main_queue(), ^{
  12376. result(ret);
  12377. });
  12378. });
  12379. }
  12380. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12381. {
  12382. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12383. int sort = [[params objectForKey:@"sort"] intValue];
  12384. NSString *sort_str = @"";
  12385. switch (sort) {
  12386. case 0:{
  12387. sort_str = @"order by w.modify_time desc";
  12388. }
  12389. break;
  12390. case 1:{
  12391. sort_str = @"order by w.modify_time asc";
  12392. }
  12393. break;
  12394. case 2:{
  12395. sort_str = @"order by m.name asc";
  12396. }
  12397. break;
  12398. case 3:{
  12399. sort_str = @"order by m.name desc";
  12400. }
  12401. break;
  12402. case 4:{
  12403. sort_str = @"order by m.description asc";
  12404. }
  12405. break;
  12406. default:
  12407. break;
  12408. }
  12409. // NSString* user = appDelegate.user;
  12410. sqlite3 *db = [iSalesDB get_db];
  12411. // order by w.create_time
  12412. 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];
  12413. // 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];
  12414. sqlite3_stmt * statement;
  12415. NSDate *date1 = [NSDate date];
  12416. // NSDate *date2 = nil;
  12417. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12418. int count=0;
  12419. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12420. {
  12421. while (sqlite3_step(statement) == SQLITE_ROW)
  12422. {
  12423. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  12424. int product_id = sqlite3_column_double(statement, 0);
  12425. char *description = (char*)sqlite3_column_text(statement, 1);
  12426. if(description==nil)
  12427. description= "";
  12428. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  12429. int item_id = sqlite3_column_double(statement, 2);
  12430. NSDate *date_image = [NSDate date];
  12431. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  12432. printf("image : ");
  12433. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  12434. // char *url = (char*)sqlite3_column_text(statement, 3);
  12435. // if(url==nil)
  12436. // url="";
  12437. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  12438. int qty = sqlite3_column_int(statement, 3);
  12439. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  12440. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  12441. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  12442. item[@"description"]= nsdescription;
  12443. item[@"img"]= nsurl;
  12444. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  12445. count++;
  12446. }
  12447. printf("total time:");
  12448. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12449. ret[@"count"]= [NSNumber numberWithInt:count];
  12450. ret[@"total_count"]= [NSNumber numberWithInt:count];
  12451. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  12452. ret[@"result"]= [NSNumber numberWithInt:2];
  12453. sqlite3_finalize(statement);
  12454. }
  12455. [iSalesDB close_db:db];
  12456. dispatch_async(dispatch_get_main_queue(), ^{
  12457. UIApplication * app = [UIApplication sharedApplication];
  12458. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12459. appDelegate.wish_count =count;
  12460. [appDelegate update_count_mark];
  12461. result(ret);
  12462. });
  12463. });
  12464. }
  12465. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12466. {
  12467. // UIApplication * app = [UIApplication sharedApplication];
  12468. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12469. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12470. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12471. sqlite3 *db = [iSalesDB get_db];
  12472. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  12473. NSString* product_id=params[@"product_id"];
  12474. NSString *item_count_str = params[@"item_count"];
  12475. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  12476. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  12477. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12478. // NSString *sql = @"";
  12479. for(int i=0;i<arr.count;i++)
  12480. {
  12481. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  12482. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  12483. __block int cart_count = 0;
  12484. if (!item_count_str) {
  12485. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  12486. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12487. NSString *model_set = [self textAtColumn:0 statement:stmt];
  12488. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  12489. cart_count = [[model_set_components lastObject] intValue];
  12490. }];
  12491. }
  12492. if(count==0)
  12493. {
  12494. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  12495. sqlite3_stmt *stmt;
  12496. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  12497. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  12498. if (item_count_arr) {
  12499. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  12500. } else {
  12501. sqlite3_bind_int(stmt,2,cart_count);
  12502. }
  12503. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  12504. [iSalesDB execSql:@"ROLLBACK" db:db];
  12505. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  12506. [iSalesDB close_db:db];
  12507. DebugLog(@"add to wishlist error");
  12508. dispatch_async(dispatch_get_main_queue(), ^{
  12509. result(ret);
  12510. });
  12511. }
  12512. } else {
  12513. int qty = 0;
  12514. if (item_count_arr) {
  12515. qty = [item_count_arr[i] intValue];
  12516. } else {
  12517. qty = cart_count;
  12518. }
  12519. 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]];
  12520. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  12521. [iSalesDB execSql:@"ROLLBACK" db:db];
  12522. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  12523. [iSalesDB close_db:db];
  12524. DebugLog(@"add to wishlist error");
  12525. dispatch_async(dispatch_get_main_queue(), ^{
  12526. result(ret);
  12527. });
  12528. }
  12529. }
  12530. }
  12531. // [iSalesDB execSql:sql db:db];
  12532. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12533. [iSalesDB execSql:@"END TRANSACTION" db:db];
  12534. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  12535. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  12536. [iSalesDB close_db:db];
  12537. ret[@"result"]= [NSNumber numberWithInt:2];
  12538. dispatch_async(dispatch_get_main_queue(), ^{
  12539. UIApplication * app = [UIApplication sharedApplication];
  12540. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12541. appDelegate.wish_count =count;
  12542. [appDelegate update_count_mark];
  12543. result(ret);
  12544. });
  12545. });
  12546. }
  12547. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12548. {
  12549. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  12550. bool bLogin = appDelegate.bLogin;
  12551. __block NSString* contact_id = appDelegate.contact_id;
  12552. __block NSString* user = appDelegate.user;
  12553. __block NSString* order_code = appDelegate.order_code;
  12554. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12555. NSString* model_name = [params valueForKey:@"product_name"];
  12556. NSString* product_id = [params valueForKey:@"product_id"];
  12557. NSString* upc_code = [params valueForKey:@"upc_code"];
  12558. NSString* category = [params valueForKey:@"category"];
  12559. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  12560. if(category==nil) {
  12561. category = default_category_id;
  12562. } else {
  12563. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  12564. // 参数重有多个category id
  12565. if (arr_0.count > 1) {
  12566. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  12567. // 取交集
  12568. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  12569. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  12570. [set_0 intersectSet:set_1];
  12571. if (set_0.count == 1) {
  12572. category = (NSString *)[set_0 anyObject];
  12573. } else {
  12574. if ([set_0 containsObject:default_category_id]) {
  12575. category = default_category_id;
  12576. } else {
  12577. category = (NSString *)[set_0 anyObject];
  12578. }
  12579. }
  12580. }
  12581. }
  12582. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12583. sqlite3 *db = [iSalesDB get_db];
  12584. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  12585. NSString *sqlQuery = nil;
  12586. if(product_id==nil)
  12587. if(model_name==nil)
  12588. {
  12589. 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='%@';
  12590. }
  12591. else
  12592. {
  12593. 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='%@';
  12594. }
  12595. else
  12596. 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=%@;
  12597. sqlite3_stmt * statement;
  12598. [ret setValue:@"2" forKey:@"result"];
  12599. [ret setValue:@"3" forKey:@"detail_section_count"];
  12600. // int count=0;
  12601. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12602. {
  12603. if (sqlite3_step(statement) == SQLITE_ROW)
  12604. {
  12605. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  12606. char *name = (char*)sqlite3_column_text(statement, 0);
  12607. if(name==nil)
  12608. name="";
  12609. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  12610. char *description = (char*)sqlite3_column_text(statement, 1);
  12611. if(description==nil)
  12612. description="";
  12613. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  12614. int product_id = sqlite3_column_int(statement, 2);
  12615. char *color = (char*)sqlite3_column_text(statement, 3);
  12616. if(color==nil)
  12617. color="";
  12618. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  12619. //
  12620. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  12621. // if(legcolor==nil)
  12622. // legcolor="";
  12623. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  12624. //
  12625. //
  12626. int availability = sqlite3_column_int(statement, 5);
  12627. //
  12628. int incoming_stock = sqlite3_column_int(statement, 6);
  12629. char *demension = (char*)sqlite3_column_text(statement, 7);
  12630. if(demension==nil)
  12631. demension="";
  12632. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  12633. // ,,,,,,,,,
  12634. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  12635. if(seat_height==nil)
  12636. seat_height="";
  12637. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  12638. char *material = (char*)sqlite3_column_text(statement, 9);
  12639. if(material==nil)
  12640. material="";
  12641. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  12642. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  12643. if(box_dim==nil)
  12644. box_dim="";
  12645. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  12646. char *volume = (char*)sqlite3_column_text(statement, 11);
  12647. if(volume==nil)
  12648. volume="";
  12649. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  12650. double weight = sqlite3_column_double(statement, 12);
  12651. char *model_set = (char*)sqlite3_column_text(statement, 13);
  12652. if(model_set==nil)
  12653. model_set="";
  12654. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  12655. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  12656. if(load_ability==nil)
  12657. load_ability="";
  12658. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  12659. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  12660. // if(default_category==nil)
  12661. // default_category="";
  12662. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  12663. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  12664. if(fabric_content==nil)
  12665. fabric_content="";
  12666. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  12667. char *assembling = (char*)sqlite3_column_text(statement, 17);
  12668. if(assembling==nil)
  12669. assembling="";
  12670. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  12671. char *made_in = (char*)sqlite3_column_text(statement, 18);
  12672. if(made_in==nil)
  12673. made_in="";
  12674. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  12675. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  12676. if(special_remarks==nil)
  12677. special_remarks="";
  12678. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  12679. int stockUcom = sqlite3_column_double(statement, 20);
  12680. char *product_group = (char*)sqlite3_column_text(statement, 21);
  12681. if(product_group==nil)
  12682. product_group="";
  12683. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  12684. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  12685. // if(fashion_selector==nil)
  12686. // fashion_selector="";
  12687. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  12688. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  12689. if(selector_field==nil)
  12690. selector_field="";
  12691. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  12692. char *property_field = (char*)sqlite3_column_text(statement, 23);
  12693. if(property_field==nil)
  12694. property_field="";
  12695. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  12696. char *packaging = (char*)sqlite3_column_text(statement, 24);
  12697. if(packaging==nil)
  12698. packaging="";
  12699. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  12700. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  12701. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  12702. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  12703. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  12704. [img_section setValue:model_s_img forKey:@"model_s_img"];
  12705. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  12706. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  12707. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  12708. NSString* Availability=nil;
  12709. if(availability>0)
  12710. Availability=[NSString stringWithFormat:@"%d",availability];
  12711. else
  12712. Availability = @"Out of Stock";
  12713. [img_section setValue:Availability forKey:@"Availability"];
  12714. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  12715. char *eta = (char*)sqlite3_column_text(statement, 25);
  12716. if(eta==nil)
  12717. eta="";
  12718. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  12719. if ([nseta isEqualToString:@"null"]) {
  12720. nseta = @"";
  12721. }
  12722. if (availability <= 0) {
  12723. [img_section setValue:nseta forKey:@"ETA"];
  12724. }
  12725. int item_id = sqlite3_column_int(statement, 26);
  12726. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  12727. NSString *content_writing = [self textAtColumn:28 statement:statement];
  12728. NSString* Price=nil;
  12729. if(bLogin==false)
  12730. Price=@"Must Sign in.";
  12731. else
  12732. {
  12733. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12734. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12735. if(price==nil)
  12736. Price=@"No Price.";
  12737. else
  12738. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12739. }
  12740. [img_section setObject:content_writing forKey:@"product_content_writing"];
  12741. [img_section setValue:Price forKey:@"price"];
  12742. [img_section setValue:nsname forKey:@"model_name"];
  12743. [img_section setValue:nsdescription forKey:@"model_descrition"];
  12744. if (order_code) { // 离线order code即so#
  12745. 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];
  12746. __block int cartQTY = 0;
  12747. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  12748. cartQTY = sqlite3_column_int(stmt, 0);
  12749. }];
  12750. if (cartQTY > 0) {
  12751. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  12752. }
  12753. }
  12754. [ret setObject:img_section forKey:@"img_section"];
  12755. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  12756. int detail0_item_count=0;
  12757. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12758. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12759. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12760. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12761. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12762. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12763. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12764. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12765. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12766. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12767. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12768. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12769. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12770. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12771. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12772. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12773. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12774. {
  12775. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12776. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12777. }
  12778. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12779. [detail0_section setValue:@"kv" forKey:@"type"];
  12780. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12781. [ret setObject:detail0_section forKey:@"detail_0"];
  12782. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12783. // [detail1_section setValue:@"detail" forKey:@"target"];
  12784. // [detail1_section setValue:@"popup" forKey:@"action"];
  12785. // [detail1_section setValue:@"content" forKey:@"type"];
  12786. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12787. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12788. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12789. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12790. [ret setObject:detail1_section forKey:@"detail_1"];
  12791. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12792. [detail2_section setValue:@"detail" forKey:@"target"];
  12793. [detail2_section setValue:@"popup" forKey:@"action"];
  12794. [detail2_section setValue:@"content" forKey:@"type"];
  12795. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12796. [detail2_section setValue:@"true" forKey:@"single_row"];
  12797. [detail2_section setValue:@"local" forKey:@"data"];
  12798. [ret setObject:detail2_section forKey:@"detail_2"];
  12799. }
  12800. sqlite3_finalize(statement);
  12801. }
  12802. else
  12803. {
  12804. [ret setValue:@"8" forKey:@"result"];
  12805. }
  12806. // DebugLog(@"count:%d",count);
  12807. [iSalesDB close_db:db];
  12808. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12809. dispatch_async(dispatch_get_main_queue(), ^{
  12810. result(ret);
  12811. });
  12812. });
  12813. }
  12814. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12815. //{
  12816. //
  12817. // UIApplication * app = [UIApplication sharedApplication];
  12818. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12819. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12820. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12821. // sqlite3 *db = [iSalesDB get_db];
  12822. // NSString* collectId=params[@"collectId"];
  12823. //
  12824. //
  12825. //
  12826. //
  12827. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12828. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12829. // [iSalesDB execSql:sqlQuery db:db];
  12830. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12831. // [iSalesDB close_db:db];
  12832. //
  12833. //
  12834. // ret[@"result"]= [NSNumber numberWithInt:2];
  12835. // dispatch_async(dispatch_get_main_queue(), ^{
  12836. //
  12837. // appDelegate.wish_count =count;
  12838. //
  12839. // [appDelegate update_count_mark];
  12840. // result(ret);
  12841. // });
  12842. //
  12843. // });
  12844. //}
  12845. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12846. {
  12847. UIApplication * app = [UIApplication sharedApplication];
  12848. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12849. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12850. [iSalesDB disable_trigger];
  12851. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12852. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12853. [iSalesDB enable_trigger];
  12854. dispatch_async(dispatch_get_main_queue(), ^{
  12855. //
  12856. // NSString* user = [params valueForKey:@"user"];
  12857. //
  12858. // NSString* password = [params valueForKey:@"password"];
  12859. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12860. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12861. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12862. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12863. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12864. [appDelegate update_count_mark];
  12865. appDelegate.can_show_price =false;
  12866. appDelegate.can_see_price =false;
  12867. appDelegate.can_create_portfolio =false;
  12868. appDelegate.can_create_order =false;
  12869. appDelegate.can_cancel_order =false;
  12870. appDelegate.can_set_cart_price =false;
  12871. appDelegate.can_delete_order =false;
  12872. appDelegate.can_submit_order =false;
  12873. appDelegate.can_set_tearsheet_price =false;
  12874. appDelegate.can_update_contact_info = false;
  12875. appDelegate.save_order_logout = false;
  12876. appDelegate.submit_order_logout = false;
  12877. appDelegate.alert_sold_in_quantities = false;
  12878. appDelegate.ipad_perm =nil ;
  12879. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12880. appDelegate.OrderFilter= nil;
  12881. [appDelegate SetSo:nil];
  12882. [appDelegate set_main_button_panel];
  12883. result(ret);
  12884. });
  12885. });
  12886. }
  12887. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12888. {
  12889. // iSalesDB.
  12890. // UIApplication * app = [UIApplication sharedApplication];
  12891. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12892. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12893. [iSalesDB disable_trigger];
  12894. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12895. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12896. [iSalesDB enable_trigger];
  12897. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12898. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12899. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12900. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12901. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12902. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12903. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12904. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12905. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12906. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12907. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12908. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12909. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12910. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12911. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12912. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12913. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12914. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12915. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12916. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12917. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12918. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12919. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12920. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12921. [arr_name addObject:customer_first_name];
  12922. [arr_name addObject:customer_last_name];
  12923. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12924. // default ship from
  12925. 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';";
  12926. __block NSString *cid = @"";
  12927. __block NSString *name = @"";
  12928. __block NSString *ext = @"";
  12929. __block NSString *contact = @"";
  12930. __block NSString *email = @"";
  12931. __block NSString *fax = @"";
  12932. __block NSString *phone = @"";
  12933. sqlite3 *db = [iSalesDB get_db];
  12934. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12935. cid = [self textAtColumn:0 statement:statment];
  12936. name = [self textAtColumn:1 statement:statment];
  12937. ext = [self textAtColumn:2 statement:statment];
  12938. contact = [self textAtColumn:3 statement:statment];
  12939. email = [self textAtColumn:4 statement:statment];
  12940. fax = [self textAtColumn:5 statement:statment];
  12941. phone = [self textAtColumn:6 statement:statment];
  12942. }];
  12943. // default carrier
  12944. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12945. __block NSString *carrier = @"";
  12946. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12947. carrier = [self textAtColumn:0 statement:statment];
  12948. }];
  12949. NSString* so_id = [self get_offline_soid:db];
  12950. if(so_id==nil)
  12951. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12952. 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,carrier) 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,carrier];
  12953. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12954. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12955. //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'
  12956. //soId
  12957. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12958. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12959. sqlite3_stmt * statement;
  12960. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12961. {
  12962. if (sqlite3_step(statement) == SQLITE_ROW)
  12963. {
  12964. // char *name = (char*)sqlite3_column_text(statement, 1);
  12965. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12966. //ret = sqlite3_column_int(statement, 0);
  12967. char *soId = (char*)sqlite3_column_text(statement, 0);
  12968. if(soId==nil)
  12969. soId="";
  12970. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12971. [ret setValue:nssoId forKey:@"soId"];
  12972. [ret setValue:nssoId forKey:@"orderCode"];
  12973. }
  12974. sqlite3_finalize(statement);
  12975. }
  12976. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12977. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12978. [iSalesDB close_db:db];
  12979. dispatch_async(dispatch_get_main_queue(), ^{
  12980. result(ret);
  12981. });
  12982. });
  12983. }
  12984. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12985. {
  12986. UIApplication * app = [UIApplication sharedApplication];
  12987. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12988. bool can_create_backorder= appDelegate.can_create_backorder;
  12989. NSString* user = appDelegate.user;
  12990. NSString* contact_id = appDelegate.contact_id;
  12991. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12992. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12993. sqlite3 *db = [iSalesDB get_db];
  12994. NSString* orderCode=params[@"orderCode"];
  12995. int sort = [[params objectForKey:@"sort"] intValue];
  12996. NSString *sort_str = @"";
  12997. switch (sort) {
  12998. case 0:{
  12999. sort_str = @"order by c.modify_time desc";
  13000. }
  13001. break;
  13002. case 1:{
  13003. sort_str = @"order by c.modify_time asc";
  13004. }
  13005. break;
  13006. case 2:{
  13007. sort_str = @"order by m.name asc";
  13008. }
  13009. break;
  13010. case 3:{
  13011. sort_str = @"order by m.name desc";
  13012. }
  13013. break;
  13014. case 4:{
  13015. sort_str = @"order by m.description asc";
  13016. }
  13017. break;
  13018. default:
  13019. break;
  13020. }
  13021. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  13022. sqlite3_stmt * statement1;
  13023. double orderdiscount=0;
  13024. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  13025. if ( dbresult1== SQLITE_OK)
  13026. {
  13027. if (sqlite3_step(statement1) == SQLITE_ROW)
  13028. {
  13029. // NSDate *row_date = [NSDate date];
  13030. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13031. orderdiscount = sqlite3_column_double(statement1, 0);
  13032. }
  13033. sqlite3_finalize(statement1);
  13034. }
  13035. 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 ];
  13036. // 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 ];
  13037. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  13038. DebugLog(@"offline_login sql:%@",sqlQuery);
  13039. sqlite3_stmt * statement;
  13040. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  13041. NSDate *date1 = [NSDate date];
  13042. int count=0;
  13043. int cart_count=0;
  13044. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  13045. if ( dbresult== SQLITE_OK)
  13046. {
  13047. while (sqlite3_step(statement) == SQLITE_ROW)
  13048. {
  13049. // NSDate *row_date = [NSDate date];
  13050. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  13051. int product_id = sqlite3_column_int(statement, 0);
  13052. char *str_price = (char*)sqlite3_column_text(statement, 1);
  13053. int item_id = sqlite3_column_int(statement, 7);
  13054. NSString* Price=nil;
  13055. if(str_price==nil)
  13056. {
  13057. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  13058. // NSDate *price_date = [NSDate date];
  13059. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  13060. // DebugLog(@"price time interval");
  13061. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  13062. if(price==nil)
  13063. Price=@"No Price.";
  13064. else
  13065. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  13066. }
  13067. else
  13068. {
  13069. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  13070. }
  13071. double discount = sqlite3_column_double(statement, 2);
  13072. int item_count = sqlite3_column_int(statement, 3);
  13073. char *line_note = (char*)sqlite3_column_text(statement, 4);
  13074. NSString *nsline_note=nil;
  13075. if(line_note!=nil)
  13076. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  13077. char *name = (char*)sqlite3_column_text(statement, 5);
  13078. NSString *nsname=nil;
  13079. if(name!=nil)
  13080. nsname= [[NSString alloc]initWithUTF8String:name];
  13081. char *description = (char*)sqlite3_column_text(statement, 6);
  13082. NSString *nsdescription=nil;
  13083. if(description!=nil)
  13084. nsdescription= [[NSString alloc]initWithUTF8String:description];
  13085. int stockUom = sqlite3_column_int(statement, 8);
  13086. int _id = sqlite3_column_int(statement, 9);
  13087. int availability = sqlite3_column_int(statement, 10);
  13088. // NSDate *subtotal_date = [NSDate date];
  13089. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  13090. // DebugLog(@"subtotal_date time interval");
  13091. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  13092. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  13093. double weight=[bsubtotaljson[@"weight"] doubleValue];
  13094. int carton=[bsubtotaljson[@"carton"] intValue];
  13095. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  13096. int mpack =[bsubtotaljson[@"mpack"] intValue];
  13097. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  13098. // NSDate *img_date = [NSDate date];
  13099. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  13100. // DebugLog(@"img_date time interval");
  13101. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  13102. itemjson[@"model"]=nsname;
  13103. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  13104. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  13105. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  13106. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  13107. itemjson[@"check"]=@"true";
  13108. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  13109. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  13110. itemjson[@"unit_price"]=Price;
  13111. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  13112. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  13113. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  13114. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  13115. itemjson[@"note"]=nsline_note;
  13116. if (!can_create_backorder) {
  13117. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  13118. }
  13119. // NSDate *date2 = [NSDate date];
  13120. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  13121. // DebugLog(@"model_bundle time interval");
  13122. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  13123. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  13124. count++;
  13125. // DebugLog(@"row time interval");
  13126. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  13127. }
  13128. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  13129. sqlite3_finalize(statement);
  13130. }
  13131. DebugLog(@"request cart total time interval");
  13132. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  13133. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  13134. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  13135. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  13136. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  13137. ret[@"count"]=[NSNumber numberWithInt:count ];
  13138. if(orderdiscount>0)
  13139. ret[@"orderDiscount"]=@(orderdiscount);
  13140. ret[@"mode"]=@"Regular Mode";
  13141. [iSalesDB close_db:db];
  13142. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  13143. DebugLog(@"general notes :%@",general_note);
  13144. ret[@"general_note"]= general_note;
  13145. dispatch_async(dispatch_get_main_queue(), ^{
  13146. result(ret);
  13147. });
  13148. });
  13149. }
  13150. @end